Skip to content

Commit ea18277

Browse files
fix: stop check-prerequisites --paths-only from writing feature.json (#3025) (#3190)
* fix: stop check-prerequisites --paths-only from writing feature.json (#3025) check-prerequisites --paths-only / -PathsOnly is documented as pure, read-only path resolution, but when SPECIFY_FEATURE_DIRECTORY was set it called the persist routine and rewrote .specify/feature.json. That dirtied the working tree and overwrote a pinned feature directory during what should be a no-op. Add an explicit opt-out at the resolver boundary instead of a global env back-channel: - bash: get_feature_paths accepts a leading --no-persist flag that skips _persist_feature_json; check-prerequisites.sh passes it in --paths-only mode. - PowerShell: Get-FeaturePathsEnv gains a -NoPersist switch that skips Save-FeatureJson; check-prerequisites.ps1 passes it in -PathsOnly mode. Normal (non-paths-only) invocations are unchanged and still persist the override, so future sessions without the env var keep working. Add regression tests asserting --paths-only/-PathsOnly leaves a pinned feature.json untouched even when the env override differs, plus a guard that normal mode still persists. * fix: use ASCII hyphen in common.ps1 comment for PS 5.1 compatibility The em-dash in the persist comment introduced non-ASCII bytes, failing test_ps1_file_is_ascii_only which enforces ASCII-only PowerShell sources for Windows PowerShell 5.1 compatibility. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> * test: add PowerShell normal-mode persistence guard (#3025) Addresses Copilot review feedback on #3190: the bash side had a `test_normal_mode_still_persists_feature_json` guard, but there was no symmetric PowerShell test asserting that running check-prerequisites.ps1 *without* -PathsOnly still persists the SPECIFY_FEATURE_DIRECTORY override into .specify/feature.json. Add test_ps_normal_mode_still_persists_feature_json, which guards against accidentally passing -NoPersist unconditionally (or flipping the default) in a future refactor. Verified it fails when -NoPersist is passed in the non -PathsOnly branch and passes with the current conditional. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> --------- Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
1 parent 00f6a80 commit ea18277

5 files changed

Lines changed: 163 additions & 8 deletions

File tree

‎scripts/bash/check-prerequisites.sh‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,14 @@ done
7878
SCRIPT_DIR="$(CDPATH="" cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
7979
source "$SCRIPT_DIR/common.sh"
8080

81-
# Get feature paths
82-
_paths_output=$(get_feature_paths) || { echo "ERROR: Failed to resolve feature paths" >&2; exit 1; }
81+
# Get feature paths.
82+
# In --paths-only mode this is pure resolution, so pass --no-persist to opt out
83+
# of the feature.json write side effect (issue #3025).
84+
if $PATHS_ONLY; then
85+
_paths_output=$(get_feature_paths --no-persist) || { echo "ERROR: Failed to resolve feature paths" >&2; exit 1; }
86+
else
87+
_paths_output=$(get_feature_paths) || { echo "ERROR: Failed to resolve feature paths" >&2; exit 1; }
88+
fi
8389
eval "$_paths_output"
8490
unset _paths_output
8591

‎scripts/bash/common.sh‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,15 @@ _persist_feature_json() {
152152
}
153153

154154
get_feature_paths() {
155+
# Read-only callers (e.g. check-prerequisites.sh --paths-only) pass
156+
# --no-persist so pure path resolution never writes .specify/feature.json,
157+
# which would dirty the working tree or overwrite a pinned value (issue #3025).
158+
local no_persist=false
159+
if [[ "${1:-}" == "--no-persist" ]]; then
160+
no_persist=true
161+
shift
162+
fi
163+
155164
# Split decl/assignment so a SPECIFY_INIT_DIR validation failure in
156165
# get_repo_root propagates as a hard error instead of being masked by `local`.
157166
local repo_root
@@ -168,8 +177,11 @@ get_feature_paths() {
168177
feature_dir="$SPECIFY_FEATURE_DIRECTORY"
169178
# Normalize relative paths to absolute under repo root
170179
[[ "$feature_dir" != /* ]] && feature_dir="$repo_root/$feature_dir"
171-
# Persist to feature.json so future sessions without the env var still work
172-
_persist_feature_json "$repo_root" "$SPECIFY_FEATURE_DIRECTORY"
180+
# Persist to feature.json so future sessions without the env var still
181+
# work — unless the caller opted out for read-only resolution (#3025).
182+
if [[ "$no_persist" != true ]]; then
183+
_persist_feature_json "$repo_root" "$SPECIFY_FEATURE_DIRECTORY"
184+
fi
173185
elif [[ -f "$repo_root/.specify/feature.json" ]]; then
174186
local _fd
175187
_fd=$(read_feature_json_feature_directory "$repo_root")

‎scripts/powershell/check-prerequisites.ps1‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,14 @@ EXAMPLES:
5656
# Source common functions
5757
. "$PSScriptRoot/common.ps1"
5858

59-
# Get feature paths
60-
$paths = Get-FeaturePathsEnv
59+
# Get feature paths.
60+
# In -PathsOnly mode this is pure resolution, so pass -NoPersist to opt out of
61+
# the feature.json write side effect (issue #3025).
62+
if ($PathsOnly) {
63+
$paths = Get-FeaturePathsEnv -NoPersist
64+
} else {
65+
$paths = Get-FeaturePathsEnv
66+
}
6167

6268
# If paths-only mode, output paths and exit (no validation)
6369
if ($PathsOnly) {

‎scripts/powershell/common.ps1‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,13 @@ function Save-FeatureJson {
143143
}
144144

145145
function Get-FeaturePathsEnv {
146+
# Read-only callers (e.g. check-prerequisites.ps1 -PathsOnly) pass -NoPersist
147+
# so pure path resolution never writes .specify/feature.json, which would
148+
# dirty the working tree or overwrite a pinned value (issue #3025).
149+
param(
150+
[switch]$NoPersist
151+
)
152+
146153
$repoRoot = Get-RepoRoot
147154
$currentBranch = Get-CurrentBranch
148155

@@ -157,8 +164,11 @@ function Get-FeaturePathsEnv {
157164
if (-not [System.IO.Path]::IsPathRooted($featureDir)) {
158165
$featureDir = Join-Path $repoRoot $featureDir
159166
}
160-
# Persist to feature.json so future sessions without the env var still work
161-
Save-FeatureJson -RepoRoot $repoRoot -FeatureDirectory $env:SPECIFY_FEATURE_DIRECTORY
167+
# Persist to feature.json so future sessions without the env var still
168+
# work - unless the caller opted out for read-only resolution (#3025).
169+
if (-not $NoPersist) {
170+
Save-FeatureJson -RepoRoot $repoRoot -FeatureDirectory $env:SPECIFY_FEATURE_DIRECTORY
171+
}
162172
} elseif (Test-Path $featureJson) {
163173
$featureJsonRaw = Get-Content -LiteralPath $featureJson -Raw
164174
try {

‎tests/test_check_prerequisites_paths_only.py‎

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,66 @@ def test_normal_mode_still_validates_branch(prereq_repo: Path) -> None:
163163
assert result.stdout.strip() == ""
164164

165165

166+
@requires_bash
167+
def test_paths_only_does_not_persist_feature_json(prereq_repo: Path) -> None:
168+
"""--paths-only must not rewrite feature.json even when the env override
169+
differs from the pinned value (#3025).
170+
171+
Path resolution is read-only, so it must never dirty the working tree or
172+
overwrite the persisted feature directory.
173+
"""
174+
pinned = "specs/001-my-feature"
175+
(prereq_repo / "specs" / "001-my-feature").mkdir(parents=True, exist_ok=True)
176+
(prereq_repo / "specs" / "002-other").mkdir(parents=True, exist_ok=True)
177+
_write_feature_json(prereq_repo, pinned)
178+
fj = prereq_repo / ".specify" / "feature.json"
179+
before = fj.read_text(encoding="utf-8")
180+
181+
script = prereq_repo / ".specify" / "scripts" / "bash" / "check-prerequisites.sh"
182+
env = _clean_env()
183+
env["SPECIFY_FEATURE_DIRECTORY"] = "specs/002-other"
184+
result = subprocess.run(
185+
["bash", str(script), "--json", "--paths-only"],
186+
cwd=prereq_repo,
187+
capture_output=True,
188+
text=True,
189+
check=False,
190+
env=env,
191+
)
192+
assert result.returncode == 0, result.stderr
193+
# The override is honored in the output...
194+
data = json.loads(result.stdout)
195+
assert "002-other" in data["FEATURE_DIR"]
196+
# ...but the pinned file on disk is untouched.
197+
assert fj.read_text(encoding="utf-8") == before
198+
199+
200+
@requires_bash
201+
def test_normal_mode_still_persists_feature_json(prereq_repo: Path) -> None:
202+
"""Without --paths-only, the env override is still persisted to feature.json,
203+
so the --no-persist opt-out does not regress normal write behavior (#3025)."""
204+
(prereq_repo / "specs" / "001-my-feature").mkdir(parents=True, exist_ok=True)
205+
feat = prereq_repo / "specs" / "002-other"
206+
feat.mkdir(parents=True, exist_ok=True)
207+
(feat / "plan.md").write_text("# plan\n", encoding="utf-8")
208+
_write_feature_json(prereq_repo, "specs/001-my-feature")
209+
fj = prereq_repo / ".specify" / "feature.json"
210+
211+
script = prereq_repo / ".specify" / "scripts" / "bash" / "check-prerequisites.sh"
212+
env = _clean_env()
213+
env["SPECIFY_FEATURE_DIRECTORY"] = "specs/002-other"
214+
result = subprocess.run(
215+
["bash", str(script), "--json"],
216+
cwd=prereq_repo,
217+
capture_output=True,
218+
text=True,
219+
check=False,
220+
env=env,
221+
)
222+
assert result.returncode == 0, result.stderr
223+
assert json.loads(fj.read_text(encoding="utf-8"))["feature_directory"] == "specs/002-other"
224+
225+
166226
# ── PowerShell tests ──────────────────────────────────────────────────────
167227

168228

@@ -283,3 +343,64 @@ def test_ps_missing_tasks_error_goes_to_stderr(prereq_repo: Path) -> None:
283343
assert "tasks.md not found" in result.stderr
284344
assert "tasks.md not found" not in result.stdout
285345
assert result.stdout.strip() == ""
346+
347+
348+
@pytest.mark.skipif(not (HAS_PWSH or _WINDOWS_POWERSHELL), reason="no PowerShell available")
349+
def test_ps_paths_only_does_not_persist_feature_json(prereq_repo: Path) -> None:
350+
"""-PathsOnly must not rewrite feature.json even when the env override
351+
differs from the pinned value (#3025)."""
352+
pinned = "specs/001-my-feature"
353+
(prereq_repo / "specs" / "001-my-feature").mkdir(parents=True, exist_ok=True)
354+
(prereq_repo / "specs" / "002-other").mkdir(parents=True, exist_ok=True)
355+
_write_feature_json(prereq_repo, pinned)
356+
fj = prereq_repo / ".specify" / "feature.json"
357+
before = fj.read_text(encoding="utf-8")
358+
359+
script = prereq_repo / ".specify" / "scripts" / "powershell" / "check-prerequisites.ps1"
360+
exe = "pwsh" if HAS_PWSH else _WINDOWS_POWERSHELL
361+
env = _clean_env()
362+
env["SPECIFY_FEATURE_DIRECTORY"] = "specs/002-other"
363+
result = subprocess.run(
364+
[exe, "-NoProfile", "-File", str(script), "-Json", "-PathsOnly"],
365+
cwd=prereq_repo,
366+
capture_output=True,
367+
text=True,
368+
check=False,
369+
env=env,
370+
)
371+
assert result.returncode == 0, result.stderr
372+
data = json.loads(result.stdout)
373+
assert "002-other" in data["FEATURE_DIR"]
374+
assert fj.read_text(encoding="utf-8") == before
375+
376+
377+
@pytest.mark.skipif(not (HAS_PWSH or _WINDOWS_POWERSHELL), reason="no PowerShell available")
378+
def test_ps_normal_mode_still_persists_feature_json(prereq_repo: Path) -> None:
379+
"""Without -PathsOnly, the env override is still persisted to feature.json,
380+
so the -NoPersist opt-out does not regress normal write behavior (#3025).
381+
382+
Symmetric to the bash test_normal_mode_still_persists_feature_json guard:
383+
asserts the default path still persists and that -NoPersist is not passed
384+
unconditionally.
385+
"""
386+
(prereq_repo / "specs" / "001-my-feature").mkdir(parents=True, exist_ok=True)
387+
feat = prereq_repo / "specs" / "002-other"
388+
feat.mkdir(parents=True, exist_ok=True)
389+
(feat / "plan.md").write_text("# plan\n", encoding="utf-8")
390+
_write_feature_json(prereq_repo, "specs/001-my-feature")
391+
fj = prereq_repo / ".specify" / "feature.json"
392+
393+
script = prereq_repo / ".specify" / "scripts" / "powershell" / "check-prerequisites.ps1"
394+
exe = "pwsh" if HAS_PWSH else _WINDOWS_POWERSHELL
395+
env = _clean_env()
396+
env["SPECIFY_FEATURE_DIRECTORY"] = "specs/002-other"
397+
result = subprocess.run(
398+
[exe, "-NoProfile", "-File", str(script), "-Json"],
399+
cwd=prereq_repo,
400+
capture_output=True,
401+
text=True,
402+
check=False,
403+
env=env,
404+
)
405+
assert result.returncode == 0, result.stderr
406+
assert json.loads(fj.read_text(encoding="utf-8"))["feature_directory"] == "specs/002-other"

0 commit comments

Comments
 (0)