raz / commit 5d91876d

commit
5d91876daa3bba964621e030193d94d36d481493
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
4e3969aa
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.

patch · log

3 files changed, +75 -11

doc/compiler.md (history) modified +6 -2

@@ -23,7 +23,7 @@
2323 | `src/X64.raz` | 1,621 | 1,330 | The instruction encoder, jumps within a procedure, the assembler |
2424 | `src/Gen.raz` | 2,841 | 1,987 | The stack machine, for x86-64: values, operations, checks, calls, frames |
2525 | `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 |
2727 | `src/Link.raz` | 408 | 316 | Tree shaking, layout, patching, the places |
2828 | `src/Elf.raz` | 450 | 356 | Static and dynamic ELF, symbol table, line numbers |
2929 | `src/Raz.raz` | 419 | 334 | The command line |
@@ -1908,7 +1908,11 @@
19081908 module.
19091909
19101910 `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
19121916 `RAZ_PATH` directory that holds a file `RAZ-VERSION` gets a line
19131917 (`libraryNote`, `majorMinor`) saying the raz its library was written
19141918 for and this one's, "0.21" for `0.21.0-5-g38d29a8`; the compiler only

src/Parse.raz (history) modified +19 -7

@@ -4530,6 +4530,15 @@
45304530 end;
45314531 end;
45324532
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+
45334542 (* Reads every other file of module n in dir, and checks its interface
45344543 against m, compiled from the file named chosen. *)
45354544 proc checkSiblings(dir, chosen: []const U8; n: INT; m: PModule; args: Sym.Args): ERROR;
@@ -4551,7 +4560,7 @@
45514560 until false;
45524561 Sys.closeDir(dp);
45534562 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);
45554564 compareIface(m, sib);
45564565 end;
45574566 return nil;
@@ -4614,7 +4623,7 @@
46144623 dir := searchDir(i);
46154624 for j := 0 to 3 do
46164625 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;
46184627 end;
46194628 end;
46204629 return "", "", false;
@@ -4627,7 +4636,7 @@
46274636 var
46284637 i, j, dev, ino, mineDev, mineIno: INT; past, chosen: BOOL; g, files, others: []const U8;
46294638 begin
4630- mineDev, mineIno := identity(say2("{}/{}", dir, f));
4639+ mineDev, mineIno := identity(modulePath(dir, f));
46314640 others := "";
46324641 past := false;
46334642 for i := -1 to numInc + LEN(userDirs) do
@@ -4635,7 +4644,7 @@
46354644 files := "";
46364645 chosen := false;
46374646 for j := 0 to 3 do
4638- g := say2("{}/{}", searchDir(i), fileName(n, j));
4647+ g := modulePath(searchDir(i), fileName(n, j));
46394648 if exists(g) then
46404649 files := say2("{} and {}", files, Diag.shownPath(g));
46414650 dev, ino := identity(g);
@@ -4647,7 +4656,7 @@
46474656 past := past or (searchDir(i) = dir);
46484657 end;
46494658 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));
46514660 Diag.help("a program has one module of each name; a symbolic link to one of these files chooses it");
46524661 return Scan.Failed;
46534662 end;
@@ -4672,7 +4681,7 @@
46724681 end;
46734682 try checkOneModule(name, dir, f, args.at);
46744683 originNote := libraryNote(dir);
4675- m := try compileFile(say2("{}/{}", dir, f), name, args, false);
4684+ m := try compileFile(modulePath(dir, f), name, args, false);
46764685 try checkSiblings(dir, f, name, m, args);
46774686 return m, nil;
46784687 end;
@@ -4773,7 +4782,10 @@
47734782 none.n := 0;
47744783 i := LEN(path);
47754784 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;
47774789 base := path[i..LEN(path)];
47784790 i := 0;
47794791 while (i < LEN(base)) and (base[i] # '.') do INC(i); end;

test/library-path.sh (history) modified +50 -2

@@ -190,6 +190,24 @@
190190 succeeds $flags
191191 }
192192
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+
193211 # Module identity is the file's device and inode: a hard link to the
194212 # chosen file is the same file.
195213 t_hard_link_chooses_same_module() {
@@ -213,6 +231,35 @@
213231 fails "module PathLib is in"
214232 }
215233
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+
216263 for t in t_default t_colon_in_home t_empty_disables_default \
217264 t_list_replaces_default t_relative_and_empty_entries t_missing_home \
218265 t_no_recursion t_missing_module_lists_all_search_directories \
@@ -220,9 +267,10 @@
220267 t_duplicate_main_and_include t_duplicate_user_directories \
221268 t_duplicate_standard_library \
222269 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 \
224271 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
226274 runcase $t
227275 done
228276