11use super :: build_types:: * ;
22use super :: packages;
3+ use crate :: build:: packages:: Package ;
34use crate :: helpers;
45use crate :: helpers:: emojis:: * ;
56use ahash:: AHashSet ;
@@ -329,6 +330,9 @@ pub fn cleanup_after_build(build_state: &BuildState) {
329330pub fn clean ( path : & Path , show_progress : bool , snapshot_output : bool , build_dev_deps : bool ) -> Result < ( ) > {
330331 let project_root = helpers:: get_abs_path ( path) ;
331332 let workspace_root = helpers:: get_workspace_root ( & project_root) ;
333+ let workspace_config_name = & workspace_root
334+ . as_ref ( )
335+ . and_then ( |wr| packages:: read_package_name ( wr) . ok ( ) ) ;
332336 let packages = packages:: make (
333337 & None ,
334338 & project_root,
@@ -348,30 +352,21 @@ pub fn clean(path: &Path, show_progress: bool, snapshot_output: bool, build_dev_
348352 ) ;
349353 let _ = std:: io:: stdout ( ) . flush ( ) ;
350354 } ;
351- packages. iter ( ) . for_each ( |( _, package) | {
352- if show_progress {
353- if snapshot_output {
354- println ! ( "Cleaning {}" , package. name)
355- } else {
356- print ! (
357- "{}{} {}Cleaning {}..." ,
358- LINE_CLEAR ,
359- style( "[1/2]" ) . bold( ) . dim( ) ,
360- SWEEP ,
361- package. name
362- ) ;
363- }
364- let _ = std:: io:: stdout ( ) . flush ( ) ;
355+ match & workspace_root {
356+ Some ( _) => {
357+ // There is a workspace detected, so will only clean the current package
358+ let package = packages
359+ . get ( & root_config_name)
360+ . expect ( "Could not find package during clean" ) ;
361+ clean_package ( show_progress, snapshot_output, package) ;
365362 }
363+ None => {
364+ packages. iter ( ) . for_each ( |( _, package) | {
365+ clean_package ( show_progress, snapshot_output, package) ;
366+ } ) ;
367+ }
368+ }
366369
367- let path_str = package. get_build_path ( ) ;
368- let path = std:: path:: Path :: new ( & path_str) ;
369- let _ = std:: fs:: remove_dir_all ( path) ;
370-
371- let path_str = package. get_ocaml_build_path ( ) ;
372- let path = std:: path:: Path :: new ( & path_str) ;
373- let _ = std:: fs:: remove_dir_all ( path) ;
374- } ) ;
375370 let timing_clean_compiler_assets_elapsed = timing_clean_compiler_assets. elapsed ( ) ;
376371
377372 if !snapshot_output && show_progress {
@@ -399,7 +394,22 @@ pub fn clean(path: &Path, show_progress: bool, snapshot_output: bool, build_dev_
399394 . get ( & build_state. root_config_name )
400395 . expect ( "Could not find root package" ) ;
401396
402- let suffix = root_package. config . suffix . as_deref ( ) . unwrap_or ( ".res.mjs" ) ;
397+ // Use the current package suffix if present.
398+ // Otherwise, use the parent suffix if present.
399+ // Fall back to .res.mjs
400+ let suffix = match root_package. config . suffix . as_deref ( ) {
401+ Some ( suffix) => suffix,
402+ None => match & workspace_config_name {
403+ None => ".res.mjs" ,
404+ Some ( workspace_config_name) => {
405+ if let Some ( package) = build_state. packages . get ( workspace_config_name) {
406+ package. config . suffix . as_deref ( ) . unwrap_or ( ".res.mjs" )
407+ } else {
408+ ".res.mjs"
409+ }
410+ }
411+ } ,
412+ } ;
403413
404414 if !snapshot_output && show_progress {
405415 println ! (
@@ -428,3 +438,28 @@ pub fn clean(path: &Path, show_progress: bool, snapshot_output: bool, build_dev_
428438
429439 Ok ( ( ) )
430440}
441+
442+ fn clean_package ( show_progress : bool , snapshot_output : bool , package : & Package ) {
443+ if show_progress {
444+ if snapshot_output {
445+ println ! ( "Cleaning {}" , package. name)
446+ } else {
447+ print ! (
448+ "{}{} {}Cleaning {}..." ,
449+ LINE_CLEAR ,
450+ style( "[1/2]" ) . bold( ) . dim( ) ,
451+ SWEEP ,
452+ package. name
453+ ) ;
454+ }
455+ let _ = std:: io:: stdout ( ) . flush ( ) ;
456+ }
457+
458+ let path_str = package. get_build_path ( ) ;
459+ let path = std:: path:: Path :: new ( & path_str) ;
460+ let _ = std:: fs:: remove_dir_all ( path) ;
461+
462+ let path_str = package. get_ocaml_build_path ( ) ;
463+ let path = std:: path:: Path :: new ( & path_str) ;
464+ let _ = std:: fs:: remove_dir_all ( path) ;
465+ }
0 commit comments