Skip to content

Commit dff32e7

Browse files
committed
Only clean current package
1 parent 776801c commit dff32e7

File tree

2 files changed

+72
-30
lines changed

2 files changed

+72
-30
lines changed

rewatch/src/build/clean.rs

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ fn remove_iast(package: &packages::Package, source_file: &Path) {
2929
));
3030
}
3131

32-
fn remove_mjs_file(source_file: &Path, suffix: &String) {
32+
fn remove_mjs_file(source_file: &Path, suffix: &str) {
3333
let _ = std::fs::remove_file(source_file.with_extension(
3434
// suffix.to_string includes the ., so we need to remove it
3535
&suffix.to_string()[1..],
@@ -59,36 +59,51 @@ pub fn remove_compile_assets(package: &packages::Package, source_file: &Path) {
5959
}
6060
}
6161

62-
fn clean_source_files(build_state: &BuildState, root_package: &packages::Package) {
62+
fn clean_source_files(
63+
build_state: &BuildState,
64+
// If the root_package is part of a workspace, we only want to clean that package,
65+
// not the entire build_state modules.
66+
workspace_has_root_config: bool,
67+
root_package: &Package,
68+
suffix: &str,
69+
) {
6370
// get all rescript file locations
6471
let rescript_file_locations = build_state
6572
.modules
6673
.values()
6774
.filter_map(|module| match &module.source_type {
6875
SourceType::SourceFile(source_file) => {
69-
let package = build_state.packages.get(&module.package_name).unwrap();
70-
Some(
71-
root_package
72-
.config
73-
.get_package_specs()
74-
.iter()
75-
.filter_map(|spec| {
76-
if spec.in_source {
77-
Some((
78-
package.path.join(&source_file.implementation.path),
79-
root_package.config.get_suffix(spec),
80-
))
81-
} else {
82-
None
83-
}
84-
})
85-
.collect::<Vec<(PathBuf, String)>>(),
86-
)
76+
if !workspace_has_root_config || module.package_name == root_package.name
77+
{
78+
let package = build_state.packages.get(&module.package_name).unwrap();
79+
Some(
80+
root_package
81+
.config
82+
.get_package_specs()
83+
.iter()
84+
.filter_map(|spec| {
85+
if spec.in_source {
86+
Some((
87+
package.path.join(&source_file.implementation.path),
88+
match &root_package.config.suffix {
89+
None => suffix,
90+
Some(sfx) => sfx,
91+
},
92+
))
93+
} else {
94+
None
95+
}
96+
})
97+
.collect::<Vec<(PathBuf, &str)>>(),
98+
)
99+
} else {
100+
None
101+
}
87102
}
88103
_ => None,
89104
})
90105
.flatten()
91-
.collect::<Vec<(PathBuf, String)>>();
106+
.collect::<Vec<(PathBuf, &str)>>();
92107

93108
rescript_file_locations
94109
.par_iter()
@@ -352,15 +367,20 @@ pub fn clean(path: &Path, show_progress: bool, snapshot_output: bool, build_dev_
352367
);
353368
let _ = std::io::stdout().flush();
354369
};
355-
match &workspace_root {
356-
Some(_) => {
357-
// There is a workspace detected, so will only clean the current package
370+
371+
let mut workspace_has_root_config = false;
372+
match &workspace_config_name {
373+
Some(workspace_config_name) if packages.contains_key(workspace_config_name) => {
374+
// The workspace package was found in the packages.
375+
// This means that the root_config and workspace_config have a parent/child relationship.
376+
// So we only want to clean the root package in this case.
358377
let package = packages
359378
.get(&root_config_name)
360379
.expect("Could not find package during clean");
361380
clean_package(show_progress, snapshot_output, package);
381+
workspace_has_root_config = true;
362382
}
363-
None => {
383+
_ => {
364384
packages.iter().for_each(|(_, package)| {
365385
clean_package(show_progress, snapshot_output, package);
366386
});
@@ -421,7 +441,7 @@ pub fn clean(path: &Path, show_progress: bool, snapshot_output: bool, build_dev_
421441
let _ = std::io::stdout().flush();
422442
}
423443

424-
clean_source_files(&build_state, root_package);
444+
clean_source_files(&build_state, workspace_has_root_config, root_package, suffix);
425445
let timing_clean_mjs_elapsed = timing_clean_mjs.elapsed();
426446

427447
if !snapshot_output && show_progress {

rewatch/src/build/packages.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -613,10 +613,32 @@ pub fn make(
613613
) -> Result<AHashMap<String, Package>> {
614614
let map = match &workspace_root {
615615
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)?
616+
// If the workspace root contains the root_folder as a dependency,
617+
// it should be considered as related, and we read the workspace.
618+
let root_name = read_package_name(root_folder)?;
619+
let workspace_config = read_config(wr)?;
620+
621+
let is_child = workspace_config
622+
.dependencies
623+
.to_owned()
624+
.unwrap_or_default()
625+
.iter()
626+
.any(|dep| dep == &root_name)
627+
|| workspace_config
628+
.dev_dependencies
629+
.to_owned()
630+
.unwrap_or_default()
631+
.iter()
632+
.any(|dep| dep == &root_name);
633+
634+
if is_child {
635+
let root_folder_str = root_folder.to_string_lossy();
636+
let workspace_root_str = wr.to_string_lossy();
637+
log::info!("Building workspace: {workspace_root_str} for {root_folder_str}",);
638+
read_packages(wr, workspace_root, show_progress, build_dev_deps)?
639+
} else {
640+
read_packages(root_folder, workspace_root, show_progress, build_dev_deps)?
641+
}
620642
}
621643
None => read_packages(root_folder, workspace_root, show_progress, build_dev_deps)?,
622644
};

0 commit comments

Comments
 (0)