raz / commit 4e3969aa

commit
4e3969aaab4df31eb0d1b8dd87b81c8f5db36c2b
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
f9e263c5
Use portable file metadata for module identity

Parse's identity took a file's device and inode from a raw SYSCALL(4)
stat into a guessed [18]INT, which is wrong off x86-64 Linux. It asks
Sys.info now. test/library-path.py is ported to POSIX sh, with the
branch's hard link and symbolic link cases.

patch · log

5 files changed, +238 -182

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

@@ -1914,7 +1914,8 @@
19141914 for and this one's, "0.21" for `0.21.0-5-g38d29a8`; the compiler only
19151915 reads the file. `checkOneModule` first makes sure no
19161916 later directory has the module too, unless the file is a link to one
1917-of its files there; `checkSiblings` then reads the files for other
1917+of its files there (a file's identity is its device and inode from `Sys.info`,
1918+which follows a symbolic link, as on every target); `checkSiblings` then reads the files for other
19181919 targets beside it, declarations only, and `compareIface` holds them
19191920 to the same exports by their fingerprints. A generic module is
19201921 compiled once per distinct list of arguments, where it is first used.

src/Parse.raz (history) modified +4 -6

@@ -4297,13 +4297,11 @@
42974297 (* Which file path is, as its device and inode: two names of one file
42984298 have the same. Zero for a file that can't be asked about. *)
42994299 proc identity(path: []const U8): (INT, INT);
4300-var a: [4096]U8; st: [18]INT; (* struct stat: st_dev, then st_ino *)
4300+var i: Sys.Info; err: INT; (* Sys.info follows a symbolic link, and is the same on every target *)
43014301 begin
4302- if LEN(path) >= 4096 then return 0, 0; end;
4303- COPY(a[0..LEN(path)], path);
4304- a[LEN(path)] := 0;
4305- if SYSTEM.SYSCALL(4, SYSTEM.ADR(a[0]), SYSTEM.ADR(st)) < 0 then return 0, 0; end;
4306- return st[0], st[1];
4302+ i, err := Sys.info(path);
4303+ if err # 0 then return 0, 0; end;
4304+ return INT(i.dev), INT(i.ino);
43074305 end;
43084306
43094307 (* s, in memory that is never freed, for what must outlast the buffer it is in *)

test/check.sh (history) modified +2 -2

@@ -21,7 +21,7 @@
2121
2222 raz=./raz
2323 # Fixtures use only their explicit module paths and the standard library.
24-# library-path.py sets up its own HOME and RAZ_PATH for user-path checks.
24+# library-path.sh sets up its own HOME and RAZ_PATH for user-path checks.
2525 RAZ_PATH=
2626 export RAZ_PATH
2727 out=build/check
@@ -403,7 +403,7 @@
403403 if test/editors.sh; then pass=$((pass + 1)); else fail=$((fail + 1)); fi
404404
405405 # Installed source libraries: default, explicit and isolated lookup.
406-if python3 test/library-path.py "$raz"; then pass=$((pass + 1)); else fail=$((fail + 1)); fi
406+if test/library-path.sh "$raz"; then pass=$((pass + 1)); else fail=$((fail + 1)); fi
407407
408408 echo "$pass passed, $fail failed"
409409 [ $fail = 0 ]

test/library-path.py deleted +0 -173

@@ -1,173 +0,0 @@
1-#!/usr/bin/env python3
2-"""Exercise the compiler's library lookup in an isolated environment."""
3-
4-import os
5-from pathlib import Path
6-import subprocess
7-import sys
8-import tempfile
9-import unittest
10-
11-
12-RAZ = str(Path(sys.argv.pop(1)).resolve())
13-
14-
15-class LibraryPaths(unittest.TestCase):
16- def setUp(self):
17- self.scratch = tempfile.TemporaryDirectory(prefix="raz-path-")
18- self.addCleanup(self.scratch.cleanup)
19- self.root = Path(self.scratch.name)
20- self.env = dict(os.environ)
21- self.env.pop("RAZ_PATH", None)
22- self.env.pop("RAZ_PROFILE", None)
23- self.env["HOME"] = str(self.root / "home space")
24- self.default = Path(self.env["HOME"]) / ".raz/lib"
25- self.main = self.root / "src/Main.raz"
26- self.write(self.main, """module Main;
27-import Out, PathLib;
28-proc main*;
29-begin Out.int(PathLib.value); Out.ln; end;
30-end Main.
31-""")
32-
33- def write(self, path, text):
34- path.parent.mkdir(parents=True, exist_ok=True)
35- path.write_text(text)
36-
37- def library(self, directory, value=42, suffix=""):
38- path = directory / f"PathLib{suffix}.raz"
39- self.write(path, f"module PathLib; const value* = {value}; end PathLib.\n")
40- return path
41-
42- def compile(self, *flags, run=False):
43- args = [RAZ, "run"] if run else [RAZ, "-o", str(self.root / "app")]
44- return subprocess.run(
45- [*args, "-Werror", *map(str, flags), str(self.main)],
46- cwd=self.root, env=self.env, text=True, capture_output=True,
47- )
48-
49- def succeeds(self, *flags, run=False):
50- result = self.compile(*flags, run=run)
51- self.assertEqual(result.returncode, 0, result.stderr)
52- if not run:
53- result = subprocess.run(
54- [str(self.root / "app")], env=self.env,
55- text=True, capture_output=True,
56- )
57- self.assertEqual(result.returncode, 0, result.stderr)
58- self.assertEqual(result.stdout, "42\n")
59-
60- def fails(self, message, *flags):
61- result = self.compile(*flags)
62- self.assertEqual(result.returncode, 1, result.stderr)
63- self.assertIn(message, result.stderr)
64-
65- def test_default(self):
66- self.library(self.default)
67- self.succeeds()
68- self.succeeds(run=True)
69-
70- def test_colon_in_home(self):
71- # Unlike RAZ_PATH, HOME is one path, not a list.
72- self.env["HOME"] = str(self.root / "home : space")
73- self.library(Path(self.env["HOME"]) / ".raz/lib")
74- self.succeeds()
75-
76- def test_empty_disables_default(self):
77- self.library(self.default)
78- self.env["RAZ_PATH"] = ""
79- self.fails("can't find module PathLib")
80- # Explicit dependencies and the bundled standard library still work.
81- self.succeeds("-I", self.default)
82-
83- def test_list_replaces_default(self):
84- self.library(self.default, 99)
85- first = self.root / "first directory"
86- second = self.root / "second directory"
87- self.library(second)
88- self.env["RAZ_PATH"] = f"{first}:{second}"
89- self.succeeds()
90- self.succeeds(run=True)
91- self.env["RAZ_PATH"] = str(first)
92- self.fails("can't find module PathLib")
93-
94- def test_relative_and_empty_entries(self):
95- self.library(self.root / "relative")
96- # A cwd module must not be found through the empty entries.
97- self.library(self.root, 99)
98- self.env["RAZ_PATH"] = ":relative::missing:"
99- self.succeeds()
100- self.env["RAZ_PATH"] = ":::"
101- self.fails("can't find module PathLib")
102-
103- def test_missing_home(self):
104- self.env.pop("HOME")
105- self.fails("can't find module PathLib")
106- self.env["HOME"] = ""
107- self.fails("can't find module PathLib")
108- self.library(self.root / "explicit")
109- self.env["RAZ_PATH"] = "explicit"
110- self.succeeds()
111-
112- def test_no_recursion(self):
113- self.library(self.default / "package")
114- self.fails("can't find module PathLib")
115- self.env["RAZ_PATH"] = str(self.default / "package")
116- self.succeeds()
117-
118- def test_missing_module_lists_all_search_directories(self):
119- self.env["RAZ_PATH"] = "first:second"
120- result = self.compile("-I", "explicit")
121- self.assertEqual(result.returncode, 1, result.stderr)
122- self.assertIn("explicit, first, second,", result.stderr)
123- self.assertIn(str(Path(RAZ).parent / "lib"), result.stderr)
124-
125- def test_no_path_expansion(self):
126- self.library(self.default)
127- for path in ("~/.raz/lib", "$HOME/.raz/lib"):
128- self.env["RAZ_PATH"] = path
129- self.fails("can't find module PathLib")
130-
131- def test_target_file_and_interface(self):
132- self.library(self.default, suffix=".linux-x86_64")
133- self.library(self.default, 99)
134- self.succeeds()
135- self.write(self.default / "PathLib.raz",
136- "module PathLib; const other* = 42; end PathLib.\n")
137- self.fails("isn't exported")
138-
139- def test_duplicate_main_and_include(self):
140- chosen = self.library(self.default)
141- for directory in (self.main.parent, self.root / "include"):
142- duplicate = self.library(directory)
143- flags = ("-I", directory) if directory != self.main.parent else ()
144- self.fails("module PathLib is in", *flags)
145- duplicate.unlink()
146- duplicate.symlink_to(chosen)
147- self.succeeds(*flags)
148- duplicate.unlink()
149-
150- def test_duplicate_user_directories(self):
151- self.library(self.default)
152- other = self.root / "other"
153- self.library(other)
154- self.env["RAZ_PATH"] = f"{self.default}:{other}"
155- self.fails("module PathLib is in")
156- self.env["RAZ_PATH"] = f"{self.default}:{self.default}"
157- self.succeeds()
158-
159- def test_duplicate_standard_library(self):
160- self.library(self.default)
161- self.write(self.default / "Out.raz", "module Out; end Out.\n")
162- self.fails("module Out is in")
163-
164- def test_user_paths_do_not_consume_include_slots(self):
165- self.library(self.default)
166- flags = [arg for _ in range(16) for arg in ("-I", "missing")]
167- self.succeeds(*flags)
168- self.env["RAZ_PATH"] = ":".join(["missing"] * 32 + [str(self.default)])
169- self.succeeds(*flags)
170-
171-
172-if __name__ == "__main__":
173- unittest.main()

test/library-path.sh (history) added (executable) +230 -0

@@ -0,0 +1,230 @@
1+#!/bin/sh
2+# Exercises the compiler's library lookup in an isolated environment:
3+# the default library in $HOME/.raz/lib, RAZ_PATH, -I, the files a module
4+# has for other targets, and modules of one name found twice.
5+# Usage: test/library-path.sh path/to/raz
6+# Each case runs in a subshell with its own scratch directory and HOME.
7+
8+raz=$(cd "$(dirname "$1")" && pwd)/$(basename "$1")
9+top=$(mktemp -d "${TMPDIR:-/tmp}/raz-path-XXXXXX") || exit 1
10+trap 'rm -rf "$top"' EXIT
11+pass=0
12+fail=0
13+
14+# Sets up a fresh scratch directory $root, with HOME in it (its name has a
15+# space), the default library directory $default and the main program.
16+setup() {
17+ root=$top/$1
18+ mkdir -p "$root/src" "$root/home space"
19+ unset RAZ_PATH RAZ_PROFILE RUN
20+ HOME="$root/home space"
21+ export HOME
22+ default=$HOME/.raz/lib
23+ main=$root/src/Main.raz
24+ cat >"$main" <<'END'
25+module Main;
26+import Out, PathLib;
27+proc main*;
28+begin Out.int(PathLib.value); Out.ln; end;
29+end Main.
30+END
31+}
32+
33+# library DIR [VALUE [SUFFIX]] writes DIR/PathLib<SUFFIX>.raz.
34+library() {
35+ mkdir -p "$1"
36+ echo "module PathLib; const value* = ${2:-42}; end PathLib." >"$1/PathLib$3.raz"
37+}
38+
39+# compile FLAGS... builds the program, its messages in $root/err. With
40+# RUN set it runs it by "raz run" instead, its output in $root/out.
41+compile() {
42+ if [ -n "$RUN" ]; then
43+ (cd "$root" && "$raz" run -Werror "$@" "$main") >"$root/out" 2>"$root/err"
44+ else
45+ (cd "$root" && "$raz" -o "$root/app" -Werror "$@" "$main") >"$root/out" 2>"$root/err"
46+ fi
47+}
48+
49+# succeeds FLAGS...: it compiles, and the program prints 42.
50+succeeds() {
51+ compile "$@" || { echo "compile failed:"; cat "$root/err"; return 1; }
52+ [ -n "$RUN" ] || "$root/app" >"$root/out" || return 1
53+ [ "$(cat "$root/out")" = 42 ] || { echo "printed: $(cat "$root/out")"; return 1; }
54+}
55+
56+# fails MESSAGE FLAGS...: compiling gives status 1 and MESSAGE in its errors.
57+fails() {
58+ msg=$1
59+ shift
60+ compile "$@"
61+ [ $? = 1 ] || { echo "expected status 1"; cat "$root/err"; return 1; }
62+ grep -F -q -- "$msg" "$root/err" || { echo "no '$msg' in:"; cat "$root/err"; return 1; }
63+}
64+
65+# runcase NAME runs the function NAME in a subshell and counts it.
66+runcase() {
67+ if (setup "$1"; "$1"); then pass=$((pass + 1))
68+ else echo "FAIL library-path: $1"; fail=$((fail + 1)); fi
69+}
70+
71+t_default() {
72+ library "$default"
73+ succeeds || return 1
74+ RUN=1 succeeds
75+}
76+
77+t_colon_in_home() { # unlike RAZ_PATH, HOME is one path, not a list
78+ HOME="$root/home : space"
79+ export HOME
80+ library "$HOME/.raz/lib"
81+ succeeds
82+}
83+
84+t_empty_disables_default() {
85+ library "$default"
86+ RAZ_PATH=; export RAZ_PATH
87+ fails "can't find module PathLib" || return 1
88+ succeeds -I "$default" # explicit directories still work
89+}
90+
91+t_list_replaces_default() {
92+ library "$default" 99
93+ library "$root/second directory"
94+ RAZ_PATH="$root/first directory:$root/second directory"; export RAZ_PATH
95+ succeeds || return 1
96+ RUN=1 succeeds || return 1
97+ RAZ_PATH="$root/first directory"
98+ fails "can't find module PathLib"
99+}
100+
101+t_relative_and_empty_entries() {
102+ library "$root/relative"
103+ library "$root" 99 # a module in the cwd must not be found by an empty entry
104+ RAZ_PATH=:relative::missing:; export RAZ_PATH
105+ succeeds || return 1
106+ RAZ_PATH=:::
107+ fails "can't find module PathLib"
108+}
109+
110+t_missing_home() {
111+ unset HOME
112+ fails "can't find module PathLib" || return 1
113+ HOME=; export HOME
114+ fails "can't find module PathLib" || return 1
115+ library "$root/explicit"
116+ RAZ_PATH=explicit; export RAZ_PATH
117+ succeeds
118+}
119+
120+t_no_recursion() {
121+ library "$default/package"
122+ fails "can't find module PathLib" || return 1
123+ RAZ_PATH=$default/package; export RAZ_PATH
124+ succeeds
125+}
126+
127+t_missing_module_lists_all_search_directories() {
128+ RAZ_PATH=first:second; export RAZ_PATH
129+ compile -I explicit
130+ [ $? = 1 ] || return 1
131+ grep -F -q "explicit, first, second," "$root/err" || { cat "$root/err"; return 1; }
132+ grep -F -q "$(dirname "$raz")/lib" "$root/err" || { cat "$root/err"; return 1; }
133+}
134+
135+t_no_path_expansion() {
136+ library "$default"
137+ for p in '~/.raz/lib' '$HOME/.raz/lib'; do
138+ RAZ_PATH=$p; export RAZ_PATH
139+ fails "can't find module PathLib" || return 1
140+ done
141+}
142+
143+t_target_file_and_interface() {
144+ library "$default" 42 .linux-x86_64
145+ library "$default" 99
146+ succeeds || return 1
147+ echo "module PathLib; const other* = 42; end PathLib." >"$default/PathLib.raz"
148+ fails "isn't exported"
149+}
150+
151+t_duplicate_main_and_include() {
152+ library "$default"
153+ for dir in "$root/src" "$root/include"; do
154+ library "$dir"
155+ flags=
156+ [ "$dir" = "$root/src" ] || flags="-I $dir"
157+ fails "module PathLib is in" $flags || return 1
158+ rm "$dir/PathLib.raz"
159+ ln -s "$default/PathLib.raz" "$dir/PathLib.raz" # a link to the chosen file is no duplicate
160+ succeeds $flags || return 1
161+ rm "$dir/PathLib.raz"
162+ done
163+}
164+
165+t_duplicate_user_directories() {
166+ library "$default"
167+ library "$root/other"
168+ RAZ_PATH=$default:$root/other; export RAZ_PATH
169+ fails "module PathLib is in" || return 1
170+ RAZ_PATH=$default:$default
171+ succeeds
172+}
173+
174+t_duplicate_standard_library() {
175+ library "$default"
176+ echo "module Out; end Out." >"$default/Out.raz"
177+ fails "module Out is in"
178+}
179+
180+t_user_paths_do_not_consume_include_slots() {
181+ library "$default"
182+ flags=
183+ p=
184+ for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16; do flags="$flags -I missing"; done
185+ for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32; do
186+ p=${p}missing:
187+ done
188+ succeeds $flags || return 1
189+ RAZ_PATH=$p$default; export RAZ_PATH
190+ succeeds $flags
191+}
192+
193+# Module identity is the file's device and inode: a hard link to the
194+# chosen file is the same file.
195+t_hard_link_chooses_same_module() {
196+ library "$default"
197+ ln "$default/PathLib.raz" "$root/src/PathLib.raz"
198+ succeeds
199+}
200+
201+# ... and equal contents in another file are not.
202+t_equal_contents_do_not_choose_distinct_module() {
203+ library "$default"
204+ cp "$default/PathLib.raz" "$root/src/PathLib.raz"
205+ fails "module PathLib is in"
206+}
207+
208+# ... and a link to some other file is not the chosen one.
209+t_symbolic_link_to_distinct_module_does_not_choose() {
210+ library "$default"
211+ library "$root/elsewhere"
212+ ln -s "$root/elsewhere/PathLib.raz" "$root/src/PathLib.raz"
213+ fails "module PathLib is in"
214+}
215+
216+for t in t_default t_colon_in_home t_empty_disables_default \
217+ t_list_replaces_default t_relative_and_empty_entries t_missing_home \
218+ t_no_recursion t_missing_module_lists_all_search_directories \
219+ t_no_path_expansion t_target_file_and_interface \
220+ t_duplicate_main_and_include t_duplicate_user_directories \
221+ t_duplicate_standard_library \
222+ t_user_paths_do_not_consume_include_slots \
223+ t_hard_link_chooses_same_module \
224+ t_equal_contents_do_not_choose_distinct_module \
225+ t_symbolic_link_to_distinct_module_does_not_choose; do
226+ runcase $t
227+done
228+
229+echo "library-path: $pass passed, $fail failed"
230+[ $fail = 0 ]