diff --git a/.devcontainer/claude-code/init-host.sh b/.devcontainer/claude-code/init-host.sh index 7e123bf..da072a9 100755 --- a/.devcontainer/claude-code/init-host.sh +++ b/.devcontainer/claude-code/init-host.sh @@ -1,6 +1,11 @@ #!/bin/sh -# Initialize Claude Code host directory for devcontainer bind mount +# Create the host directories that devcontainer.json bind-mounts. # This script runs on the HOST before the container is created. +# docker refuses to create the container when a bind source is missing, so a +# host that has never run `gh auth login` (no ~/.config/gh) or never run Claude +# Code (no ~/.claude) could not start the container without this. # mkdir -p is idempotent - it only creates if missing, won't clobber existing. +# Keep this list in step with the ${localEnv:HOME} bind mounts in +# devcontainer.json; test/test_devcontainer.py fails when they drift. -mkdir -p "$HOME/.claude" +mkdir -p "$HOME/.claude" "$HOME/.config/gh" diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 7e26bf5..ba473fe 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -1,7 +1,15 @@ { - // Uses the template's prebuilt image by default for fast startup. - // To build locally: pixi run dev-use-local - // To use this repo's own prebuilt image: pixi run dev-use-prebuilt + // Uses python_template's prebuilt image by default for fast startup. + // + // Template-only: python_template itself also ships .devcontainer/Dockerfile, + // .devcontainer/ci/ (the config its image is built from) and two pixi + // tasks that toggle this block -- `pixi run dev-use-local` (uncomment the + // "build" block below and build from the Dockerfile) and + // `pixi run dev-use-prebuilt` (point "image" at this repo's own image). + // A child repo that kept only devcontainer.json, post-create.sh and + // claude-code/init-host.sh (the initializeCommand below) has none of + // those; there the commented "build" block is inert, and to build locally + // you bring them over from blooop/python_template first. // "build": { // "dockerfile": "Dockerfile", // "context": ".." @@ -9,13 +17,13 @@ "image": "ghcr.io/blooop/python_template/devcontainer:latest", // There is deliberately no "features" block. The claude-code feature is - // already baked into the image above: .devcontainer/ci/devcontainer.json, - // which is the config CI builds that image from, declares - // "../claude-code". Declaring it again here made the devcontainer spec - // build a derived image on the first launch of every branch, reinstalling - // something the pulled image already carried -- so the "prebuilt image for - // fast startup" above still paid for a build. With this block gone the - // launch is a pull and nothing else. + // already baked into the image above: python_template's + // .devcontainer/ci/devcontainer.json, which is the config its CI builds + // that image from, declares "../claude-code". Declaring it again here + // made the devcontainer spec build a derived image on the first launch of + // every branch, reinstalling something the pulled image already carried -- + // so the "prebuilt image for fast startup" above still paid for a build. + // With this block gone the launch is a pull and nothing else. // // Adding a feature back here is allowed and costs that derived build again. // If that ever becomes the normal case, the fix is devlaunch's design -- @@ -63,7 +71,9 @@ }, // Three mounts. The pixi volume keeps the environment out of the bind- - // mounted workspace; the other two share host credentials. + // mounted workspace; the other two share host credentials. docker will not + // create the container when a bind source is missing, so initializeCommand + // (.devcontainer/claude-code/init-host.sh) creates both on the host first. // // ~/.config/gh is here because it works. `gh` uses a system keyring when // one is available and falls back to hosts.yml when it is not, and on this diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 322103d..11c54e0 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -13,7 +13,6 @@ updates: dev-dependencies: #try to group all development dependencies updates into a single pr patterns: - - "check-manifest" - "prek" - "pytest" - "pytest-cov" @@ -24,6 +23,9 @@ updates: #try to group all third party library updates into a single pr patterns: - "*" + # Template-only: this watches .devcontainer/Dockerfile. A child repo that + # uses the prebuilt image and has no Dockerfile should drop this entry, or + # every Dependabot run reports a missing dependency file. - package-ecosystem: "docker" directory: "/.devcontainer" schedule: diff --git a/.vscode/tasks.json b/.vscode/tasks.json index b83b945..ef97eec 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -52,7 +52,7 @@ "label": "pytest duration", "detail": "Run pytest and track the duration of each test", "type": "shell", - "command": "pytest --durations=0" + "command": "pixi run pytest --durations=0" }, { "label": "check CI", diff --git a/test/test_devcontainer.py b/test/test_devcontainer.py new file mode 100644 index 0000000..dff6fce --- /dev/null +++ b/test/test_devcontainer.py @@ -0,0 +1,49 @@ +import re +import subprocess +from pathlib import Path + +REPO = Path(__file__).resolve().parent.parent +DEVCONTAINER = REPO / ".devcontainer" + + +def host_bind_sources() -> list[str]: + config = (DEVCONTAINER / "devcontainer.json").read_text(encoding="utf-8") + live = "\n".join(line for line in config.splitlines() if not line.lstrip().startswith("//")) + sources = [] + for mount in re.findall(r'"([^"]*\btype=bind\b[^"]*)"', live): + fields = dict(field.split("=", 1) for field in mount.split(",") if "=" in field) + home, _, rest = fields.get("source", "").partition("${localEnv:HOME}/") + if not home and rest: + sources.append(rest) + return sources + + +def test_init_host_creates_every_home_bind_source(tmp_path): + """docker refuses to create the container when a bind source is missing.""" + sources = host_bind_sources() + assert sources, "found no ${localEnv:HOME} bind mounts in devcontainer.json" + + subprocess.run( + [str(DEVCONTAINER / "claude-code" / "init-host.sh")], + env={"HOME": str(tmp_path), "PATH": "/usr/bin:/bin"}, + check=True, + ) + + missing = [s for s in sources if not (tmp_path / s).is_dir()] + assert not missing, f"init-host.sh did not create: {missing}" + + +def test_init_host_keeps_existing_dirs(tmp_path): + """A second run must not fail or touch what is already there.""" + gh = tmp_path / ".config" / "gh" + gh.mkdir(parents=True) + (gh / "hosts.yml").write_text("token", encoding="utf-8") + + for _ in range(2): + subprocess.run( + [str(DEVCONTAINER / "claude-code" / "init-host.sh")], + env={"HOME": str(tmp_path), "PATH": "/usr/bin:/bin"}, + check=True, + ) + + assert (gh / "hosts.yml").read_text(encoding="utf-8") == "token"