From 4e3969aaab4df31eb0d1b8dd87b81c8f5db36c2b Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Mon, 26 Oct 2026 17:03:27 +0000 Subject: [PATCH] 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. --- diff --git a/doc/compiler.md b/doc/compiler.md index 93f8a90..4c66ef3 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -1914,7 +1914,8 @@ for and this one's, "0.21" for `0.21.0-5-g38d29a8`; the compiler only reads the file. `checkOneModule` first makes sure no later directory has the module too, unless the file is a link to one -of its files there; `checkSiblings` then reads the files for other +of its files there (a file's identity is its device and inode from `Sys.info`, +which follows a symbolic link, as on every target); `checkSiblings` then reads the files for other targets beside it, declarations only, and `compareIface` holds them to the same exports by their fingerprints. A generic module is compiled once per distinct list of arguments, where it is first used. diff --git a/src/Parse.raz b/src/Parse.raz index eeaf29a..fb9268d 100644 --- a/src/Parse.raz +++ b/src/Parse.raz @@ -4297,13 +4297,11 @@ (* Which file path is, as its device and inode: two names of one file have the same. Zero for a file that can't be asked about. *) proc identity(path: []const U8): (INT, INT); -var a: [4096]U8; st: [18]INT; (* struct stat: st_dev, then st_ino *) +var i: Sys.Info; err: INT; (* Sys.info follows a symbolic link, and is the same on every target *) begin - if LEN(path) >= 4096 then return 0, 0; end; - COPY(a[0..LEN(path)], path); - a[LEN(path)] := 0; - if SYSTEM.SYSCALL(4, SYSTEM.ADR(a[0]), SYSTEM.ADR(st)) < 0 then return 0, 0; end; - return st[0], st[1]; + i, err := Sys.info(path); + if err # 0 then return 0, 0; end; + return INT(i.dev), INT(i.ino); end; (* s, in memory that is never freed, for what must outlast the buffer it is in *) diff --git a/test/check.sh b/test/check.sh index b9f9a6d..f982d29 100755 --- a/test/check.sh +++ b/test/check.sh @@ -21,7 +21,7 @@ raz=./raz # Fixtures use only their explicit module paths and the standard library. -# library-path.py sets up its own HOME and RAZ_PATH for user-path checks. +# library-path.sh sets up its own HOME and RAZ_PATH for user-path checks. RAZ_PATH= export RAZ_PATH out=build/check @@ -403,7 +403,7 @@ if test/editors.sh; then pass=$((pass + 1)); else fail=$((fail + 1)); fi # Installed source libraries: default, explicit and isolated lookup. -if python3 test/library-path.py "$raz"; then pass=$((pass + 1)); else fail=$((fail + 1)); fi +if test/library-path.sh "$raz"; then pass=$((pass + 1)); else fail=$((fail + 1)); fi echo "$pass passed, $fail failed" [ $fail = 0 ] diff --git a/test/library-path.py b/test/library-path.py deleted file mode 100644 index 30ce257..0000000 --- a/test/library-path.py +++ /dev/null @@ -1,173 +0,0 @@ -#!/usr/bin/env python3 -"""Exercise the compiler's library lookup in an isolated environment.""" - -import os -from pathlib import Path -import subprocess -import sys -import tempfile -import unittest - - -RAZ = str(Path(sys.argv.pop(1)).resolve()) - - -class LibraryPaths(unittest.TestCase): - def setUp(self): - self.scratch = tempfile.TemporaryDirectory(prefix="raz-path-") - self.addCleanup(self.scratch.cleanup) - self.root = Path(self.scratch.name) - self.env = dict(os.environ) - self.env.pop("RAZ_PATH", None) - self.env.pop("RAZ_PROFILE", None) - self.env["HOME"] = str(self.root / "home space") - self.default = Path(self.env["HOME"]) / ".raz/lib" - self.main = self.root / "src/Main.raz" - self.write(self.main, """module Main; -import Out, PathLib; -proc main*; -begin Out.int(PathLib.value); Out.ln; end; -end Main. -""") - - def write(self, path, text): - path.parent.mkdir(parents=True, exist_ok=True) - path.write_text(text) - - def library(self, directory, value=42, suffix=""): - path = directory / f"PathLib{suffix}.raz" - self.write(path, f"module PathLib; const value* = {value}; end PathLib.\n") - return path - - def compile(self, *flags, run=False): - args = [RAZ, "run"] if run else [RAZ, "-o", str(self.root / "app")] - return subprocess.run( - [*args, "-Werror", *map(str, flags), str(self.main)], - cwd=self.root, env=self.env, text=True, capture_output=True, - ) - - def succeeds(self, *flags, run=False): - result = self.compile(*flags, run=run) - self.assertEqual(result.returncode, 0, result.stderr) - if not run: - result = subprocess.run( - [str(self.root / "app")], env=self.env, - text=True, capture_output=True, - ) - self.assertEqual(result.returncode, 0, result.stderr) - self.assertEqual(result.stdout, "42\n") - - def fails(self, message, *flags): - result = self.compile(*flags) - self.assertEqual(result.returncode, 1, result.stderr) - self.assertIn(message, result.stderr) - - def test_default(self): - self.library(self.default) - self.succeeds() - self.succeeds(run=True) - - def test_colon_in_home(self): - # Unlike RAZ_PATH, HOME is one path, not a list. - self.env["HOME"] = str(self.root / "home : space") - self.library(Path(self.env["HOME"]) / ".raz/lib") - self.succeeds() - - def test_empty_disables_default(self): - self.library(self.default) - self.env["RAZ_PATH"] = "" - self.fails("can't find module PathLib") - # Explicit dependencies and the bundled standard library still work. - self.succeeds("-I", self.default) - - def test_list_replaces_default(self): - self.library(self.default, 99) - first = self.root / "first directory" - second = self.root / "second directory" - self.library(second) - self.env["RAZ_PATH"] = f"{first}:{second}" - self.succeeds() - self.succeeds(run=True) - self.env["RAZ_PATH"] = str(first) - self.fails("can't find module PathLib") - - def test_relative_and_empty_entries(self): - self.library(self.root / "relative") - # A cwd module must not be found through the empty entries. - self.library(self.root, 99) - self.env["RAZ_PATH"] = ":relative::missing:" - self.succeeds() - self.env["RAZ_PATH"] = ":::" - self.fails("can't find module PathLib") - - def test_missing_home(self): - self.env.pop("HOME") - self.fails("can't find module PathLib") - self.env["HOME"] = "" - self.fails("can't find module PathLib") - self.library(self.root / "explicit") - self.env["RAZ_PATH"] = "explicit" - self.succeeds() - - def test_no_recursion(self): - self.library(self.default / "package") - self.fails("can't find module PathLib") - self.env["RAZ_PATH"] = str(self.default / "package") - self.succeeds() - - def test_missing_module_lists_all_search_directories(self): - self.env["RAZ_PATH"] = "first:second" - result = self.compile("-I", "explicit") - self.assertEqual(result.returncode, 1, result.stderr) - self.assertIn("explicit, first, second,", result.stderr) - self.assertIn(str(Path(RAZ).parent / "lib"), result.stderr) - - def test_no_path_expansion(self): - self.library(self.default) - for path in ("~/.raz/lib", "$HOME/.raz/lib"): - self.env["RAZ_PATH"] = path - self.fails("can't find module PathLib") - - def test_target_file_and_interface(self): - self.library(self.default, suffix=".linux-x86_64") - self.library(self.default, 99) - self.succeeds() - self.write(self.default / "PathLib.raz", - "module PathLib; const other* = 42; end PathLib.\n") - self.fails("isn't exported") - - def test_duplicate_main_and_include(self): - chosen = self.library(self.default) - for directory in (self.main.parent, self.root / "include"): - duplicate = self.library(directory) - flags = ("-I", directory) if directory != self.main.parent else () - self.fails("module PathLib is in", *flags) - duplicate.unlink() - duplicate.symlink_to(chosen) - self.succeeds(*flags) - duplicate.unlink() - - def test_duplicate_user_directories(self): - self.library(self.default) - other = self.root / "other" - self.library(other) - self.env["RAZ_PATH"] = f"{self.default}:{other}" - self.fails("module PathLib is in") - self.env["RAZ_PATH"] = f"{self.default}:{self.default}" - self.succeeds() - - def test_duplicate_standard_library(self): - self.library(self.default) - self.write(self.default / "Out.raz", "module Out; end Out.\n") - self.fails("module Out is in") - - def test_user_paths_do_not_consume_include_slots(self): - self.library(self.default) - flags = [arg for _ in range(16) for arg in ("-I", "missing")] - self.succeeds(*flags) - self.env["RAZ_PATH"] = ":".join(["missing"] * 32 + [str(self.default)]) - self.succeeds(*flags) - - -if __name__ == "__main__": - unittest.main() diff --git a/test/library-path.sh b/test/library-path.sh new file mode 100755 index 0000000..1fb4c3e --- /dev/null +++ b/test/library-path.sh @@ -0,0 +1,230 @@ +#!/bin/sh +# Exercises the compiler's library lookup in an isolated environment: +# the default library in $HOME/.raz/lib, RAZ_PATH, -I, the files a module +# has for other targets, and modules of one name found twice. +# Usage: test/library-path.sh path/to/raz +# Each case runs in a subshell with its own scratch directory and HOME. + +raz=$(cd "$(dirname "$1")" && pwd)/$(basename "$1") +top=$(mktemp -d "${TMPDIR:-/tmp}/raz-path-XXXXXX") || exit 1 +trap 'rm -rf "$top"' EXIT +pass=0 +fail=0 + +# Sets up a fresh scratch directory $root, with HOME in it (its name has a +# space), the default library directory $default and the main program. +setup() { + root=$top/$1 + mkdir -p "$root/src" "$root/home space" + unset RAZ_PATH RAZ_PROFILE RUN + HOME="$root/home space" + export HOME + default=$HOME/.raz/lib + main=$root/src/Main.raz + cat >"$main" <<'END' +module Main; +import Out, PathLib; +proc main*; +begin Out.int(PathLib.value); Out.ln; end; +end Main. +END +} + +# library DIR [VALUE [SUFFIX]] writes DIR/PathLib.raz. +library() { + mkdir -p "$1" + echo "module PathLib; const value* = ${2:-42}; end PathLib." >"$1/PathLib$3.raz" +} + +# compile FLAGS... builds the program, its messages in $root/err. With +# RUN set it runs it by "raz run" instead, its output in $root/out. +compile() { + if [ -n "$RUN" ]; then + (cd "$root" && "$raz" run -Werror "$@" "$main") >"$root/out" 2>"$root/err" + else + (cd "$root" && "$raz" -o "$root/app" -Werror "$@" "$main") >"$root/out" 2>"$root/err" + fi +} + +# succeeds FLAGS...: it compiles, and the program prints 42. +succeeds() { + compile "$@" || { echo "compile failed:"; cat "$root/err"; return 1; } + [ -n "$RUN" ] || "$root/app" >"$root/out" || return 1 + [ "$(cat "$root/out")" = 42 ] || { echo "printed: $(cat "$root/out")"; return 1; } +} + +# fails MESSAGE FLAGS...: compiling gives status 1 and MESSAGE in its errors. +fails() { + msg=$1 + shift + compile "$@" + [ $? = 1 ] || { echo "expected status 1"; cat "$root/err"; return 1; } + grep -F -q -- "$msg" "$root/err" || { echo "no '$msg' in:"; cat "$root/err"; return 1; } +} + +# runcase NAME runs the function NAME in a subshell and counts it. +runcase() { + if (setup "$1"; "$1"); then pass=$((pass + 1)) + else echo "FAIL library-path: $1"; fail=$((fail + 1)); fi +} + +t_default() { + library "$default" + succeeds || return 1 + RUN=1 succeeds +} + +t_colon_in_home() { # unlike RAZ_PATH, HOME is one path, not a list + HOME="$root/home : space" + export HOME + library "$HOME/.raz/lib" + succeeds +} + +t_empty_disables_default() { + library "$default" + RAZ_PATH=; export RAZ_PATH + fails "can't find module PathLib" || return 1 + succeeds -I "$default" # explicit directories still work +} + +t_list_replaces_default() { + library "$default" 99 + library "$root/second directory" + RAZ_PATH="$root/first directory:$root/second directory"; export RAZ_PATH + succeeds || return 1 + RUN=1 succeeds || return 1 + RAZ_PATH="$root/first directory" + fails "can't find module PathLib" +} + +t_relative_and_empty_entries() { + library "$root/relative" + library "$root" 99 # a module in the cwd must not be found by an empty entry + RAZ_PATH=:relative::missing:; export RAZ_PATH + succeeds || return 1 + RAZ_PATH=::: + fails "can't find module PathLib" +} + +t_missing_home() { + unset HOME + fails "can't find module PathLib" || return 1 + HOME=; export HOME + fails "can't find module PathLib" || return 1 + library "$root/explicit" + RAZ_PATH=explicit; export RAZ_PATH + succeeds +} + +t_no_recursion() { + library "$default/package" + fails "can't find module PathLib" || return 1 + RAZ_PATH=$default/package; export RAZ_PATH + succeeds +} + +t_missing_module_lists_all_search_directories() { + RAZ_PATH=first:second; export RAZ_PATH + compile -I explicit + [ $? = 1 ] || return 1 + grep -F -q "explicit, first, second," "$root/err" || { cat "$root/err"; return 1; } + grep -F -q "$(dirname "$raz")/lib" "$root/err" || { cat "$root/err"; return 1; } +} + +t_no_path_expansion() { + library "$default" + for p in '~/.raz/lib' '$HOME/.raz/lib'; do + RAZ_PATH=$p; export RAZ_PATH + fails "can't find module PathLib" || return 1 + done +} + +t_target_file_and_interface() { + library "$default" 42 .linux-x86_64 + library "$default" 99 + succeeds || return 1 + echo "module PathLib; const other* = 42; end PathLib." >"$default/PathLib.raz" + fails "isn't exported" +} + +t_duplicate_main_and_include() { + library "$default" + for dir in "$root/src" "$root/include"; do + library "$dir" + flags= + [ "$dir" = "$root/src" ] || flags="-I $dir" + fails "module PathLib is in" $flags || return 1 + rm "$dir/PathLib.raz" + ln -s "$default/PathLib.raz" "$dir/PathLib.raz" # a link to the chosen file is no duplicate + succeeds $flags || return 1 + rm "$dir/PathLib.raz" + done +} + +t_duplicate_user_directories() { + library "$default" + library "$root/other" + RAZ_PATH=$default:$root/other; export RAZ_PATH + fails "module PathLib is in" || return 1 + RAZ_PATH=$default:$default + succeeds +} + +t_duplicate_standard_library() { + library "$default" + echo "module Out; end Out." >"$default/Out.raz" + fails "module Out is in" +} + +t_user_paths_do_not_consume_include_slots() { + library "$default" + flags= + p= + 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 + 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 + p=${p}missing: + done + succeeds $flags || return 1 + RAZ_PATH=$p$default; export RAZ_PATH + succeeds $flags +} + +# 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() { + library "$default" + ln "$default/PathLib.raz" "$root/src/PathLib.raz" + succeeds +} + +# ... and equal contents in another file are not. +t_equal_contents_do_not_choose_distinct_module() { + library "$default" + cp "$default/PathLib.raz" "$root/src/PathLib.raz" + fails "module PathLib is in" +} + +# ... and a link to some other file is not the chosen one. +t_symbolic_link_to_distinct_module_does_not_choose() { + library "$default" + library "$root/elsewhere" + ln -s "$root/elsewhere/PathLib.raz" "$root/src/PathLib.raz" + fails "module PathLib is in" +} + +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 \ + t_no_path_expansion t_target_file_and_interface \ + 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_equal_contents_do_not_choose_distinct_module \ + t_symbolic_link_to_distinct_module_does_not_choose; do + runcase $t +done + +echo "library-path: $pass passed, $fail failed" +[ $fail = 0 ]