From ec64fb5ef1d2906c14b2c81ceca85e4877848e85 Mon Sep 17 00:00:00 2001 From: carlos-alm Date: Fri, 7 Aug 2026 23:46:50 -0600 Subject: [PATCH 1/2] fix(skills): mktemp templates with a non-trailing X suffix break on macOS (#2157) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit BSD mktemp (macOS) only randomizes a TRAILING run of X's — a suffix directly after it (mktemp "tmp.XXXXXXXXXX.ext") returns the template literally instead of creating a unique path. create-skill/SKILL.md mandated exactly this broken pattern in Patterns 2/3/13/14 and the Phase 5 checklist, so every generated skill following it got a fixed, non-unique temp path on macOS: concurrent sessions collided on the same literal file, and re-runs failed with "mkstemp failed ... File exists". lint-skill.sh and smoke-test-skill.sh reproduced the same broken pattern in their own mktemp calls. Fix: rewrite every mandated example to mktemp -d a trailing-X directory template, placing the extensioned file inside it when an extension is required. Add a lint-skill.sh check that flags any mktemp template whose X run is not trailing, so a future skill reintroducing this is caught automatically instead of silently shipping. docs check acknowledged. --- .claude/skills/create-skill/SKILL.md | 23 ++-- .../skills/create-skill/scripts/lint-skill.sh | 20 ++- .../create-skill/scripts/smoke-test-skill.sh | 6 +- .../unit/lint-skill-mktemp-trailing-x.test.ts | 114 ++++++++++++++++++ 4 files changed, 153 insertions(+), 10 deletions(-) create mode 100644 tests/unit/lint-skill-mktemp-trailing-x.test.ts diff --git a/.claude/skills/create-skill/SKILL.md b/.claude/skills/create-skill/SKILL.md index 15e3ab4ce..8a068e91c 100644 --- a/.claude/skills/create-skill/SKILL.md +++ b/.claude/skills/create-skill/SKILL.md @@ -181,8 +181,13 @@ git show HEAD:$FILE 2>/dev/null | codegraph where --file - **Correct:** ````markdown ```bash -PREV_FILE=$(mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX.js") # adjust extension to match the language of $FILE; template syntax is portable (macOS + Linux) -trap 'rm -f "$PREV_FILE"' EXIT +# mktemp -d with a trailing-X template, then a named file with the right +# extension inside it — a suffix directly after the X run (mktemp +# "tmp.XXXXXXXXXX.js") returns the template literally on macOS BSD mktemp +# instead of randomizing it (issue #2157); only a trailing X run is portable. +PREV_DIR=$(mktemp -d "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX") +PREV_FILE="$PREV_DIR/prev.js" # adjust extension to match the language of $FILE +trap 'rm -rf "$PREV_DIR"' EXIT # $FILE is expected to be set by the surrounding loop, e.g. for FILE in $(git diff --name-only HEAD); do ... done # 2>/dev/null: suppress git's "fatal: Path X does not exist in HEAD" — the else branch already warns the user if git show HEAD:"$FILE" > "$PREV_FILE" 2>/dev/null; then @@ -190,17 +195,19 @@ if git show HEAD:"$FILE" > "$PREV_FILE" 2>/dev/null; then else echo "WARN: $FILE is new or unreadable in HEAD — skipping before/after comparison" fi -rm -f "$PREV_FILE" +rm -rf "$PREV_DIR" trap - EXIT ``` ```` ### Pattern 3: Temp files need extensions -Codegraph's language detection is extension-based. Temp files passed to codegraph must have the correct extension: +Codegraph's language detection is extension-based. Temp files passed to codegraph must have the correct extension — but `mktemp`'s own template can only end in a trailing `X` run (a suffix directly after the X's, like `tmp.XXXXXXXXXX.js`, returns the template literally instead of randomizing it on macOS BSD `mktemp` — issue #2157). Create a temp directory instead and place the named, extensioned file inside it: ```bash -mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX.js" # NOT just mktemp — template syntax is cross-platform (macOS + Linux) +TMP_DIR=$(mktemp -d "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX") # NOT just mktemp — template syntax is cross-platform (macOS + Linux) +trap 'rm -rf "$TMP_DIR"' EXIT +TMP_FILE="$TMP_DIR/probe.js" # extension lives on the file name, not the mktemp template ``` ### Pattern 4: No hardcoded temp paths @@ -281,7 +288,7 @@ This supports both idempotent re-runs and resume-after-failure. Avoid shell constructs that behave differently across platforms: - Use `find ... -name "*.ext" | grep -q .` instead of `find ... -quit` (non-portable) or glob expansion (`ls *.ext`) which differs between bash versions -- Use `mktemp` with template syntax (`mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX.ext"`) — GNU flags like `--suffix` and `-p` are not available on macOS BSD `mktemp` +- Use `mktemp` with template syntax (`mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"`) — GNU flags like `--suffix` and `-p` are not available on macOS BSD `mktemp`. The `X` run must be **trailing** — a suffix directly after it (`tmp.XXXXXXXXXX.ext`) returns the template literally instead of randomizing it on macOS BSD `mktemp` (issue #2157); when an extension is required (Pattern 3), `mktemp -d` a directory and place the named file inside it instead - Use `sed -i.bak` instead of `sed -i ''` (GNU vs BSD incompatibility) - Document any platform-specific behavior with a comment: `# NOTE: requires GNU coreutils` @@ -290,7 +297,7 @@ Avoid shell constructs that behave differently across platforms: Any phase that creates temp files or modifies repo state must set a cleanup trap. Without it, errors mid-phase leak temp files or leave dirty state: ```bash -TMPFILE=$(mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX.json") +TMPFILE=$(mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX") trap 'rm -f "$TMPFILE"' EXIT # ... operations that might fail ... # Reset when done if more work follows: @@ -556,7 +563,7 @@ trap - EXIT Mentally trace a second execution of the skill on the same state: - Does Phase 0 handle pre-existing artifacts (skip, warn, overwrite)? - Do file-creation steps fail if the file already exists? -- Are `mktemp` paths unique across runs (they should be by default)? +- Are `mktemp` paths unique across runs? Check every template's `X` run is **trailing** (`tmp.XXXXXXXXXX`, not `tmp.XXXXXXXXXX.ext`) — a suffix after the X's breaks uniqueness on macOS BSD `mktemp` (issue #2157) even though it works on Linux Document any idempotency fix applied. diff --git a/.claude/skills/create-skill/scripts/lint-skill.sh b/.claude/skills/create-skill/scripts/lint-skill.sh index 87a243f7f..8bc6847db 100644 --- a/.claude/skills/create-skill/scripts/lint-skill.sh +++ b/.claude/skills/create-skill/scripts/lint-skill.sh @@ -31,7 +31,12 @@ warn() { echo "WARN: $1"; WARNINGS=$((WARNINGS + 1)); } # Extract bash blocks (skip quadruple-backtick example regions) and check # if UPPER_CASE variables assigned in one block are referenced in a later # block without file-based persistence. -BLOCKS_FILE=$(mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX.blocks") +# Trailing X's only (no suffix) — BSD mktemp (macOS) only randomizes a +# trailing X run; a suffix after it (e.g. ".blocks") returns the template +# literally instead of a unique path (issue #2157). The extension isn't +# load-bearing here (this file is only ever grepped/awked, never dispatched +# by codegraph's extension-based language detection). +BLOCKS_FILE=$(mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX") trap 'rm -f "$BLOCKS_FILE"' EXIT # Extract bash blocks with block index, skipping those inside ```` regions. @@ -296,6 +301,19 @@ while IFS=$'\t' read -r bnum line; do fi done < "$BLOCKS_FILE" +# ── Check 10b: mktemp template with a non-trailing X run (issue #2157) ── +# BSD mktemp (macOS) only randomizes a TRAILING run of X's — a suffix +# directly after it (tmp.XXXXXXXXXX.ext) returns the template literally +# instead of creating a unique path, causing silent collisions across +# concurrent runs and "File exists" failures on re-runs. Matches any X{6,} +# run immediately followed by something other than another X, a closing +# quote/paren, or whitespace. +while IFS=$'\t' read -r bnum line; do + if [[ "$line" =~ mktemp ]] && [[ "$line" =~ X{6,}[^X\"\'\ \)] ]]; then + warn "bash block $bnum: mktemp template's X run is not trailing — BSD mktemp (macOS) won't randomize it; use a trailing-X template, or mktemp -d a directory and place the named/extensioned file inside it (Pattern 3/13)" + fi +done < "$BLOCKS_FILE" + # ── Check 11: Hardcoded /tmp/ paths ───────────────────────────────── line_num=0 in_quad=false diff --git a/.claude/skills/create-skill/scripts/smoke-test-skill.sh b/.claude/skills/create-skill/scripts/smoke-test-skill.sh index 54e4d3eae..fabaa352b 100644 --- a/.claude/skills/create-skill/scripts/smoke-test-skill.sh +++ b/.claude/skills/create-skill/scripts/smoke-test-skill.sh @@ -17,7 +17,11 @@ if [ ! -f "$SKILL_FILE" ]; then exit 1 fi -TMPBLOCK=$(mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX.sh") +# Trailing X's only (no suffix) — BSD mktemp (macOS) only randomizes a +# trailing X run; a suffix after it (e.g. ".sh") returns the template +# literally instead of a unique path (issue #2157). bash -n below doesn't +# need the .sh extension anyway. +TMPBLOCK=$(mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX") trap 'rm -f "$TMPBLOCK"' EXIT PASS=0 diff --git a/tests/unit/lint-skill-mktemp-trailing-x.test.ts b/tests/unit/lint-skill-mktemp-trailing-x.test.ts new file mode 100644 index 000000000..7daff1358 --- /dev/null +++ b/tests/unit/lint-skill-mktemp-trailing-x.test.ts @@ -0,0 +1,114 @@ +/** + * 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', +); + +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; status: number | null } { + 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('bash', [LINT_SCRIPT, skillPath], { encoding: 'utf8' }); + return { stdout: result, status: 0 }; + } catch (err) { + const e = err as { stdout?: string; status?: number | null }; + return { stdout: e.stdout ?? '', status: e.status ?? 1 }; + } finally { + fs.rmSync(dir, { recursive: true, force: true }); + } +} + +describe('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 } = runLint('TMPFILE=$(mktemp "/tmp/tmp.XXXXXXXXXX.js")'); + 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 } = runLint('BLOCKS_FILE=$(mktemp "/tmp/tmp.XXXXXXXXXX.blocks")'); + expect(stdout).toContain("mktemp template's X run is not trailing"); + }); + + it('does not flag a trailing-X-only template', () => { + const { stdout } = runLint('TMPFILE=$(mktemp "/tmp/tmp.XXXXXXXXXX")'); + 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 } = runLint( + [ + 'TMP_DIR=$(mktemp -d "/tmp/tmp.XXXXXXXXXX")', + 'trap \'rm -rf "$TMP_DIR"\' EXIT', + 'TMP_FILE="$TMP_DIR/probe.js"', + ].join('\n'), + ); + 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 } = runLint('WORK_DIR=$(mktemp -d)'); + expect(stdout).not.toContain("mktemp template's X run is not trailing"); + }); +}); + +describe('.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'); + let stdout: string; + try { + stdout = execFileSync('bash', [LINT_SCRIPT, realSkillPath], { encoding: 'utf8' }); + } catch (err) { + stdout = (err as { stdout?: string }).stdout ?? ''; + } + expect(stdout).not.toContain("mktemp template's X run is not trailing"); + }); +}); From 34db32c28768ffb2cde5d7d85a62d4396979bec7 Mon Sep 17 00:00:00 2001 From: carlos-alm Date: Sat, 8 Aug 2026 00:13:56 -0600 Subject: [PATCH 2/2] fix(test): resolve a real bash 4+ for lint-skill.sh, verify it actually ran (#2157 CI) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two bugs in the new regression test: 1. lint-skill.sh requires bash 4+; macOS ships bash 3.2 as the `bash` on PATH (including GitHub Actions' macos-latest runner), so execFileSync('bash', ...) invoked a bash that rejects the script outright. Resolve a real bash 4+ explicitly (checking common Homebrew install paths) and skip the suite if none is found, rather than assuming plain "bash" is adequate. 2. The "does not flag ..." assertions used stdout.not.toContain(...), which passes vacuously on an EMPTY stdout — exactly what happens when the script fails to even start. This is why only the "flags ..." tests (which assert toContain, not not.toContain) surfaced the bash-version failure in CI; the others silently "passed" for the wrong reason. Every case now also asserts the script actually reached its own summary line. docs check acknowledged. --- .../unit/lint-skill-mktemp-trailing-x.test.ts | 139 +++++++++++------- 1 file changed, 89 insertions(+), 50 deletions(-) diff --git a/tests/unit/lint-skill-mktemp-trailing-x.test.ts b/tests/unit/lint-skill-mktemp-trailing-x.test.ts index 7daff1358..6f6f93b59 100644 --- a/tests/unit/lint-skill-mktemp-trailing-x.test.ts +++ b/tests/unit/lint-skill-mktemp-trailing-x.test.ts @@ -31,6 +31,26 @@ const LINT_SCRIPT = path.join( '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 @@ -51,64 +71,83 @@ rules here example here `; -function runLint(bashBlock: string): { stdout: string; status: number | null } { +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('bash', [LINT_SCRIPT, skillPath], { encoding: 'utf8' }); - return { stdout: result, status: 0 }; + const result = execFileSync(BASH4!, [LINT_SCRIPT, skillPath], { encoding: 'utf8' }); + return { stdout: result, ranSuccessfully: true }; } catch (err) { - const e = err as { stdout?: string; status?: number | null }; - return { stdout: e.stdout ?? '', status: e.status ?? 1 }; + 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 }); } } -describe('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 } = runLint('TMPFILE=$(mktemp "/tmp/tmp.XXXXXXXXXX.js")'); - 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 } = runLint('BLOCKS_FILE=$(mktemp "/tmp/tmp.XXXXXXXXXX.blocks")'); - expect(stdout).toContain("mktemp template's X run is not trailing"); - }); - - it('does not flag a trailing-X-only template', () => { - const { stdout } = runLint('TMPFILE=$(mktemp "/tmp/tmp.XXXXXXXXXX")'); - 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 } = runLint( - [ - 'TMP_DIR=$(mktemp -d "/tmp/tmp.XXXXXXXXXX")', - 'trap \'rm -rf "$TMP_DIR"\' EXIT', - 'TMP_FILE="$TMP_DIR/probe.js"', - ].join('\n'), - ); - 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 } = runLint('WORK_DIR=$(mktemp -d)'); - expect(stdout).not.toContain("mktemp template's X run is not trailing"); - }); -}); - -describe('.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'); - let stdout: string; - try { - stdout = execFileSync('bash', [LINT_SCRIPT, realSkillPath], { encoding: 'utf8' }); - } catch (err) { - stdout = (err as { stdout?: string }).stdout ?? ''; - } - expect(stdout).not.toContain("mktemp template's X run is not trailing"); - }); -}); +// 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"); + }); + }, +);