Restore executor tool paths after login shell startup (#49467)

## Why

Login shell startup can reset `PATH`, preventing commands and their children from finding bundled tools such as `rg` in executor-reported directories.

## What changed

When `login_shell_package_path` is enabled, prepend missing executor-reported directories to `PATH` inside the POSIX login shell before running the requested command. Preserve existing entries and the order of newly added directories, and export `PATH` for child commands.

Respect explicit `PATH` overrides and preserve the requested login mode when directories are unusable or `PATH` is read-only. Keep shell diagnostics and `$LINENO` aligned with the original script, including when directory names contain newlines. The feature remains disabled by default.

## Testing

Add shell tests for nested tool lookup, existing entries, quoting, line numbers, and read-only `PATH`. Extend remote execution tests to cover feature gating, explicit overrides, unusable directories, and preservation of the original command in execution events.

GitOrigin-RevId: 301aa34e72cc6292293693250e190c19903f390a
This commit is contained in:
Adam Perry @ OpenAI
2026-09-30 03:22:55 +00:00
committed by copyberry
parent 8c3612fb63
commit 76a6e55d5a
5 changed files with 447 additions and 20 deletions
+63
View File
@@ -1,4 +1,5 @@
use super::*;
use pretty_assertions::assert_eq;
use std::path::PathBuf;
use std::process::Command;
@@ -143,6 +144,68 @@ fn derive_exec_args() {
);
}
/// Diagnostics and `$LINENO` must refer to the original script, even with newlines in the package path.
#[test]
fn codex_path_setup_preserves_shell_script_line_numbers() -> anyhow::Result<()> {
let temp_dir = tempfile::tempdir()?;
let run = |argv: &[String]| {
Command::new(&argv[0])
.args(&argv[1..])
.env("ZDOTDIR", temp_dir.path())
.env("HOME", temp_dir.path())
.env_remove("BASH_ENV")
.output()
};
for shell_type in [ShellType::Bash, ShellType::Zsh, ShellType::Sh] {
let Some(shell) = get_shell(shell_type) else {
assert_eq!(shell_type, ShellType::Zsh, "only zsh may be unavailable");
continue;
};
let invocation = ShellInvocation {
shell,
use_login_shell: true,
};
for directory in [
"codex-path",
"codex '\\c[*]$(printf bad)`printf bad`\npath\n",
] {
let directory = temp_dir.path().join(directory);
let path = PathUri::from_host_native_path(&directory)?;
let commands = [
":\nif then",
"printf '%s\\n' \"$LINENO\"\nprintf '%s\\n' \"$LINENO\"",
];
for command in commands {
if command.contains("LINENO") && shell_type == ShellType::Sh {
continue; // dash does not implement LINENO.
}
let original = invocation
.shell
.derive_exec_args(command, /*use_login_shell*/ true);
let wrapped = invocation
.derive_exec_args_with_path_prepends(command, std::slice::from_ref(&path))
.expect("a POSIX package path should be usable");
assert_eq!(
run(&wrapped)?,
run(&original)?,
"{shell_type:?}: {directory:?}"
);
assert_eq!(wrapped[2].lines().count(), command.lines().count());
}
let command = "printf '%s' \"$PATH\"";
let wrapped = invocation
.derive_exec_args_with_path_prepends(command, std::slice::from_ref(&path))
.expect("a POSIX package path should be usable");
let output = run(&wrapped)?;
assert!(output.status.success());
let actual_path = String::from_utf8(output.stdout)?;
assert_eq!(actual_path.split(':').next(), directory.to_str());
}
}
Ok(())
}
#[tokio::test]
async fn test_current_shell_detects_zsh() {
let shell = Command::new("sh")