Skip to content

fix(#822): getTemplatesDir resolves from both compiled and source layouts - #829

Merged
admarble merged 2 commits into
mainfrom
feature/822-fix-templates-gettemplatesdir-resolves-one-lev
Jul 27, 2026
Merged

admarble merged 2 commits into
mainfrom
feature/822-fix-templates-gettemplatesdir-resolves-one-lev

Conversation

@admarble

@admarble admarble commented Jul 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary

getTemplatesDir() resolved the bundled templates/ at a fixed ../../../ offset calibrated for the compiled layout (dist/src/lib → package root). Run from source under tsx, __dirname is <repo>/src/lib, so three up landed one directory above the repo. copyDir's ENOENT skip then swallowed every copy, the version marker was written anyway, and sync printed ✔ Synced having installed nothing but .claude/skills/.sequant-version.

  • Resolution probes two bounded candidates (compiled first, so consumers are unaffected) and ranks them. Ranking is load-bearing: the two offsets are not mutually exclusive — from <repo>/src/lib the compiled offset resolves to <repo>/../templates, so a bare existence probe would bind to any unrelated templates/ sitting beside the checkout. A candidate whose parent holds a package.json named sequant wins outright (the same anchor version.ts, manifest.ts and bin/preflight.ts already use); a merely-existing candidate is still accepted as a fallback so no working layout regresses.
  • Loud failure: a missing templates root now exits non-zero naming the path. sync's guard sits ahead of the version fast path — that path diffs against the same missing directory, sees no drift, and reports "already up to date" over an empty tree. init's sits ahead of every prompt and write, including --upgrade-skills.
  • Per-subdirectory ENOENT skips are untouched and now pinned by a test — a stack shipping without memory/ must still copy cleanly.
  • Resolution is memoized: it is now stat-backed and getTemplateContent calls it once per template file, which added ~2ms to each drift scan (measured 2.10ms → 0.02ms).

Consumers were never affected — this was a trap for contributors testing sync/init from source.

AC verification

AC Status Evidence
AC-1 — resolves from both layouts, unit test pinning both offsets ✅ MET resolveTemplatesDirFrom takes an injectable ranker (a test cannot relocate import.meta.url); 6 unit tests pin compiled, source, stray-ancestor, both-valid, exists-fallback, neither-exists. Live: tsx bin/cli.ts sync → 21 skills; node dist/bin/cli.js sync → 21 skills
AC-2 — sync/init exit non-zero naming the path; per-subdir skips stay non-fatal ✅ MET Live: both exit 1 naming /nope/templates. Integration tests cover both commands; init case also asserts no settings.json was written. Unit test pins that a templates tree without agents//hooks/ still copies cleanly
AC-3 — regression test: source-invoked sync installs spec/exec/qa ✅ MET sync-source-invocation.integration.test.ts spawns the real CLI under tsx with SEQUANT_TEMPLATES_DIR cleared, asserts not.toEqual([".sequant-version"]) plus each core skill

Every new test was mutation-verified. Restoring the original one-line bug reproduced the exact issue signature and failed the AC-3 assertion with expected [ '.sequant-version' ] to not deeply equal [ '.sequant-version' ]; removing the source offset failed 3 tests; removing init's guard failed the init case (it exits 0 without it).

Test plan

Behavior change worth noting

The same root cause silently disabled the post-command auto-sync in bin/cli.ts for source invocations. With resolution fixed, a source-invoked command in a project with a stale version marker now performs a real auto-sync where it previously no-opped and only bumped the marker (verified: 1 → 22 entries). This is restored parity with the compiled binary, which has always behaved this way — and #814's protection still holds, so in-place customizations are preserved (verified).

Non-Goals honored

Distribution layout unchanged; #814 customizable-files handling untouched. SEQUANT_TEMPLATES_DIR remains the unconditional first choice, returned verbatim and never probed — the new guard validates whatever root was resolved, override included, which is what AC-2 asks for.

Closes #822

🤖 Generated with Claude Code

admarble added 2 commits July 27, 2026 05:07
getTemplatesDir() resolved templates/ at a fixed ../../../ offset calibrated
for the compiled layout (dist/src/lib -> package root). Run from source under
tsx, __dirname is <repo>/src/lib, so three up landed one directory above the
repo. copyDir's ENOENT skip then swallowed every copy, the version marker was
written anyway, and sync printed "Synced" having installed nothing but
.claude/skills/.sequant-version.

Resolution now probes two bounded candidates (compiled first, so consumers are
unaffected) and ranks them: a candidate whose parent holds a package.json named
"sequant" wins outright, since the two offsets are not mutually exclusive — from
<repo>/src/lib the compiled offset resolves to <repo>/../templates, and a bare
existence probe would bind to any unrelated templates/ beside the checkout. A
merely-existing candidate is still accepted as a fallback so no working layout
regresses. SEQUANT_TEMPLATES_DIR remains the unconditional first choice.

A missing templates root now fails loudly: sync and init exit non-zero naming
the resolved path. sync's guard sits ahead of the version fast path, which
would otherwise diff against the same missing directory and report "already up
to date" over an empty tree. init's sits ahead of every prompt and write. The
per-subdirectory ENOENT skip is untouched and now pinned by a test.

Resolution is memoized: it is now stat-backed and getTemplateContent calls it
once per template file, which added ~2ms to each drift scan.

Fixes #822
AC-2 names both `sync` and `init`, but only sync's failure branch had a
regression test. init's guard was covered solely by the unit-level mock, which
stubs assertTemplatesDirExists out entirely — so removing the guard from
init.ts left the suite green.

Adds a source-invoked `init --yes --skip-setup` case asserting non-zero exit,
the missing path in the output, and that no settings.json was written (the
guard sits ahead of every write, so a broken install must not half-provision).
Verified by mutation: without the guard, init exits 0 and the new test fails.
@admarble
admarble merged commit 4bd8e57 into main Jul 27, 2026
5 checks passed
@admarble
admarble deleted the feature/822-fix-templates-gettemplatesdir-resolves-one-lev branch July 27, 2026 14:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(templates): getTemplatesDir resolves one level too high under tsx — source-run sync/init prints "✔ Synced" while installing nothing

1 participant