From a55bc1721b63acc74e4c8edd39864ce2e13d2240 Mon Sep 17 00:00:00 2001 From: mignot Date: Wed, 2 Dec 2026 20:52:12 +0100 Subject: [PATCH] fix(template): ship a src-layout starter package and smoke test A fresh render failed scripts/check.sh (pytest exit 5, no tests) and the starter build-system could not build a flat-layout package. Declare [tool.setuptools.packages.find] where = ["src"] and create the starter src package, py.typed and tests/test_import.py from a copy-only _tasks entry, so `copier update` never adds them to existing consumers. The copy now needs --trust; package_name is validated to start with a letter or underscore. Refs #33 Co-Authored-By: Claude Sonnet 5.5 --- .agents/generated-rules.json | 2 +- .agents/rules/copier-template.md | 2 +- .claude/rules/copier-template.md | 2 +- AGENTS.md | 2 +- README.md | 4 ++-- copier.yml | 23 +++++++++++++++++++++++ docs/migration-guide.md | 4 ++-- scripts/tests/agent-template.sh | 9 +++++---- template/pyproject.toml.jinja | 3 +++ 9 files changed, 39 insertions(+), 12 deletions(-) diff --git a/.agents/generated-rules.json b/.agents/generated-rules.json index abf47e8..164b5af 100644 --- a/.agents/generated-rules.json +++ b/.agents/generated-rules.json @@ -1,5 +1,5 @@ { - "copier-template": "d4711ceadf0cf1bb10d81a3a65b892b6bf0a0a20f40496fcb32407024152d15e", + "copier-template": "60f7fea420e20a336163bd3abe79207db5753e8875ac9b7b5cca420d134f1c53", "documentation-toc": "52734c4c63f46babf96aeae1260407add66dbec2d997f90a25ef01b7e1479127", "github-actions": "c7bc81aede69023b0715ce8c0712f8e147cf5c2b6d90acf6a47a912ba99a182f", "markdown-documentation": "027f47636d9d510e6cbd5505f9339c1aeee6f41dd823c0b478d409dde1ae4538", diff --git a/.agents/rules/copier-template.md b/.agents/rules/copier-template.md index 83470db..20fcd32 100644 --- a/.agents/rules/copier-template.md +++ b/.agents/rules/copier-template.md @@ -4,7 +4,7 @@ These files are **rendered into consumer repositories** by `copier copy` and mer ## `copier.yml` -- `_skip_if_exists` lists **bootstrap-only** files (`pyproject.toml`, agent guidance); everything else under `template/` is updated on `copier update`. `_skip_if_exists` only protects files that exist: a file a consumer deletes is re-created on update, so optional files use a question-gated filename (`agent_guidance`). A file removed from the template is deleted from consumers, local edits included. +- `_skip_if_exists` lists **bootstrap-only** files (`pyproject.toml`, agent guidance); everything else under `template/` is updated on `copier update`. `_skip_if_exists` only protects files that exist: a file a consumer deletes is re-created on update, so optional files use a question-gated filename (`agent_guidance`). The starter `src/` package and `tests/` are created by a `_tasks` entry gated on `_copier_operation == 'copy'`, never as template files, so `copier update` cannot add them to existing consumers; the task skips a repo that already has a Python package. A file removed from the template is deleted from consumers, local edits included. - Renaming or removing a question is **breaking** for existing `.copier-answers.yml` files; document it in **`CHANGELOG.md`** and **`docs/migration-guide.md`**. ## `template/baselines/ruff.toml` diff --git a/.claude/rules/copier-template.md b/.claude/rules/copier-template.md index c18365e..1fd0472 100644 --- a/.claude/rules/copier-template.md +++ b/.claude/rules/copier-template.md @@ -10,7 +10,7 @@ These files are **rendered into consumer repositories** by `copier copy` and mer ## `copier.yml` -- `_skip_if_exists` lists **bootstrap-only** files (`pyproject.toml`, agent guidance); everything else under `template/` is updated on `copier update`. `_skip_if_exists` only protects files that exist: a file a consumer deletes is re-created on update, so optional files use a question-gated filename (`agent_guidance`). A file removed from the template is deleted from consumers, local edits included. +- `_skip_if_exists` lists **bootstrap-only** files (`pyproject.toml`, agent guidance); everything else under `template/` is updated on `copier update`. `_skip_if_exists` only protects files that exist: a file a consumer deletes is re-created on update, so optional files use a question-gated filename (`agent_guidance`). The starter `src/` package and `tests/` are created by a `_tasks` entry gated on `_copier_operation == 'copy'`, never as template files, so `copier update` cannot add them to existing consumers; the task skips a repo that already has a Python package. A file removed from the template is deleted from consumers, local edits included. - Renaming or removing a question is **breaking** for existing `.copier-answers.yml` files; document it in **`CHANGELOG.md`** and **`docs/migration-guide.md`**. ## `template/baselines/ruff.toml` diff --git a/AGENTS.md b/AGENTS.md index 43e4fde..f3211d9 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -17,6 +17,6 @@ This repository is an organization-wide standards and template bundle, not an in - Pinning: workflow `uses:` lines and documented examples use release tags (`@vX.Y.Z`) or commit SHAs, never long-lived `@main`. - Consumer guidance: changes to `template/` stay consistent with `docs/development.md`, `docs/reusable-workflows.md` and `README.md`. - CI contract: `scripts/check.sh` targets and the job ids of the template `ci.yml` are public API; changing them is breaking and documented in `docs/migration-guide.md`. -- Validate: `copier copy --vcs-ref HEAD . "$TMPDIR/demo" --defaults --data package_name=demo && cd "$TMPDIR/demo" && git init -q && uv lock && git add -A && bash scripts/check.sh lint`. +- Validate: `copier copy --vcs-ref HEAD . "$TMPDIR/demo" --defaults --trust --data package_name=demo && cd "$TMPDIR/demo" && git init -q && uv lock && git add -A && bash scripts/check.sh lint`. - Python policy follows `docs/string-enums.md` (`StrEnum`, not `(str, Enum)`), `template/baselines/ruff.toml` (`UP` selected) and strict mypy plus pytest-cov in `template/pyproject.toml.jinja`; tools and remote pre-commit `rev:` stay pinned. - `README.md` and `docs/development.md` are the source of truth. diff --git a/README.md b/README.md index 928ba45..db561fb 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ Portable agent guidance is an optional Copier bootstrap; see [development guidan ## Repository Layout -- `copier.yml` + `template/`: **[Copier](https://copier.readthedocs.io/)** template rendered into consumer repos — `.pre-commit-config.yaml`, `baselines/ruff.toml`, `scripts/check.sh`, `.github/workflows/ci.yml` (updated by `copier update`), plus a bootstrap-only `pyproject.toml`. +- `copier.yml` + `template/`: **[Copier](https://copier.readthedocs.io/)** template rendered into consumer repos — `.pre-commit-config.yaml`, `baselines/ruff.toml`, `scripts/check.sh`, `.github/workflows/ci.yml` (updated by `copier update`), plus a bootstrap-only `pyproject.toml`; `copier copy` (with `--trust`) also creates missing `src//` and `tests/` starters. - `docs/`: **[development.md](docs/development.md)** (hub), migration guide, versioning, CI contract, and style notes (e.g. [`string-enums.md`](docs/string-enums.md)). - `scripts/tests/`: repository checks, including the read-only sync between **`STANDARDS_VERSION`** and every pin carrying the Release Please version marker (see [docs/versioning.md](docs/versioning.md)). @@ -63,7 +63,7 @@ Portable agent guidance is an optional Copier bootstrap; see [development guidan ```bash uv tool install copier==9.18.2 -copier copy gh:BehindTheMusicTree/python-project-standards /path/to/repo # new repo +copier copy --trust gh:BehindTheMusicTree/python-project-standards /path/to/repo # new repo copier update # later, from the repo root uv lock && bash scripts/check.sh # new repo: lock once, then validate ``` diff --git a/copier.yml b/copier.yml index 0590687..e4eb2f8 100644 --- a/copier.yml +++ b/copier.yml @@ -1,4 +1,16 @@ _subdirectory: template +# Starter package and smoke test: created on `copier copy` only, and only in a repo with no +# Python package yet (an `--overwrite` adoption keeps its own layout). Template files would be +# added by `copier update`, which must not touch a consumer's layout. +_tasks: + - when: "{{ _copier_operation == 'copy' }}" + command: | + if find . \( -name .git -o -name .venv -o -name node_modules \) -prune -o -name __init__.py -not -path './tests/*' -print -quit | grep -q .; then exit 0; fi + mkdir -p "src/{{ package_module }}" tests + [ -e "src/{{ package_module }}/__init__.py" ] || printf '"""%s."""\n' '{{ package_name }}' > "src/{{ package_module }}/__init__.py" + [ -e "src/{{ package_module }}/py.typed" ] || : > "src/{{ package_module }}/py.typed" + [ -e tests/test_import.py ] || printf 'import {{ package_module }}\n\n\ndef test_package_is_importable() -> None:\n assert {{ package_module }}.__doc__\n' > tests/test_import.py + _skip_if_exists: - pyproject.toml - AGENTS.md @@ -8,6 +20,17 @@ _skip_if_exists: package_name: type: str help: Distribution name for pyproject.toml + validator: >- + {% if not package_name | regex_search('^[A-Za-z_][A-Za-z0-9._-]*$') %} + Must start with a letter or underscore and contain only letters, digits, ".", "-" and "_" + {% elif (package_name | lower | replace('-', '_') | replace('.', '_')) in ['false', 'none', 'true', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield'] %} + Must not be a Python keyword once lowercased + {% endif %} + +package_module: + type: str + when: false + default: "{{ package_name | lower | replace('-', '_') | replace('.', '_') }}" agent_guidance: type: bool diff --git a/docs/migration-guide.md b/docs/migration-guide.md index 22979e5..dad519b 100644 --- a/docs/migration-guide.md +++ b/docs/migration-guide.md @@ -24,7 +24,7 @@ Use this guide to adopt the baseline in an existing repository. Install Copier with an exact pin (for example `uv tool install copier==9.18.2`), then from the repository root: ```bash -copier copy --vcs-ref v6.0.0 gh:BehindTheMusicTree/python-project-standards . --overwrite # x-release-please-version +copier copy --trust --vcs-ref v6.0.0 gh:BehindTheMusicTree/python-project-standards . --overwrite # x-release-please-version git add -p # keep repo-specific hooks and settings the template overwrote ``` @@ -34,7 +34,7 @@ Questions (`copier.yml`): - **`agent_guidance`** — see [Optional agent guidance](#optional-agent-guidance). - **`uv_sync_args`** — extra `uv sync` arguments for `scripts/check.sh` and `ci.yml` (for example `--extra processing`). -`pyproject.toml` is **bootstrap-only** (`_skip_if_exists`): rendered for new repos, never touched in existing ones. Move dev tools into `[dependency-groups] dev` (PEP 735) and merge relevant sections from [`template/pyproject.toml.jinja`](../template/pyproject.toml.jinja) by hand (metadata, dependencies, **`[tool.mypy]`**, pytest). +`pyproject.toml` is **bootstrap-only** (`_skip_if_exists`): rendered for new repos, never touched in existing ones. `copier copy` also runs a copy-only task (so it needs `--trust`; `copier update` never runs it) that creates a `src//` placeholder with `py.typed` and `tests/test_import.py` when the repo has no Python package yet (an `--overwrite` adoption of an existing project is left alone), so `check.sh` passes on a fresh render. The module name is `package_name` lowercased with hyphens and dots turned into `_`; `package_name` must start with a letter or underscore. `pyproject.toml` declares `[tool.setuptools.packages.find] where = ["src"]` so the package builds. Move dev tools into `[dependency-groups] dev` (PEP 735) and merge relevant sections from [`template/pyproject.toml.jinja`](../template/pyproject.toml.jinja) by hand (metadata, dependencies, **`[tool.mypy]`**, pytest). Commit **`.copier-answers.yml`**: it records the adopted release (`_commit`) and answers so later updates know what to merge. diff --git a/scripts/tests/agent-template.sh b/scripts/tests/agent-template.sh index db65920..8191468 100644 --- a/scripts/tests/agent-template.sh +++ b/scripts/tests/agent-template.sh @@ -6,9 +6,10 @@ trap 'rm -rf "$tmp"' EXIT mkdir "$tmp/source" cp "$root/copier.yml" "$tmp/source/" cp -R "$root/template" "$tmp/source/" -copier copy --defaults --data package_name=demo "$tmp/source" "$tmp/default" >/dev/null +copier copy --defaults --trust --data package_name=demo "$tmp/source" "$tmp/default" >/dev/null [[ ! -e "$tmp/default/AGENTS.md" ]] [[ -x "$tmp/default/scripts/check.sh" ]] +[[ -f "$tmp/default/src/demo/__init__.py" && -f "$tmp/default/tests/test_import.py" ]] grep -qx 'uv sync --frozen' "$tmp/default/scripts/check.sh" actionlint "$tmp/default/.github/workflows/ci.yml" # A stub uv records the calls; it fails the pre-commit import only when asked to. @@ -41,12 +42,12 @@ for args in other '' 'lint test'; do done # A global pre-commit binary must not stand in for the missing venv module. if NO_PRE_COMMIT=1 check lint; then exit 1; fi -copier copy --defaults --data package_name=demo --data 'uv_sync_args=--extra processing' "$tmp/source" "$tmp/extra" >/dev/null +copier copy --defaults --trust --data package_name=demo --data 'uv_sync_args=--extra processing' "$tmp/source" "$tmp/extra" >/dev/null [[ -x "$tmp/extra/scripts/check.sh" ]] actionlint "$tmp/extra/.github/workflows/ci.yml" [[ "$(grep -c 'uv-sync-args: "--extra processing"' "$tmp/extra/.github/workflows/ci.yml")" == 2 ]] grep -qx 'uv sync --frozen --extra processing' "$tmp/extra/scripts/check.sh" -copier copy --defaults --data package_name=demo --data agent_guidance=true "$tmp/source" "$tmp/enabled" >/dev/null +copier copy --defaults --trust --data package_name=demo --data agent_guidance=true "$tmp/source" "$tmp/enabled" >/dev/null [[ -f "$tmp/enabled/AGENTS.md" && -f "$tmp/enabled/CLAUDE.md" ]] python3 "$1" "$tmp/enabled" actionlint "$tmp/enabled/.github/workflows/agent-drift.yml" @@ -54,7 +55,7 @@ mkdir -p "$tmp/existing/.github/workflows" printf 'existing agents\n' > "$tmp/existing/AGENTS.md" printf 'existing claude\n' > "$tmp/existing/CLAUDE.md" printf 'existing workflow\n' > "$tmp/existing/.github/workflows/agent-drift.yml" -copier copy --defaults --overwrite --data package_name=demo --data agent_guidance=true "$tmp/source" "$tmp/existing" >/dev/null +copier copy --defaults --trust --overwrite --data package_name=demo --data agent_guidance=true "$tmp/source" "$tmp/existing" >/dev/null [[ "$(cat "$tmp/existing/AGENTS.md")" == 'existing agents' ]] [[ "$(cat "$tmp/existing/CLAUDE.md")" == 'existing claude' ]] [[ "$(cat "$tmp/existing/.github/workflows/agent-drift.yml")" == 'existing workflow' ]] diff --git a/template/pyproject.toml.jinja b/template/pyproject.toml.jinja index 925ac69..ad59d14 100644 --- a/template/pyproject.toml.jinja +++ b/template/pyproject.toml.jinja @@ -8,6 +8,9 @@ version = "0.1.0" requires-python = ">=3.12" dependencies = [] +[tool.setuptools.packages.find] +where = ["src"] + [dependency-groups] dev = [ "pre-commit==4.5.1",