Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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`.
Expand Down
1 change: 1 addition & 0 deletions changelog.d/+eessi-lmod-init.bugfix.md
Original file line number Diff line number Diff line change
@@ -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.
57 changes: 49 additions & 8 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -1618,19 +1619,49 @@ 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
\\keep=(WORKING_DIR TMPDIR TMP TEMP EASYBUILD_BUILDPATH EASYBUILD_TMPDIR XDG_CACHE_HOME CARGO_HOME EASYBUILD_SOURCEPATH EASYBUILD_TMP_LOGDIR)
\\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
Expand Down Expand Up @@ -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", .{});
Expand Down
5 changes: 5 additions & 0 deletions tests/test_list_doctor_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
87 changes: 87 additions & 0 deletions tests/test_lmod_init_cli.py
Original file line number Diff line number Diff line change
@@ -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()
Loading