diff --git a/scripts/install/install.sh b/scripts/install/install.sh index 78ad516c61..93588cb78f 100755 --- a/scripts/install/install.sh +++ b/scripts/install/install.sh @@ -991,7 +991,9 @@ classify_existing_codex() { ;; esac - if [ -f "$existing_path" ] && grep -F "#!/usr/bin/env node" "$existing_path" >/dev/null 2>&1; then + if [ -f "$existing_path" ] && + LC_ALL=C head -n 1 "$existing_path" 2>/dev/null | + LC_ALL=C grep -aFx '#!/usr/bin/env node' >/dev/null 2>&1; then case "$existing_path" in *".bun"*) printf 'bun\n' diff --git a/scripts/install/test_install_sh.py b/scripts/install/test_install_sh.py index 28fb2943fb..1b0fccf893 100644 --- a/scripts/install/test_install_sh.py +++ b/scripts/install/test_install_sh.py @@ -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", } )