Repository navigation
fix(skills): mktemp templates with a non-trailing X suffix break on macOS #2343
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
Merged
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| /** | ||
| * Regression test for issue #2157: `.claude/skills/create-skill/SKILL.md` | ||
| * mandated `mktemp "/tmp/tmp.XXXXXXXXXX.ext"` as its "portable" | ||
| * temp-file template. BSD `mktemp` (macOS) only randomizes a TRAILING run | ||
| * of `X`s — a suffix directly after it returns the template literally | ||
| * instead of creating a unique path, so every skill generated by | ||
| * `/create-skill` following that pattern got a fixed, non-unique temp path | ||
| * on macOS: concurrent sessions collided on the same literal file, and | ||
| * re-runs failed outright with "mkstemp failed ... File exists". | ||
| * | ||
| * The fix rewrites every mandated example to use a trailing-X `mktemp -d` | ||
| * template (creating a temp directory) with the extensioned file placed | ||
| * inside it, and adds a `lint-skill.sh` check (this test's subject) so a | ||
| * future skill reintroducing the broken pattern is caught automatically. | ||
| */ | ||
| import { execFileSync } from 'node:child_process'; | ||
| import fs from 'node:fs'; | ||
| import os from 'node:os'; | ||
| import path from 'node:path'; | ||
| import { fileURLToPath } from 'node:url'; | ||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| const __dirname = path.dirname(fileURLToPath(import.meta.url)); | ||
| const REPO_ROOT = path.resolve(__dirname, '..', '..'); | ||
| const LINT_SCRIPT = path.join( | ||
| REPO_ROOT, | ||
| '.claude', | ||
| 'skills', | ||
| 'create-skill', | ||
| 'scripts', | ||
| 'lint-skill.sh', | ||
| ); | ||
|
|
||
| // lint-skill.sh requires bash 4+ (associative arrays). macOS ships bash 3.2 | ||
| // (Apple stopped updating it at the GPLv2/v3 boundary) as the `bash` on | ||
| // PATH, including on GitHub Actions' macos-latest runner — resolve a real | ||
| // bash 4+ explicitly rather than assuming plain "bash" is adequate. | ||
| function resolveBash4(): string | null { | ||
| const candidates = ['/opt/homebrew/bin/bash', '/usr/local/bin/bash', 'bash']; | ||
| for (const candidate of candidates) { | ||
| try { | ||
| const version = execFileSync(candidate, ['--version'], { encoding: 'utf8' }); | ||
| const match = version.match(/version (\d+)\./); | ||
| if (match && Number(match[1]) >= 4) return candidate; | ||
| } catch { | ||
| // candidate not on PATH — try the next one | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| const BASH4 = resolveBash4(); | ||
|
|
||
| const FRONTMATTER = `--- | ||
| name: lint-test-skill | ||
| description: test | ||
| argument-hint: none | ||
| allowed-tools: Bash | ||
| --- | ||
|
|
||
| ## Phase 0 | ||
|
|
||
| pre-flight | ||
|
|
||
| ## Rules | ||
|
|
||
| rules here | ||
|
|
||
| ## Examples | ||
|
|
||
| example here | ||
| `; | ||
|
|
||
| function runLint(bashBlock: string): { stdout: string; ranSuccessfully: boolean } { | ||
| const content = `${FRONTMATTER}\n\`\`\`bash\n${bashBlock}\n\`\`\`\n\n**Exit condition:** done\n`; | ||
| const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'lint-skill-mktemp-')); | ||
| const skillPath = path.join(dir, 'SKILL.md'); | ||
| fs.writeFileSync(skillPath, content); | ||
| try { | ||
| const result = execFileSync(BASH4!, [LINT_SCRIPT, skillPath], { encoding: 'utf8' }); | ||
| return { stdout: result, ranSuccessfully: true }; | ||
| } catch (err) { | ||
| const e = err as { stdout?: string }; | ||
| const stdout = e.stdout ?? ''; | ||
| // lint-skill.sh only exits non-zero when it found an ERROR (as opposed | ||
| // to a warning) — this test's fixtures only ever trigger warnings, so a | ||
| // non-zero exit here means the script itself failed to run (e.g. a | ||
| // bash-version mismatch), not that it ran and found nothing. | ||
| return { stdout, ranSuccessfully: /lint-skill: \d+ error/.test(stdout) }; | ||
| } finally { | ||
| fs.rmSync(dir, { recursive: true, force: true }); | ||
| } | ||
| } | ||
|
|
||
| // Every "does not flag" assertion below is meaningless if the script never | ||
| // actually ran (e.g. execFileSync throwing before producing output) — an | ||
| // empty/missing stdout would vacuously satisfy `not.toContain(...)`. | ||
| // Asserting ranSuccessfully first turns a silent script failure into a | ||
| // loud, specific test failure instead of a false pass. | ||
| describe.skipIf(BASH4 === null)( | ||
| 'lint-skill.sh flags mktemp templates with a non-trailing X run (#2157)', | ||
| () => { | ||
| it('flags a suffix directly after the X run (the exact bug reported)', () => { | ||
| const { stdout, ranSuccessfully } = runLint('TMPFILE=$(mktemp "/tmp/tmp.XXXXXXXXXX.js")'); | ||
| expect(ranSuccessfully).toBe(true); | ||
| expect(stdout).toContain("mktemp template's X run is not trailing"); | ||
| }); | ||
|
|
||
| it('flags a non-extension suffix too (any character directly after the X run)', () => { | ||
| const { stdout, ranSuccessfully } = runLint( | ||
| 'BLOCKS_FILE=$(mktemp "/tmp/tmp.XXXXXXXXXX.blocks")', | ||
| ); | ||
| expect(ranSuccessfully).toBe(true); | ||
| expect(stdout).toContain("mktemp template's X run is not trailing"); | ||
| }); | ||
|
|
||
| it('does not flag a trailing-X-only template', () => { | ||
| const { stdout, ranSuccessfully } = runLint('TMPFILE=$(mktemp "/tmp/tmp.XXXXXXXXXX")'); | ||
| expect(ranSuccessfully).toBe(true); | ||
| expect(stdout).not.toContain("mktemp template's X run is not trailing"); | ||
| }); | ||
|
|
||
| it('does not flag mktemp -d with a trailing-X template (the recommended fix)', () => { | ||
| const { stdout, ranSuccessfully } = runLint( | ||
| [ | ||
| 'TMP_DIR=$(mktemp -d "/tmp/tmp.XXXXXXXXXX")', | ||
| 'trap \'rm -rf "$TMP_DIR"\' EXIT', | ||
| 'TMP_FILE="$TMP_DIR/probe.js"', | ||
| ].join('\n'), | ||
| ); | ||
| expect(ranSuccessfully).toBe(true); | ||
| expect(stdout).not.toContain("mktemp template's X run is not trailing"); | ||
| }); | ||
|
|
||
| it('does not flag a bare mktemp -d with no template at all', () => { | ||
| const { stdout, ranSuccessfully } = runLint('WORK_DIR=$(mktemp -d)'); | ||
| expect(ranSuccessfully).toBe(true); | ||
| expect(stdout).not.toContain("mktemp template's X run is not trailing"); | ||
| }); | ||
| }, | ||
| ); | ||
|
|
||
| describe.skipIf(BASH4 === null)( | ||
| '.claude/skills/create-skill/SKILL.md itself has no non-trailing-X mktemp templates', | ||
| () => { | ||
| it('passes lint-skill.sh with no mktemp-related warnings', () => { | ||
| const realSkillPath = path.join(REPO_ROOT, '.claude', 'skills', 'create-skill', 'SKILL.md'); | ||
| const stdout = execFileSync(BASH4!, [LINT_SCRIPT, realSkillPath], { encoding: 'utf8' }); | ||
| expect(stdout).toContain('lint-skill: 0 error'); | ||
| expect(stdout).not.toContain("mktemp template's X run is not trailing"); | ||
| }); | ||
| }, | ||
| ); | ||
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.
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.
runLintwritesSKILL.mdunder a randomly named directory while its frontmatter declaresname: lint-test-skill, so the linter reports a name mismatch and exits non-zero. Because the negative cases inspect only stdout and ignorestatus, they pass despite this unrelated error and therefore do not establish that the recommendedmktempforms pass lint cleanly.