spec-kit icon indicating copy to clipboard operation
spec-kit copied to clipboard

feat(scripts): add SPECIFY_INIT_DIR to target a member project from the repo root

Open PascalThuet opened this issue 5 months ago • 4 comments

Summary

Adds SPECIFY_INIT_DIR as an explicit project-root override for Spec Kit scripts that may be invoked from outside the target project, such as a monorepo root or CI task.

Behavior

  • SPECIFY_INIT_DIR=/path/to/project makes supported core scripts operate on that Spec Kit project.
  • The value must resolve to an existing directory containing .specify/.
  • Invalid values fail fast. Scripts do not silently fall back to cwd or the git toplevel.
  • The git extension feature-branch entrypoint inherits the core resolver when available; if SPECIFY_INIT_DIR is set but only minimal or stale core helpers are available, it errors instead of guessing a root.

Changes

  • Adds Bash and PowerShell project-root resolvers for SPECIFY_INIT_DIR in the core common scripts.
  • Applies the override through core repo/feature path resolution and core feature creation.
  • Makes the bundled git extension feature-branch creation use the centralized core resolver without duplicating validation logic.
  • Documents the environment variable in the core reference.
  • Adds tests for valid targets, invalid targets, relative paths, monorepo-style invocation, file-path rejection, and missing/stale-core git extension fallback.

Validation

  • uv run pytest tests/test_init_dir.py tests/extensions/git/test_git_extension.py -q
  • uvx ruff check tests/extensions/git/test_git_extension.py
  • bash -n extensions/git/scripts/bash/create-new-feature-branch.sh scripts/bash/common.sh scripts/bash/create-new-feature.sh
  • git diff --check

pwsh is not available in my local environment, so PowerShell runtime tests are included but skipped locally.

PascalThuet avatar Jun 07 '26 14:06 PascalThuet

Architectural feedback

Thanks for the thorough work here — the strict contract (must exist + must contain .specify/) and test coverage are solid.

However, the resolver duplication across 3 script families (core, git-common, agent-context) diverges from how other env var overrides work in Spec Kit. Compare with SPECIFY_FEATURE_DIRECTORY:

  • Read once in core get_feature_paths() / Get-FeaturePathsEnv
  • Extensions that source core get it for free
  • No per-extension copies of the resolution logic

SPECIFY_INIT_DIR should follow the same pattern:

  1. Resolve once in get_repo_root() (core common.sh / common.ps1) — you already do this, and it works.
  2. Drop the duplicated resolvers from git-common.sh, git-common.ps1, and update-agent-context.{sh,ps1}. Extensions that source core already inherit get_repo_root(). Extensions that don't (agent-context) can read the project root from .specify/init-options.json or simply rely on the caller having cd'd — same as today.
  3. The "extensions can run without core installed" scenario is already handled by the existing _find_project_root walk-up-from-script-dir logic in those extensions. SPECIFY_INIT_DIR doesn't need a separate code path for that — if core isn't installed, the env var isn't being resolved by core either, so the contract is moot.

This eliminates ~60 lines of duplicated validation and the drift risk across 3 independent copies, while keeping the strict fail-fast behavior you designed.

mnriem avatar Jun 09 '26 13:06 mnriem

@mnriem done — centralized on the core resolver and dropped the per-extension resolver copies.

  • git-common.{sh,ps1} and update-agent-context.{sh,ps1}: resolver copies removed.
  • Git feature-branch creation now inherits get_repo_root / Get-RepoRoot by sourcing core; there is no duplicate SPECIFY_INIT_DIR resolver in the extension.
  • auto-commit.{sh,ps1}: override dropped; the existing script-dir walk-up still resolves the installed member project.
  • agent-context: back to cwd-based behavior.

One gap this exposed: if SPECIFY_INIT_DIR is set but the git extension only has minimal or stale core helpers available, it could silently ignore the override and target the wrong root. Rather than re-add resolver duplication, the extension now guards that case: SPECIFY_INIT_DIR set + core resolver unavailable → hard error instead of guessing.

Updated on behalf of @PascalThuet by Codex (GPT-5).

PascalThuet avatar Jun 09 '26 14:06 PascalThuet

Commit: 5ca5f55cfdbcce148a7063dd5c91cc4cbb550ff9

What changed:

  • Merged current origin/main into feat/monorepo-init-dir and resolved the CHANGELOG.md conflict while preserving the PR changelog entry above the 0.10.3 release section.
  • Addressed the two open Copilot comments in tests/test_init_dir.py by asserting FEATURE_DIR output exists before normalizing separators.

Validation run locally:

  • uv run pytest tests/test_init_dir.py tests/extensions/git/test_git_extension.py -q -> 49 passed, 27 skipped
  • uvx ruff check tests/test_init_dir.py tests/extensions/git/test_git_extension.py -> passed
  • bash -n extensions/git/scripts/bash/create-new-feature-branch.sh scripts/bash/common.sh scripts/bash/create-new-feature.sh -> passed
  • git diff --check -> passed

GitHub now reports the PR as mergeable.

PascalThuet avatar Jun 16 '26 15:06 PascalThuet

Please address Copilot feedback and revert changes to CHANGELOG.md

mnriem avatar Jun 16 '26 22:06 mnriem