-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
fix(hooks): respect package manager formatter fallback #365
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -75,6 +75,20 @@ function cleanupTestDir(testDir) { | |
| fs.rmSync(testDir, { recursive: true, force: true }); | ||
| } | ||
|
|
||
| function createCommandShim(binDir, name, logFile) { | ||
| const shimPath = path.join(binDir, name); | ||
| const script = `#!/bin/sh | ||
| { | ||
| printf '%s\n' "$(basename "$0")" | ||
| for arg in "$@"; do | ||
| printf '%s\n' "$arg" | ||
| done | ||
| } > ${JSON.stringify(logFile)} | ||
| `; | ||
| fs.writeFileSync(shimPath, script, { mode: 0o755 }); | ||
| return shimPath; | ||
| } | ||
|
|
||
| // Test suite | ||
| async function runTests() { | ||
| console.log('\n=== Testing Hook Scripts ===\n'); | ||
|
|
@@ -701,6 +715,64 @@ async function runTests() { | |
| assert.ok(result.stdout.includes('tool_input'), 'Should pass through original data'); | ||
| })) passed++; else failed++; | ||
|
|
||
| if (await asyncTest('uses CLAUDE_PACKAGE_MANAGER runner for formatter fallback', async () => { | ||
| const testDir = createTestDir(); | ||
| const binDir = path.join(testDir, 'bin'); | ||
| const logFile = path.join(testDir, 'pnpm.log'); | ||
| fs.mkdirSync(binDir, { recursive: true }); | ||
| createCommandShim(binDir, 'pnpm', logFile); | ||
|
|
||
| const testFile = path.join(testDir, 'src', 'example.ts'); | ||
| fs.mkdirSync(path.dirname(testFile), { recursive: true }); | ||
| fs.writeFileSync(path.join(testDir, 'package.json'), JSON.stringify({ name: 'pm-env-test' })); | ||
| fs.writeFileSync(path.join(testDir, '.prettierrc'), '{}'); | ||
| fs.writeFileSync(testFile, 'const answer=42;\n'); | ||
|
|
||
| try { | ||
| const stdinJson = JSON.stringify({ tool_input: { file_path: testFile } }); | ||
| const result = await runScript(path.join(scriptsDir, 'post-edit-format.js'), stdinJson, { | ||
| CLAUDE_PACKAGE_MANAGER: 'pnpm', | ||
| PATH: `${binDir}${path.delimiter}${process.env.PATH || ''}` | ||
| }); | ||
|
|
||
| assert.strictEqual(result.code, 0, 'Should exit 0 with pnpm fallback'); | ||
| const logLines = fs.readFileSync(logFile, 'utf8').trim().split('\n'); | ||
| assert.deepStrictEqual(logLines, ['pnpm', 'dlx', 'prettier', '--write', testFile]); | ||
| } finally { | ||
| cleanupTestDir(testDir); | ||
| } | ||
| })) passed++; else failed++; | ||
|
Comment on lines
+718
to
+744
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Test will fail on Windows due to platform-specific shim issue. The test creates a Additionally, the assertion at Line 740 expects 🔧 Proposed fix: Handle Windows in test assertion assert.strictEqual(result.code, 0, 'Should exit 0 with pnpm fallback');
const logLines = fs.readFileSync(logFile, 'utf8').trim().split('\n');
- assert.deepStrictEqual(logLines, ['pnpm', 'dlx', 'prettier', '--write', testFile]);
+ const expectedBin = process.platform === 'win32' ? 'pnpm.cmd' : 'pnpm';
+ assert.deepStrictEqual(logLines, [expectedBin, 'dlx', 'prettier', '--write', testFile]);🤖 Prompt for AI Agents |
||
|
|
||
| if (await asyncTest('uses project package manager config for formatter fallback', async () => { | ||
| const testDir = createTestDir(); | ||
| const binDir = path.join(testDir, 'bin'); | ||
| const logFile = path.join(testDir, 'bunx.log'); | ||
| fs.mkdirSync(binDir, { recursive: true }); | ||
| fs.mkdirSync(path.join(testDir, '.claude'), { recursive: true }); | ||
| createCommandShim(binDir, 'bunx', logFile); | ||
|
|
||
| const testFile = path.join(testDir, 'src', 'example.ts'); | ||
| fs.mkdirSync(path.dirname(testFile), { recursive: true }); | ||
| fs.writeFileSync(path.join(testDir, 'package.json'), JSON.stringify({ name: 'pm-project-test' })); | ||
| fs.writeFileSync(path.join(testDir, 'biome.json'), '{}'); | ||
| fs.writeFileSync(path.join(testDir, '.claude', 'package-manager.json'), JSON.stringify({ packageManager: 'bun' })); | ||
| fs.writeFileSync(testFile, 'const answer=42;\n'); | ||
|
|
||
| try { | ||
| const stdinJson = JSON.stringify({ tool_input: { file_path: testFile } }); | ||
| const result = await runScript(path.join(scriptsDir, 'post-edit-format.js'), stdinJson, { | ||
| CLAUDE_PACKAGE_MANAGER: '', | ||
| PATH: `${binDir}${path.delimiter}${process.env.PATH || ''}` | ||
| }); | ||
|
|
||
| assert.strictEqual(result.code, 0, 'Should exit 0 with project-config fallback'); | ||
| const logLines = fs.readFileSync(logFile, 'utf8').trim().split('\n'); | ||
| assert.deepStrictEqual(logLines, ['bunx', '@biomejs/biome', 'format', '--write', testFile]); | ||
| } finally { | ||
| cleanupTestDir(testDir); | ||
| } | ||
| })) passed++; else failed++; | ||
|
Comment on lines
+746
to
+774
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same cross-platform issue affects the bunx test. The 🔧 Proposed fix: Handle Windows in test assertion assert.strictEqual(result.code, 0, 'Should exit 0 with project-config fallback');
const logLines = fs.readFileSync(logFile, 'utf8').trim().split('\n');
- assert.deepStrictEqual(logLines, ['bunx', '@biomejs/biome', 'format', '--write', testFile]);
+ const expectedBin = process.platform === 'win32' ? 'bunx.cmd' : 'bunx';
+ assert.deepStrictEqual(logLines, [expectedBin, '@biomejs/biome', 'format', '--write', testFile]);🤖 Prompt for AI Agents |
||
|
|
||
| // post-edit-typecheck.js tests | ||
| console.log('\npost-edit-typecheck.js:'); | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cross-platform issue: POSIX-only shims cause Windows CI failures.
This
createCommandShimfunction creates POSIX shell scripts, but on Windows:getRunnerBinreturns.cmdsuffixed binaries (e.g.,pnpm.cmd).cmdextension#!/bin/sh,printf,for arg in "$@")This is the root cause of the pipeline failures:
🔧 Proposed fix: Platform-aware shim creation
function createCommandShim(binDir, name, logFile) { - const shimPath = path.join(binDir, name); - const script = `#!/bin/sh + const isWindows = process.platform === 'win32'; + const shimPath = path.join(binDir, isWindows ? `${name}.cmd` : name); + + const script = isWindows + ? `@echo off +echo %~n0> "${logFile}" +:loop +if "%~1"=="" goto done +echo %~1>> "${logFile}" +shift +goto loop +:done +` + : `#!/bin/sh { printf '%s\\n' "$(basename "$0")" for arg in "$@"; do printf '%s\\n' "$arg" done } > ${JSON.stringify(logFile)} `; fs.writeFileSync(shimPath, script, { mode: 0o755 }); return shimPath; }As per coding guidelines: "Ensure cross-platform compatibility for Windows, macOS, and Linux via Node.js scripts".
📝 Committable suggestion
🤖 Prompt for AI Agents