From d2c26df7d1464c51f493fdf6a44cfa381902ce84 Mon Sep 17 00:00:00 2001 From: Rohit Goswami Date: Thu, 19 Nov 2026 14:04:47 +0100 Subject: [PATCH] feat(dev): --pure starts from no loaded modules and no loader variables Without it the modules a shell already has stay loaded beside the ones dev names. --pure runs module purge and unsets LD_LIBRARY_PATH, LD_PRELOAD and LD_AUDIT once a module command exists and before the stack loads. The eval hint repeats the option. --- changelog.d/+dev-pure.feature.md | 1 + docs/orgmode/reference/cli.org | 10 +++++++--- docs/reference/cli.md | 12 +++++++++--- src/main.zig | 28 ++++++++++++++++++++++------ tests/test_dev_cli_modes.py | 15 +++++++++++++++ 5 files changed, 54 insertions(+), 12 deletions(-) create mode 100644 changelog.d/+dev-pure.feature.md diff --git a/changelog.d/+dev-pure.feature.md b/changelog.d/+dev-pure.feature.md new file mode 100644 index 0000000..d552a05 --- /dev/null +++ b/changelog.d/+dev-pure.feature.md @@ -0,0 +1 @@ +`eb-local dev --pure` runs `module purge` and unsets `LD_LIBRARY_PATH`, `LD_PRELOAD` and `LD_AUDIT` before the stack loads, so modules and loader variables the shell already had do not reach the build environment. diff --git a/docs/orgmode/reference/cli.org b/docs/orgmode/reference/cli.org index 96fdb1d..7b43fc4 100644 --- a/docs/orgmode/reference/cli.org +++ b/docs/orgmode/reference/cli.org @@ -12,7 +12,7 @@ eb-local [OPTION]... --explain [eb options] eb-local eb [--] [eb options] eb-local --explain eb [--] [eb options] eb-local [OPTION]... env [--strict] [--json] -eb-local [OPTION]... dev [--strict] [--json] [--buildenv MODULE] [--envrc] [MODULE...] +eb-local [OPTION]... dev [--strict] [--pure] [--json] [--buildenv MODULE] [--envrc] [MODULE...] eb-local rpath-wrappers [--for FAMILY | --unwrapped] eb-local check [--recursive] [--quiet] [--json] FILE... eb-local check (--module NAME/VERSION... | --dev | --prefix DIR) [--recursive] [--quiet] [--json] @@ -60,14 +60,15 @@ directory you own. eb-local env [--strict] [--json] lines that make your installed modules visible; --strict exits when the EESSI init file is missing (also when EB_LOCAL_ENV_STRICT=1) - eb-local dev [--strict] [--json] [--buildenv MODULE] [--envrc] [MODULE...] + eb-local dev [--strict] [--pure] [--json] [--buildenv MODULE] [--envrc] [MODULE...] the same, plus MODULEs and a build environment, so gcc, make, cmake and meson just work; --strict exits when the EESSI init file is missing (also when EB_LOCAL_ENV_STRICT=1); --buildenv names the build environment to load; --json prints the change as JSON, for env too; - --envrc writes that line to .envrc for direnv + --envrc writes that line to .envrc for direnv; + --pure purges loaded modules and LD_* first eb-local rpath-wrappers [--for FAMILY | --unwrapped] lines that put RPATH wrappers for the compilers on PATH first; dev uses them when no build @@ -331,6 +332,9 @@ eval "$(/path/to/eb-local dev MODULE...)" #+end_src The program is named by its own path, and =--config=, =--profile=, =--project= and =--buildenv= are repeated on the line. A block already in the file is replaced and every other line is kept; a file without one gets it appended. A begin line without its end line is refused. Run =direnv allow= once per change. direnv evaluates the file in bash and gives the calling shell the change, so the environment follows the directory in bash, zsh, fish, nushell and the other shells direnv has a hook for, and is taken back on leaving it. As with =--json=, only variables reach the calling shell. +| =--pure= | Before anything loads, run =module purge= and unset =LD_LIBRARY_PATH=, =LD_PRELOAD= and =LD_AUDIT=. | + +Without =--pure=, the modules the shell already has stay loaded beside the ones =dev= names, and the loader variables stay as they are, with a warning for =LD_LIBRARY_PATH=. With it, the build environment is the stack, the named modules and the build environment only. Sticky modules survive =module purge=. A site that preloads a library for every process (XALT through =LD_PRELOAD=) loses that in this shell. A =MODULE= starts with a letter or digit and holds letters, digits, =.=, =_=, =+=, =-= and single slashes, with none at the end. With =LD_LIBRARY_PATH= set in the shell, =dev= warns on stderr. diff --git a/docs/reference/cli.md b/docs/reference/cli.md index 55f6616..47e202f 100644 --- a/docs/reference/cli.md +++ b/docs/reference/cli.md @@ -14,7 +14,7 @@ eb-local [OPTION]... --explain [eb options] eb-local eb [--] [eb options] eb-local --explain eb [--] [eb options] eb-local [OPTION]... env [--strict] [--json] -eb-local [OPTION]... dev [--strict] [--json] [--buildenv MODULE] [--envrc] [MODULE...] +eb-local [OPTION]... dev [--strict] [--pure] [--json] [--buildenv MODULE] [--envrc] [MODULE...] eb-local rpath-wrappers [--for FAMILY | --unwrapped] eb-local check [--recursive] [--quiet] [--json] FILE... eb-local check (--module NAME/VERSION... | --dev | --prefix DIR) [--recursive] [--quiet] [--json] @@ -62,14 +62,15 @@ directory you own. eb-local env [--strict] [--json] lines that make your installed modules visible; --strict exits when the EESSI init file is missing (also when EB_LOCAL_ENV_STRICT=1) - eb-local dev [--strict] [--json] [--buildenv MODULE] [--envrc] [MODULE...] + eb-local dev [--strict] [--pure] [--json] [--buildenv MODULE] [--envrc] [MODULE...] the same, plus MODULEs and a build environment, so gcc, make, cmake and meson just work; --strict exits when the EESSI init file is missing (also when EB_LOCAL_ENV_STRICT=1); --buildenv names the build environment to load; --json prints the change as JSON, for env too; - --envrc writes that line to .envrc for direnv + --envrc writes that line to .envrc for direnv; + --pure purges loaded modules and LD_* first eb-local rpath-wrappers [--for FAMILY | --unwrapped] lines that put RPATH wrappers for the compilers on PATH first; dev uses them when no build @@ -334,6 +335,11 @@ eval "$(/path/to/eb-local dev MODULE...)" The program is named by its own path, and `--config`, `--profile`, `--project` and `--buildenv` are repeated on the line. A block already in the file is replaced and every other line is kept; a file without one gets it appended. A begin line without its end line is refused. Run `direnv allow` once per change. direnv evaluates the file in bash and gives the calling shell the change, so the environment follows the directory in bash, zsh, fish, nushell and the other shells direnv has a hook for, and is taken back on leaving it. As with `--json`, only variables reach the calling shell. +| `--pure` | Before anything loads, run `module purge` and unset `LD_LIBRARY_PATH`, `LD_PRELOAD` and `LD_AUDIT`. | +|---|---| + +Without `--pure`, the modules the shell already has stay loaded beside the ones `dev` names, and the loader variables stay as they are, with a warning for `LD_LIBRARY_PATH`. With it, the build environment is the stack, the named modules and the build environment only. Sticky modules survive `module purge`. A site that preloads a library for every process (XALT through `LD_PRELOAD`) loses that in this shell. + A `MODULE` starts with a letter or digit and holds letters, digits, `.`, `_`, `+`, `-` and single slashes, with none at the end. With `LD_LIBRARY_PATH` set in the shell, `dev` warns on stderr. diff --git a/src/main.zig b/src/main.zig index 337570c..1839b3c 100644 --- a/src/main.zig +++ b/src/main.zig @@ -65,14 +65,15 @@ const usage_text = \\ eb-local env [--strict] [--json] lines that make your installed modules visible; \\ --strict exits when the EESSI init file is missing \\ (also when EB_LOCAL_ENV_STRICT=1) - \\ eb-local dev [--strict] [--json] [--buildenv MODULE] [--envrc] [MODULE...] + \\ eb-local dev [--strict] [--pure] [--json] [--buildenv MODULE] [--envrc] [MODULE...] \\ the same, plus MODULEs and a build environment, \\ so gcc, make, cmake and meson just work; \\ --strict exits when the EESSI init file is missing \\ (also when EB_LOCAL_ENV_STRICT=1); \\ --buildenv names the build environment to load; \\ --json prints the change as JSON, for env too; - \\ --envrc writes that line to .envrc for direnv + \\ --envrc writes that line to .envrc for direnv; + \\ --pure purges loaded modules and LD_* first \\ eb-local rpath-wrappers [--for FAMILY | --unwrapped] \\ lines that put RPATH wrappers for the compilers \\ on PATH first; dev uses them when no build @@ -290,7 +291,16 @@ const env_strict_prelude = \\ ; -fn environmentLines(c: Ctx, s: config.Settings, p: Place, strict: bool) []const u8 { +/// `dev --pure`: start from no loaded modules and no loader variables, so +/// what the shell had before does not reach the build environment. Sticky +/// modules stay, as `module purge` leaves them. +const pure_prelude = + \\module purge >/dev/null 2>&1 || true + \\unset LD_LIBRARY_PATH LD_PRELOAD LD_AUDIT + \\ +; + +fn environmentLines(c: Ctx, s: config.Settings, p: Place, strict: bool, pure: bool) []const u8 { const a = c.a; var out: std.ArrayList(u8) = .empty; const add = struct { @@ -300,6 +310,7 @@ fn environmentLines(c: Ctx, s: config.Settings, p: Place, strict: bool) []const }.f; if (strict) add(a, &out, env_strict_prelude); add(a, &out, lmod_init); + if (pure) add(a, &out, pure_prelude); add(a, &out, str(a, "module unload {s} >/dev/null 2>&1 || true\n", .{q(a, s.extend_module.value)})); add(a, &out, \\unset EESSI_CVMFS_INSTALL EESSI_SITE_INSTALL EESSI_USER_INSTALL EESSI_PROJECT_INSTALL @@ -367,6 +378,7 @@ fn replay(a: Allocator, command: []const u8, s: config.Settings, g: Global, cano if (s.project.value != null) parts.appendSlice(a, &.{ "--project", q(a, canonical_prefix) }) catch oom(); parts.append(a, command) catch oom(); if (std.mem.eql(u8, command, "dev")) { + if (g.pure) parts.append(a, "--pure") catch oom(); if (g.buildenv) |p| parts.appendSlice(a, &.{ "--buildenv", q(a, p) }) catch oom(); for (extra) |m| parts.append(a, q(a, m)) catch oom(); } @@ -480,6 +492,8 @@ const Global = struct { json_out: bool = false, /// `dev --envrc`: write the dev line to `.envrc` here, for direnv. envrc: bool = false, + /// `dev --pure`: purge the loaded modules and the loader variables first. + pure: bool = false, }; fn nonEmpty(v: ?[]const u8) ?[]const u8 { @@ -684,7 +698,7 @@ fn emitEnvChange(c: Ctx, kind: []const u8, lines: []const u8) u8 { fn cmdEnv(c: Ctx, s: config.Settings, g: Global) u8 { const p = place(c, s); - const lines = environmentLines(c, s, p, envStrict(c, g)); + const lines = environmentLines(c, s, p, envStrict(c, g), false); if (g.json_out) return emitEnvChange(c, "env", lines); evalHint(replay(c.a, "env", s, g, p.prefix, &.{})); cli.put(1, lines); @@ -753,7 +767,7 @@ fn cmdDev(c: Ctx, s: config.Settings, g: Global, extra: []const []const u8) u8 { if (g.envrc) return writeEnvrc(c, replay(a, "dev", s, g, installed.prefix, extra)); if (!g.json_out) evalHint(replay(a, "dev", s, g, installed.prefix, extra)); var out: std.ArrayList(u8) = .empty; - out.appendSlice(a, environmentLines(c, s, installed, envStrict(c, g))) catch oom(); + out.appendSlice(a, environmentLines(c, s, installed, envStrict(c, g), g.pure)) catch oom(); // The modules the program uses first, the build environment last, so // its wrappers come first on PATH. When the program links, they record // a run path for each directory on LIBRARY_PATH, which the modules set @@ -776,7 +790,7 @@ fn cmdDev(c: Ctx, s: config.Settings, g: Global, extra: []const []const u8) u8 { out.appendSlice(a, str(a, "export EB_LOCAL_DEV_PREFIX={s}\"/$EESSI_VERSION/$EESSI_SOFTWARE_SUBDIR${{EESSI_ACCELERATOR_TARGET:+/$EESSI_ACCELERATOR_TARGET}}\"\n", .{q(a, dev_root)})) catch oom(); if (g.json_out) return emitEnvChange(c, "dev", out.items); cli.put(1, out.items); - if (nonEmpty(c.env.get("LD_LIBRARY_PATH")) != null) { + if (!g.pure and nonEmpty(c.env.get("LD_LIBRARY_PATH")) != null) { warn("LD_LIBRARY_PATH is set in this shell; programs built here do not need it,", .{}); cli.err(" and it can mix host libraries into EESSI's. Unset it unless you know you need it.\n", .{}); } @@ -4869,6 +4883,8 @@ pub fn main(init: std.process.Init) u8 { g.json_out = true; } else if (std.mem.eql(u8, x, "--envrc")) { g.envrc = true; + } else if (std.mem.eql(u8, x, "--pure")) { + g.pure = true; } else if (std.mem.eql(u8, x, "--buildenv")) { g.buildenv = value(args, &i, "--buildenv"); } else if (joined(x, "--buildenv")) |v| { diff --git a/tests/test_dev_cli_modes.py b/tests/test_dev_cli_modes.py index f678b54..8ee99c6 100644 --- a/tests/test_dev_cli_modes.py +++ b/tests/test_dev_cli_modes.py @@ -110,6 +110,21 @@ def test_the_envrc_block_is_valid_bash_that_runs_the_dev_lines(self): capture_output=True, text=True, ) self.assertTrue(run.stdout.startswith(str(self.prefix) + "/dev/"), run.stdout + run.stderr) + def test_pure_purges_modules_and_loader_variables_before_the_stack_loads(self): + plain = self.run_dev(["FFTW/3.3.10-GCC-15.2.0"], LD_LIBRARY_PATH="/host/lib") + pure = self.run_dev(["--pure", "FFTW/3.3.10-GCC-15.2.0"], LD_LIBRARY_PATH="/host/lib") + self.assertEqual(pure.returncode, 0, pure.stderr) + purge = "module purge >/dev/null 2>&1 || true\nunset LD_LIBRARY_PATH LD_PRELOAD LD_AUDIT\n" + self.assertNotIn("module purge", plain.stdout) + self.assertEqual(pure.stdout.count(purge), 1) + # the rest of the lines is what dev prints without it + self.assertEqual(pure.stdout.replace(purge, ""), plain.stdout) + # after a module command exists, before anything is loaded + self.assertLess(pure.stdout.index("type module"), pure.stdout.index(purge)) + self.assertLess(pure.stdout.index(purge), pure.stdout.index("module load")) + # the variable is unset by the lines, so there is nothing to warn about + self.assertIn("LD_LIBRARY_PATH is set", plain.stderr) + self.assertNotIn("LD_LIBRARY_PATH is set", pure.stderr) def test_names_the_host_ca_bundle_without_replacing_one_the_shell_set(self): got = self.run_dev([])