Skip to content

Commit 32edc0a

Browse files
cknittclaude
andauthored
Look up compiled files under the module name as written first (#8925)
Load-path lookups (Misc.find_in_path_uncap, Config_util.find_opt and the .cmi loader in Bs_cmi_load) accept a file under the module name as written or uncapitalized, and tried the uncapitalized name first. On a case-insensitive file system both spellings match the same file, so the returned path read `src/aBC.cmi` in "does not match the interface" errors and in other messages naming the file. The lookups now try the name as written first and fall back to the uncapitalized one. On a case-sensitive file system only one spelling exists for a module, so the result is the same file as before. Diagnostic (text): Before: does not match the interface src/aBC.cmi: After: does not match the interface src/ABC.cmi: Review: Is the lookup order load-bearing? Both spellings exist side by side only for two source files of the same module name, which the build rejects as duplicates. Capitalized files, the common case, now resolve with one file check instead of two. Area: typechecker codegen tests Output: unchanged Tests: +tests/ounit_tests/ounit_find_in_path_tests.ml ~tests/ounit_tests/ounit_tests_main.ml Claude-Session: https://claude.ai/code/session_01Kr4cjoUi2f5VpMvE6wT9bo Signed-off-by: Christoph Knittel <ck@cca.io> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 9f3f1d7 commit 32edc0a

7 files changed

Lines changed: 108 additions & 13 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Find compiled files under the module's name as written before its uncapitalized form, so error messages on case-insensitive file systems show `ABC.cmi` instead of `aBC.cmi`.

‎compiler/core/bs_cmi_load.ml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,6 @@ let load_cmi ~unit_name : Env.Persistent_signature.t option =
5050
| exception Unix.Unix_error (error, _, _) ->
5151
raise (Sys_error (path ^ ": " ^ Unix.error_message error)))
5252
in
53-
try_names (if lower_name = name then [name] else [lower_name; name])
53+
try_names (if lower_name = name then [name] else [name; lower_name])
5454
in
5555
find (Config.get_load_path ())

‎compiler/core/config_util.ml‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,21 @@
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

25+
(* Try the name as written before its uncapitalized form: on a
26+
case-insensitive file system both match, and the result is shown in error
27+
messages and used in generated import paths. *)
2528
let find_in_path_uncap path name =
2629
let uname = Ext_string.uncapitalize_ascii name in
2730
let rec try_dir = function
2831
| [] -> None
2932
| dir :: rem ->
30-
let ufullname = Filename.concat dir uname in
31-
if Sys.file_exists (Compiler_request_state.resolve_path ufullname) then
32-
Some ufullname
33+
let fullname = Filename.concat dir name in
34+
if Sys.file_exists (Compiler_request_state.resolve_path fullname) then
35+
Some fullname
3336
else
34-
let fullname = Filename.concat dir name in
35-
if Sys.file_exists (Compiler_request_state.resolve_path fullname) then
36-
Some fullname
37+
let ufullname = Filename.concat dir uname in
38+
if Sys.file_exists (Compiler_request_state.resolve_path ufullname) then
39+
Some ufullname
3740
else try_dir rem
3841
in
3942
try_dir path

‎compiler/ext/misc.ml‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ let find_in_path_uncap path name =
6868
| dir :: rem ->
6969
let fullname = Filename.concat dir name
7070
and ufullname = Filename.concat dir uname in
71-
if Sys.file_exists (Compiler_request_state.resolve_path ufullname) then
72-
ufullname
73-
else if Sys.file_exists (Compiler_request_state.resolve_path fullname)
74-
then fullname
71+
if Sys.file_exists (Compiler_request_state.resolve_path fullname) then
72+
fullname
73+
else if Sys.file_exists (Compiler_request_state.resolve_path ufullname)
74+
then ufullname
7575
else try_dir rem
7676
in
7777
try_dir path

‎compiler/ext/misc.mli‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ val protect_refs : ref_and_value list -> (unit -> 'a) -> 'a
3838
even if [f] raises an exception. *)
3939

4040
val find_in_path_uncap : string list -> string -> string
41-
(* Same, but search also for uncapitalized name, i.e.
41+
(* Search the path for name, then for its uncapitalized form, i.e.
4242
if name is Foo.ml, allow /path/Foo.ml and /path/foo.ml
43-
to match. *)
43+
to match, preferring the former. *)
4444

4545
val remove_file : string -> unit
4646
(* Delete the given file if it exists. Never raise an error. *)
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
open OUnit
2+
3+
(* Load-path lookups accept a module's file under its name as written and
4+
uncapitalized. They return the name as written when it exists, because on a
5+
case-insensitive file system both spellings name the same file and the
6+
result shows up in error messages and import paths. *)
7+
8+
let rec remove_tree path =
9+
match (Unix.lstat path).st_kind with
10+
| Unix.S_DIR ->
11+
Array.iter
12+
(fun name -> remove_tree (Filename.concat path name))
13+
(Sys.readdir path);
14+
Unix.rmdir path
15+
| _ -> Sys.remove path
16+
| exception Unix.Unix_error (Unix.ENOENT, _, _) -> ()
17+
18+
let with_temp_dir action =
19+
let path = Filename.temp_file "rescript-find-in-path-" "" in
20+
Sys.remove path;
21+
Unix.mkdir path 0o755;
22+
let path = Unix.realpath path in
23+
Fun.protect ~finally:(fun () -> remove_tree path) (fun () -> action path)
24+
25+
let write_cmi path =
26+
ignore
27+
(Cmi_format.create_cmi path
28+
{
29+
Cmi_format.cmi_name = "Abc";
30+
cmi_sign = [];
31+
cmi_crcs = [];
32+
cmi_flags = [];
33+
}
34+
: Digest.t)
35+
36+
(* [files] are created in a temporary directory, which is the load path while
37+
[check] runs with that directory. *)
38+
let in_load_path files check =
39+
with_temp_dir (fun root ->
40+
List.iter (fun name -> write_cmi (Filename.concat root name)) files;
41+
Compiler_request_state.with_fresh ~cwd:root (fun () ->
42+
Config.set_load_path [root];
43+
check root))
44+
45+
let printer = Fun.id
46+
47+
let lookups =
48+
[
49+
( "misc",
50+
fun name ->
51+
Misc.find_in_path_uncap (Config.get_load_path ()) (name ^ ".cmi") );
52+
( "config_util",
53+
fun name -> Option.get (Config_util.find_opt (name ^ ".cmi")) );
54+
( "bs_cmi_load",
55+
fun name ->
56+
(Option.get (Bs_cmi_load.load_cmi ~unit_name:name))
57+
.Env.Persistent_signature.filename );
58+
]
59+
60+
let as_written_first_tests _ =
61+
List.iter
62+
(fun (lookup_name, lookup) ->
63+
in_load_path ["Abc.cmi"; "abc.cmi"] (fun root ->
64+
assert_equal ~printer ~msg:lookup_name
65+
(Filename.concat root "Abc.cmi")
66+
(lookup "Abc"));
67+
in_load_path ["Abc.cmi"] (fun root ->
68+
assert_equal ~printer ~msg:lookup_name
69+
(Filename.concat root "Abc.cmi")
70+
(lookup "Abc")))
71+
lookups
72+
73+
(* Only a case-sensitive file system tells the two spellings apart. *)
74+
let uncapitalized_fallback_tests _ =
75+
List.iter
76+
(fun (lookup_name, lookup) ->
77+
in_load_path ["abc.cmi"] (fun root ->
78+
let found = lookup "Abc" in
79+
if not (Sys.file_exists (Filename.concat root "Abc.cmi")) then
80+
assert_equal ~printer ~msg:lookup_name
81+
(Filename.concat root "abc.cmi")
82+
found))
83+
lookups
84+
85+
let suites =
86+
__FILE__
87+
>::: [
88+
"as_written_first" >:: as_written_first_tests;
89+
"uncapitalized_fallback" >:: uncapitalized_fallback_tests;
90+
]

‎tests/ounit_tests/ounit_tests_main.ml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ let suites =
3535
Ounit_typeopt_tests.suites;
3636
Ounit_gentype_tests.suites;
3737
Ounit_compiler_request_tests.suites;
38+
Ounit_find_in_path_tests.suites;
3839
]
3940

4041
let _ = OUnit.run_test_tt_main suites

0 commit comments

Comments
 (0)