Skip to content

Commit 2a7ea0c

Browse files
tsnobipclaude
andauthored
Record the module after a qualified free path in the AST header (#8939)
Why: In a namespaced package, a module that names a sibling only as `Ns.Target` got no dependency on it: the `-bs-ast` header listed the head `Ns` alone, and both build systems drop their own namespace, since it does not say which module to wait for. User could then compile before or alongside Target, failing with "Ns.Target is an alias for module Target-Ns, which is missing", or stay stale after Target changed. Whether it failed depended on scheduling, so `rescript-rust` reported it on projects where `rescript` happened not to. Diagnostic removed (clean build of a namespaced package): Example: // Target.res let value = 1 // User.res let value = Ns.Target.value + 1 Before (diagnostic): The module Ns.Target is an alias for module Target-Ns, which is missing After (diagnostic): (none) Dependencies modified (AST header of User.res): Before (text): Ns After (text): Ns Ns.Target Depend now also records `S.M` for a free path `S.M…`. Both build systems already resolve `Ns.M` to `M-Ns` inside namespace `Ns` and take the head of any other dotted name, so they need no change. Review: Does the larger header change dependencies outside the own namespace? Only by adding precise ones: Rust takes the head of `Belt.Array`, and the OCaml builder resolves `Dep.X` to `X-Dep` when Dep is another package's namespace, beside the namespace map it already depends on. Signed-off-by: Paul Tsnobiladzé <2479216+tsnobip@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 2b7ac3e commit 2a7ea0c

4 files changed

Lines changed: 68 additions & 1 deletion

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- Make a module reached only as `Namespace.Module` from inside its own
2+
namespace a dependency, so it builds first and its changes rebuild the user.

‎compiler/ml/depend.ml‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,14 @@ let add_names s =
6565
let rec add_path bv ?(p = []) = function
6666
| Lident s ->
6767
let free =
68-
try lookup_free (s :: p) bv with Not_found -> String_set.singleton s
68+
try lookup_free (s :: p) bv
69+
with Not_found -> (
70+
(* Also record [S.M] for a qualified free path: when [S] is the
71+
namespace of the module's own package, the build system resolves
72+
[M] in that namespace, which [S] alone cannot tell it. *)
73+
match p with
74+
| [] -> String_set.singleton s
75+
| m :: _ -> String_set.of_list [s; s ^ "." ^ m])
6976
in
7077
add_names free
7178
| Ldot (l, s) -> add_path bv ~p:(s :: p) l
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: A module reached only through its own namespace is a dependency"
7+
8+
fixture=$(mktemp -d 2>/dev/null || mktemp -d -t rewatch-qualified-own-namespace)
9+
trap "rm -rf '$fixture'" EXIT
10+
11+
mkdir -p "$fixture/src"
12+
13+
cat > "$fixture/rescript.json" <<'JSON'
14+
{
15+
"name": "rewatch-qualified-own-namespace",
16+
"namespace": "Ns",
17+
"sources": { "dir": "src" },
18+
"package-specs": { "module": "esmodule", "in-source": true },
19+
"suffix": ".mjs"
20+
}
21+
JSON
22+
23+
# User names Target only as Ns.Target, so its AST header must say so: the
24+
# bare namespace does not tell the build system which module to wait for.
25+
cat > "$fixture/src/Target.res" <<'RES'
26+
let value = 1
27+
RES
28+
29+
cat > "$fixture/src/User.res" <<'RES'
30+
let value = Ns.Target.value + 1
31+
RES
32+
33+
cd "$fixture"
34+
if ! compiler_output=$("$REWATCH_EXECUTABLE" build 2>&1); then
35+
error "Clean build compiled User before Target"
36+
printf "%s\n" "$compiler_output" >&2
37+
exit 1
38+
fi
39+
40+
# Changing Target must recompile User and report its new type error.
41+
cat > src/Target.res <<'RES'
42+
let value = "not an int"
43+
RES
44+
45+
if compiler_output=$("$REWATCH_EXECUTABLE" build 2>&1); then
46+
error "Incremental build did not recompile User after Target changed"
47+
printf "%s\n" "$compiler_output" >&2
48+
exit 1
49+
fi
50+
51+
if ! printf '%s\n' "$compiler_output" | grep -q "User.res"; then
52+
error "Incremental build did not report the error in User"
53+
printf "%s\n" "$compiler_output" >&2
54+
exit 1
55+
fi
56+
57+
success "Modules reached through their own namespace are dependencies"

‎rewatch/tests/suite.sh‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ fi
8787
./compile/19-utf8-warning.sh &&
8888
./compile/20-schedule-independent-modules-after-error.sh &&
8989
./compile/21-rebuild-dependency-for-package-specs.sh &&
90+
./compile/22-qualified-own-namespace-dependency.sh &&
9091
./compile/14-no-testrepo-changes.sh &&
9192
./compile/15-no-new-files.sh &&
9293
./compile/16-snapshots-unchanged.sh &&

0 commit comments

Comments
 (0)