From 5d91876daa3bba964621e030193d94d36d481493 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Mon, 26 Oct 2026 17:07:36 +0000 Subject: [PATCH] Preserve root directories when resolving module files A main file at the root (/Main.raz) had "" for its directory, and a directory given as lib/ made lib//Sys.raz, so listing a directory and opening its files could name different places: with the files at /, a target file whose interface differs was used with no error. compileMain keeps "/", and modulePath joins without doubling a slash. Tests: a trailing-slash case and a chroot case for the root. --- diff --git a/doc/compiler.md b/doc/compiler.md index 4c66ef3..6a01b09 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -23,7 +23,7 @@ | `src/X64.raz` | 1,621 | 1,330 | The instruction encoder, jumps within a procedure, the assembler | | `src/Gen.raz` | 2,841 | 1,987 | The stack machine, for x86-64: values, operations, checks, calls, frames | | `src/Item.raz` | 2,553 | 2,051 | Items: types, constants, variables, operators, conversions, calls | -| `src/Parse.raz` | 4,806 | 4,093 | Declarations, statements, expressions, built-ins, recovery, warnings | +| `src/Parse.raz` | 4,816 | 4,098 | Declarations, statements, expressions, built-ins, recovery, warnings | | `src/Link.raz` | 408 | 316 | Tree shaking, layout, patching, the places | | `src/Elf.raz` | 450 | 356 | Static and dynamic ELF, symbol table, line numbers | | `src/Raz.raz` | 419 | 334 | The command line | @@ -1908,7 +1908,11 @@ module. `importModule` finds a module's file, trying the target's names first, -and compiles it if it isn't yet. A module found in an `-I` or +and compiles it if it isn't yet. `compileMain` keeps `/` as the +directory of a main file at the root, and `modulePath` joins a directory +and a name without a second slash when the directory ends in one (`lib/`), +so that listing a directory and opening its files name the same places, +`/` too. A module found in an `-I` or `RAZ_PATH` directory that holds a file `RAZ-VERSION` gets a line (`libraryNote`, `majorMinor`) saying the raz its library was written for and this one's, "0.21" for `0.21.0-5-g38d29a8`; the compiler only diff --git a/src/Parse.raz b/src/Parse.raz index fb9268d..013ef99 100644 --- a/src/Parse.raz +++ b/src/Parse.raz @@ -4530,6 +4530,15 @@ end; end; +(* A directory and a module name, keeping the root's slash and not + adding another when the directory already ends in one: "lib/" gives + "lib/Sys.raz", not "lib//Sys.raz", and "/" gives "/Sys.raz". *) +proc modulePath(dir, file: []const U8): []const U8; +begin + if (LEN(dir) > 0) and (dir[LEN(dir) - 1] = '/') then return say2("{}{}", dir, file); end; + return say2("{}/{}", dir, file); +end; + (* Reads every other file of module n in dir, and checks its interface against m, compiled from the file named chosen. *) proc checkSiblings(dir, chosen: []const U8; n: INT; m: PModule; args: Sym.Args): ERROR; @@ -4551,7 +4560,7 @@ until false; Sys.closeDir(dp); for i := 0 to numOthers - 1 do - sib := try compileFile(say2("{}/{}", dir, others[i]), n, args, true); + sib := try compileFile(modulePath(dir, others[i]), n, args, true); compareIface(m, sib); end; return nil; @@ -4614,7 +4623,7 @@ dir := searchDir(i); for j := 0 to 3 do f := fileName(n, j); - if exists(say2("{}/{}", dir, f)) then return dir, f, true; end; + if exists(modulePath(dir, f)) then return dir, f, true; end; end; end; return "", "", false; @@ -4627,7 +4636,7 @@ var i, j, dev, ino, mineDev, mineIno: INT; past, chosen: BOOL; g, files, others: []const U8; begin - mineDev, mineIno := identity(say2("{}/{}", dir, f)); + mineDev, mineIno := identity(modulePath(dir, f)); others := ""; past := false; for i := -1 to numInc + LEN(userDirs) do @@ -4635,7 +4644,7 @@ files := ""; chosen := false; for j := 0 to 3 do - g := say2("{}/{}", searchDir(i), fileName(n, j)); + g := modulePath(searchDir(i), fileName(n, j)); if exists(g) then files := say2("{} and {}", files, Diag.shownPath(g)); dev, ino := identity(g); @@ -4647,7 +4656,7 @@ past := past or (searchDir(i) = dir); end; if LEN(others) = 0 then return nil; end; - Diag.error(at, say3("module {} is in {}{}", nameOf(n), Diag.shownPath(say2("{}/{}", dir, f)), others)); + Diag.error(at, say3("module {} is in {}{}", nameOf(n), Diag.shownPath(modulePath(dir, f)), others)); Diag.help("a program has one module of each name; a symbolic link to one of these files chooses it"); return Scan.Failed; end; @@ -4672,7 +4681,7 @@ end; try checkOneModule(name, dir, f, args.at); originNote := libraryNote(dir); - m := try compileFile(say2("{}/{}", dir, f), name, args, false); + m := try compileFile(modulePath(dir, f), name, args, false); try checkSiblings(dir, f, name, m, args); return m, nil; end; @@ -4773,7 +4782,10 @@ none.n := 0; i := LEN(path); while (i > 0) and (path[i - 1] # '/') do DEC(i); end; - if i = 0 then mainDir := "."; else mainDir := path[0..i - 1]; end; + (* the directory is the path less its last "/"; for "/Main.raz" that would be "", so keep the root *) + if i = 0 then mainDir := "."; + elsif i = 1 then mainDir := "/"; + else mainDir := path[0..i - 1]; end; base := path[i..LEN(path)]; i := 0; while (i < LEN(base)) and (base[i] # '.') do INC(i); end; diff --git a/test/library-path.sh b/test/library-path.sh index 1fb4c3e..ba7ea3e 100755 --- a/test/library-path.sh +++ b/test/library-path.sh @@ -190,6 +190,24 @@ succeeds $flags } +# A directory given with a trailing slash, as -I or in RAZ_PATH, joins to +# a module's name with no second slash, in the messages too. +t_trailing_slash_keeps_sibling_paths() { + dir=$root/target + library "$dir" 42 .linux-x86_64 + echo "module PathLib; const other* = 42; end PathLib." >"$dir/PathLib.raz" + for source in include user; do + if [ $source = include ]; then RAZ_PATH=; else RAZ_PATH=$dir/; fi + export RAZ_PATH + if [ $source = include ]; then compile -I "$dir/"; else compile; fi + [ $? = 1 ] || { echo "$source: expected status 1"; return 1; } + grep -F -q "isn't exported as in" "$root/err" || { echo "$source:"; cat "$root/err"; return 1; } + grep -F -q "$dir/PathLib.linux-x86_64.raz" "$root/err" || { echo "$source:"; cat "$root/err"; return 1; } + grep -F -q "$dir/PathLib.raz" "$root/err" || { echo "$source:"; cat "$root/err"; return 1; } + if grep -F -q "//" "$root/err"; then echo "$source: //"; cat "$root/err"; return 1; fi + done +} + # Module identity is the file's device and inode: a hard link to the # chosen file is the same file. t_hard_link_chooses_same_module() { @@ -213,6 +231,35 @@ fails "module PathLib is in" } +# The main file at the root of a file system has "/" for its directory, +# not "": its module's files for other targets are still compared. Needs +# a chroot, made by an unprivileged user namespace; skipped without one. +t_root_directory_interface() { + if ! unshare -r true 2>/dev/null; then + echo "skipped t_root_directory_interface: unshare -r not permitted" + return 0 + fi + r=$root/chroot + mkdir -p "$r/out" + cp "$raz" "$r/raz" + cp -R "$(dirname "$raz")/lib" "$r/lib" + cat >"$r/iface.raz" <<'END' +module iface; +import Out, Variant; +proc main*; +begin Out.int(Variant.f); Out.ln; end; +end iface. +END + echo "module Variant; const f* = 1; end Variant." >"$r/Variant.raz" + # the target's file exports f too, so only the interface check can + # refuse it: unchecked, the program builds and prints 2 + echo "module Variant; const f* = 2; const g* = 1; end Variant." >"$r/Variant.linux-x86_64.raz" + unshare -r chroot "$r" /raz -o /out/app /iface.raz >"$root/out" 2>"$root/err" + [ $? = 1 ] || { echo "expected status 1"; cat "$root/err"; return 1; } + grep -F -q "isn't exported as in" "$root/err" || { cat "$root/err"; return 1; } + if grep -F -q "//" "$root/err"; then cat "$root/err"; return 1; fi +} + for t in t_default t_colon_in_home t_empty_disables_default \ t_list_replaces_default t_relative_and_empty_entries t_missing_home \ t_no_recursion t_missing_module_lists_all_search_directories \ @@ -220,9 +267,10 @@ t_duplicate_main_and_include t_duplicate_user_directories \ t_duplicate_standard_library \ t_user_paths_do_not_consume_include_slots \ - t_hard_link_chooses_same_module \ + t_trailing_slash_keeps_sibling_paths t_hard_link_chooses_same_module \ t_equal_contents_do_not_choose_distinct_module \ - t_symbolic_link_to_distinct_module_does_not_choose; do + t_symbolic_link_to_distinct_module_does_not_choose \ + t_root_directory_interface; do runcase $t done