Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions apps/code/src/main/utils/fixPath.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,46 @@ describe("fixPath", () => {
expect(parts).toContain("/opt/homebrew/bin");
});

it("preserves entries from the inherited PATH that the login shell lacks", async () => {
// Simulate launching from a terminal where .zshrc has added nvm/mise
// paths that the -lc resolution (only sources .zprofile) won't see.
process.env.PATH =
"/Users/me/.nvm/versions/node/v22.0.0/bin:/Users/me/.local/share/pnpm:/usr/bin:/bin";
mockSpawnSync.mockReturnValue({
status: 0,
stdout: shellOutput("/opt/homebrew/bin:/usr/bin:/bin"),
});

const { fixPath } = await import("./fixPath");
fixPath();

const parts = process.env.PATH?.split(":") ?? [];
// Inherited entries (added by .zshrc, missing from .zprofile) survive.
expect(parts).toContain("/Users/me/.nvm/versions/node/v22.0.0/bin");
expect(parts).toContain("/Users/me/.local/share/pnpm");
// Shell-resolved entries are still merged in.
expect(parts).toContain("/opt/homebrew/bin");
});

it("preserves inherited PATH entries when reading from the cache", async () => {
process.env.PATH = "/Users/me/.nvm/versions/node/v22.0.0/bin:/usr/bin:/bin";
mockExistsSync.mockReturnValue(true);
mockReadFileSync.mockReturnValue(
JSON.stringify({
path: "/opt/homebrew/bin:/usr/bin:/bin",
timestamp: Date.now(),
}),
);

const { fixPath } = await import("./fixPath");
fixPath();

const parts = process.env.PATH?.split(":") ?? [];
expect(parts).toContain("/Users/me/.nvm/versions/node/v22.0.0/bin");
expect(parts).toContain("/opt/homebrew/bin");
expect(mockSpawnSync).not.toHaveBeenCalled();
});
Comment on lines +154 to +192
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Prefer parameterised tests for repeated structure

The two new tests ("preserves entries from the inherited PATH that the login shell lacks" and "preserves inherited PATH entries when reading from the cache") have nearly identical structure: set process.env.PATH to a rich terminal PATH, configure either the shell-resolution or cache mock, call fixPath(), and assert the same inherited entries survive. Per the team's convention, these should be collapsed into a single it.each that drives both code paths (live shell and cache hit) from one set of assertions, keeping the inherited-PATH check DRY.

Context Used: Do not attempt to comment on incorrect alphabetica... (source)

Prompt To Fix With AI
This is a comment left during a code review.
Path: apps/code/src/main/utils/fixPath.test.ts
Line: 154-192

Comment:
**Prefer parameterised tests for repeated structure**

The two new tests ("preserves entries from the inherited PATH that the login shell lacks" and "preserves inherited PATH entries when reading from the cache") have nearly identical structure: set `process.env.PATH` to a rich terminal PATH, configure either the shell-resolution or cache mock, call `fixPath()`, and assert the same inherited entries survive. Per the team's convention, these should be collapsed into a single `it.each` that drives both code paths (live shell and cache hit) from one set of assertions, keeping the inherited-PATH check DRY.

**Context Used:** Do not attempt to comment on incorrect alphabetica... ([source](https://app.greptile.com/review/custom-context?memory=instruction-0))

How can I resolve this? If you propose a fix, please make it concise.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!


it("returns early on win32 without touching PATH", async () => {
setPlatform("win32");
process.env.PATH = "C:\\Windows\\System32";
Expand Down
36 changes: 26 additions & 10 deletions apps/code/src/main/utils/fixPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* includes /opt/homebrew/bin, ~/.local/bin, etc.
*
* This reads the PATH from the user's default shell (in login mode) and
* applies it to process.env.PATH so child processes have access to
* merges it into process.env.PATH so child processes have access to
* user-installed binaries.
*
* IMPORTANT: We use `-lc` (login, non-interactive) instead of `-ilc`
Expand All @@ -13,6 +13,13 @@
* subprocesses and cause zombie process chains when the timeout kills
* only the parent shell.
*
* Because `-lc` skips .zshrc, version-manager paths (nvm, mise, volta) and
* other entries added there are missing from the resolved shell PATH. We
* therefore *merge* with the inherited process.env.PATH rather than
* replacing it — when launched from a terminal (e.g. `pnpm dev`), the
* inherited PATH already has those entries and must be preserved so git
* pre-commit hooks (husky, lint-staged, etc.) can find their tools.
*
* See: https://github.com/PostHog/code/issues/1399
*/

Expand Down Expand Up @@ -170,21 +177,31 @@ function getShellPath(shell: string): string | undefined {
return env?.PATH;
}

function mergeFallbackPaths(basePath: string | undefined): string {
const base = basePath ? basePath.split(":").filter(Boolean) : [];
const existing = new Set(base);
const additions = FALLBACK_PATHS.filter((p) => !existing.has(p));
return [...additions, ...base].join(":");
function mergePaths(sources: (string | undefined)[]): string {
const seen = new Set<string>();
const result: string[] = [];
for (const source of sources) {
if (!source) continue;
for (const segment of source.split(":")) {
if (segment && !seen.has(segment)) {
seen.add(segment);
result.push(segment);
}
}
}
return result.join(":");
}

export function fixPath(): void {
if (process.platform === "win32") {
return;
}

const originalPath = process.env.PATH;

const cached = readCachedPath();
if (cached) {
process.env.PATH = mergeFallbackPaths(cached);
process.env.PATH = mergePaths([...FALLBACK_PATHS, originalPath, cached]);
return;
}

Expand All @@ -193,10 +210,9 @@ export function fixPath(): void {

if (shellPath) {
const cleaned = stripAnsi(shellPath);
const merged = mergeFallbackPaths(cleaned);
process.env.PATH = merged;
process.env.PATH = mergePaths([...FALLBACK_PATHS, originalPath, cleaned]);
writeCachedPath(cleaned);
} else {
process.env.PATH = mergeFallbackPaths(process.env.PATH);
process.env.PATH = mergePaths([...FALLBACK_PATHS, originalPath]);
}
}
Loading