From 7cbd7d12b1d81ee284ffe5395f1afd64a25cbb0a Mon Sep 17 00:00:00 2001 From: Rohit Goswami Date: Tue, 17 Nov 2026 04:34:20 +0000 Subject: [PATCH] fix: find EESSI's Lmod where the shell has no module command On a host whose only Lmod is EESSI's, as EESSI's native install sets it up, a build in an allocation stopped with "eb-local: no module command found": the build script tried only $LMOD_PKG/init/bash and /etc/profile.d/lmod.sh. `eval "$(eb-local env)"` and dev printed module commands a new shell could not run, and doctor said that nothing stopped a build. A build, env and dev now try EESSI's own init last, the one `eb-local eb` sources. doctor names the init a build sources, and with a site file fails when none of the three can be read. The doctor tests set LMOD_CMD, as Lmod's init does, since their module double stands in for Lmod's. --- README.md | 3 +- changelog.d/+eessi-lmod-init.bugfix.md | 1 + src/main.zig | 57 ++++++++++++++--- tests/test_list_doctor_cli.py | 5 ++ tests/test_lmod_init_cli.py | 87 ++++++++++++++++++++++++++ 5 files changed, 144 insertions(+), 9 deletions(-) create mode 100644 changelog.d/+eessi-lmod-init.bugfix.md create mode 100644 tests/test_lmod_init_cli.py diff --git a/README.md b/README.md index 4237c19..8011b7a 100644 --- a/README.md +++ b/README.md @@ -139,6 +139,7 @@ A project build runs with the umask EESSI-extend sets for a project install, 002 A successful build removes its workspace, unless `EB_LOCAL_KEEP_WORK=1` is set; a failed one keeps it for inspection until the allocation ends. The build does not pass `LD_LIBRARY_PATH`, `LD_PRELOAD` or `LD_AUDIT` through to `eb`, and it does not run `BASH_ENV` or honour `SHELLOPTS`, so a startup file or a trace cannot see the build or its secrets. +In a shell or job without a module command, a build, `env` and `dev` source the first Lmod init they can read: `$LMOD_PKG/init/bash` (`/usr/share/lmod/lmod/init/bash` with `LMOD_PKG` unset), `/etc/profile.d/lmod.sh`, then EESSI's own, the init `eb-local eb` sources, for a host whose only Lmod is EESSI's. `eb-local doctor` names the one a build would source. The 1.x Bash script tries only the first two, in a build. ## EESSI-extend, without a site file @@ -314,7 +315,7 @@ Build them in an allocation on this node type; each target needs its own build. `eb-local list GSL` or `eb-local list GSL/2.8-GCC-15.2.0` narrows it. -`eb-local doctor` checks, one line each, what would stop a build, `eb-local eb` or a module load: the site file, the secrets a build reads (never their values), the prefix and its permissions, install variables and `LD_LIBRARY_PATH` left in the shell, the EESSI init `eb-local eb` sources, the allocation's node-local mount, whether EESSI is loaded and your modules are on `MODULEPATH`, eessi-arch-guard, and rtrash. +`eb-local doctor` checks, one line each, what would stop a build, `eb-local eb` or a module load: the site file, the secrets a build reads (never their values), the prefix and its permissions, install variables and `LD_LIBRARY_PATH` left in the shell, the EESSI init `eb-local eb` sources, the allocation's node-local mount, whether EESSI is loaded and your modules are on `MODULEPATH`, the Lmod init a build sources where the shell has no module command, eessi-arch-guard, and rtrash. Inside a job with EESSI loaded, it asks eessi-arch-guard, where that is on `PATH`, as a build asks it, and shows the target it selects; a guard that cannot select one is a `warn`, since a build's own run of it decides. Each line says `ok`, `info`, `warn` or `FAIL`; it exits 1 when one says `FAIL`, and changes nothing. With a site file, what stops a build is a `FAIL`, and what stops only `eb-local eb` is a `warn`. diff --git a/changelog.d/+eessi-lmod-init.bugfix.md b/changelog.d/+eessi-lmod-init.bugfix.md new file mode 100644 index 0000000..6b59a3f --- /dev/null +++ b/changelog.d/+eessi-lmod-init.bugfix.md @@ -0,0 +1 @@ +A build, `env` and `dev` in a shell without a module command source EESSI's own Lmod, from the init `eb-local eb` sources, when the host has no Lmod init of its own. On a host whose only Lmod is EESSI's, a build stopped with `eb-local: no module command found` and `eval "$(eb-local env)"` printed `module: command not found`, while `eb-local doctor` said that nothing stopped a build. `doctor` now names the init a build sources, and with a site file fails when none of the three can be read; the build's message names them too. diff --git a/src/main.zig b/src/main.zig index 4d48829..b722e86 100644 --- a/src/main.zig +++ b/src/main.zig @@ -188,6 +188,7 @@ fn environmentLines(c: Ctx, s: config.Settings, p: Place) []const u8 { o.appendSlice(al, text) catch oom(); } }.f; + add(a, &out, lmod_init); 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 @@ -1618,6 +1619,36 @@ const test_prelude = \\ ; +/// What a shell without a module command sources for one, in this order: +/// the host's Lmod, then EESSI's own, from the init `eb-local eb` sources, +/// for a host whose only Lmod is EESSI's. `lmodInits` names the same files. +const lmod_init = + \\if ! type module >/dev/null 2>&1; then + \\ for eb_local_init in "${LMOD_PKG:-/usr/share/lmod/lmod}/init/bash" /etc/profile.d/lmod.sh \ + \\ "${EESSI_CVMFS_REPO:-/cvmfs/software.eessi.io}/versions/${EESSI_VERSION:-2026.06}/init/lmod/bash"; do + \\ [ -r "$eb_local_init" ] && { . "$eb_local_init" >/dev/null; break; } + \\ done + \\ unset eb_local_init + \\fi + \\ +; + +/// The files `lmod_init` tries, in its order. +fn lmodInits(a: Allocator, env: *const std.process.Environ.Map) [3][]const u8 { + const pkg = nonEmpty(env.get("LMOD_PKG")) orelse "/usr/share/lmod/lmod"; + return .{ + std.fs.path.join(a, &.{ pkg, "init", "bash" }) catch oom(), + "/etc/profile.d/lmod.sh", + extend.initPath(a, env) catch oom(), + }; +} + +fn readable(path: []const u8) bool { + var zb: [sys.path_max:0]u8 = undefined; + const zp = sys.z(&zb, path) catch return false; + return linux.errno(linux.faccessat(linux.AT.FDCWD, zp.ptr, linux.R_OK, 0)) == .SUCCESS; +} + const build_script = "set -eo pipefail\n" ++ (if (build_options.test_ops) test_prelude else "") ++ \\stack=$1 extend=$2 install_var=$3 prefix=$4 mode=$5 \\shift 5 @@ -1625,12 +1656,12 @@ const build_script = "set -eo pipefail\n" ++ (if (build_options.test_ops) test_p \\declare -A saved \\for v in "${keep[@]}"; do saved[$v]=${!v}; done \\restore() { for v in "${keep[@]}"; do export "$v=${saved[$v]}"; done; } - \\if ! type module >/dev/null 2>&1; then - \\ for init in "${LMOD_PKG:-/usr/share/lmod/lmod}/init/bash" /etc/profile.d/lmod.sh; do - \\ [ -r "$init" ] && { source "$init"; break; } - \\ done - \\fi - \\type module >/dev/null 2>&1 || { echo "eb-local: no module command found" >&2; exit 1; } + \\ +++ lmod_init ++ + \\type module >/dev/null 2>&1 || { + \\ echo "eb-local: no module command found; none of ${LMOD_PKG:-/usr/share/lmod/lmod}/init/bash, /etc/profile.d/lmod.sh or ${EESSI_CVMFS_REPO:-/cvmfs/software.eessi.io}/versions/${EESSI_VERSION:-2026.06}/init/lmod/bash defined one" >&2 + \\ exit 1 + \\} \\# EESSI-extend reads the variables when it loads, so it is loaded fresh. \\module unload "$extend" >/dev/null 2>&1 || true \\unset EESSI_CVMFS_INSTALL EESSI_SITE_INSTALL EESSI_USER_INSTALL EESSI_PROJECT_INSTALL @@ -2279,8 +2310,18 @@ fn cmdDoctor(c: Ctx, s: config.Settings, g: Global) u8 { }; if (seen) note(a, &out, "ok ", "your modules are on MODULEPATH", .{}) else note(a, &out, "warn", "your modules are not on MODULEPATH in this shell; eval \"$(eb-local env)\"", .{}); } else |_| {} - if (nonEmpty(c.env.get("LMOD_CMD")) == null and nonEmpty(c.env.get("MODULESHOME")) == null) - note(a, &out, "info", "no module command in this shell; builds source Lmod's init themselves", .{}); + if (nonEmpty(c.env.get("LMOD_CMD")) == null and nonEmpty(c.env.get("MODULESHOME")) == null) { + const inits = lmodInits(a, c.env); + for (inits) |file| { + if (readable(file)) { + note(a, &out, "info", "no module command in this shell; builds, env and dev source {s}", .{file}); + break; + } + } else { + note(a, &out, if (site) "FAIL" else "warn", "no module command in this shell, and none of {s}, {s} or {s} can be read; builds stop there, and the module commands env and dev print do not run", .{ inits[0], inits[1], inits[2] }); + if (site) failed = true; + } + } if (proc.which(a, "rtrash", c.env.get("PATH") orelse "") catch null) |_| { note(a, &out, "ok ", "rtrash is on PATH: clean and uninstall move files to the trash", .{}); } else note(a, &out, "warn", "rtrash is not on PATH: clean and uninstall remove nothing unless given --delete", .{}); diff --git a/tests/test_list_doctor_cli.py b/tests/test_list_doctor_cli.py index 6bccc6d..ca79a45 100644 --- a/tests/test_list_doctor_cli.py +++ b/tests/test_list_doctor_cli.py @@ -32,6 +32,11 @@ @unittest.skipUnless(binary.BIN, "list and doctor are features of the Zig build") class ListAndDoctor(StorageFixture): + def setUp(self): + super().setUp() + # The module double stands in for Lmod's, whose init sets LMOD_CMD. + self.env["LMOD_CMD"] = "/usr/share/lmod/lmod/libexec/lmod" + def run_tool(self, args, **extra): argv, env = binary.command(self.script, args, dict(self.env, **extra)) return subprocess.run(argv, env=env, cwd=self.root, capture_output=True, text=True) diff --git a/tests/test_lmod_init_cli.py b/tests/test_lmod_init_cli.py new file mode 100644 index 0000000..aa53f63 --- /dev/null +++ b/tests/test_lmod_init_cli.py @@ -0,0 +1,87 @@ +#!/usr/bin/env python3 +"""A host whose only Lmod is EESSI's: a build, env and doctor, through the command line of the Zig build. + +The shell eb-local runs in has no module command and the host has no Lmod +init. The fake EESSI init defines the module and eb doubles of +test_storage, as EESSI's init defines Lmod's module function; the eb +program on PATH runs that eb. +""" +from pathlib import Path +import shlex +import subprocess +import unittest + +import binary +from test_storage import StorageFixture + +EB = '#!/bin/bash\ndeclare -F eb >/dev/null || { echo "no eb double" >&2; exit 127; }\neb "$@"\n' + + +@unittest.skipUnless(binary.BIN, "the EESSI Lmod fallback is a feature of the Zig build") +class EessiLmod(StorageFixture): + def setUp(self): + super().setUp() + if Path("/etc/profile.d/lmod.sh").exists(): + self.skipTest("/etc/profile.d/lmod.sh is this host's Lmod init, which comes before EESSI's") + self.init = self.root / "repo" / "versions" / "2026.06" / "init" / "lmod" / "bash" + bin_dir = self.root / "bin" + bin_dir.mkdir() + (bin_dir / "eb").write_text(EB) + (bin_dir / "eb").chmod(0o755) + for name in [n for n in self.env if n.startswith("BASH_FUNC_")]: + del self.env[name] + for name in ("BASH_ENV", "LMOD_CMD", "MODULESHOME", "MODULEPATH", "EESSI_VERSION"): + self.env.pop(name, None) + self.env.update(PATH=f"{bin_dir}:/usr/bin:/bin", LMOD_PKG=str(self.root / "no-lmod"), + EESSI_CVMFS_REPO=str(self.root / "repo")) + self.tried = f"{self.root}/no-lmod/init/bash, /etc/profile.d/lmod.sh or {self.init}" + + def eessi_init(self): + self.init.parent.mkdir(parents=True) + self.init.write_text(f"source {shlex.quote(str(self.root / 'doubles.sh'))}\necho 'EESSI init'\n") + + def test_a_build_sources_eessis_lmod_when_the_host_has_none(self): + run = self.run_wrapper() + self.assertEqual(run.returncode, 1, run.stdout + run.stderr) + self.assertIn(f"eb-local: no module command found; none of {self.tried} defined one\n", run.stderr) + self.assertFalse((self.root / "eb.json").exists()) + + self.eessi_init() + run = self.run_wrapper() + self.assertEqual(run.returncode, 0, run.stdout + run.stderr) + self.assertTrue((self.root / "eb.json").exists()) + self.assertNotIn("EESSI init", run.stdout) + + def test_env_in_a_shell_without_a_module_command_loads_eessi(self): + self.eessi_init() + run = self.run_wrapper(["env"], SLURM_JOB_ID="") + self.assertEqual(run.returncode, 0, run.stderr) + shell = subprocess.run(["bash", "--noprofile", "--norc", "-c", run.stdout + 'printf %s "$EESSI_SOFTWARE_SUBDIR"'], + env=self.env, cwd=self.root, capture_output=True, text=True) + self.assertEqual(shell.returncode, 0, shell.stderr) + self.assertNotIn("command not found", shell.stderr) + self.assertEqual(shell.stdout, "x86_64/amd/zen4") + + def test_doctor_names_the_init_a_build_sources_and_fails_without_one(self): + run = self.run_wrapper(["doctor"]) + self.assertEqual(run.returncode, 1, run.stdout) + self.assertIn(f"FAIL no module command in this shell, and none of {self.tried} can be read; builds stop there", + run.stdout) + self.assertTrue(run.stdout.rstrip().endswith("Something above says FAIL; builds stop there."), run.stdout) + + self.eessi_init() + run = self.run_wrapper(["doctor"]) + self.assertEqual(run.returncode, 0, run.stdout) + self.assertIn(f"info no module command in this shell; builds, env and dev source {self.init}\n", run.stdout) + self.assertNotIn("FAIL", run.stdout) + + def test_doctor_without_a_site_file_warns_that_env_and_dev_need_a_module_command(self): + for installed in ("/etc/eb-local/config.toml", "/etc/eb-local.conf"): + if Path(installed).exists(): + self.skipTest(f"{installed} is this host's site file") + run = self.run_wrapper(["doctor"], EB_LOCAL_CONFIG="", XDG_CONFIG_HOME=str(self.root / "xdg"), SLURM_JOB_ID="") + self.assertIn(f"warn no module command in this shell, and none of {self.tried} can be read", run.stdout) + + +if __name__ == "__main__": + unittest.main()