Skip to content

fix(init): walk the template through one pruned candidate emitter - #1715

Merged
c-vigo merged 4 commits into
devfrom
bugfix/1700-template-walk-emitter
Sep 25, 2026
Merged

c-vigo merged 4 commits into
devfrom
bugfix/1700-template-walk-emitter

Conversation

@c-vigo

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

Copy link
Copy Markdown
Contributor

Description

Two latent bugs in the template-tree walks of assets/init-workspace.sh, one fix.

#1700. sweep_scaffold_writable walked find -L "$src_dir" -mindepth 1 with no excludes. The image bakes a .venv inside /root/assets/workspace that the copy step's rsync --exclude='.git' --exclude='.venv' never ships, so the sweep walked hundreds of baked venv paths and, wherever a consumer's own .venv had a file at the same relative path, chmod u+w'd a consumer file. Harmless in practice, but the same blind spot #1693 closed for placeholder substitution: a template-derived walk that does not apply the copy step's excludes reaches files devkit did not ship.

#1703. Three walks derived a workspace path with rel="${src_path#"$src_dir"/}". With a trailing slash on TEMPLATE_DIR, GNU find prints /…/workspace/a.txt, the prefix /…/workspace// never strips, the destination never exists, and the walk silently does nothing — exit 0, no warning. Reproduced: with TEMPLATE_DIR=".../tmpl/" a read-only preserved README.md stayed 444 (control: 644), a shipped pkg/lib.sh stayed 644 (control: 755), and --preview --force printed "118 file(s) will be ADDED" as absolute-path garbage where the control printed "98 file(s) will be OVERWRITTEN".

The design correction from triage. "Derive the excludes from EXCLUDE_ARGS" does not work: .git/.venv are literal rsync flags and never in EXCLUDE_ARGS; EXCLUDE_ARGS is built after the --preview walk and is unset in the smoke branch; and feeding the preserved-file/mode excludes into the chmod sweep would stop preserved consumer files getting u+w. The single source of truth is COPY_PRUNE_NAMES=(.git .venv), consumed by both rsync calls (via a derived COPY_PRUNE_EXCLUDES) and by one shared emitter:

emit_template_candidates <src_dir> [find-predicates...]   # NUL-delimited WORKSPACE destination paths

It normalises ${1%/}, prunes COPY_PRUNE_NAMES by basename at any depth under -L, and is consumed by sweep_scaffold_writable (no type predicate — directories keep getting u+w), emit_substitution_candidates (-type f, #1693's routine) and the +x sweep (-type f -name '*.sh'). The --preview classifier walk stays separate (it needs rel for absent destinations and carries extra docs/issues|pull-requests excludes); it and every other reader are fixed by one line after the only assignment: TEMPLATE_DIR="${TEMPLATE_DIR%/}".

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

  • TEMPLATE_DIR="${TEMPLATE_DIR%/}" right after the default assignment
  • COPY_PRUNE_NAMES=(.git .venv) + derived COPY_PRUNE_EXCLUDES; both rsync copies (smoke and normal) use it instead of literal --exclude flags; the smoke --exclude='/CHANGELOG.md' is unchanged
  • The smoke overlay rsync derives the same excludes, so the overlay's walks and its copy agree (review finding)
  • emit_template_candidates() added before its first consumer; sweep_scaffold_writable, emit_substitution_candidates and the +x sweep now read destinations from it (NUL-delimited end to end); their own -e && ! -L / -f && ! -L / -f filters and the batched xargs chmod are untouched
  • Comments that quoted the literal excludes now point at COPY_PRUNE_NAMES

tests/bats/upgrade-atomicity.bats

tests/bats/init-workspace.bats

Changelog Entry

No changelog needed — both issues say so; the sweep's visible effect on consumer files was a no-op u+w on venv files, and no in-repo caller passes a trailing slash today.

Testing

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

Manual Testing Details

  • bats -j 4 tests/bats/init-workspace.bats tests/bats/upgrade-atomicity.bats → 311 ok, 0 failures (twice); bats -j 4 install.bats release-mirror-fold.bats release-mirror-fold-lint.bats → 111 ok
  • shellcheck -x assets/init-workspace.sh clean; bash -n clean
  • Trailing-slash symptoms reproduced out of band on dev and gone on the branch (see Description)
  • prek run --all-files → all hooks passed

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

Additional Notes

The production case (baked .venv of store symlinks in the image) is covered by analogy through temp TEMPLATE_DIRs, as the existing init-workspace tests are. unset 'prune[-1]' needs bash ≥ 4.3, which the dev shell and image both have.

Refs: #1700, #1703

The overlay's walks now prune `.git`/`.venv` through the shared emitter,
but its rsync carried no excludes, so a walk could skip a path the copy
had written — the inverse of the bug being fixed. Derive the overlay's
excludes from the same COPY_PRUNE_NAMES.

Refs: #1700, #1703
@c-vigo
c-vigo merged commit ecb8f8d into dev Sep 25, 2026
15 checks passed
@c-vigo
c-vigo deleted the bugfix/1700-template-walk-emitter branch September 25, 2026 15:30
c-vigo added a commit that referenced this pull request Sep 25, 2026
…1722)

## Description

#1715 made `COPY_PRUNE_NAMES=(.git .venv)` the single source of truth
for the copy step's prunes, consumed by both rsync copies and by
`emit_template_candidates` (the `u+w` sweep, the substitution pass, the
`+x` sweep). The `--preview` classifier was left out because it needs
the source-relative path for destinations that do not exist yet. It kept
its own `! -path "*/.git/*" ! -path "*/.venv/*"`, which differs in kind:
`-path` skips only a directory's *contents*, while the emitter, like
rsync, prunes any entry *named* `.git` or `.venv`. So a file with one of
those names, at any depth, was reported as ADDED but never copied.

`dest → rel` is lossless — both sides glue with the same
`"$WORKSPACE_DIR/"` — so the preview now consumes the emitter and strips
the prefix. That deletes the duplicated walk and gains the `-L` /
`-mindepth 1` / basename-prune semantics for free. The emitter moves up
next to `COPY_PRUNE_NAMES` (a pure, verbatim move) because the preview
block precedes its old definition point. `-type f` stays explicit (the
emitter defaults to `-true`). The two `docs/issues|pull-requests`
predicates stay, with their comment corrected: they came from #951 to
mirror rsync excludes that #1466 later deleted, the template ships
neither path, so they are report-only.

The report is byte-identical on the pristine template: `--preview
--force` before and after diffed clean (117 ADDED rows, same order) —
the prune only removes entries and traversal order is unchanged.

## Type of Change

- [ ] `feat` -- New feature
- [x] `fix` -- Bug fix
- [ ] `docs` -- Documentation only
- [ ] `chore` -- Maintenance task (deps, config, etc.)
- [ ] `refactor` -- Code restructuring (no behavior change)
- [x] `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`

- `emit_template_candidates` moved verbatim to just below the
`COPY_PRUNE_NAMES` / `COPY_PRUNE_EXCLUDES` block
- Preview walk: `while IFS= read -r -d '' workspace_file; do
rel_path="${workspace_file#"$WORKSPACE_DIR"/}"; …` fed by
`emit_template_candidates "$TEMPLATE_DIR" -type f ! -path
"*/docs/issues/*" ! -path "*/docs/pull-requests/*"`; everything
downstream of `rel_path` / `workspace_file` unchanged; comment corrected
(#951 / #1466)

`tests/bats/init-workspace.bats`

- RED next to the #951 `.venv` over-report test: a file `.git` and a
nested file `pkg/.venv` in the template must not appear as ` + .git` / `
+ pkg/.venv` in `--preview --force` (`refute_line`, never `--partial
".git"`) after a positive anchor on a known ADDED row, and neither
exists after a real `--force` scaffold. On `dev` both phantom rows
appear

## Changelog Entry

No changelog needed — the issue says so; the visible change is two
phantom preview rows for a template shape devkit does not ship.

## Testing

- [x] Tests pass locally (`just test`)
- [x] Manual testing performed (describe below)

### Manual Testing Details

- `bats -j 4`: `init-workspace.bats` 305/306 (the one failure is
pre-existing and environmental — test 296 needs PyYAML in the plain `nix
develop` python; identical at the parent commit),
`upgrade-atomicity.bats` 6/6, `install.bats` 95/95
- Pristine-template `--preview --force` report before vs after:
identical
- `shellcheck -x` and `bash -n` clean; file mode still 100755; `prek run
--all-files` → 55 hooks passed

## Checklist

- [x] My code follows the project's style guidelines
- [x] I have performed a self-review of my code
- [x] 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)
- [x] My changes generate no new warnings or errors
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes
- [x] Any dependent changes have been merged and published

## Additional Notes

Rewriting the script through a temp file and `mv` silently dropped its
executable bit once during implementation; the structural BATS test
caught it and the committed blob is 100755.

Refs: #1716
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