Escape installer PATH entries for shell metacharacters (#62055)

## Why

Installation directories containing shell metacharacters can be interpreted incorrectly in shell profiles and the printed launch command.

## What changed

Escape installation directories in the generated `PATH` export and reuse that export in the current-terminal instructions. Preserve the escape sequences when rewriting an existing profile block by passing the line through the `awk` environment.

## Testing

Add a regression test that installs into directories containing shell metacharacters, verifies `codex` lookup after adding and updating the profile, and executes the printed launch command.

GitOrigin-RevId: b6c6044382128c97dfb503516f5eeaa934c85bbb
This commit is contained in:
Eric Traut
2026-11-17 14:38:53 +00:00
committed by copyberry
parent 5a4aff8431
commit 640b86dc87
2 changed files with 50 additions and 39 deletions
+33 -1
View File
@@ -135,6 +135,37 @@ class InstallShTest(unittest.TestCase):
)
self.assertTrue(os.access(host_path, os.X_OK))
def test_shell_lookup_after_adding_and_updating_profile(self) -> None:
with tempfile.TemporaryDirectory() as temp_dir:
root = Path(temp_dir)
archive, checksum, metadata = create_package_release(root)
for name in ["literal$HOME", r'quote" back\tslash and !tick`']:
install_dir = root / name
result, _ = run_installer_in(
root,
VERSION,
install_dir=install_dir,
metadata_json=metadata,
archive_path=archive,
checksum_path=checksum,
force_macos=True,
)
self.assertEqual(result.returncode, 0, result.stderr)
env = {"HOME": str(root / "home"), "PATH": "/usr/bin:/bin"}
lookup = subprocess.check_output(
["/bin/sh", "-c", '. "$HOME/.profile" && command -v codex'],
env=env,
text=True,
)
self.assertEqual(lookup.rstrip("\n"), str(install_dir / "codex"))
instructions = result.stdout.split("Current terminal: ", 1)[1]
launched = subprocess.check_output(
["/bin/sh", "-c", instructions.splitlines()[0]], env=env, text=True
)
self.assertEqual(launched.strip(), f"codex-cli {VERSION}")
def test_noninteractive_install_warns_about_existing_npm_codex(self) -> None:
with tempfile.TemporaryDirectory() as temp_dir:
root = Path(temp_dir)
@@ -1017,6 +1048,7 @@ def run_installer_in(
release: str,
*,
installer_path: Path = INSTALL_SCRIPT,
install_dir: Path | None = None,
metadata_failure: bool = False,
metadata_json: str | None = None,
releases_metadata_json: str | None = None,
@@ -1163,7 +1195,7 @@ def run_installer_in(
env.update(
{
"CODEX_HOME": str(root / "codex-home"),
"CODEX_INSTALL_DIR": str(root / "install-bin"),
"CODEX_INSTALL_DIR": str(install_dir or root / "install-bin"),
"CODEX_NON_INTERACTIVE": "1",
"CODEX_RELEASE": release,
"CODEX_INSTALL_DAEMON_ONLY": "1" if daemon_only else "0",