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.
| @@ -23,7 +23,7 @@ | ||
| 23 | 23 | | `src/X64.raz` | 1,621 | 1,330 | The instruction encoder, jumps within a procedure, the assembler | |
| 24 | 24 | | `src/Gen.raz` | 2,841 | 1,987 | The stack machine, for x86-64: values, operations, checks, calls, frames | |
| 25 | 25 | | `src/Item.raz` | 2,553 | 2,051 | Items: types, constants, variables, operators, conversions, calls | |
| 26 | -| `src/Parse.raz` | 4,806 | 4,093 | Declarations, statements, expressions, built-ins, recovery, warnings | | |
| 26 | +| `src/Parse.raz` | 4,816 | 4,098 | Declarations, statements, expressions, built-ins, recovery, warnings | | |
| 27 | 27 | | `src/Link.raz` | 408 | 316 | Tree shaking, layout, patching, the places | |
| 28 | 28 | | `src/Elf.raz` | 450 | 356 | Static and dynamic ELF, symbol table, line numbers | |
| 29 | 29 | | `src/Raz.raz` | 419 | 334 | The command line | |
| @@ -1908,7 +1908,11 @@ | ||
| 1908 | 1908 | module. |
| 1909 | 1909 | |
| 1910 | 1910 | `importModule` finds a module's file, trying the target's names first, |
| 1911 | -and compiles it if it isn't yet. A module found in an `-I` or | |
| 1911 | +and compiles it if it isn't yet. `compileMain` keeps `/` as the | |
| 1912 | +directory of a main file at the root, and `modulePath` joins a directory | |
| 1913 | +and a name without a second slash when the directory ends in one (`lib/`), | |
| 1914 | +so that listing a directory and opening its files name the same places, | |
| 1915 | +`/` too. A module found in an `-I` or | |
| 1912 | 1916 | `RAZ_PATH` directory that holds a file `RAZ-VERSION` gets a line |
| 1913 | 1917 | (`libraryNote`, `majorMinor`) saying the raz its library was written |
| 1914 | 1918 | for and this one's, "0.21" for `0.21.0-5-g38d29a8`; the compiler only |
| @@ -4530,6 +4530,15 @@ | ||
| 4530 | 4530 | end; |
| 4531 | 4531 | end; |
| 4532 | 4532 | |
| 4533 | +(* A directory and a module name, keeping the root's slash and not | |
| 4534 | + adding another when the directory already ends in one: "lib/" gives | |
| 4535 | + "lib/Sys.raz", not "lib//Sys.raz", and "/" gives "/Sys.raz". *) | |
| 4536 | +proc modulePath(dir, file: []const U8): []const U8; | |
| 4537 | +begin | |
| 4538 | + if (LEN(dir) > 0) and (dir[LEN(dir) - 1] = '/') then return say2("{}{}", dir, file); end; | |
| 4539 | + return say2("{}/{}", dir, file); | |
| 4540 | +end; | |
| 4541 | + | |
| 4533 | 4542 | (* Reads every other file of module n in dir, and checks its interface |
| 4534 | 4543 | against m, compiled from the file named chosen. *) |
| 4535 | 4544 | proc checkSiblings(dir, chosen: []const U8; n: INT; m: PModule; args: Sym.Args): ERROR; |
| @@ -4551,7 +4560,7 @@ | ||
| 4551 | 4560 | until false; |
| 4552 | 4561 | Sys.closeDir(dp); |
| 4553 | 4562 | for i := 0 to numOthers - 1 do |
| 4554 | - sib := try compileFile(say2("{}/{}", dir, others[i]), n, args, true); | |
| 4563 | + sib := try compileFile(modulePath(dir, others[i]), n, args, true); | |
| 4555 | 4564 | compareIface(m, sib); |
| 4556 | 4565 | end; |
| 4557 | 4566 | return nil; |
| @@ -4614,7 +4623,7 @@ | ||
| 4614 | 4623 | dir := searchDir(i); |
| 4615 | 4624 | for j := 0 to 3 do |
| 4616 | 4625 | f := fileName(n, j); |
| 4617 | - if exists(say2("{}/{}", dir, f)) then return dir, f, true; end; | |
| 4626 | + if exists(modulePath(dir, f)) then return dir, f, true; end; | |
| 4618 | 4627 | end; |
| 4619 | 4628 | end; |
| 4620 | 4629 | return "", "", false; |
| @@ -4627,7 +4636,7 @@ | ||
| 4627 | 4636 | var |
| 4628 | 4637 | i, j, dev, ino, mineDev, mineIno: INT; past, chosen: BOOL; g, files, others: []const U8; |
| 4629 | 4638 | begin |
| 4630 | - mineDev, mineIno := identity(say2("{}/{}", dir, f)); | |
| 4639 | + mineDev, mineIno := identity(modulePath(dir, f)); | |
| 4631 | 4640 | others := ""; |
| 4632 | 4641 | past := false; |
| 4633 | 4642 | for i := -1 to numInc + LEN(userDirs) do |
| @@ -4635,7 +4644,7 @@ | ||
| 4635 | 4644 | files := ""; |
| 4636 | 4645 | chosen := false; |
| 4637 | 4646 | for j := 0 to 3 do |
| 4638 | - g := say2("{}/{}", searchDir(i), fileName(n, j)); | |
| 4647 | + g := modulePath(searchDir(i), fileName(n, j)); | |
| 4639 | 4648 | if exists(g) then |
| 4640 | 4649 | files := say2("{} and {}", files, Diag.shownPath(g)); |
| 4641 | 4650 | dev, ino := identity(g); |
| @@ -4647,7 +4656,7 @@ | ||
| 4647 | 4656 | past := past or (searchDir(i) = dir); |
| 4648 | 4657 | end; |
| 4649 | 4658 | if LEN(others) = 0 then return nil; end; |
| 4650 | - Diag.error(at, say3("module {} is in {}{}", nameOf(n), Diag.shownPath(say2("{}/{}", dir, f)), others)); | |
| 4659 | + Diag.error(at, say3("module {} is in {}{}", nameOf(n), Diag.shownPath(modulePath(dir, f)), others)); | |
| 4651 | 4660 | Diag.help("a program has one module of each name; a symbolic link to one of these files chooses it"); |
| 4652 | 4661 | return Scan.Failed; |
| 4653 | 4662 | end; |
| @@ -4672,7 +4681,7 @@ | ||
| 4672 | 4681 | end; |
| 4673 | 4682 | try checkOneModule(name, dir, f, args.at); |
| 4674 | 4683 | originNote := libraryNote(dir); |
| 4675 | - m := try compileFile(say2("{}/{}", dir, f), name, args, false); | |
| 4684 | + m := try compileFile(modulePath(dir, f), name, args, false); | |
| 4676 | 4685 | try checkSiblings(dir, f, name, m, args); |
| 4677 | 4686 | return m, nil; |
| 4678 | 4687 | end; |
| @@ -4773,7 +4782,10 @@ | ||
| 4773 | 4782 | none.n := 0; |
| 4774 | 4783 | i := LEN(path); |
| 4775 | 4784 | while (i > 0) and (path[i - 1] # '/') do DEC(i); end; |
| 4776 | - if i = 0 then mainDir := "."; else mainDir := path[0..i - 1]; end; | |
| 4785 | + (* the directory is the path less its last "/"; for "/Main.raz" that would be "", so keep the root *) | |
| 4786 | + if i = 0 then mainDir := "."; | |
| 4787 | + elsif i = 1 then mainDir := "/"; | |
| 4788 | + else mainDir := path[0..i - 1]; end; | |
| 4777 | 4789 | base := path[i..LEN(path)]; |
| 4778 | 4790 | i := 0; |
| 4779 | 4791 | while (i < LEN(base)) and (base[i] # '.') do INC(i); end; |
| @@ -190,6 +190,24 @@ | ||
| 190 | 190 | succeeds $flags |
| 191 | 191 | } |
| 192 | 192 | |
| 193 | +# A directory given with a trailing slash, as -I or in RAZ_PATH, joins to | |
| 194 | +# a module's name with no second slash, in the messages too. | |
| 195 | +t_trailing_slash_keeps_sibling_paths() { | |
| 196 | + dir=$root/target | |
| 197 | + library "$dir" 42 .linux-x86_64 | |
| 198 | + echo "module PathLib; const other* = 42; end PathLib." >"$dir/PathLib.raz" | |
| 199 | + for source in include user; do | |
| 200 | + if [ $source = include ]; then RAZ_PATH=; else RAZ_PATH=$dir/; fi | |
| 201 | + export RAZ_PATH | |
| 202 | + if [ $source = include ]; then compile -I "$dir/"; else compile; fi | |
| 203 | + [ $? = 1 ] || { echo "$source: expected status 1"; return 1; } | |
| 204 | + grep -F -q "isn't exported as in" "$root/err" || { echo "$source:"; cat "$root/err"; return 1; } | |
| 205 | + grep -F -q "$dir/PathLib.linux-x86_64.raz" "$root/err" || { echo "$source:"; cat "$root/err"; return 1; } | |
| 206 | + grep -F -q "$dir/PathLib.raz" "$root/err" || { echo "$source:"; cat "$root/err"; return 1; } | |
| 207 | + if grep -F -q "//" "$root/err"; then echo "$source: //"; cat "$root/err"; return 1; fi | |
| 208 | + done | |
| 209 | +} | |
| 210 | + | |
| 193 | 211 | # Module identity is the file's device and inode: a hard link to the |
| 194 | 212 | # chosen file is the same file. |
| 195 | 213 | t_hard_link_chooses_same_module() { |
| @@ -213,6 +231,35 @@ | ||
| 213 | 231 | fails "module PathLib is in" |
| 214 | 232 | } |
| 215 | 233 | |
| 234 | +# The main file at the root of a file system has "/" for its directory, | |
| 235 | +# not "": its module's files for other targets are still compared. Needs | |
| 236 | +# a chroot, made by an unprivileged user namespace; skipped without one. | |
| 237 | +t_root_directory_interface() { | |
| 238 | + if ! unshare -r true 2>/dev/null; then | |
| 239 | + echo "skipped t_root_directory_interface: unshare -r not permitted" | |
| 240 | + return 0 | |
| 241 | + fi | |
| 242 | + r=$root/chroot | |
| 243 | + mkdir -p "$r/out" | |
| 244 | + cp "$raz" "$r/raz" | |
| 245 | + cp -R "$(dirname "$raz")/lib" "$r/lib" | |
| 246 | + cat >"$r/iface.raz" <<'END' | |
| 247 | +module iface; | |
| 248 | +import Out, Variant; | |
| 249 | +proc main*; | |
| 250 | +begin Out.int(Variant.f); Out.ln; end; | |
| 251 | +end iface. | |
| 252 | +END | |
| 253 | + echo "module Variant; const f* = 1; end Variant." >"$r/Variant.raz" | |
| 254 | + # the target's file exports f too, so only the interface check can | |
| 255 | + # refuse it: unchecked, the program builds and prints 2 | |
| 256 | + echo "module Variant; const f* = 2; const g* = 1; end Variant." >"$r/Variant.linux-x86_64.raz" | |
| 257 | + unshare -r chroot "$r" /raz -o /out/app /iface.raz >"$root/out" 2>"$root/err" | |
| 258 | + [ $? = 1 ] || { echo "expected status 1"; cat "$root/err"; return 1; } | |
| 259 | + grep -F -q "isn't exported as in" "$root/err" || { cat "$root/err"; return 1; } | |
| 260 | + if grep -F -q "//" "$root/err"; then cat "$root/err"; return 1; fi | |
| 261 | +} | |
| 262 | + | |
| 216 | 263 | for t in t_default t_colon_in_home t_empty_disables_default \ |
| 217 | 264 | t_list_replaces_default t_relative_and_empty_entries t_missing_home \ |
| 218 | 265 | t_no_recursion t_missing_module_lists_all_search_directories \ |
| @@ -220,9 +267,10 @@ | ||
| 220 | 267 | t_duplicate_main_and_include t_duplicate_user_directories \ |
| 221 | 268 | t_duplicate_standard_library \ |
| 222 | 269 | t_user_paths_do_not_consume_include_slots \ |
| 223 | - t_hard_link_chooses_same_module \ | |
| 270 | + t_trailing_slash_keeps_sibling_paths t_hard_link_chooses_same_module \ | |
| 224 | 271 | t_equal_contents_do_not_choose_distinct_module \ |
| 225 | - t_symbolic_link_to_distinct_module_does_not_choose; do | |
| 272 | + t_symbolic_link_to_distinct_module_does_not_choose \ | |
| 273 | + t_root_directory_interface; do | |
| 226 | 274 | runcase $t |
| 227 | 275 | done |
| 228 | 276 |