From ecf32b3b4ec4d1d1427a84b208b8c7177073cbfb Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Mon, 19 Oct 2026 20:45:26 +0000 Subject: [PATCH] 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). --- diff --git a/PROPOSALS.md b/PROPOSALS.md index 739824d..215361c 100644 --- a/PROPOSALS.md +++ b/PROPOSALS.md @@ -6,8 +6,8 @@ ## Installed source libraries and RAZ_PATH -**Status.** Approved by the owner on 2026-10-19, to be implemented -after this proposal is committed and pushed. This changes the compiler's +**Status.** Approved by the owner on 2026-10-19 and implemented after +this proposal was committed and pushed. This changes the compiler's module search paths and documents an installation convention; it adds no language syntax or package manager. diff --git a/README.md b/README.md index ae18403..dd62685 100644 --- a/README.md +++ b/README.md @@ -102,9 +102,38 @@ `raz Main.raz` compiles module `Main` and every module it imports, and writes the executable `Main`. A module `Name` lives in `Name.raz`, found in the main module's directory, then in each `-I` directory, then in -`lib`. A module whose code depends on the system comes in one file per -target, `Name.linux-x86_64.raz` before `Name.raz`; the compiler checks +the user library directories, then in `lib`. A module whose code depends +on the system comes in one file per target, `Name.linux-x86_64.raz` +before `Name.raz`; the compiler checks that all of them export the same things. + +Install third-party source modules directly in `$HOME/.raz/lib` to use +them from any project without `-I` or a Git submodule. Keep their +target-specific files beside them; directories are not searched +recursively. A library's makefile or Robfile can provide install and +uninstall tasks for its own files. For example, for a library `Json`: + +```sh +mkdir -p "$HOME/.raz/lib" +install -m644 Json.raz "$HOME/.raz/lib/Json.raz" +raz Main.raz # import Json; needs no -I +``` + +`RAZ_PATH` replaces the default directory with a colon-separated list: + +```sh +export RAZ_PATH="$HOME/.raz/lib:$HOME/work/raz-libs" +RAZ_PATH= raz -I deps/json Main.raz # use only the project's dependency +``` + +An unset `RAZ_PATH` uses `$HOME/.raz/lib`; an empty one disables user +libraries while keeping the standard library. With no nonempty `HOME`, +there is no default. Empty list entries are ignored. Relative paths +start at the compiler's working directory; the compiler does not expand +`~` or variables within them. Duplicate modules are errors across all +search directories, with the report's symbolic-link exception, so `-I` +does not override an installed module. Projects that need particular +dependency versions can set `RAZ_PATH` for their build. An error is shown with its file, line and column, the line of source underlined, and notes and help where they help: where a name is diff --git a/doc/compiler.md b/doc/compiler.md index 41925cf..5311aff 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -23,10 +23,10 @@ | `src/X64.raz` | 1,567 | 1,292 | The instruction encoder, jumps within a procedure, the assembler | | `src/Gen.raz` | 2,561 | 1,824 | The stack machine, for x86-64: values, operations, checks, calls, frames | | `src/Item.raz` | 2,175 | 1,747 | Items: types, constants, variables, operators, conversions, calls | -| `src/Parse.raz` | 4,251 | 3,676 | Declarations, statements, expressions, built-ins, recovery, warnings | +| `src/Parse.raz` | 4,260 | 3,683 | Declarations, statements, expressions, built-ins, recovery, warnings | | `src/Link.raz` | 407 | 315 | Tree shaking, layout, patching, the places | | `src/Elf.raz` | 449 | 355 | Static and dynamic ELF, symbol table, line numbers | -| `src/Raz.raz` | 369 | 289 | The command line | +| `src/Raz.raz` | 417 | 332 | The command line | Every file opens with a comment that says what it is, what it depends on and what it leaves out. Read those first; they are the map. @@ -1768,6 +1768,14 @@ rest of raz's `argv`. Nothing is left on disk, and the program's status is the command's. +Before compiling, `userLibraries` reads `RAZ_PATH` with `Sys.envAt`, +which distinguishes an unset variable from an explicitly empty one. +Unset means `$HOME/.raz/lib` if `HOME` is nonempty; set means a +colon-separated list, with empty entries ignored. `Parse.setUserDirs` +keeps its slices in `userDirs`, separate from the 16 `-I` directories. `searchDir` +puts them between those and the standard library, for both finding +modules and checking duplicate names. No subdirectories are searched. + ## This compiler's choices What the report's appendix A leaves to an implementation, and what @@ -1781,7 +1789,8 @@ loader is there. - **Finding modules.** `raz Main.raz` looks for `X`, imported, in `Main.raz`'s directory, then in each `-I dir` in the order given - (16 at most), then in `lib` beside the `raz` executable, following + (16 at most), then in `RAZ_PATH`'s directories (or `$HOME/.raz/lib` + when unset), then in `lib` beside the `raz` executable, following a link to it (`make install` puts `raz` and `lib/` together in `PREFIX/lib/raz`). In each directory it tries `X.linux-x86_64.raz`, `X.linux.raz`, `X.x86_64.raz` and `X.raz`, in that order, and takes diff --git a/doc/raz.1 b/doc/raz.1 index 34c7d6b..f90f6a3 100644 --- a/doc/raz.1 +++ b/doc/raz.1 @@ -1,5 +1,5 @@ .\" raz(1) - the raz compiler -.Dd October 17, 2026 +.Dd October 19, 2026 .Dt RAZ 1 .Os .Sh NAME @@ -36,7 +36,9 @@ .Ar Main.raz , then in each .Fl I -directory in turn, then in the library, +directory in turn, then in the user library directories (see +.Sx ENVIRONMENT ) , +then in the library, .Pa lib/ beside the .Nm @@ -142,6 +144,28 @@ .El .Sh ENVIRONMENT .Bl -tag -width "RAZ_PROFILE" +.It Ev RAZ_PATH +Colon-separated directories of installed raz source modules, searched +after the +.Fl I +directories and before the standard library. +When unset, the user library directory is +.Pa $HOME/.raz/lib ; +with no nonempty +.Ev HOME , +there is no default. +An explicitly empty +.Ev RAZ_PATH +disables user library directories; it does not disable the standard +library. Empty entries are ignored, not taken as the working directory. +Relative paths are relative to the compiler's working directory. +The compiler does not expand a tilde or variables within paths. +Directories are not searched recursively: install a library's source +modules directly in one of them, with their target-specific siblings. +The duplicate-module error and symbolic-link exception apply across +all search directories; +.Fl I +does not override an installed module. .It Ev RAZ_PROFILE Set to anything, in the environment of a program built without .Fl s diff --git a/doc/raz.md b/doc/raz.md index 3efd368..e876a75 100644 --- a/doc/raz.md +++ b/doc/raz.md @@ -281,7 +281,16 @@ **Finding modules.** The compiler looks for module `Name` in the main module's directory, then in each directory given with `-I`, then in the -`lib` directory beside the compiler. In each directory it takes the +user library directories, then in the `lib` directory beside the +compiler. User libraries live in `$HOME/.raz/lib` by default. +`RAZ_PATH`, when set, replaces that default with a colon-separated +list of directories; an empty value disables user libraries. Empty +entries are ignored. With no nonempty `HOME`, there is no default. +Paths are not searched recursively; relative paths are relative to the +compiler's working directory, and `~` and variables inside them are +not expanded. Install a library's source files directly in one of these +directories, with its target-specific files beside them. In each +directory the compiler takes the first of `Name.OS-ARCH.raz`, `Name.OS.raz`, `Name.ARCH.raz` and `Name.raz` that exists, where `OS-ARCH` is the target, for now only `linux-x86_64`. So a module whose code depends on the machine or the diff --git a/src/Parse.raz b/src/Parse.raz index c99e979..9ae8e95 100644 --- a/src/Parse.raz +++ b/src/Parse.raz @@ -75,6 +75,7 @@ libDir*, mainDir: []U8; incDirs*: [16][]U8; numInc*: INT; + userDirs: [][]U8; (* RAZ_PATH, or HOME/.raz/lib; set by the driver *) firstModule*, lastModule*: PModule; (* in the order they were finished *) checkMode: BOOL; (* reading a file only for its interface *) @@ -141,6 +142,12 @@ INC(numInc); end; +(* The driver's user library directories, kept until compilation ends *) +proc setUserDirs*(dirs: [][]U8); +begin + userDirs := dirs; +end; + proc nameOf(n: INT): []U8; begin return Scan.nameOf(n); @@ -4043,12 +4050,14 @@ return nil; end; -(* The directories searched for modules, in order, from -1 to numInc *) +(* The directories searched for modules: main, -I, user libraries, lib *) proc searchDir(i: INT): []U8; begin if i = -1 then return mainDir; end; - if i = numInc then return libDir; end; - return incDirs[i]; + if i < numInc then return incDirs[i]; end; + i := i - numInc; + if i < LEN(userDirs) then return userDirs[i]; end; + return libDir; end; (* The file of module n in dir that suffix j says: Name.linux-x86_64.raz @@ -4061,7 +4070,7 @@ proc moduleFile(n: INT): ([]U8, []U8, BOOL); var i, j: INT; dir, f: []U8; begin - for i := -1 to numInc do + for i := -1 to numInc + LEN(userDirs) do dir := searchDir(i); for j := 0 to 3 do f := fileName(n, j); @@ -4081,7 +4090,7 @@ mineDev, mineIno := identity(say2("{}/{}", dir, f)); others := ""; past := false; - for i := -1 to numInc do + for i := -1 to numInc + LEN(userDirs) do if past then files := ""; chosen := false; @@ -4117,7 +4126,7 @@ if not found then Diag.error(args.at, say1("can't find module {}", nameOf(name))); where := Diag.shownPath(searchDir(-1)); - for i := 0 to numInc do where := say2("{}, {}", where, Diag.shownPath(searchDir(i))); end; + for i := 0 to numInc + LEN(userDirs) do where := say2("{}, {}", where, Diag.shownPath(searchDir(i))); end; Diag.help(say2("looked for {}.raz in {}", nameOf(name), where)); return nil, Scan.Failed; end; diff --git a/src/Raz.raz b/src/Raz.raz index 6aab3dc..92e817e 100644 --- a/src/Raz.raz +++ b/src/Raz.raz @@ -198,6 +198,53 @@ return p; end; +(* User libraries are source directories, not packages to discover + recursively. Read NAME=value entries so an empty RAZ_PATH differs + from an unset one; Sys.env alone gives "" for both. The slices and + their array live until the compiler exits, as command-line paths do. *) +proc userLibraries; +var path, s: []U8; dirs: ^[]U8; i, first, n: INT; more, found: BOOL; +begin + i := 0; + repeat + s, more := Sys.envAt(i); + if not more then break; end; + if (LEN(s) >= 9) and (s[0..9] = "RAZ_PATH=") then + path := s[9..LEN(s)]; + found := true; + break; + end; + INC(i); + until false; + if not found then + s := Sys.env("HOME"); + if LEN(s) > 0 then + dirs := ^[]U8(Scan.grab(SIZE([]U8))); + dirs[0] := say1("{}/.raz/lib", s); + Parse.setUserDirs(dirs[0..1]); + end; + return; + end; + if LEN(path) = 0 then return; end; + n := 1; + for i := 0 to LEN(path) - 1 do + if path[i] = ':' then INC(n); end; + end; + dirs := ^[]U8(Scan.grab(n * SIZE([]U8))); + n := 0; + first := 0; + for i := 0 to LEN(path) do + if (i = LEN(path)) or (path[i] = ':') then + if i > first then + dirs[n] := path[first..i]; + INC(n); + end; + first := i + 1; + end; + end; + Parse.setUserDirs(dirs[0..n]); +end; + proc main*: INT; var path, outPath, s: []U8; dir: []U8; @@ -274,6 +321,7 @@ i := LEN(dir); while (i > 0) and (dir[i - 1] # '/') do DEC(i); end; if i = 0 then Parse.setLibDir("lib"); else Parse.setLibDir(say1("{}lib", dir[0..i])); end; + userLibraries(); if LEN(outPath) = 0 then outPath := path; last := LEN(outPath); diff --git a/test/check.sh b/test/check.sh index ec70a80..bbd60be 100755 --- a/test/check.sh +++ b/test/check.sh @@ -19,6 +19,10 @@ # the tutorial's, tools/Pretty.raz, and the library's own tests. 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. +RAZ_PATH= +export RAZ_PATH out=build/check mkdir -p $out ulimit -c 0 # the tests stopped by a signal leave no core @@ -318,6 +322,9 @@ # the editors' support, headless, where the editors are installed 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 echo "$pass passed, $fail failed" [ $fail = 0 ] diff --git a/test/library-path.py b/test/library-path.py new file mode 100644 index 0000000..30ce257 --- /dev/null +++ b/test/library-path.py @@ -0,0 +1,173 @@ +#!/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()