raz / commit ecf32b3b

commit
ecf32b3b4ec4d1d1427a84b208b8c7177073cbfb
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
728c3aa9
Find installed source libraries through RAZ_PATH

Search HOME/.raz/lib when RAZ_PATH is unset, or its colon-separated directories when set. An empty value disables user paths while keeping the bundled standard library. Preserve duplicate and target-interface checks across all paths.

Document source installation and isolated builds. Add 14 regression tests and isolate existing fixtures from user libraries. Validation: make check (1013 passed), make fmt, make fixpoint, make bench, make prev-bench, and make loc (11662 code lines).

patch · log

9 files changed, +324 -16

PROPOSALS.md modified +2 -2

@@ -6,8 +6,8 @@
66
77 ## Installed source libraries and RAZ_PATH
88
9-**Status.** Approved by the owner on 2026-10-19, to be implemented
10-after this proposal is committed and pushed. This changes the compiler's
9+**Status.** Approved by the owner on 2026-10-19 and implemented after
10+this proposal was committed and pushed. This changes the compiler's
1111 module search paths and documents an installation convention; it adds
1212 no language syntax or package manager.
1313

README.md (history) modified +31 -2

@@ -102,9 +102,38 @@
102102 `raz Main.raz` compiles module `Main` and every module it imports, and
103103 writes the executable `Main`. A module `Name` lives in `Name.raz`, found
104104 in the main module's directory, then in each `-I` directory, then in
105-`lib`. A module whose code depends on the system comes in one file per
106-target, `Name.linux-x86_64.raz` before `Name.raz`; the compiler checks
105+the user library directories, then in `lib`. A module whose code depends
106+on the system comes in one file per target, `Name.linux-x86_64.raz`
107+before `Name.raz`; the compiler checks
107108 that all of them export the same things.
109+
110+Install third-party source modules directly in `$HOME/.raz/lib` to use
111+them from any project without `-I` or a Git submodule. Keep their
112+target-specific files beside them; directories are not searched
113+recursively. A library's makefile or Robfile can provide install and
114+uninstall tasks for its own files. For example, for a library `Json`:
115+
116+```sh
117+mkdir -p "$HOME/.raz/lib"
118+install -m644 Json.raz "$HOME/.raz/lib/Json.raz"
119+raz Main.raz # import Json; needs no -I
120+```
121+
122+`RAZ_PATH` replaces the default directory with a colon-separated list:
123+
124+```sh
125+export RAZ_PATH="$HOME/.raz/lib:$HOME/work/raz-libs"
126+RAZ_PATH= raz -I deps/json Main.raz # use only the project's dependency
127+```
128+
129+An unset `RAZ_PATH` uses `$HOME/.raz/lib`; an empty one disables user
130+libraries while keeping the standard library. With no nonempty `HOME`,
131+there is no default. Empty list entries are ignored. Relative paths
132+start at the compiler's working directory; the compiler does not expand
133+`~` or variables within them. Duplicate modules are errors across all
134+search directories, with the report's symbolic-link exception, so `-I`
135+does not override an installed module. Projects that need particular
136+dependency versions can set `RAZ_PATH` for their build.
108137
109138 An error is shown with its file, line and column, the line of source
110139 underlined, and notes and help where they help: where a name is

doc/compiler.md (history) modified +12 -3

@@ -23,10 +23,10 @@
2323 | `src/X64.raz` | 1,567 | 1,292 | The instruction encoder, jumps within a procedure, the assembler |
2424 | `src/Gen.raz` | 2,561 | 1,824 | The stack machine, for x86-64: values, operations, checks, calls, frames |
2525 | `src/Item.raz` | 2,175 | 1,747 | Items: types, constants, variables, operators, conversions, calls |
26-| `src/Parse.raz` | 4,251 | 3,676 | Declarations, statements, expressions, built-ins, recovery, warnings |
26+| `src/Parse.raz` | 4,260 | 3,683 | Declarations, statements, expressions, built-ins, recovery, warnings |
2727 | `src/Link.raz` | 407 | 315 | Tree shaking, layout, patching, the places |
2828 | `src/Elf.raz` | 449 | 355 | Static and dynamic ELF, symbol table, line numbers |
29-| `src/Raz.raz` | 369 | 289 | The command line |
29+| `src/Raz.raz` | 417 | 332 | The command line |
3030
3131 Every file opens with a comment that says what it is, what it depends
3232 on and what it leaves out. Read those first; they are the map.
@@ -1768,6 +1768,14 @@
17681768 rest of raz's `argv`. Nothing is left on disk, and the program's
17691769 status is the command's.
17701770
1771+Before compiling, `userLibraries` reads `RAZ_PATH` with `Sys.envAt`,
1772+which distinguishes an unset variable from an explicitly empty one.
1773+Unset means `$HOME/.raz/lib` if `HOME` is nonempty; set means a
1774+colon-separated list, with empty entries ignored. `Parse.setUserDirs`
1775+keeps its slices in `userDirs`, separate from the 16 `-I` directories. `searchDir`
1776+puts them between those and the standard library, for both finding
1777+modules and checking duplicate names. No subdirectories are searched.
1778+
17711779 ## This compiler's choices
17721780
17731781 What the report's appendix A leaves to an implementation, and what
@@ -1781,7 +1789,8 @@
17811789 loader is there.
17821790 - **Finding modules.** `raz Main.raz` looks for `X`, imported, in
17831791 `Main.raz`'s directory, then in each `-I dir` in the order given
1784- (16 at most), then in `lib` beside the `raz` executable, following
1792+ (16 at most), then in `RAZ_PATH`'s directories (or `$HOME/.raz/lib`
1793+ when unset), then in `lib` beside the `raz` executable, following
17851794 a link to it (`make install` puts `raz` and `lib/` together in
17861795 `PREFIX/lib/raz`). In each directory it tries `X.linux-x86_64.raz`,
17871796 `X.linux.raz`, `X.x86_64.raz` and `X.raz`, in that order, and takes

doc/raz.1 (history) modified +26 -2

@@ -1,5 +1,5 @@
11 .\" raz(1) - the raz compiler
2-.Dd October 17, 2026
2+.Dd October 19, 2026
33 .Dt RAZ 1
44 .Os
55 .Sh NAME
@@ -36,7 +36,9 @@
3636 .Ar Main.raz ,
3737 then in each
3838 .Fl I
39-directory in turn, then in the library,
39+directory in turn, then in the user library directories (see
40+.Sx ENVIRONMENT ) ,
41+then in the library,
4042 .Pa lib/
4143 beside the
4244 .Nm
@@ -142,6 +144,28 @@
142144 .El
143145 .Sh ENVIRONMENT
144146 .Bl -tag -width "RAZ_PROFILE"
147+.It Ev RAZ_PATH
148+Colon-separated directories of installed raz source modules, searched
149+after the
150+.Fl I
151+directories and before the standard library.
152+When unset, the user library directory is
153+.Pa $HOME/.raz/lib ;
154+with no nonempty
155+.Ev HOME ,
156+there is no default.
157+An explicitly empty
158+.Ev RAZ_PATH
159+disables user library directories; it does not disable the standard
160+library. Empty entries are ignored, not taken as the working directory.
161+Relative paths are relative to the compiler's working directory.
162+The compiler does not expand a tilde or variables within paths.
163+Directories are not searched recursively: install a library's source
164+modules directly in one of them, with their target-specific siblings.
165+The duplicate-module error and symbolic-link exception apply across
166+all search directories;
167+.Fl I
168+does not override an installed module.
145169 .It Ev RAZ_PROFILE
146170 Set to anything, in the environment of a program built without
147171 .Fl s

doc/raz.md (history) modified +10 -1

@@ -281,7 +281,16 @@
281281
282282 **Finding modules.** The compiler looks for module `Name` in the main
283283 module's directory, then in each directory given with `-I`, then in the
284-`lib` directory beside the compiler. In each directory it takes the
284+user library directories, then in the `lib` directory beside the
285+compiler. User libraries live in `$HOME/.raz/lib` by default.
286+`RAZ_PATH`, when set, replaces that default with a colon-separated
287+list of directories; an empty value disables user libraries. Empty
288+entries are ignored. With no nonempty `HOME`, there is no default.
289+Paths are not searched recursively; relative paths are relative to the
290+compiler's working directory, and `~` and variables inside them are
291+not expanded. Install a library's source files directly in one of these
292+directories, with its target-specific files beside them. In each
293+directory the compiler takes the
285294 first of `Name.OS-ARCH.raz`, `Name.OS.raz`, `Name.ARCH.raz` and
286295 `Name.raz` that exists, where `OS-ARCH` is the target, for now only
287296 `linux-x86_64`. So a module whose code depends on the machine or the

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

@@ -75,6 +75,7 @@
7575 libDir*, mainDir: []U8;
7676 incDirs*: [16][]U8;
7777 numInc*: INT;
78+ userDirs: [][]U8; (* RAZ_PATH, or HOME/.raz/lib; set by the driver *)
7879 firstModule*, lastModule*: PModule; (* in the order they were finished *)
7980
8081 checkMode: BOOL; (* reading a file only for its interface *)
@@ -141,6 +142,12 @@
141142 INC(numInc);
142143 end;
143144
145+(* The driver's user library directories, kept until compilation ends *)
146+proc setUserDirs*(dirs: [][]U8);
147+begin
148+ userDirs := dirs;
149+end;
150+
144151 proc nameOf(n: INT): []U8;
145152 begin
146153 return Scan.nameOf(n);
@@ -4043,12 +4050,14 @@
40434050 return nil;
40444051 end;
40454052
4046-(* The directories searched for modules, in order, from -1 to numInc *)
4053+(* The directories searched for modules: main, -I, user libraries, lib *)
40474054 proc searchDir(i: INT): []U8;
40484055 begin
40494056 if i = -1 then return mainDir; end;
4050- if i = numInc then return libDir; end;
4051- return incDirs[i];
4057+ if i < numInc then return incDirs[i]; end;
4058+ i := i - numInc;
4059+ if i < LEN(userDirs) then return userDirs[i]; end;
4060+ return libDir;
40524061 end;
40534062
40544063 (* The file of module n in dir that suffix j says: Name.linux-x86_64.raz
@@ -4061,7 +4070,7 @@
40614070 proc moduleFile(n: INT): ([]U8, []U8, BOOL);
40624071 var i, j: INT; dir, f: []U8;
40634072 begin
4064- for i := -1 to numInc do
4073+ for i := -1 to numInc + LEN(userDirs) do
40654074 dir := searchDir(i);
40664075 for j := 0 to 3 do
40674076 f := fileName(n, j);
@@ -4081,7 +4090,7 @@
40814090 mineDev, mineIno := identity(say2("{}/{}", dir, f));
40824091 others := "";
40834092 past := false;
4084- for i := -1 to numInc do
4093+ for i := -1 to numInc + LEN(userDirs) do
40854094 if past then
40864095 files := "";
40874096 chosen := false;
@@ -4117,7 +4126,7 @@
41174126 if not found then
41184127 Diag.error(args.at, say1("can't find module {}", nameOf(name)));
41194128 where := Diag.shownPath(searchDir(-1));
4120- for i := 0 to numInc do where := say2("{}, {}", where, Diag.shownPath(searchDir(i))); end;
4129+ for i := 0 to numInc + LEN(userDirs) do where := say2("{}, {}", where, Diag.shownPath(searchDir(i))); end;
41214130 Diag.help(say2("looked for {}.raz in {}", nameOf(name), where));
41224131 return nil, Scan.Failed;
41234132 end;

src/Raz.raz (history) modified +48 -0

@@ -198,6 +198,53 @@
198198 return p;
199199 end;
200200
201+(* User libraries are source directories, not packages to discover
202+ recursively. Read NAME=value entries so an empty RAZ_PATH differs
203+ from an unset one; Sys.env alone gives "" for both. The slices and
204+ their array live until the compiler exits, as command-line paths do. *)
205+proc userLibraries;
206+var path, s: []U8; dirs: ^[]U8; i, first, n: INT; more, found: BOOL;
207+begin
208+ i := 0;
209+ repeat
210+ s, more := Sys.envAt(i);
211+ if not more then break; end;
212+ if (LEN(s) >= 9) and (s[0..9] = "RAZ_PATH=") then
213+ path := s[9..LEN(s)];
214+ found := true;
215+ break;
216+ end;
217+ INC(i);
218+ until false;
219+ if not found then
220+ s := Sys.env("HOME");
221+ if LEN(s) > 0 then
222+ dirs := ^[]U8(Scan.grab(SIZE([]U8)));
223+ dirs[0] := say1("{}/.raz/lib", s);
224+ Parse.setUserDirs(dirs[0..1]);
225+ end;
226+ return;
227+ end;
228+ if LEN(path) = 0 then return; end;
229+ n := 1;
230+ for i := 0 to LEN(path) - 1 do
231+ if path[i] = ':' then INC(n); end;
232+ end;
233+ dirs := ^[]U8(Scan.grab(n * SIZE([]U8)));
234+ n := 0;
235+ first := 0;
236+ for i := 0 to LEN(path) do
237+ if (i = LEN(path)) or (path[i] = ':') then
238+ if i > first then
239+ dirs[n] := path[first..i];
240+ INC(n);
241+ end;
242+ first := i + 1;
243+ end;
244+ end;
245+ Parse.setUserDirs(dirs[0..n]);
246+end;
247+
201248 proc main*: INT;
202249 var
203250 path, outPath, s: []U8; dir: []U8;
@@ -274,6 +321,7 @@
274321 i := LEN(dir);
275322 while (i > 0) and (dir[i - 1] # '/') do DEC(i); end;
276323 if i = 0 then Parse.setLibDir("lib"); else Parse.setLibDir(say1("{}lib", dir[0..i])); end;
324+ userLibraries();
277325 if LEN(outPath) = 0 then
278326 outPath := path;
279327 last := LEN(outPath);

test/check.sh (history) modified +7 -0

@@ -19,6 +19,10 @@
1919 # the tutorial's, tools/Pretty.raz, and the library's own tests.
2020
2121 raz=./raz
22+# Fixtures use only their explicit module paths and the standard library.
23+# library-path.py sets up its own HOME and RAZ_PATH for user-path checks.
24+RAZ_PATH=
25+export RAZ_PATH
2226 out=build/check
2327 mkdir -p $out
2428 ulimit -c 0 # the tests stopped by a signal leave no core
@@ -318,6 +322,9 @@
318322
319323 # the editors' support, headless, where the editors are installed
320324 if test/editors.sh; then pass=$((pass + 1)); else fail=$((fail + 1)); fi
325+
326+# Installed source libraries: default, explicit and isolated lookup.
327+if python3 test/library-path.py "$raz"; then pass=$((pass + 1)); else fail=$((fail + 1)); fi
321328
322329 echo "$pass passed, $fail failed"
323330 [ $fail = 0 ]

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

@@ -0,0 +1,173 @@
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()