Skip to content

fix(workspace): preserve the consumer .pre-commit-config.yaml on upgrade - #895

Merged
c-vigo merged 3 commits into
devfrom
bugfix/878-preserve-precommit-config
Jul 7, 2026
Merged

c-vigo merged 3 commits into
devfrom
bugfix/878-preserve-precommit-config

Conversation

@c-vigo

@c-vigo c-vigo commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Description

Closes #878

The scaffold upgrade (init-workspace.sh --force) replaced the consumer's .pre-commit-config.yaml wholesale — it was not in PRESERVE_FILES — silently dropping repo-specific configuration: the global exclude: block (hyrr: physics data tables, generated schemas, jupytext pairs — post-upgrade the hook suite "fixed" ~45 files it must never touch) and per-hook exclude: keys (hyrr: the detect-private-key exclusion for a file carrying PEM marker literals → false positive).

Design decision

Chosen: preserve-with-managed-warning, the first option listed on the issue and the one consistent with the #877 precedent (#891): the consumer owns the file. .pre-commit-config.yaml joins PRESERVE_FILES, and the upgrade adds two non-fatal guards:

  • Diff hint — when the preserved file differs from the incoming template, the installer prints a unified diff so template hook-stack evolution stays visible and consumers fold it in deliberately (nothing is lost silently in either direction: the consumer file is kept verbatim, the template delta is printed in full). A stock consumer whose file matches the template gets no noise.
  • Parse gate — the preserved config is checked with prek validate-config (prek ships in devTools, so it is on PATH in the image and the dev-shell); a config the runner cannot load breaks every commit in the new image, so the installer warns loudly, pointing at the exact command. Same philosophy as the fix(workspace): scaffold upgrade strands base recipes — justfile.project is preserved but now owns sync/precommit/test, breaking the shipped ci.yml #877 just --list parse gate: probe with the real tool, warn, never fail the scaffold.

No .bak-upgrade backup file is needed: under preserve, the consumer's data is never touched, and the diff output carries the template side.

Why not replace-but-carry-over (rewrite from template, re-apply consumer exclude:/files: keys):

Implications for #881 and template-driven hook evolution

A pure preserve means hook-stack evolution (runner migrations, compat fixes like default_language_version: python3.14 #803 or typos --force-exclude #859, new hooks) no longer arrives automatically — a consumer upgrading from a pre-prek scaffold keeps a pre-prek-era config. This PR mitigates that with the always-printed template diff plus the MIGRATION.md step, and the design keeps the #881 path open rather than harder: the new post-scaffold guard block is the natural hook point for future surgical repairs on the preserved file (#877-style), e.g. an upgrade-time scan/rewrite of retired pre-commit invocations in preserved files, or a targeted key repair once a YAML-capable tool is in the toolchain. Config-side, prek is a drop-in reader of .pre-commit-config.yaml, so preserved configs keep loading; the pre-commit binary references that #881 covers live in justfile.project/.githooks, which were already preserved before this PR — #881's scope is unchanged.

Changes Made

  • assets/init-workspace.sh:
  • tests/bats/init-workspace.bats: four new end-to-end tests (customizations survive verbatim; diff hint printed with template content visible; no hint when the file matches the template; invalid preserved config warns and stays preserved). TDD: RED committed first, then GREEN.
  • docs/MIGRATION.md: new step 2 in the 0.3.x upgrade checklist describing the preservation, the diff review, and the prek validate-config check.
  • CHANGELOG.md (+ hook-synced .devcontainer mirror): Unreleased/Fixed entry.

Type of Change

  • fix -- Bug fix

Modifiers

  • Breaking change (!)

Changelog Entry

Fixed

  • Scaffold upgrade replaces .pre-commit-config.yaml, silently clobbering repo-specific hook config (#878)
    • The upgrade overwrote the consumer's .pre-commit-config.yaml wholesale, dropping the repo-specific global exclude: block and per-hook exclude: keys — the hook suite then rewrote data files it must never touch and false-flagged PEM marker literals. The file is now preserved on upgrade (like justfile.project, #877).
    • Because template hook-stack evolution no longer arrives automatically, init-workspace --force prints a diff of the preserved file against the incoming template so consumers can fold changes in deliberately, and warns (non-fatally) when the preserved config does not parse under prek validate-config — a config the runner cannot load breaks every commit in the new image.

Testing

  • Tests pass locally: bats tests/bats/init-workspace.bats (72 ok), full hook suite green (just precommit)
  • Manual testing performed (describe below)

Manual Testing Details

Ran init-workspace.sh --force against a temp workspace carrying a hyrr-shaped .pre-commit-config.yaml (global exclude: ^data/stopping/|\.dat$ + detect-private-key per-hook exclude): the consumer file survived verbatim, the installer printed the "yours was kept" banner with the full unified diff against the template, and the scaffold completed successfully. A second run on a stock (template-identical) workspace printed no warning.

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
  • 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
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published

c-vigo added 3 commits July 7, 2026 14:02
…g.yaml

RED for #878: upgrading a workspace whose .pre-commit-config.yaml carries a
global exclude block and a per-hook exclude must keep the consumer file
verbatim, print a diff hint against the template, and warn (non-fatally)
when the preserved config does not validate under prek.

Refs: #878
The scaffold upgrade replaced .pre-commit-config.yaml wholesale, silently
dropping the repo-specific global exclude: block and per-hook exclude:
keys (field-validated on hyrr: the hook suite rewrote ~45 data files it
must never touch and detect-private-key false-flagged PEM marker
literals). The consumer owns the file, like justfile.project (#877): it
joins PRESERVE_FILES, the upgrade prints a diff against the incoming
template so hook-stack evolution stays visible, and a prek
validate-config gate warns (non-fatally) when the preserved config would
break every commit in the new image.

Refs: #878
@c-vigo
c-vigo merged commit adb1634 into dev Jul 7, 2026
13 checks passed
@c-vigo
c-vigo deleted the bugfix/878-preserve-precommit-config branch July 7, 2026 12:21
c-vigo added a commit that referenced this pull request Jul 7, 2026
…erence scan (#896)

## Description

Closes #881. **Stacked on #895** (branched from
`bugfix/878-preserve-precommit-config`; this PR shows only its own
commits once #895 merges).

Field evidence (EXOPET/vault, 0.3.5 → 0.4.0): the 0.4.0 image ships
`prek` only (#778), but consumer files preserved on upgrade still invoke
`pre-commit` and exit 127 — the preserved `justfile.project`
`precommit:` recipe and the repo-managed
`.githooks/{pre-commit,commit-msg,prepare-commit-msg}` hooks broke every
commit in-image. Even a repaired justfile cannot save a consumer whose
own hook scripts call the removed tool, so the fix has an image half and
an upgrade half.

## Type of Change

- [x] `fix` -- Bug fix

### Modifiers

- [ ] Breaking change (`!`) -- This change breaks backward compatibility

## Changes Made

**(a) Image compat shim, one release cycle** (`flake.nix`,
`imageTools`):

- A `writeShellScriptBin "pre-commit"` shim that prints a one-line
deprecation notice to **stderr** (stdout stays clean for pipelines) and
`exec`s `${prek}/bin/prek "$@"` — prek is drop-in for `run`-style
invocations. Marked `DEPRECATED — remove in 0.5 (#881)`. Image-only
(`imageTools`, not `devTools`), so the dev-shell, home modules, and the
parity gate are untouched.

**(b) Upgrade-time scan** (`assets/init-workspace.sh`, alongside the
#877/#878 guards):

- After the scaffold, `init-workspace --force` scans the post-scaffold
`justfile.project`, all `.githooks/` files, and
`.pre-commit-config.yaml` for invocation-shaped `pre-commit` references
and warns **non-fatally** with `file:line`, pointing at `prek`, the
shim's one-cycle lifespan, and the MIGRATION.md checklist.
- False-positive handling: the pattern only matches `pre-commit` framed
as a command word (start/whitespace/shell punctuation on both sides), so
the `.pre-commit-config.yaml` filename (leading `.`),
`pre-commit/pre-commit-hooks` repo URLs (leading `/`), `pre-commit.com`
links (trailing `.`), and `prek` never trip it; comment lines and YAML
stage-name list items (`- pre-commit`) are filtered. Scanning
post-scaffold state means fresh scaffolds and rsync-refreshed template
hooks (which call `prek`) are silent by construction.

**Docs**: MIGRATION.md upgrade-checklist step 3 rewritten — full rename
surface (justfile recipes, repo-owned `.githooks` beyond the shipped
three, hook `entry:` lines, CI configs), the shim's 0.4.x-only lifespan,
and the NixOS `#!/bin/bash` → `#!/usr/bin/env bash` shebang gotcha in
old-scaffold hooks.

## Changelog Entry

### Fixed
- **`pre-commit` binary dropped without a compat path — preserved
consumer recipes and `.githooks` calling it break**
([#881](#881))
- The 0.4.0 image retired the Python `pre-commit` for `prek`
([#778](#778)), but files
preserved on upgrade still invoke it and exit 127 (field-validated on a
0.3.5 → 0.4.0 consumer: the preserved `justfile.project` `precommit`
recipe and repo-managed `.githooks` scripts broke every commit). The
image now ships a **deprecated one-cycle `pre-commit → prek` shim**
(stderr notice, removed in 0.5) so consumer hook scripts keep working
while they migrate.
- `init-workspace --force` scans the post-scaffold `justfile.project`,
`.githooks/` scripts and `.pre-commit-config.yaml` for invocation-shaped
`pre-commit` references and warns (non-fatally) with `file:line`,
pointing at the MIGRATION.md rename checklist — which now also covers
the NixOS `#!/bin/bash` shebang gotcha in old-scaffold `.githooks`.

## Testing

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

TDD, both halves RED-committed first:

- bats: `upgrade warns file:line when preserved files still invoke
pre-commit (#881)` RED (`not ok`) → GREEN; plus stock-scaffold and
false-positive no-hit guards. Full `init-workspace.bats` suite: **75/75
ok**.
- image: `test_pre_commit_compat_shim` RED against the pre-shim locally
built image (`exit 127: pre-commit: command not found` — the exact field
failure) → GREEN after rebuild. `TestDevelopmentTools`: **24 passed**.

### Manual Testing Details

```
$ podman run --rm ghcr.io/vig-os/devcontainer:dev bash -lc 'pre-commit --version'
pre-commit: deprecated compat shim, dispatching to prek — rename your invocation; the shim is removed in 0.5 (#881)
prek 0.3.11
```

Full hook suite: `just precommit` exit 0, 52 hooks passed, 0 failed.

## 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
- [x] I have updated the documentation accordingly (edit
`docs/templates/`, then run `just docs`)
- [x] 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
- [ ] Any dependent changes have been merged and published (**stacked on
#895**, review in progress)

## Additional Notes

- The shim is deliberately a bridge, not a supported path: the
deprecation notice is the consumer-visible removal deadline (0.5),
cross-referenced in the flake comment and MIGRATION.md.
- `test_pre_commit_installed` no longer asserts `pre-commit` absence
(superseded by the shim test for this one cycle); re-add the absence
assertion when the shim is removed in 0.5.

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