Skip to content

Commit 99a242e

Browse files
[codex-cli] Add ripgrep as a dependency for node environment (#2237)
## Summary Ripgrep is our preferred tool for file search. When users install via `brew install codex`, it's automatically installed as a dependency. We want to ensure that users running via an npm install also have this tool! Microsoft has already solved this problem for VS Code - let's not reinvent the wheel. This approach of appending to the PATH directly might be a bit heavy-handed, but feels reasonably robust to a variety of environment concerns. Open to thoughts on better approaches here! ## Testing - [x] confirmed this import approach works with `node -e "const { rgPath } = require('@vscode/ripgrep'); require('child_process').spawn(rgPath, ['--version'], { stdio: 'inherit' })"` - [x] Ran codex.js locally with `rg` uninstalled, asked it to run `which rg`. Output below: ``` ⚡ Ran command which rg; echo $? ⎿ /Users/dylan.hurd/code/dh--npm-rg/node_modules/@vscode/ripgrep/bin/rg 0 codex Re-running to confirm the path and exit code. - Path: `/Users/dylan.hurd/code/dh--npm-rg/node_modules/@vscode/ripgrep/bin/rg` - Exit code: `0` ```
1 parent 08ed618 commit 99a242e

File tree

5 files changed

+196
-5
lines changed

5 files changed

+196
-5
lines changed

codex-cli/bin/codex.js

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ switch (platform) {
4343
targetTriple = "x86_64-pc-windows-msvc.exe";
4444
break;
4545
case "arm64":
46-
// We do not build this today, fall through...
46+
// We do not build this today, fall through...
4747
default:
4848
break;
4949
}
@@ -65,9 +65,43 @@ const binaryPath = path.join(__dirname, "..", "bin", `codex-${targetTriple}`);
6565
// receives a fatal signal, both processes exit in a predictable manner.
6666
const { spawn } = await import("child_process");
6767

68+
async function tryImport(moduleName) {
69+
try {
70+
// eslint-disable-next-line node/no-unsupported-features/es-syntax
71+
return await import(moduleName);
72+
} catch (err) {
73+
return null;
74+
}
75+
}
76+
77+
async function resolveRgDir() {
78+
const ripgrep = await tryImport("@vscode/ripgrep");
79+
if (!ripgrep?.rgPath) {
80+
return null;
81+
}
82+
return path.dirname(ripgrep.rgPath);
83+
}
84+
85+
function getUpdatedPath(newDirs) {
86+
const pathSep = process.platform === "win32" ? ";" : ":";
87+
const existingPath = process.env.PATH || "";
88+
const updatedPath = [
89+
...newDirs,
90+
...existingPath.split(pathSep).filter(Boolean),
91+
].join(pathSep);
92+
return updatedPath;
93+
}
94+
95+
const additionalDirs = [];
96+
const rgDir = await resolveRgDir();
97+
if (rgDir) {
98+
additionalDirs.push(rgDir);
99+
}
100+
const updatedPath = getUpdatedPath(additionalDirs);
101+
68102
const child = spawn(binaryPath, process.argv.slice(2), {
69103
stdio: "inherit",
70-
env: { ...process.env, CODEX_MANAGED_BY_NPM: "1" },
104+
env: { ...process.env, PATH: updatedPath, CODEX_MANAGED_BY_NPM: "1" },
71105
});
72106

73107
child.on("error", (err) => {
@@ -120,4 +154,3 @@ if (childResult.type === "signal") {
120154
} else {
121155
process.exit(childResult.exitCode);
122156
}
123-

codex-cli/package-lock.json

Lines changed: 119 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

codex-cli/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,11 @@
1616
"repository": {
1717
"type": "git",
1818
"url": "git+https://github.com/openai/codex.git"
19+
},
20+
"dependencies": {
21+
"@vscode/ripgrep": "^1.15.14"
22+
},
23+
"devDependencies": {
24+
"prettier": "^3.3.3"
1925
}
2026
}

package-lock.json

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"private": true,
44
"description": "Tools for repo-wide maintenance.",
55
"scripts": {
6-
"format": "prettier --check *.json *.md .github/workflows/*.yml",
7-
"format:fix": "prettier --write *.json *.md .github/workflows/*.yml"
6+
"format": "prettier --check *.json *.md .github/workflows/*.yml **/*.js",
7+
"format:fix": "prettier --write *.json *.md .github/workflows/*.yml **/*.js"
88
},
99
"devDependencies": {
1010
"prettier": "^3.5.3"

0 commit comments

Comments
 (0)