Skip to content

Commit 66fad0f

Browse files
Allow clean with invalid module graphs
Signed-off-by: Florian Hammerschmidt <florianh89@gmail.com>
1 parent 3102f42 commit 66fad0f

3 files changed

Lines changed: 58 additions & 31 deletions

File tree

‎rewatch/src/build/clean.rs‎

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use super::build_types::*;
22
use super::packages;
3-
use crate::build;
43
use crate::build::packages::Package;
5-
use crate::config::{Config, SourceMapCommand};
4+
use crate::config::Config;
65
use crate::helpers;
76
use crate::helpers::emojis::*;
87
use crate::project_context::ProjectContext;
9-
use ahash::AHashSet;
8+
use ahash::{AHashMap, AHashSet};
109
use anyhow::Result;
1110
use console::style;
1211
use rayon::prelude::*;
@@ -68,36 +67,32 @@ pub fn remove_compile_assets(package: &packages::Package, source_file: &Path) {
6867
}
6968
}
7069

71-
fn clean_source_files(build_state: &BuildState, root_config: &Config) {
72-
// get all rescript file locations
73-
let rescript_file_locations = build_state
74-
.modules
70+
fn clean_source_files(packages: &AHashMap<String, Package>, root_config: &Config) {
71+
let rescript_file_locations = packages
7572
.values()
76-
.filter_map(|module| match &module.source_type {
77-
SourceType::SourceFile(source_file) => {
78-
build_state.packages.get(&module.package_name).map(|package| {
73+
.filter_map(|package| package.source_files.as_ref().map(|files| (package, files)))
74+
.flat_map(|(package, files)| {
75+
files
76+
.keys()
77+
.filter(|file| {
78+
file.extension()
79+
.and_then(|extension| extension.to_str())
80+
.is_some_and(helpers::is_implementation_file)
81+
})
82+
.flat_map(move |file| {
7983
root_config
8084
.get_package_specs()
8185
.into_iter()
82-
.filter_map(|spec| {
83-
if spec.in_source {
84-
Some((
85-
package.path.join(&source_file.implementation.path),
86-
match spec.suffix {
87-
None => root_config.get_suffix(&spec),
88-
Some(suffix) => suffix,
89-
},
90-
))
91-
} else {
92-
None
93-
}
86+
.filter(|spec| spec.in_source)
87+
.map(move |spec| {
88+
let suffix = match spec.suffix {
89+
None => root_config.get_suffix(&spec),
90+
Some(suffix) => suffix,
91+
};
92+
(package.path.join(file), suffix)
9493
})
95-
.collect::<Vec<(PathBuf, String)>>()
9694
})
97-
}
98-
_ => None,
9995
})
100-
.flatten()
10196
.collect::<Vec<(PathBuf, String)>>();
10297

10398
rescript_file_locations
@@ -342,7 +337,6 @@ pub fn cleanup_after_build(build_state: &BuildCommandState) {
342337
#[instrument(name = "clean.clean", skip_all)]
343338
pub fn clean(path: &Path, show_progress: bool, plain_output: bool, prod: bool) -> Result<()> {
344339
let project_context = ProjectContext::new(path)?;
345-
let compiler_info = build::get_compiler_info(&project_context)?;
346340
// `clean` always acts on the full set of source directories regardless of which features are
347341
// active. We explicitly pass `None` so every tagged source folder is included and its
348342
// artifacts can be removed, even for features the user hasn't enabled for this build.
@@ -376,9 +370,7 @@ pub fn clean(path: &Path, show_progress: bool, plain_output: bool, prod: bool) -
376370
}
377371

378372
let timing_clean_mjs = Instant::now();
379-
let mut build_state = BuildState::new(project_context, packages, compiler_info, SourceMapCommand::Build);
380-
packages::parse_packages(&mut build_state)?;
381-
let root_config = build_state.get_root_config();
373+
let root_config = project_context.get_root_config();
382374
let suffix_for_print = match root_config.package_specs {
383375
None => match &root_config.suffix {
384376
None => String::from(".js"),
@@ -408,7 +400,7 @@ pub fn clean(path: &Path, show_progress: bool, plain_output: bool, prod: bool) -
408400
let _ = std::io::stdout().flush();
409401
}
410402

411-
clean_source_files(&build_state, root_config);
403+
clean_source_files(&packages, root_config);
412404
let timing_clean_mjs_elapsed = timing_clean_mjs.elapsed();
413405

414406
if !plain_output && show_progress {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/bash
2+
cd $(dirname $0)
3+
source "../utils.sh"
4+
cd ../../testrepo
5+
6+
bold "Test: Clean succeeds when the module graph is invalid"
7+
8+
rewatch clean > /dev/null 2>&1
9+
rewatch build > /dev/null 2>&1
10+
11+
mkdir -p packages/main/src/dupe-a packages/main/src/dupe-b
12+
echo 'let value = 1' > packages/main/src/dupe-a/DuplicateModule.res
13+
echo 'let value = 2' > packages/main/src/dupe-b/DuplicateModule.res
14+
15+
error_output=$(rewatch clean 2>&1)
16+
clean_status=$?
17+
18+
rm -rf packages/main/src/dupe-a packages/main/src/dupe-b
19+
20+
if [ $clean_status -ne 0 ]; then
21+
error "Clean failed for an invalid module graph"
22+
printf "%s\n" "$error_output" >&2
23+
rewatch build > /dev/null 2>&1
24+
exit 1
25+
fi
26+
27+
if [ -f packages/main/src/Main.bs.js ]; then
28+
error "Clean left an in-source output behind"
29+
rewatch build > /dev/null 2>&1
30+
exit 1
31+
fi
32+
33+
rewatch build > /dev/null 2>&1
34+
success "Invalid module graph cleaned"

‎rewatch/tests/suite.sh‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ fi
165165
./clean/02-clean-dev-dependencies.sh &&
166166
./clean/03-clean-node-modules.sh &&
167167
./clean/04-clean-rebuild-no-compiler-update.sh &&
168+
./clean/05-clean-invalid-module-graph.sh &&
168169

169170
# Experimental tests
170171
./experimental/01-experimental-features-emit.sh &&

0 commit comments

Comments
 (0)