Skip to content

Commit 7bd7e58

Browse files
tsnobipclaude
andauthored
Spell files below source dir "." without a leading "./" (#8940)
* Spell files below source dir "." without a leading "./" Why: With a source dir "." (or "./src"), `rescript` rebuilt the same modules and everything depending on them on every build, while `rescript-rust` compiled nothing. Discovery joined the dir to each file name, giving "./A.res", but bsc records "A.res" in the AST and outputs are expected at "A.js" and "ui/U.js". Cleanup matched no AST to "./A.res" and removed it, and the inventory of "." listed "./ui/U.js", which matched no module's output, so the next build removed it and recompiled U. For a package with `"sources": [{"dir": "."}, {"dir": "ui"}]`: Before (text): unchanged builds of that package Cleaned 1/2 Parsed 1 source files Compiled 1 modules After (text): unchanged builds of that package Cleaned 0/2 Parsed 0 source files Compiled 0 modules Discovery now drops "." segments from file paths, and the output path of a module of the package root no longer starts with "./". Directory paths stay as written, so `-bs-gentype-source-dir`, `.sourcedirs.json` and `-bs-package-output` are unchanged. Review: Why not normalize where paths are compared? Cleanup, parse freshness, compile assets and the output inventory all key on these paths; fixing their spelling once covers every comparison. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Paul Tsnobiladzé <2479216+tsnobip@users.noreply.github.com> * Spell out-of-source outputs of root modules without "/./" With `"in-source": false`, the output path of a module of source dir "." was "lib/es6/./A.mjs" while the output inventory finds "lib/es6/A.mjs". Only the missing lib/bs mirror kept cleanup from deleting the real output, so the output path now drops every "." segment, and the shared test also builds the out-of-source layout. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Paul Tsnobiladzé <2479216+tsnobip@users.noreply.github.com> --------- Signed-off-by: Paul Tsnobiladzé <2479216+tsnobip@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 2a7ea0c commit 7bd7e58

6 files changed

Lines changed: 94 additions & 5 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- Stop `rescript` from rebuilding the modules of a `"."` or `"./src"` source
2+
dir, and their dependents, on every build when nothing changed.

‎rewatch-ocaml/build_artifacts.ml‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@ let relative_output_directory path (spec : Config.package_spec) =
1212

1313
let generated_js_path (config : Config.t) path (spec : Config.package_spec) =
1414
let output_dir = relative_output_directory path spec in
15+
(* The output of a module of the package root is "A.js" or "lib/es6/A.js",
16+
as the output inventory spells it, not "./A.js" or "lib/es6/./A.js". *)
1517
Filename.concat config.root
16-
(Filename.concat output_dir
17-
(Filename.remove_extension (Filename.basename path)
18-
^ Config.package_spec_suffix config spec))
18+
(Source.without_current_dir_segments
19+
(Filename.concat output_dir
20+
(Filename.remove_extension (Filename.basename path)
21+
^ Config.package_spec_suffix config spec)))
1922

2023
let published_ast_path ~ocaml_dir source =
2124
Filename.concat ocaml_dir (Filename.basename (Source.ast_path source))

‎rewatch-ocaml/source.ml‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,22 @@ let interface_mismatch_error implementation interface =
8181
different path names or different cases: `%s` vs `%s`"
8282
implementation interface)
8383

84+
(* [path] without its "." segments, so a file found below source dir "." or
85+
"./src" has the path bsc records for it, "A.res" or "src/A.res". Cleanup and
86+
freshness match modules to their ASTs and outputs by these paths: a
87+
"./A.res" matched no AST, and a "./ui/U.js" in the inventory of "." matched
88+
no module's output, so each build removed them and rebuilt the module. *)
89+
let rec without_current_dir_segments path =
90+
let directory = Filename.dirname path in
91+
let base = Filename.basename path in
92+
if directory = path || path = Filename.current_dir_name then path
93+
else if base = Filename.current_dir_name then
94+
without_current_dir_segments directory
95+
else
96+
match without_current_dir_segments directory with
97+
| directory when directory = Filename.current_dir_name -> base
98+
| directory -> Filename.concat directory base
99+
84100
(* A package source tree has three consumers with deliberately different
85101
recursion rules. Compilation follows directory links and honors source
86102
activation/subdirs; cleanup inventories every real descendant but treats
@@ -134,7 +150,11 @@ let scan_source ~root (source : Config.source) ~discover_modules ~on_missing
134150
| Some kind ->
135151
candidates :=
136152
{
137-
file = {path = relative_path; modified = metadata.Unix.st_mtime};
153+
file =
154+
{
155+
path = without_current_dir_segments relative_path;
156+
modified = metadata.Unix.st_mtime;
157+
};
138158
kind;
139159
is_dev = source.is_dev;
140160
}
@@ -143,7 +163,9 @@ let scan_source ~root (source : Config.source) ~discover_modules ~on_missing
143163
List.iter
144164
(fun name ->
145165
let relative_path = Filename.concat relative name in
146-
let absolute_path = Filename.concat root relative_path in
166+
let absolute_path =
167+
Filename.concat root (without_current_dir_segments relative_path)
168+
in
147169
match Unix.lstat absolute_path with
148170
| metadata -> (
149171
match metadata.Unix.st_kind with

‎rewatch-ocaml/source.mli‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ type source_kind = Implementation | Interface
2323

2424
val source_kind : string -> source_kind option
2525
val module_name : string -> string
26+
27+
val without_current_dir_segments : string -> string
28+
(** [path] without its "." segments: "./src/./A.res" is "src/A.res". *)
29+
2630
val namespace_members : entry:string option -> module_ list -> module_ list
2731

2832
val duplicate_error :
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/bin/bash
2+
3+
cd $(dirname $0)
4+
source "../utils.sh"
5+
6+
bold "Test: Modules of source dir \".\" are not rebuilt when nothing changed"
7+
8+
fixtures=$(mktemp -d 2>/dev/null || mktemp -d -t rewatch-root-source-dir)
9+
trap "rm -rf '$fixtures'" EXIT
10+
11+
# "." spells its modules "./Root.res" unless the build system drops the
12+
# segment, while bsc records "Root.res" in the AST and the output inventory
13+
# finds "Root.mjs" or "lib/es6/Root.mjs". A mismatch makes every build clean
14+
# those artifacts.
15+
check_layout() {
16+
local name=$1 in_source=$2 root_output=$3 ui_output=$4
17+
local fixture="$fixtures/$name"
18+
mkdir -p "$fixture/ui"
19+
20+
cat > "$fixture/rescript.json" <<JSON
21+
{
22+
"name": "rewatch-root-source-dir-$name",
23+
"sources": [{ "dir": "." }, { "dir": "ui" }],
24+
"package-specs": { "module": "esmodule", "in-source": $in_source },
25+
"suffix": ".mjs"
26+
}
27+
JSON
28+
29+
printf 'let value = 1\n' > "$fixture/Root.res"
30+
printf 'let value = Root.value + 1\n' > "$fixture/ui/Component.res"
31+
32+
if ! compiler_output=$(cd "$fixture" && "$REWATCH_EXECUTABLE" build 2>&1); then
33+
error "Initial $name fixture build failed"
34+
printf "%s\n" "$compiler_output" >&2
35+
return 1
36+
fi
37+
38+
# Several unchanged builds, since a removed output only shows on the next one.
39+
for attempt in 1 2 3; do
40+
compiler_output=$(cd "$fixture" && "$REWATCH_EXECUTABLE" build 2>&1)
41+
if ! printf '%s\n' "$compiler_output" | grep -q "Compiled 0 modules"; then
42+
error "Unchanged $name build $attempt recompiled modules"
43+
printf "%s\n" "$compiler_output" >&2
44+
return 1
45+
fi
46+
done
47+
48+
if [ ! -f "$fixture/$root_output" ] || [ ! -f "$fixture/$ui_output" ]; then
49+
error "Unchanged $name builds removed generated outputs"
50+
return 1
51+
fi
52+
}
53+
54+
check_layout in-source true Root.mjs ui/Component.mjs || exit 1
55+
check_layout out-of-source false lib/es6/Root.mjs lib/es6/ui/Component.mjs || exit 1
56+
57+
success "Modules of source dir \".\" are not rebuilt when nothing changed"

‎rewatch/tests/suite.sh‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ fi
8888
./compile/20-schedule-independent-modules-after-error.sh &&
8989
./compile/21-rebuild-dependency-for-package-specs.sh &&
9090
./compile/22-qualified-own-namespace-dependency.sh &&
91+
./compile/23-root-source-dir-rebuild.sh &&
9192
./compile/14-no-testrepo-changes.sh &&
9293
./compile/15-no-new-files.sh &&
9394
./compile/16-snapshots-unchanged.sh &&

0 commit comments

Comments
 (0)