Skip to content

Commit 6a3eed1

Browse files
Allow clean with invalid module graphs (#8640)
* Allow cleaning projects with an invalid module graph Signed-off-by: Florian Hammerschmidt <florianh89@gmail.com> * Restore approved clean follow-ups after rebase Restore the changelog fragment, duplicate-package regression, and progress suffix list that were lost during the rebase. Cleanup behavior matches the approved revision while retaining master's remove_js_file rename and suite updates. Validation: Fresh Rust build, all 146 unit tests, all six cleanup integration tests, Clippy, Rust formatting, shell syntax, and changelog validation pass. Signed-off-by: Florian Hammerschmidt <florianh89@gmail.com> * Align cleanup parity checks with successful Rust cleanup The parity suite still expected Rust cleanup to panic without a compiler, reject a missing runtime, and leave generated files after duplicate-module errors. Require successful cleanup in both implementations and removal of both duplicate outputs. Remove the obsolete out-of-source cleanup difference from the OCaml build-system guide. Validation: Fresh Rust build, five focused command-parity cases from the CI script, all six cleanup integration tests for each CLI, canonical suite coverage, shell syntax, changelog validation, and whitespace checks pass. Signed-off-by: Florian Hammerschmidt <florianh89@gmail.com> --------- Signed-off-by: Florian Hammerschmidt <florianh89@gmail.com>
1 parent 2f84e00 commit 6a3eed1

7 files changed

Lines changed: 168 additions & 64 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Allow `rescript clean` to remove generated files when the source module graph is invalid, including out-of-source `lib/es6` and `lib/js` outputs in the project and its dependencies. https://github.com/rescript-lang/rescript/pull/8640

‎rewatch-ocaml/README.md‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ These are intentional:
9191
- Configuration errors use the compiler-style form `path: message`, and JSON
9292
syntax errors use `path:line:column: message`. Messages name the field and
9393
the accepted values instead of the parser's internal types.
94-
- `rescript clean` also removes out-of-source JavaScript outputs.
9594
- The CLI uses Cmdliner:
9695
- help is rendered as a man page;
9796
- `--no-timing` takes no value;

‎rewatch-ocaml/tests/check_command_validation.sh‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -747,17 +747,17 @@ run_case compiler-args-no-project panic reject compiler-args "$work/orphan/A.res
747747
run_missing_bsc_case build-missing-bsc build "$project"
748748
run_missing_bsc_case format-missing-bsc format "$project/src/A.res"
749749
RESCRIPT_BSC_EXE="$work/missing-bsc" run_both clean "$work/clean-missing-bsc"
750-
if [ "$(classify "$rust_status")" != panic ] || [ "$ocaml_status" -ne 0 ] || \
750+
if [ "$rust_status" -ne 0 ] || [ "$ocaml_status" -ne 0 ] || \
751751
[ -e "$work/clean-missing-bsc/lib/bs/marker" ]; then
752-
echo "clean-missing-bsc: expected Rust panic and successful OCaml cleanup" >&2
752+
echo "clean-missing-bsc: expected both implementations to clean without a compiler" >&2
753753
dump_both
754754
exit 1
755755
fi
756756
checked=$((checked + 1))
757757
run_both --unset RESCRIPT_RUNTIME clean "$work/clean-missing-runtime"
758-
if [ "$(classify "$rust_status")" != reject ] || [ "$ocaml_status" -ne 0 ] || \
758+
if [ "$rust_status" -ne 0 ] || [ "$ocaml_status" -ne 0 ] || \
759759
[ -e "$work/clean-missing-runtime/lib/bs/marker" ]; then
760-
echo "clean-missing-runtime: expected Rust rejection and successful OCaml cleanup" >&2
760+
echo "clean-missing-runtime: expected both implementations to clean without a runtime" >&2
761761
dump_both
762762
exit 1
763763
fi
@@ -1541,9 +1541,9 @@ run_case clean-missing-dependency exit2 exit2 clean "$work/missing-dependency"
15411541
run_case clean-configless-dependency exit2 exit2 clean "$work/configless-dependency"
15421542
run_case clean-malformed-dependency exit2 exit2 clean "$work/malformed-dependency"
15431543
run_both clean "$work/clean-duplicate-{impl}"
1544-
if [ "$(classify "$rust_status")" != reject ] || [ "$ocaml_status" -ne 0 ] || \
1545-
[ ! -e "$work/clean-duplicate-rust/src/one/A.js" ] || \
1546-
[ ! -e "$work/clean-duplicate-rust/src/two/A.js" ] || \
1544+
if [ "$rust_status" -ne 0 ] || [ "$ocaml_status" -ne 0 ] || \
1545+
[ -e "$work/clean-duplicate-rust/src/one/A.js" ] || \
1546+
[ -e "$work/clean-duplicate-rust/src/two/A.js" ] || \
15471547
[ -e "$work/clean-duplicate-rust/lib/bs/marker" ] || \
15481548
[ -e "$work/clean-duplicate-ocaml/src/one/A.js" ] || \
15491549
[ -e "$work/clean-duplicate-ocaml/src/two/A.js" ] || \

‎rewatch/src/build/clean.rs‎

Lines changed: 39 additions & 56 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,37 +67,36 @@ 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
75-
.values()
76-
.filter_map(|module| match &module.source_type {
77-
SourceType::SourceFile(source_file) => {
78-
build_state.packages.get(&module.package_name).map(|package| {
79-
root_config
80-
.get_package_specs()
81-
.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-
}
94-
})
95-
.collect::<Vec<(PathBuf, String)>>()
96-
})
70+
fn clean_source_files(packages: &AHashMap<String, Package>, root_config: &Config) {
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));
9797
}
98-
_ => None,
99-
})
100-
.flatten()
101-
.collect::<Vec<(PathBuf, String)>>();
98+
}
99+
}
102100

103101
rescript_file_locations
104102
.par_iter()
@@ -342,7 +340,6 @@ pub fn cleanup_after_build(build_state: &BuildCommandState) {
342340
#[instrument(name = "clean.clean", skip_all)]
343341
pub fn clean(path: &Path, show_progress: bool, plain_output: bool, prod: bool) -> Result<()> {
344342
let project_context = ProjectContext::new(path)?;
345-
let compiler_info = build::get_compiler_info(&project_context)?;
346343
// `clean` always acts on the full set of source directories regardless of which features are
347344
// active. We explicitly pass `None` so every tagged source folder is included and its
348345
// artifacts can be removed, even for features the user hasn't enabled for this build.
@@ -376,27 +373,13 @@ pub fn clean(path: &Path, show_progress: bool, plain_output: bool, prod: bool) -
376373
}
377374

378375
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();
382-
let suffix_for_print = match root_config.package_specs {
383-
None => match &root_config.suffix {
384-
None => String::from(".js"),
385-
Some(suffix) => suffix.clone(),
386-
},
387-
Some(_) => root_config
388-
.get_package_specs()
389-
.into_iter()
390-
.filter_map(|spec| {
391-
if spec.in_source {
392-
spec.suffix.or_else(|| root_config.suffix.clone())
393-
} else {
394-
None
395-
}
396-
})
397-
.collect::<Vec<String>>()
398-
.join(", "),
399-
};
376+
let root_config = project_context.get_root_config();
377+
let suffix_for_print = root_config
378+
.get_package_specs()
379+
.iter()
380+
.map(|spec| root_config.get_suffix(spec))
381+
.collect::<Vec<String>>()
382+
.join(", ");
400383

401384
if !plain_output && show_progress {
402385
print!(
@@ -408,7 +391,7 @@ pub fn clean(path: &Path, show_progress: bool, plain_output: bool, prod: bool) -
408391
let _ = std::io::stdout().flush();
409392
}
410393

411-
clean_source_files(&build_state, root_config);
394+
clean_source_files(&packages, root_config);
412395
let timing_clean_mjs_elapsed = timing_clean_mjs.elapsed();
413396

414397
if !plain_output && show_progress {
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
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+
31+
rewatch build > /dev/null 2>&1
32+
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
41+
42+
error_output=$(rewatch clean 2>&1)
43+
clean_status=$?
44+
45+
if [ $clean_status -ne 0 ]; then
46+
error "Clean failed for an invalid module graph"
47+
printf "%s\n" "$error_output" >&2
48+
exit 1
49+
fi
50+
51+
if [ -f src/Test.mjs ] || [ -f lib/es6/src/Test.out.mjs ]; then
52+
error "Clean left a generated output behind"
53+
exit 1
54+
fi
55+
56+
restore_fixture
57+
trap - EXIT
58+
success "Invalid module graph cleaned"
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/bin/bash
2+
set -e
3+
cd "$(dirname "$0")"
4+
source "../utils.sh"
5+
6+
bold "Test: Clean removes same-named modules from every package"
7+
8+
test_dir=$(pwd)
9+
fixture=$(mktemp -d)
10+
trap 'cd "$test_dir"; rm -rf "$fixture"' EXIT
11+
cd "$fixture"
12+
13+
node <<'NODE'
14+
const fs = require("fs");
15+
const config = name => ({
16+
name,
17+
sources: ["src"],
18+
namespace: false,
19+
"package-specs": {module: "esmodule", "in-source": true},
20+
suffix: ".mjs",
21+
});
22+
const dependencies = ["clean-duplicate-a", "clean-duplicate-b"];
23+
fs.mkdirSync("src");
24+
fs.writeFileSync("src/Main.res", "let value = 0\n");
25+
fs.writeFileSync("rescript.json", JSON.stringify({...config("clean-duplicates"), dependencies}));
26+
for (const [index, name] of dependencies.entries()) {
27+
const directory = `node_modules/${name}`;
28+
fs.mkdirSync(`${directory}/src`, {recursive: true});
29+
fs.writeFileSync(`${directory}/package.json`, JSON.stringify({name, version: "1.0.0"}));
30+
fs.writeFileSync(`${directory}/rescript.json`, JSON.stringify(config(name)));
31+
fs.writeFileSync(`${directory}/src/Foo.res`, `let value = ${index}\n`);
32+
}
33+
NODE
34+
35+
# Build separately so both packages have real output before root-level cleanup.
36+
for package in clean-duplicate-a clean-duplicate-b; do
37+
if ! output=$(cd "node_modules/$package" && rewatch build 2>&1); then
38+
error "Could not build $package"
39+
printf '%s\n' "$output" >&2
40+
exit 1
41+
fi
42+
if [ ! -f "node_modules/$package/src/Foo.mjs" ]; then
43+
error "Test setup did not produce $package/src/Foo.mjs"
44+
exit 1
45+
fi
46+
done
47+
48+
if ! output=$(rewatch clean 2>&1); then
49+
error "Could not clean packages with same-named modules"
50+
printf '%s\n' "$output" >&2
51+
exit 1
52+
fi
53+
54+
for package in clean-duplicate-a clean-duplicate-b; do
55+
if [ -f "node_modules/$package/src/Foo.mjs" ]; then
56+
error "Clean left $package/src/Foo.mjs behind"
57+
exit 1
58+
fi
59+
done
60+
61+
success "Same-named modules cleaned from both packages"

‎rewatch/tests/suite.sh‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ fi
122122
./clean/02-clean-dev-dependencies.sh &&
123123
./clean/03-clean-node-modules.sh &&
124124
./clean/04-clean-rebuild-no-compiler-update.sh &&
125+
./clean/05-clean-invalid-module-graph.sh &&
126+
./clean/06-clean-duplicate-names-across-packages.sh &&
125127

126128
# Experimental tests
127129
./experimental/01-experimental-features-emit.sh &&

0 commit comments

Comments
 (0)