From 0ca82bed50ec7f233932d8b9c1ddb917736b782d Mon Sep 17 00:00:00 2001 From: Rohit Goswami Date: Thu, 19 Nov 2026 12:26:10 +0000 Subject: [PATCH] fix: search an empty PATH entry from RPATH wrappers The wrapper looked up the real compiler with bash read. read drops a trailing colon, and the loop passed over an empty entry, so the wrapper never searched the current directory. It reported a compiler that is only in the current directory as missing, and it ran a later copy when the current directory came first on PATH. An empty PATH entry is the current directory, including the entry a trailing colon names. execvp searches it, and rpath-wrappers already finds a compiler there through proc.which. The wrapper now splits PATH on colons and keeps every entry. An unset PATH still uses /bin:/usr/bin, the default command search path. --- changelog.d/+rpath-empty-path-entry.bugfix.md | 1 + src/rpath.zig | 18 +++++++++-- tests/test_rpath_wrappers_cli.py | 32 +++++++++++++++++++ 3 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 changelog.d/+rpath-empty-path-entry.bugfix.md diff --git a/changelog.d/+rpath-empty-path-entry.bugfix.md b/changelog.d/+rpath-empty-path-entry.bugfix.md new file mode 100644 index 0000000..45dd6b5 --- /dev/null +++ b/changelog.d/+rpath-empty-path-entry.bugfix.md @@ -0,0 +1 @@ +`eb-local` RPATH wrappers now search an empty `PATH` entry as the current directory, including a trailing colon, as `execvp` does. diff --git a/src/rpath.zig b/src/rpath.zig index 19cbd37..026da19 100644 --- a/src/rpath.zig +++ b/src/rpath.zig @@ -30,7 +30,9 @@ pub const self_linking = [_][]const u8{ "clang", "clang++", "flang", "flang-new" pub const marker = ".eb-local-rpath"; /// One script for every name: it runs the next command of its own name on -/// PATH. When that command links, it adds a run path for each absolute +/// PATH. An empty entry is the current directory, including the one a +/// trailing colon names, as execvp searches. When that command links, it +/// adds a run path for each absolute /// library directory named with -L, and for a compiler driver for each /// LIBRARY_PATH entry, which EESSI's modules set and the driver passes to /// the linker; system and compatibility-layer directories are left out, as @@ -41,9 +43,19 @@ pub const script = \\# RPATH wrapper written by eb-local; see `eb-local help`. \\name=${0##*/} \\real= - \\IFS=: read -r -a path_dirs <<< "$PATH" + \\# read drops a trailing colon, so an empty entry would disappear. Split + \\# on colons and keep every one. An empty entry is the current directory. + \\# An unset PATH is the default command search path, not the current directory. + \\path_dirs=() + \\if [ -n "${PATH+x}" ]; then _eb_path_rest="${PATH}:"; else _eb_path_rest="/bin:/usr/bin:"; fi + \\while [ -n "$_eb_path_rest" ]; do + \\ path_dirs+=("${_eb_path_rest%%:*}") + \\ _eb_path_rest="${_eb_path_rest#*:}" + \\done + \\unset _eb_path_rest \\for d in "${path_dirs[@]}"; do - \\ if [ -n "$d" ] && [ ! -e "$d/.eb-local-rpath" ] && [ -f "$d/$name" ] && [ -x "$d/$name" ]; then + \\ [ -n "$d" ] || d=. + \\ if [ ! -e "$d/.eb-local-rpath" ] && [ -f "$d/$name" ] && [ -x "$d/$name" ]; then \\ real=$d/$name \\ break \\ fi diff --git a/tests/test_rpath_wrappers_cli.py b/tests/test_rpath_wrappers_cli.py index 38786ce..4280b9d 100644 --- a/tests/test_rpath_wrappers_cli.py +++ b/tests/test_rpath_wrappers_cli.py @@ -269,6 +269,38 @@ def test_hostile_wrapper_destinations_exit_two(self): self.assertTrue(link.is_symlink()) self.assertFalse(outside.exists()) + def _run_wrapper(self, setup_stdout, path, cwd): + script = setup_stdout + "gcc -c foo.c\n" + return subprocess.run( + ["/bin/bash", "--noprofile", "--norc", "-c", script], + env={"PATH": path, "LC_ALL": "C.UTF-8", "LANG": "C.UTF-8"}, + cwd=cwd, + capture_output=True, + text=True, + ) + + def test_an_empty_path_entry_is_the_current_directory(self): + got = self.run_cmd(["--for", "gcc"]) + self.assertEqual(got.returncode, 0, got.stderr) + cwd_gcc = self.root / "gcc" + cwd_gcc.write_text("#!/bin/sh\nprintf 'from-cwd\\n'\n", encoding="utf-8") + cwd_gcc.chmod(0o755) + + # A trailing colon is an empty entry after every other directory. + trailing = self._run_wrapper(got.stdout, "", self.root) + self.assertEqual(trailing.returncode, 0, trailing.stdout + trailing.stderr) + self.assertEqual(trailing.stdout, "from-cwd\n") + self.assertEqual(trailing.stderr, "") + + # An empty entry in the middle is the current directory, and it wins + # over a later directory that also has gcc. + self.log.write_text("", encoding="utf-8") + middle = self._run_wrapper(got.stdout, f":{self.bin}", self.root) + self.assertEqual(middle.returncode, 0, middle.stdout + middle.stderr) + self.assertEqual(middle.stdout, "from-cwd\n") + self.assertEqual(middle.stderr, "") + self.assertEqual(self.log.read_text(encoding="utf-8"), "") + def test_tmpfs_enospc_exits_two_and_cleans_wrapper_tree(self): mnt = hostile_fs.enter_full_tmpfs(self) if mnt is None: