-
Notifications
You must be signed in to change notification settings - Fork 856
fix(claude): gate shell hook on CLI installation #2105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
lilinxiong
wants to merge
3
commits into
lidge-jun:dev
from
lilinxiong:fix/claude-shell-hook-install-detection
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| import { afterEach, beforeEach, describe, expect, test } from "bun:test"; | ||
| import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; | ||
| import { tmpdir } from "node:os"; | ||
| import { delimiter, join } from "node:path"; | ||
| import { claudeCodeCliInstalled, reconcileShellHook } from "../src/server/system-env"; | ||
|
|
||
| const originalPlatform = process.platform; | ||
| let originalHome: string | undefined; | ||
| let originalPath: string | undefined; | ||
| let root = ""; | ||
| let binDir = ""; | ||
| let zshrcPath = ""; | ||
|
|
||
| function setPlatform(platform: NodeJS.Platform): void { | ||
| Object.defineProperty(process, "platform", { configurable: true, value: platform }); | ||
| } | ||
|
|
||
| /** Create the smallest executable that represents a Claude Code CLI on PATH. */ | ||
| function installClaudeCli(): void { | ||
| const executable = join(binDir, "claude"); | ||
| writeFileSync(executable, "#!/bin/sh\nexit 0\n", { mode: 0o755 }); | ||
| chmodSync(executable, 0o755); | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| root = mkdtempSync(join(tmpdir(), "ocx-claude-hook-")); | ||
| binDir = join(root, "bin"); | ||
| zshrcPath = join(root, ".zshrc"); | ||
| mkdirSync(binDir); | ||
| originalHome = process.env.HOME; | ||
| originalPath = process.env.PATH; | ||
| process.env.HOME = root; | ||
| process.env.PATH = binDir; | ||
| setPlatform("darwin"); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| if (originalHome === undefined) delete process.env.HOME; | ||
| else process.env.HOME = originalHome; | ||
| if (originalPath === undefined) delete process.env.PATH; | ||
| else process.env.PATH = originalPath; | ||
| setPlatform(originalPlatform); | ||
| rmSync(root, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| describe("Claude Code shell-hook reconciliation", () => { | ||
| test("does not create .zshrc when Claude Code is absent", () => { | ||
| expect(claudeCodeCliInstalled()).toBe(false); | ||
| expect(reconcileShellHook(true)).toMatchObject({ changed: false, state: "absent" }); | ||
| expect(existsSync(zshrcPath)).toBe(false); | ||
| }); | ||
|
|
||
| test("removes only the stale OpenCodex hook when Claude Code is absent", () => { | ||
| writeFileSync(zshrcPath, [ | ||
| "export USER_SETTING=1", | ||
| "# opencodex claude-env hook", | ||
| "[ -f ~/.opencodex/claude-env.sh ] && source ~/.opencodex/claude-env.sh", | ||
| "alias keep-me='yes'", | ||
| "", | ||
| ].join("\n")); | ||
|
|
||
| expect(reconcileShellHook(true)).toEqual({ | ||
| changed: true, | ||
| state: "absent", | ||
| reason: "Claude Code not installed", | ||
| }); | ||
| const content = readFileSync(zshrcPath, "utf8"); | ||
| expect(content).toContain("export USER_SETTING=1"); | ||
| expect(content).toContain("alias keep-me='yes'"); | ||
| expect(content).not.toContain("opencodex claude-env hook"); | ||
| expect(content).not.toContain("claude-env.sh"); | ||
| }); | ||
|
|
||
| test("installs the hook only for an executable Claude Code CLI and stays idempotent", () => { | ||
| installClaudeCli(); | ||
| expect(claudeCodeCliInstalled()).toBe(true); | ||
| expect(reconcileShellHook(true)).toEqual({ changed: true, state: "installed" }); | ||
| expect(reconcileShellHook(true)).toEqual({ | ||
| changed: false, | ||
| state: "installed", | ||
| reason: "already installed", | ||
| }); | ||
| expect(readFileSync(zshrcPath, "utf8").match(/opencodex claude-env hook/g)).toHaveLength(1); | ||
| }); | ||
|
|
||
| test("does not treat a non-executable claude file as an installed CLI", () => { | ||
| writeFileSync(join(binDir, "claude"), "#!/bin/sh\nexit 0\n", { mode: 0o644 }); | ||
|
|
||
| expect(claudeCodeCliInstalled()).toBe(false); | ||
| expect(reconcileShellHook(true)).toMatchObject({ changed: false, state: "absent" }); | ||
| expect(existsSync(zshrcPath)).toBe(false); | ||
| }); | ||
|
|
||
| test("removes the hook when system environment integration is inactive", () => { | ||
| installClaudeCli(); | ||
| reconcileShellHook(true); | ||
|
|
||
| expect(reconcileShellHook(false)).toEqual({ | ||
| changed: true, | ||
| state: "absent", | ||
| reason: "system environment inactive", | ||
| }); | ||
| expect(readFileSync(zshrcPath, "utf8")).not.toContain("opencodex claude-env hook"); | ||
| }); | ||
|
|
||
| test("ignores empty PATH segments instead of trusting a workspace-local claude file", () => { | ||
| writeFileSync(join(root, "claude"), "#!/bin/sh\nexit 0\n", { mode: 0o755 }); | ||
| const previousCwd = process.cwd(); | ||
| process.chdir(root); | ||
| process.env.PATH = `${delimiter}${binDir}`; | ||
|
|
||
| try { | ||
| expect(claudeCodeCliInstalled()).toBe(false); | ||
| } finally { | ||
| process.chdir(previousCwd); | ||
| } | ||
| }); | ||
|
|
||
| test("reports hook-removal failures instead of claiming the hook is absent", () => { | ||
| mkdirSync(zshrcPath); | ||
|
|
||
| expect(reconcileShellHook(false)).toEqual({ | ||
| changed: false, | ||
| state: "failed", | ||
| reason: "read/write failed", | ||
| }); | ||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| test("start and ensure reconcile the hook from the actual injection result", async () => { | ||
| const source = await Bun.file(new URL("../src/cli/index.ts", import.meta.url)).text(); | ||
|
|
||
| expect(source).not.toMatch(/\n\s*installShellHook\(\);/); | ||
| expect(source.match(/reconcileShellHook\(systemEnv\.injected\)/g)).toHaveLength(2); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.