Fix Node launcher detection in the standalone installer (#62120)

## Why

Searching the entire executable for Node shebang text can cause the
installer to mistake a native Codex binary for an npm or Bun launcher.

## What changed

Require an exact first-line match for `#!/usr/bin/env node` when
identifying an existing Codex executable as npm- or Bun-managed.

## Testing

Cover a Node launcher and an ELF fixture containing the same shebang
text. Verify that only the launcher triggers the npm warning and the
shell profile update when the install directory is already on `PATH`.

GitOrigin-RevId: 3566a41f2431684aafaf4332a9217ab4f7ceb2a5
This commit is contained in:
Eric Traut
2026-11-17 22:43:51 +00:00
committed by copyberry
parent b8f6787e95
commit 53cd1894aa
2 changed files with 35 additions and 21 deletions
+32 -20
View File
@@ -166,26 +166,33 @@ class InstallShTest(unittest.TestCase):
)
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)
archive, checksum, metadata = create_package_release(root)
(root / "bin").mkdir()
write_executable(root / "bin" / "codex", "#!/usr/bin/env node\n")
def test_noninteractive_install_detects_npm_by_shebang(self) -> None:
for launcher, is_npm in (
("#!/usr/bin/env node\n", True),
("\x7fELF\0#!/usr/bin/env node\n", False),
):
with self.subTest(is_npm=is_npm), tempfile.TemporaryDirectory() as temp_dir:
root = Path(temp_dir)
archive, checksum, metadata = create_package_release(root)
(root / "bin").mkdir()
write_executable(root / "bin" / "codex", launcher)
result, _ = run_installer_in(
root,
VERSION,
metadata_json=metadata,
archive_path=archive,
checksum_path=checksum,
force_macos=True,
)
result, _ = run_installer_in(
root,
VERSION,
metadata_json=metadata,
archive_path=archive,
checksum_path=checksum,
force_macos=True,
install_dir_on_path=True,
)
self.assertEqual(result.returncode, 0, result.stderr)
self.assertIn(
"Leaving the existing npm-managed Codex installed", result.stderr
)
self.assertEqual(result.returncode, 0, result.stderr)
self.assertEqual(
"Leaving the existing npm-managed Codex installed" in result.stderr,
is_npm,
)
self.assertEqual((root / "home" / ".profile").exists(), is_npm)
def test_releases_latest_installs_verified_package_by_default(self) -> None:
with tempfile.TemporaryDirectory() as temp_dir:
@@ -1064,6 +1071,7 @@ def run_installer_in(
fail_ps: bool = False,
daemon_only: bool = False,
manual_update: bool = False,
install_dir_on_path: bool = False,
) -> tuple[subprocess.CompletedProcess[str], list[str]]:
bin_dir = root / "bin"
bin_dir.mkdir(exist_ok=True)
@@ -1191,11 +1199,15 @@ def run_installer_in(
home = root / "home"
home.mkdir(exist_ok=True)
install_dir = install_dir or root / "install-bin"
path = f"{bin_dir}:/usr/bin:/bin"
if install_dir_on_path:
path = f"{bin_dir}:{install_dir}:/usr/bin:/bin"
env = os.environ.copy()
env.update(
{
"CODEX_HOME": str(root / "codex-home"),
"CODEX_INSTALL_DIR": str(install_dir or root / "install-bin"),
"CODEX_INSTALL_DIR": str(install_dir),
"CODEX_NON_INTERACTIVE": "1",
"CODEX_RELEASE": release,
"CODEX_INSTALL_DAEMON_ONLY": "1" if daemon_only else "0",
@@ -1219,7 +1231,7 @@ def run_installer_in(
"CODEX_TEST_RELEASES_MODE": releases_mode,
"CODEX_TEST_REQUEST_LOG": str(request_log),
"HOME": str(home),
"PATH": f"{bin_dir}:/usr/bin:/bin",
"PATH": path,
"SHELL": "/bin/sh",
}
)