-
Notifications
You must be signed in to change notification settings - Fork 46
Fix Windows CLI shim execution #445
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
Open
ayskobtw-lil
wants to merge
4
commits into
profullstack:master
Choose a base branch
from
ayskobtw-lil:codex/windows-pandoc-test-shim
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+106
−7
Open
Changes from all commits
Commits
Show all changes
4 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import { mkdtemp, rm, writeFile } from 'node:fs/promises'; | ||
| import { tmpdir } from 'node:os'; | ||
| import { delimiter, join } from 'node:path'; | ||
| import { afterEach, describe, expect, it } from 'vitest'; | ||
| import { ensureCli, exec } from './exec.js'; | ||
|
|
||
| const tempDirs: string[] = []; | ||
| const oldPath = process.env.PATH; | ||
|
|
||
| afterEach(async () => { | ||
| process.env.PATH = oldPath; | ||
| await Promise.all(tempDirs.splice(0).map((dir) => rm(dir, { recursive: true, force: true }))); | ||
| }); | ||
|
|
||
| describe('exec', () => { | ||
| it('preserves percent-wrapped arguments on Windows shell execution', async () => { | ||
| const binDir = await mkdtemp(join(tmpdir(), 'sh1pt-exec-bin-')); | ||
| tempDirs.push(binDir); | ||
| await installEchoArgsCli(binDir, 'sh1pt-echo-args'); | ||
| process.env.PATH = `${binDir}${delimiter}${oldPath ?? ''}`; | ||
|
|
||
| const result = await exec('sh1pt-echo-args', ['%SH1PT_EXEC_LITERAL%', 'C:\\tmp\\path\\'], { | ||
| log: () => {}, | ||
| }); | ||
|
|
||
| expect(result.exitCode).toBe(0); | ||
| expect(JSON.parse(result.stdout.trim())).toEqual(['%SH1PT_EXEC_LITERAL%', 'C:\\tmp\\path\\']); | ||
| }); | ||
| }); | ||
|
|
||
| describe('ensureCli', () => { | ||
| it('throws when a command exits non-zero instead of reporting it as installed', async () => { | ||
| const binDir = await mkdtemp(join(tmpdir(), 'sh1pt-exec-bin-')); | ||
| tempDirs.push(binDir); | ||
| await installFailingCli(binDir, 'sh1pt-missing-version'); | ||
| process.env.PATH = `${binDir}${delimiter}${oldPath ?? ''}`; | ||
|
|
||
| await expect(ensureCli('sh1pt-missing-version', 'install it', () => {})) | ||
| .rejects.toThrow('sh1pt-missing-version not installed. install it'); | ||
| }); | ||
| }); | ||
|
|
||
| async function installFailingCli(binDir: string, name: string): Promise<void> { | ||
| if (process.platform === 'win32') { | ||
| await writeFile(join(binDir, `${name}.cmd`), '@echo off\r\nexit /b 9009\r\n', 'utf-8'); | ||
| return; | ||
| } | ||
|
|
||
| const script = join(binDir, name); | ||
| await writeFile(script, '#!/usr/bin/env sh\nexit 127\n', { encoding: 'utf-8', mode: 0o755 }); | ||
| } | ||
|
|
||
| async function installEchoArgsCli(binDir: string, name: string): Promise<void> { | ||
| const helper = join(binDir, 'echo-args.js'); | ||
| await writeFile(helper, 'console.log(JSON.stringify(process.argv.slice(2)));\n', 'utf-8'); | ||
|
|
||
| if (process.platform === 'win32') { | ||
| await writeFile( | ||
| join(binDir, `${name}.cmd`), | ||
| `@echo off\r\n"${process.execPath}" "%~dp0echo-args.js" %*\r\n`, | ||
| 'utf-8', | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| const script = join(binDir, name); | ||
| await writeFile(script, `#!/usr/bin/env sh\n"${process.execPath}" "${helper}" "$@"\n`, { | ||
| encoding: 'utf-8', | ||
| mode: 0o755, | ||
| }); | ||
| } |
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
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.
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.
ensureClinow rejects installed CLIs that exit non-zero for--versionOn Linux (where
spawnstill directly executes the binary), any CLI that returns a non-zero exit code from--version— even though it is installed and working — will now be thrown ascommand not found: <cmd>and ultimately reported as "not installed". Before this PR, onlyENOENTtriggered that branch on Linux; a non-zero exit was silently ignored and treated as "present". The broaderexitCode !== 0guard is necessary on Windows (where cmd.exe swallowsENOENTand returns 9009), but applying it unconditionally on Linux changes the contract for every caller. Consider limiting the exitCode check toprocess.platform === 'win32'so the Linux path retains its original ENOENT-only semantics.