Skip to content

Commit a90a656

Browse files
Clean generated outputs without a build graph
Signed-off-by: Florian Hammerschmidt <florianh89@gmail.com>
1 parent f4f8c5c commit a90a656

2 files changed

Lines changed: 63 additions & 36 deletions

File tree

‎rewatch/src/build/clean.rs‎

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -68,32 +68,35 @@ pub fn remove_compile_assets(package: &packages::Package, source_file: &Path) {
6868
}
6969

7070
fn clean_source_files(packages: &AHashMap<String, Package>, root_config: &Config) {
71-
let rescript_file_locations = packages
72-
.values()
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| {
83-
root_config
84-
.get_package_specs()
85-
.into_iter()
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)
93-
})
94-
})
95-
})
96-
.collect::<Vec<(PathBuf, String)>>();
71+
let mut rescript_file_locations = Vec::new();
72+
for package in packages.values() {
73+
let Some(source_files) = &package.source_files else {
74+
continue;
75+
};
76+
for source_file in source_files.keys().filter(|source_file| {
77+
source_file
78+
.extension()
79+
.and_then(|extension| extension.to_str())
80+
.is_some_and(helpers::is_implementation_file)
81+
}) {
82+
for spec in root_config.get_package_specs() {
83+
let output_base = if spec.in_source {
84+
package.path.join(source_file)
85+
} else {
86+
package
87+
.path
88+
.join("lib")
89+
.join(spec.get_out_of_source_dir())
90+
.join(source_file)
91+
};
92+
let suffix = match &spec.suffix {
93+
None => root_config.get_suffix(&spec),
94+
Some(suffix) => suffix.clone(),
95+
};
96+
rescript_file_locations.push((output_base, suffix));
97+
}
98+
}
99+
}
97100

98101
rescript_file_locations
99102
.par_iter()

‎rewatch/tests/clean/05-clean-invalid-module-graph.sh‎

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,53 @@ cd ../../testrepo
66
bold "Test: Clean succeeds when the module graph is invalid"
77

88
rewatch clean > /dev/null 2>&1
9+
cp rescript.json rescript.json.clean-invalid.bak
10+
11+
restore_fixture() {
12+
rm -rf src/dupe-a src/dupe-b
13+
if [ -f rescript.json.clean-invalid.bak ]; then
14+
mv rescript.json.clean-invalid.bak rescript.json
15+
fi
16+
rewatch build > /dev/null 2>&1 || true
17+
}
18+
trap restore_fixture EXIT
19+
20+
node -e '
21+
const fs = require("fs");
22+
const path = process.argv[1];
23+
const config = JSON.parse(fs.readFileSync(path, "utf8"));
24+
config["package-specs"] = [
25+
{module: "esmodule", "in-source": true},
26+
{module: "esmodule", "in-source": false, suffix: ".out.mjs"},
27+
];
28+
fs.writeFileSync(path, `${JSON.stringify(config, null, 2)}\n`);
29+
' rescript.json
30+
931
rewatch build > /dev/null 2>&1
1032

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
33+
if [ ! -f src/Test.mjs ] || [ ! -f lib/es6/src/Test.out.mjs ]; then
34+
error "Test setup did not produce both in-source and out-of-source outputs"
35+
exit 1
36+
fi
37+
38+
mkdir -p src/dupe-a src/dupe-b
39+
echo 'let value = 1' > src/dupe-a/DuplicateModule.res
40+
echo 'let value = 2' > src/dupe-b/DuplicateModule.res
1441

1542
error_output=$(rewatch clean 2>&1)
1643
clean_status=$?
1744

18-
rm -rf packages/main/src/dupe-a packages/main/src/dupe-b
19-
2045
if [ $clean_status -ne 0 ]; then
2146
error "Clean failed for an invalid module graph"
2247
printf "%s\n" "$error_output" >&2
23-
rewatch build > /dev/null 2>&1
2448
exit 1
2549
fi
2650

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
51+
if [ -f src/Test.mjs ] || [ -f lib/es6/src/Test.out.mjs ]; then
52+
error "Clean left a generated output behind"
3053
exit 1
3154
fi
3255

33-
rewatch build > /dev/null 2>&1
56+
restore_fixture
57+
trap - EXIT
3458
success "Invalid module graph cleaned"

0 commit comments

Comments
 (0)