Skip to content

fix(init): scope placeholder substitution to the paths devkit ships - #1707

Merged
c-vigo merged 9 commits into
devfrom
feature/1693-scoped-placeholder-substitution
Sep 25, 2026
Merged

c-vigo merged 9 commits into
devfrom
feature/1693-scoped-placeholder-substitution

Conversation

@c-vigo

@c-vigo c-vigo commented Sep 25, 2026

Copy link
Copy Markdown
Contributor

Description

init-workspace.sh had two placeholder-substitution paths: an image-only fast
path that sed'ed one file per entry of a manifest baked into the image (#718),
and a runtime fallback that batched grep -rl | xargs sed -i over the whole
workspace (#1687). This replaces both with one routine whose candidate set is
the files devkit ships, mapped into the workspace.

The issue's premise needs a correction. #1693 called the manifest path
"15-20x slower" on the strength of a ~117-entry list. The baked manifest has
13 entries, not 117, and on a clean tree the manifest path measured 31 ms
against 16 ms for the fallback — speed is noise at that size, and the
"retire it because it is slower" framing does not hold.

What does hold, and is the real reason to land this, is scope. The fallback
greps $WORKSPACE_DIR, which in the container is the consumer's mounted repo —
.venv, node_modules and all. Any file at a path devkit does not ship, holding
a literal {{SHORT_NAME}}, {{ORG_NAME}} or {{GITHUB_REPOSITORY}} token, was
rewritten in place on the first scaffold and again on every upgrade.
Reproduced with seeded files; two of the new BATS tests fail on dev for exactly
that reason. The manifest path never had that reach, so retiring it without
scoping the survivor would have made the consumer-facing behaviour strictly
worse. Scoping is also cheaper where it matters: on a 7.4k-file consumer tree the
walk drops from 405 ms to 38 ms.

What this does not change, stated plainly. The new scope is the set of
paths devkit ships, not the set of files devkit owns. A consumer file living
at a shipped path that an upgrade preserves rather than overwrites — README.md,
.typos.toml, .pre-commit-config.yaml, renovate.json, a consumer flake.nix,
.devcontainer/* under direnv/bare — still has its tokens resolved. That is
the retired manifest path's reach exactly, and the reach sweep_scaffold_writable
still has for its chmod, so it is neither a regression nor a change; it was
simply never written down. PRESERVE_FILES is a copy-time concept and does not
carry over to substitution. The changelog entry and the routine's contract
comment both say so now.

Type of Change

  • feat -- New feature
  • fix -- Bug fix
  • docs -- Documentation only
  • chore -- Maintenance task (deps, config, etc.)
  • refactor -- Code restructuring (no behavior change)
  • test -- Adding or updating tests
  • ci -- CI/CD pipeline changes
  • build -- Build system or dependency changes
  • revert -- Reverts a previous commit
  • style -- Code style (formatting, whitespace)

Modifiers

  • Breaking change (!) -- This change breaks backward compatibility

Changes Made

assets/init-workspace.sh

  • New emit_substitution_candidates <src_dir>: walks find -L "$src_dir" -mindepth 1 -type f, skipping .git/ and .venv/, maps each relative path to
    $WORKSPACE_DIR/$rel and emits it NUL-terminated when the destination is a
    regular file and not a symlink. It maps template paths to workspace paths the
    same way sweep_scaffold_writable does — a shared idiom, not an identical
    routine: the filters differ (that one takes -e of any type for chmod, this
    one regular files only, and it prunes where the other does not).
  • The pass runs it for $TEMPLATE_DIR, plus $SMOKE_TEST_DIR under
    --smoke-test (the overlay lands outside the template tree — same guard
    sweep_scaffold_writable uses), and pipes the result through one batched
    xargs -0 grep -lZ and one batched xargs -0 sed -i.
  • .git and .venv are pruned by name at any depth
    (\( -name .git -o -name .venv \) -prune), which is what the copy rsync's
    --exclude='.git' --exclude='.venv' mean — they are basename patterns — so the
    walk cannot reach a destination the copy never wrote. The case that matters in
    production is the venv the image bakes inside /root/assets/workspace ([BUG] Nix image missing /root/assets/workspace/.venv — consumer post-create.sh aborts on first line #735).
    Pruning is also cheaper than filtering: the directories are not descended.
  • emit_substitution_candidates normalizes a trailing slash off its argument
    (${1%/}). Without it, TEMPLATE_DIR=".../workspace/" makes the prefix strip
    look for .../workspace//, which no find output starts with, so every
    relative path stays absolute, no destination resolves, and the pass substitutes
    nothing while exiting 0 — a scaffold that silently ships 13 files with live
    tokens.
  • The [CHORE] BATS runtime: parallelize the suite, batch the init-workspace.sh fork loops, collapse redundant scaffolds #1687 selection-semantics comment survives as the routine's contract —
    regular files only, symlinked destinations skipped, .git/ skipped, binaries
    matched (no -I), failures swallowed so neither grep exit 1 nor exit 2 trips
    set -o pipefail.
  • Deleted: the MANIFEST_FILE variable, the if [[ -f "$MANIFEST_FILE" ]]
    branch, and both log lines (Using build-time manifest (N files),
    Warning: Manifest not found, searching at runtime (slower)).
  • SCRIPT_DIR stays — it locates parse-github-remote-lib.sh, justfile.d/,
    gitignore.d/ and the smoke-test overlay — and its comment now says so
    instead of "for manifest location". Four other comments that claimed the
    manifest "already lists" a path (seed_node_justfile_project's call site,
    render_license's header, its inline rationale, its call site) are reworded to
    the template-shipped set.

flake.nix

  • The build step that greped the template and wrote
    /root/assets/.placeholder-manifest.txt is removed. Nothing reads it.

tests/

  • Four new behavioural BATS tests (init-workspace.bats), each RED before its
    fix:
    • two seed files at paths the template never ships — docs/my-template.tmpl
      and .venv/lib/consumer-tokens.txt, each carrying all three tokens — and
      assert they survive a first scaffold and an upgrade untouched while the
      managed files still resolve;
    • one scaffolds with a trailing slash on TEMPLATE_DIR and asserts
      justfile.project is substituted;
    • one builds a template copy carrying .venv/lib/pyvenv.cfg and
      pkg/.venv/pyvenv.cfg, seeds both paths in the workspace with tokens, and
      asserts neither is touched — the nested one is what the old path-prefix
      filter leaked.
  • test_image.py::test_placeholder_manifest_baked becomes
    test_placeholder_manifest_absent: a negative pin, so neither the baked file
    nor the flake step creeps back.
  • scripts/sync_manifest.py's MANIFEST is a different mechanism
    (test_manifest_files, verify_file_identity) and is untouched.

Changelog Entry

### Changed

- **Placeholder substitution is scoped to the paths devkit ships**
  ([#1693](https://github.com/vig-os/devkit/issues/1693))
  - `init-workspace.sh` had two substitution paths: an image-only fast path
    reading a manifest baked into the image, and a runtime fallback that greped
    the workspace. There is now one routine, and its candidate set is the
    template-shipped paths mapped into the workspace — built the same way as the
    `chmod u+w` scaffold sweep builds its own — plus the smoke overlay's when one
    was applied
  - **A file at a path devkit does not ship is no longer rewritten.** The
    retired fallback walked the whole workspace, which in the container is the
    mounted repo — `.venv` and `node_modules` included — so any file holding a
    literal `{{ SHORT_NAME }}`, `{{ ORG_NAME }}` or `{{ GITHUB_REPOSITORY }}`
    token was substituted in place on a scaffold and again on every upgrade.
    Only paths devkit ships are reachable now. (Those three tokens are spelled
    with inner spaces throughout this entry so that the scaffolded copy of this
    changelog is not itself rewritten by the pass it describes.)
  - Reach is **unchanged** for the paths devkit does ship. A consumer file
    living at one of them that an upgrade preserves rather than overwrites —
    `README.md`, `.typos.toml`, `.pre-commit-config.yaml`, `renovate.json`, a
    consumer `flake.nix`, `.devcontainer/*` under `direnv`/`bare` — still has
    those tokens resolved, exactly as the retired manifest path resolved them
    and exactly as the `chmod u+w` sweep still reaches them. This is not a
    regression and not a change; it is the boundary being stated
  - The image no longer ships `/root/assets/.placeholder-manifest.txt` and the
    flake step that generated it is gone, together with the
    `Using build-time manifest (N files)` and
    `Warning: Manifest not found, searching at runtime (slower)` lines a
    scaffold used to print
  - Rendered output is byte- and permission-identical in all four delivery
    modes, under `--smoke-test`, and for a Node scaffold with
    `DEVKIT_LICENSE=proprietary`. A template file the consumer deleted, or one a
    mode prunes, is skipped silently

Two notes on that entry.

The #1687 bullet in the same ## Unreleased section had its "the host-side
placeholder-substitution fallback" changed to "pass", so both entries stay
true in one release now that there is no second path for it to be the fallback of.

The three tokens are written with inner spaces because
assets/workspace/.devcontainer/CHANGELOG.md is template-shipped: the mirror is
copied into every scaffold and is therefore in the candidate set, so a literal
{{SHORT_NAME}} in a changelog entry renders as "a literal testproj" in the
consumer's copy. Verified: the scaffolded mirror carries no token from this entry.

Testing

  • Tests pass locally (just test)
  • Manual testing performed (describe below)

just test is not checked because it builds on the local :dev image, which is
a known false-failure trap for test_image.py / test_integration.py; CI's
test-image job is the authority for those. Everything just test runs that
does not need the image was run and is reported below.

Manual Testing Details

BATS — bats -j 8 tests/bats/init-workspace.bats: 304/304 (300 before,
plus four new tests). bats -j 8 tests/bats/install.bats: 95/95.

Python (host-runnable) — pytest tests/test_utils.py tests/test_install_script.py packages/vig-utils/tests: 654 passed.
pytest tests/test_image.py --collect-only: 112 collected, with
TestFileStructure::test_placeholder_manifest_absent in place of the retired
test_placeholder_manifest_baked.

Every new test was confirmed RED before its fix:

  • consumer-file scope (RED on dev): the seeded file came back as
    name: testproj / org: test / repo: test/repo, i.e. rewritten;
  • trailing slash: the run succeeded and justfile.project still read
    {{SHORT_NAME}} — the pass had silently done nothing;
  • nested .venv: pkg/.venv/pyvenv.cfg came back as
    prompt = testproj test test/repo.

Host-side byte-identity proof — the dev script (git show dev:assets/init-workspace.sh) and this branch's, each rendering the same six
cases into fresh trees, compared with diff -r and with
find . -printf '%m %y %p\n' | LC_ALL=C sort | sha256sum:

case files diff -r mode+type sha256 (truncated)
--mode both 117 clean 179411cb1ffd3afc identical
--mode devcontainer 115 clean 282f5fb0a1e3edec identical
--mode direnv 93 clean 7063d83f9b6d1d1b identical
--mode bare 92 clean 186b3bd5895626a0 identical
--smoke-test --mode both 119 clean 5cb8373e9452920e identical
Node + DEVKIT_LICENSE=proprietary 118 clean e29777fff4f255fb identical

No managed file in any rendered tree retains a {{SHORT_NAME}}, {{ORG_NAME}}
or {{GITHUB_REPOSITORY}} token. .vig-os still carries {{IMAGE_TAG}} in
every tree, old and new alike: that token is resolved by the image build, never
by this pass.

Absent destinations are skipped silently. devcontainer and bare mode
exercise this on every run — the template ships flake.nix and .envrc, the
rendered workspace has neither — and both renders above are byte-identical with
no find/grep/sed/xargs noise in the log. Separately verified by deleting
SECURITY.md, .typos.toml, justfile.project and docs/CONTRIBUTING.md from
a rendered tree and re-running the upgrade: exit 0, no error output, no leftover
tokens.

Static checks — shellcheck -S warning -x assets/init-workspace.sh clean;
prek run --all-files clean (53 hooks passed, nothing modified) after the
sync-manifest hook regenerated assets/workspace/.devcontainer/CHANGELOG.md,
which is committed alongside CHANGELOG.md.

Flake eval — nix eval .#packages.x86_64-linux.devkitImage.drvPath still
evaluates, and the drvPath moves relative to dev, as expected: the image no
longer contains the manifest file. No hash is quoted here — the changelog mirror
is baked into the image, so the value changes with every edit to this branch's
CHANGELOG.md, and a pasted hash would be stale by the time anyone checked it.

Image-level proof: pending CI artifact. Acceptance criterion 3 asks for the
diff -r + %m %y %p comparison across the old and new image, not just
host-side, and that needs this PR's image built. A reusable driver is ready —
it mirrors tests/conftest.py::_build_podman_cmd (mounts $ws:/workspace,
passes SHORT_NAME / ORG_NAME / GITHUB_REPOSITORY), renders all six cases
under both images, excludes .vig-os from the comparison and reports its
DEVKIT_VERSION drift separately, and asserts the retired manifest is absent
from the new image. It will be run against the CI-built image before merge and
its output posted here. test_image.py and test_integration.py in the
test-image job are the suite-level half of the same proof and cannot be run
locally (a stale local :dev image is a known false-failure trap).

Checklist

  • My code follows the project's style guidelines
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have updated the documentation accordingly (edit docs/templates/, then run just docs)
  • I have updated CHANGELOG.md in the [Unreleased] section (and pasted the entry above)
  • My changes generate no new warnings or errors
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published

No documentation update: docs/ carries no prose about the placeholder manifest.
The only mentions live in docs/issues/issue-718.md, docs/issues/issue-1687.md,
docs/pull-requests/pr-802.md and pr-813.md, which are historical records of
those changes and are not rewritten. The released CHANGELOG entry for #718 that
names the manifest also stays as written.

Additional Notes

  • Follow-up owed (issue to be filed): the trailing-slash bug fixed here in
    emit_substitution_candidates is latent in three siblings that share the same
    ${src_path#"$src_dir"/} mapping idiom — sweep_scaffold_writable, the
    --preview walk and the +x sweep. They are deliberately not touched:
    out of this issue's scope, and a drive-by across three production walks is not
    what this PR should carry.
  • assets/workspace/.devcontainer/CHANGELOG.md already carried four literal
    substitution tokens before this branch, all inside released entries (the
    vigOS/devc org default, init-precommit.sh's hard-coded root, the perf: bake the build-time placeholder manifest into the Nix image #718
    manifest entry itself, and the --repo flag entry). Those are rewritten in a
    consumer's scaffolded mirror today and were rewritten before this PR too —
    identical reach, since the mirror is template-shipped either way. Released
    entries are not edited, so they stay; worth its own issue if anyone cares.
  • render_license still resolves {{ORG_NAME}} itself rather than leaning on
    the shared pass. That reasoning is unchanged by the scoping — the proprietary
    template lives in assets/licenses/, outside the walked tree, and its output
    is only reachable because assets/workspace/LICENSE happens to carry the same
    token — so the comment is reworded, not the code.
  • No changelog entry for the trailing-slash and nested-.venv fixes on their
    own: both are defects in a routine this same PR introduces, so relative to the
    last release there is nothing for a consumer to read beyond the entry already
    written.
  • Commits, in order: the two scope RED tests (test), the routine (fix — it
    stops rewriting files devkit does not ship; the manifest retirement is the
    mechanical part), the flake step and image pin (build), the changelog
    (docs), then the trailing-slash test + fix and the .venv-prune test + fix
    (test/fix pairs, each RED first).

Refs: #1693

…ution pass

Two behavioural tests seed files the template never ships -- one under docs/,
one under .venv/lib/ -- each carrying literal {{SHORT_NAME}}, {{ORG_NAME}} and
{{GITHUB_REPOSITORY}}, and assert they survive a first scaffold and an upgrade
untouched while the managed files still resolve.

Both fail today: the runtime substitution walk greps the WHOLE workspace, so it
rewrites consumer files that happen to carry a token.

Refs: #1693
One substitution routine replaces the image-only manifest branch and the
whole-workspace fallback. emit_substitution_candidates walks a source tree and
emits the workspace destinations that exist as regular files, derived exactly as
sweep_scaffold_writable derives its chmod set; the pass runs it for
$TEMPLATE_DIR (plus $SMOKE_TEST_DIR under --smoke-test) and pipes the result
through one batched grep and one batched sed.

Scoping is the point. The retired fallback greped $WORKSPACE_DIR, so every
consumer file carrying a literal {{SHORT_NAME}}/{{ORG_NAME}}/{{GITHUB_REPOSITORY}}
was rewritten on scaffold AND on upgrade -- in the image that is the mounted
repo, .venv and node_modules included. That is the defect this closes, hence
fix, not refactor; the manifest retirement and the two log lines are the
mechanical part. .venv is dropped from the template walk to mirror the copy
rsync's --exclude, because the image bakes a venv inside the template tree.

MANIFEST_FILE is gone; SCRIPT_DIR stays (library, justfile.d, gitignore.d,
smoke-test overlay) and its comment now says so, as do the four other comments
that claimed the manifest "already lists" a path.

Refs: #1693
Nothing reads /root/assets/.placeholder-manifest.txt since the substitution pass
became a single scoped routine, so the flake step that grepped the template at
build time is gone. test_placeholder_manifest_baked becomes
test_placeholder_manifest_absent: a negative pin, so neither the file nor the
build step creeps back.

scripts/sync_manifest.py's MANIFEST is a different mechanism and is untouched.

Refs: #1693
… slash

A trailing slash makes the candidate walk's prefix strip look for `…/workspace//`,
which no `find` output starts with, so every relative path stays absolute, no
destination resolves and the pass substitutes nothing while exiting 0. The
scaffold then ships 13 managed files with live tokens.

Fails today: the run succeeds and justfile.project still reads {{SHORT_NAME}}.

Refs: #1693
… dir

`${1%/}` in emit_substitution_candidates, so the prefix strip that maps a
template path to its workspace destination cannot be defeated by a caller
passing TEMPLATE_DIR with a trailing slash.

The same latent bug exists in sweep_scaffold_writable, the --preview walk and
the +x sweep, which share the mapping idiom. Out of scope here and left for a
follow-up issue rather than fixed by a drive-by.

Refs: #1693
…mantics

The copy rsync excludes `.git` and `.venv` by basename, so it skips them at any
depth. The candidate walk excludes them by path prefix, which only covers the top
level, so a nested `.venv` leaks into the pass and a consumer's own virtualenv
files get substituted.

Fails today on the nested case: $ws/pkg/.venv/pyvenv.cfg comes back rewritten.

Refs: #1693
… walk

The copy rsync's `--exclude='.git' --exclude='.venv'` are basename patterns and
match at any depth; the walk's `-not -path "$src_dir/.venv/*"` only matched the
top level, so a nested one leaked and the pass could reach a destination the copy
never wrote. `\( -name .git -o -name .venv \) -prune` is the faithful form, and
cheaper: pruned directories are no longer descended.

The contract comment also stops claiming the set is derived "exactly as"
sweep_scaffold_writable derives its own -- the mapping idiom is shared, the
filters are not -- and now states the boundary plainly: a consumer file sitting
at a path devkit ships is still substituted, as it always was.

Refs: #1693
@c-vigo c-vigo self-assigned this Sep 25, 2026
@c-vigo

c-vigo commented Sep 25, 2026

Copy link
Copy Markdown
Contributor Author

Image-level byte-identity proof (acceptance criterion)

OLD = the image built by PR #1706's CI run (36119970797, dev-20b170c: dev's script, manifest baked, 4 MANIFEST_FILE references). NEW = this PR's CI-built image artifact (run 36120369838, dev-e41fc45). Both loaded locally with podman load; each ran init-workspace.sh --force --no-prompts into an empty bind-mounted workspace for six cases, then diff -r plus find -printf '%m %y %p' | sort.

Case Files old / new Content Modes
--mode both 117 / 117 identical except .devcontainer/CHANGELOG.md identical
--mode devcontainer 115 / 115 identical except .devcontainer/CHANGELOG.md identical
--mode direnv 93 / 93 identical identical
--mode bare 92 / 92 identical identical
--smoke-test --mode both 119 / 119 identical except .devcontainer/CHANGELOG.md identical
Node + DEVKIT_LICENSE=proprietary 118 / 118 identical except .devcontainer/CHANGELOG.md identical

The one content difference is this PR's own changelog entry, mirrored into the scaffolded .devcontainer/CHANGELOG.md in the four modes that ship it (the sync-manifest mirror is baked into the image, so it cannot be otherwise). .vig-os differs only in DEVKIT_VERSION (dev-20b170c → dev-e41fc45), excluded from the comparison and reported separately. No {{SHORT_NAME}}, {{ORG_NAME}} or {{GITHUB_REPOSITORY}} token is left in any managed file of the NEW trees. /root/assets/.placeholder-manifest.txt is present in OLD and absent in NEW.

CI on this PR: all 17 checks green, including Image Tests (test_placeholder_manifest_absent) and Integration Tests (container scaffold through install.sh). The trailing-slash walks in the three sibling sweeps are tracked in #1703; the baked-.venv sweep in #1700.

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.

1 participant