Skip to content

Commit 776801c

Browse files
committed
Be more aware of the workspace during clean
1 parent 81f4b2f commit 776801c

File tree

2 files changed

+67
-24
lines changed

2 files changed

+67
-24
lines changed

rewatch/src/build/clean.rs

Lines changed: 58 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::build_types::*;
22
use super::packages;
3+
use crate::build::packages::Package;
34
use crate::helpers;
45
use crate::helpers::emojis::*;
56
use ahash::AHashSet;
@@ -329,6 +330,9 @@ pub fn cleanup_after_build(build_state: &BuildState) {
329330
pub 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+
}

rewatch/src/build/packages.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,15 @@ pub fn make(
611611
show_progress: bool,
612612
build_dev_deps: bool,
613613
) -> Result<AHashMap<String, Package>> {
614-
let map = read_packages(root_folder, workspace_root, show_progress, build_dev_deps)?;
614+
let map = match &workspace_root {
615+
Some(wr) => {
616+
let root_folder_str = root_folder.to_string_lossy();
617+
let workspace_root_str = wr.to_string_lossy();
618+
log::debug!("Building workspace: {workspace_root_str} for {root_folder_str}",);
619+
read_packages(wr, workspace_root, show_progress, build_dev_deps)?
620+
}
621+
None => read_packages(root_folder, workspace_root, show_progress, build_dev_deps)?,
622+
};
615623

616624
/* Once we have the deduplicated packages, we can add the source files for each - to minimize
617625
* the IO */

0 commit comments

Comments
 (0)