Files
template-go-web/scripts/is-template.sh
T
mathiasandClaude Opus 5 b9bbbae3bb
CD / Detect unsubstituted template (pull_request) Successful in 1s
CD / Lint / Test / Vet (pull_request) Skipped
CD / Build & Import (pull_request) Skipped
CD / Deploy via GitOps (pull_request) Skipped
CD / Detect unsubstituted template (push) Successful in 1s
CD / Lint / Test / Vet (push) Skipped
CD / Build & Import (push) Skipped
CD / Deploy via GitOps (push) Skipped
fix(ci): make the template guard survive its own substitution
Closes #9. Closes #10.

Every child repo born from this template has had fake-green CI: cd.yml's
guard grepped a literal placeholder, and both scaffolders substitute every
file in the tree -- including the guard. In a generated repo the check
became `grep -q 'git.d-ma.be/mathias/<name>' go.mod` against a go.mod whose
first line is `module git.d-ma.be/mathias/<name>`, so it matched forever,
check/build/deploy all skipped, and the run reported success having built
nothing. Confirmed in cad-atlas (#9) and openbanking-api (#10).

The second occurrence landed directly underneath a comment warning against
exactly this. That is the tell: an inline shell block in a workflow cannot
be tested, so a fix for it can only be reviewed by reading, and reading is
what missed it twice.

So the detection moves to scripts/is-template.sh and gets a test.

It greps the SHAPE of a placeholder rather than any literal token, which
also catches leftovers the scaffolders know nothing about, and a missing
target now exits non-zero instead of answering "false" -- failing open
there would skip CI on a repo that is not a template at all, which is the
same fake-green failure by another road.

scripts/tests/is-template_test.sh runs a scaffolder-style substitution over
a copy of the guard and asserts it still answers correctly. Both shipped
bugs fail that test.

The test assembles placeholder tokens at runtime rather than writing them
whole, for the same reason the guard does: a fixture containing a literal
token is substituted before the test runs, and the suite would then fail in
every generated repo while passing here. The first draft of the guard was
itself caught by this -- its explanatory comment named a token literally.

Verified by simulating a generated child (whole tree substituted):

    child  go.mod module git.d-ma.be/mathias/childproj
    guard  child    -> false   (CI runs)
    guard  template -> true    (template still skips its own build)
    suite  in child -> 8 passed, 0 failed
    suite  in template -> 8 passed, 0 failed

Wired into `task check`, so the guard that decides whether CI runs at all
in every child is covered by the same gate as the Go tests.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-17 16:29:29 +02:00

48 lines
2.0 KiB
Bash
Executable File

#!/usr/bin/env bash
# Is this checkout still a raw, unsubstituted template?
#
# Prints "true" or "false". cd.yml's guard job reads it and skips check/build/
# deploy when true, so that a template repo does not try to build itself.
#
# WHY A PATTERN AND NOT A LITERAL
#
# The scaffolders (gitea-mcp create_project_from_template, local-dev
# scripts/new-project.sh) substitute placeholders in EVERY file of the tree —
# including this one. So a guard that greps a whole placeholder token is
# rewritten by the very substitution it exists to detect: in the template it
# greps the module-path placeholder, and in the generated repo that same line
# has become a grep for the real module path — which the generated go.mod
# declares on its first line. It matches forever, every job below skips, and the
# run reports green having built nothing.
#
# That shipped twice: #9 (cad-atlas) and #10 (openbanking-api), the second one
# sitting directly under a comment warning against exactly this.
#
# Note this comment names no placeholder literally, for the same reason. A
# comment that gets substituted stops explaining the thing it documents — and
# scripts/tests/is-template_test.sh enforces that, which is how the first draft
# of this file was caught.
#
# The regex below matches the SHAPE of a placeholder and contains no complete
# token, so there is nothing for a scaffolder to rewrite. It also catches any
# other leftover placeholder, not just the two the scaffolders know about.
#
# scripts/tests/is-template_test.sh substitutes a copy of this file and asserts
# it still answers correctly — that case is the regression test for the class.
set -euo pipefail
target="${1:-go.mod}"
if [ ! -f "$target" ]; then
# Never answer "false" here. A missing target would skip CI on a repo that is
# not a template at all, which is the same fake-green failure by another road.
echo "is-template: no such file: $target" >&2
exit 2
fi
if grep -qE '__[A-Z][A-Z0-9_]*__' "$target"; then
echo "true"
else
echo "false"
fi