From 6ac4d40a5ef589417779a985d8c5d917168577a1 Mon Sep 17 00:00:00 2001 From: pengzh1 <24854675+pengzh1@users.noreply.github.com> Date: Wed, 2 Dec 2026 16:56:21 +0800 Subject: [PATCH 1/5] fix(scripts): treat a feature.json pointer to a missing directory as no feature `check-prerequisites --json --paths-only` exited 0 and printed FEATURE_DIR, FEATURE_SPEC, IMPL_PLAN and TASKS under a directory that no longer exists whenever .specify/feature.json still pointed at it, while the same command with no feature.json exited 1. get_feature_paths in the bash, python and PowerShell helpers accepted the stored feature_directory without checking that its target is a directory. The feature.json branch now requires the normalized path to be a directory and otherwise emits the existing missing-pointer error and exits 1, so a stale pointer, a pointer to a regular file and a pointer to a dangling symlink are all treated as no feature. The SPECIFY_FEATURE_DIRECTORY branch is unchanged: naming a directory that does not exist yet is a legitimate use of the explicit override. Assisted-by: ZCode (model: GLM-5.4, autonomous) --- scripts/bash/common.sh | 7 ++ scripts/powershell/common.ps1 | 8 ++ scripts/python/common.py | 10 ++ tests/test_feature_json_stale_pointer.py | 135 +++++++++++++++++++++++ 4 files changed, 160 insertions(+) create mode 100644 tests/test_feature_json_stale_pointer.py diff --git a/scripts/bash/common.sh b/scripts/bash/common.sh index 55bcffa25c..a21e447c03 100644 --- a/scripts/bash/common.sh +++ b/scripts/bash/common.sh @@ -205,6 +205,13 @@ get_feature_paths() { feature_dir="$_fd" # Normalize relative paths to absolute under repo root [[ "$feature_dir" != /* ]] && feature_dir="$repo_root/$feature_dir" + # A persisted pointer whose target is gone (renumbered or deleted specs + # dir) is no feature context: fail like a missing pointer instead of + # handing out paths under a directory that does not exist (#5566). + if [[ ! -d "$feature_dir" ]]; then + echo "ERROR: Feature directory not found. Set SPECIFY_FEATURE_DIRECTORY or run the specify command to create .specify/feature.json." >&2 + return 1 + fi else echo "ERROR: Feature directory not found. Set SPECIFY_FEATURE_DIRECTORY or ensure .specify/feature.json contains feature_directory." >&2 return 1 diff --git a/scripts/powershell/common.ps1 b/scripts/powershell/common.ps1 index bd80cacc94..1fc7df0c43 100644 --- a/scripts/powershell/common.ps1 +++ b/scripts/powershell/common.ps1 @@ -208,6 +208,14 @@ function Get-FeaturePathsEnv { if (-not [System.IO.Path]::IsPathRooted($featureDir)) { $featureDir = Join-Path $repoRoot $featureDir } + # A persisted pointer whose target is gone (renumbered or deleted specs + # dir) is no feature context: fail like a missing pointer instead of + # handing out paths under a directory that does not exist (#5566). + if (-not (Test-Path -LiteralPath $featureDir -PathType Container)) { + [Console]::Error.WriteLine("ERROR: Feature directory not found. Set SPECIFY_FEATURE_DIRECTORY or run the specify command to create .specify/feature.json.") + if ($ReturnNullOnError) { return $null } + exit 1 + } } else { [Console]::Error.WriteLine("ERROR: Feature directory not found. Set SPECIFY_FEATURE_DIRECTORY or ensure .specify/feature.json contains feature_directory.") if ($ReturnNullOnError) { return $null } diff --git a/scripts/python/common.py b/scripts/python/common.py index 928f969772..0274da3198 100644 --- a/scripts/python/common.py +++ b/scripts/python/common.py @@ -168,6 +168,16 @@ def get_feature_paths( feature_dir = Path(stored) if not feature_dir.is_absolute(): feature_dir = repo_root / feature_dir + # A persisted pointer whose target is gone (renumbered or deleted specs + # dir) is no feature context: fail like a missing pointer instead of + # handing out paths under a directory that does not exist (#5566). + if not feature_dir.is_dir(): + print( + "ERROR: Feature directory not found. Set SPECIFY_FEATURE_DIRECTORY " + "or run the specify command to create .specify/feature.json.", + file=sys.stderr, + ) + raise SystemExit(1) else: print( "ERROR: Feature directory not found. Set SPECIFY_FEATURE_DIRECTORY " diff --git a/tests/test_feature_json_stale_pointer.py b/tests/test_feature_json_stale_pointer.py new file mode 100644 index 0000000000..5882d45d09 --- /dev/null +++ b/tests/test_feature_json_stale_pointer.py @@ -0,0 +1,135 @@ +"""A ``.specify/feature.json`` pointer to a missing directory is no feature context. + +``check-prerequisites --json --paths-only`` used to exit 0 and print paths under a +directory that is gone; with no ``feature.json`` at all the same command exits 1. +""" + +from __future__ import annotations + +import json +import os +import shutil +import subprocess +import sys +from pathlib import Path + +import pytest + +from tests.conftest import requires_bash +from tests.parity_helpers import clean_env, run + +SCRIPTS = Path(__file__).resolve().parent.parent / "scripts" + +HAS_PWSH = shutil.which("pwsh") is not None +_WINDOWS_POWERSHELL = ( + shutil.which("powershell.exe") or shutil.which("powershell") +) if os.name == "nt" else None +requires_pwsh = pytest.mark.skipif( + not (HAS_PWSH or _WINDOWS_POWERSHELL), reason="no PowerShell available" +) +PORTS = [ + pytest.param("bash", marks=requires_bash), + pytest.param("powershell", marks=requires_pwsh), + pytest.param("python"), +] + +# Printed by every port when there is no feature context at all (#5566). +MISSING_POINTER_ERROR = ( + "ERROR: Feature directory not found. Set SPECIFY_FEATURE_DIRECTORY " + "or run the specify command to create .specify/feature.json." +) + + +@pytest.fixture +def project(tmp_path: Path) -> Path: + """A git repository with the three check-prerequisites variants installed.""" + repo = tmp_path / "proj" + (repo / ".specify").mkdir(parents=True) + ident = ["-c", "user.name=Test User", "-c", "user.email=test@example.com"] + subprocess.run(["git", "init", "-q"], cwd=repo, check=True) + subprocess.run(["git", *ident, "commit", "--allow-empty", "-qm", "init"], cwd=repo, check=True) + for folder, names in ( + ("bash", "common.sh check-prerequisites.sh"), + ("powershell", "common.ps1 check-prerequisites.ps1"), + ("python", "common.py check_prerequisites.py"), + ): + target = repo / ".specify" / "scripts" / folder + target.mkdir(parents=True) + for name in names.split(): + shutil.copy(SCRIPTS / folder / name, target / name) + return repo + + +def _point_at(repo: Path, feature_directory: str) -> None: + body = json.dumps({"feature_directory": feature_directory}, separators=(",", ":")) + (repo / ".specify" / "feature.json").write_text(body + "\n", encoding="utf-8") + + +def _paths_only(repo: Path, port: str, env: dict[str, str] | None = None): + scripts = repo / ".specify" / "scripts" + exe = "pwsh" if HAS_PWSH else _WINDOWS_POWERSHELL + if port == "bash": + cmd = ["bash", str(scripts / "bash" / "check-prerequisites.sh"), "--json", "--paths-only"] + elif port == "powershell": + cmd = [exe, "-NoProfile", "-File", str(scripts / "powershell" / "check-prerequisites.ps1"), "-Json", "-PathsOnly"] + else: + cmd = [sys.executable, str(scripts / "python" / "check_prerequisites.py"), "--json", "--paths-only"] + return run(cmd, repo, env or clean_env()) + + +@pytest.mark.parametrize("port", PORTS) +@pytest.mark.parametrize("kind", ["missing_dir", "regular_file", "dangling_symlink"]) +def test_stale_pointer_fails_like_no_pointer(project: Path, port: str, kind: str) -> None: + target = project / "specs" / "001-gone" + target.parent.mkdir(parents=True) + if kind == "regular_file": + target.write_text("not a directory\n", encoding="utf-8") + elif kind == "dangling_symlink": + try: + target.symlink_to(target.with_name("001-never-created"), target_is_directory=True) + except (OSError, NotImplementedError): + pytest.skip("this platform cannot create a symlink here") + _point_at(project, "specs/001-gone") + + result = _paths_only(project, port) + + assert result.returncode == 1, result.stderr + result.stdout + assert MISSING_POINTER_ERROR in result.stderr, result.stderr + # The symptom: a FEATURE_DIR printed under the missing path. + assert "FEATURE_DIR" not in result.stdout, result.stdout + + +@pytest.mark.parametrize("port", PORTS) +def test_valid_pointer_still_resolves(project: Path, port: str) -> None: + feature = project / "specs" / "001-present" + feature.mkdir(parents=True) + (feature / "plan.md").write_text("# plan\n", encoding="utf-8") + _point_at(project, "specs/001-present") + + result = _paths_only(project, port) + + assert result.returncode == 0, result.stderr + payload = json.loads(result.stdout) + assert Path(payload["FEATURE_DIR"]) == feature + assert Path(payload["IMPL_PLAN"]) == feature / "plan.md" + + +@pytest.mark.parametrize("port", PORTS) +def test_missing_feature_json_still_fails(project: Path, port: str) -> None: + result = _paths_only(project, port) + + assert result.returncode == 1, result.stderr + result.stdout + assert MISSING_POINTER_ERROR in result.stderr, result.stderr + + +@pytest.mark.parametrize("port", PORTS) +def test_env_var_pointing_at_missing_dir_is_kept(project: Path, port: str) -> None: + """The explicit override may name a directory that is created later.""" + env = clean_env() + env["SPECIFY_FEATURE_DIRECTORY"] = "specs/999-not-created-yet" + + result = _paths_only(project, port, env) + + assert result.returncode == 0, result.stderr + payload = json.loads(result.stdout) + assert Path(payload["FEATURE_DIR"]) == project / "specs" / "999-not-created-yet" From 093a38610539d806e9c8aeb0b24ebcc23802051d Mon Sep 17 00:00:00 2001 From: pengzh1 <24854675+pengzh1@users.noreply.github.com> Date: Wed, 2 Dec 2026 17:20:58 +0800 Subject: [PATCH 2/5] test(setup-plan,init): create the feature directory the fixture's pointer names With the stale-pointer fix in get_feature_paths these tests fail for the right reason: they wrote a .specify/feature.json pointer to a directory that was never created and relied on setup-plan recreating it. Their subject is unchanged (composing SPECIFY_INIT_DIR with an existing feature.json; plan.md created and not overwritten; JSON output parseable) and the fixture only used the pointer as setup, so the tests now create that directory themselves. Adjusted in tests/test_setup_plan_no_overwrite.py: the plan_repo fixture, test_setup_plan_preserves_existing_plan, test_setup_plan_skip_message_on_stderr_in_json_mode, test_ps_setup_plan_preserves_existing_plan and test_ps_setup_plan_does_not_match_another_features_plan. Adjusted in tests/test_init_dir.py: test_composes_with_target_feature_json. Assisted-by: ZCode (model: GLM-5.4, autonomous) --- tests/test_init_dir.py | 1 + tests/test_setup_plan_no_overwrite.py | 9 ++++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/tests/test_init_dir.py b/tests/test_init_dir.py index fff9d4ef62..b169207dc2 100644 --- a/tests/test_init_dir.py +++ b/tests/test_init_dir.py @@ -190,6 +190,7 @@ def test_composes_with_target_feature_json(tmp_path: Path) -> None: (web / ".specify" / "feature.json").write_text( '{"feature_directory": "specs/004-fj"}' ) + (web / "specs" / "004-fj").mkdir(parents=True) env = {**_clean_env(), "SPECIFY_INIT_DIR": str(web)} result = _bash("get_feature_paths", cwd=tmp_path, env=env) assert result.returncode == 0, result.stderr diff --git a/tests/test_setup_plan_no_overwrite.py b/tests/test_setup_plan_no_overwrite.py index cabed97d3e..a53c7ab887 100644 --- a/tests/test_setup_plan_no_overwrite.py +++ b/tests/test_setup_plan_no_overwrite.py @@ -85,6 +85,8 @@ def plan_repo(tmp_path: Path) -> Path: _install_bash_scripts(repo) _install_ps_scripts(repo) _write_feature_json(repo) + # A persisted pointer must name an existing directory (#5566). + (repo / "specs" / "001-my-feature").mkdir(parents=True) return repo @@ -116,7 +118,7 @@ def test_setup_plan_creates_plan_when_missing(plan_repo: Path) -> None: def test_setup_plan_preserves_existing_plan(plan_repo: Path) -> None: """Rerun must not overwrite an existing plan.md.""" feat = plan_repo / "specs" / "001-my-feature" - feat.mkdir(parents=True) + feat.mkdir(parents=True, exist_ok=True) existing_content = "# My carefully authored plan\n\nDo not overwrite me.\n" (feat / "plan.md").write_text(existing_content, encoding="utf-8") @@ -138,7 +140,7 @@ def test_setup_plan_preserves_existing_plan(plan_repo: Path) -> None: def test_setup_plan_skip_message_on_stderr_in_json_mode(plan_repo: Path) -> None: """In --json mode, status messages must go to stderr, not stdout.""" feat = plan_repo / "specs" / "001-my-feature" - feat.mkdir(parents=True) + feat.mkdir(parents=True, exist_ok=True) (feat / "plan.md").write_text("# existing\n", encoding="utf-8") script = plan_repo / ".specify" / "scripts" / "bash" / "setup-plan.sh" @@ -206,7 +208,7 @@ def test_ps_setup_plan_preserves_existing_plan(plan_repo: Path, feature_name: st """Rerun must not overwrite an existing plan.md.""" _write_feature_json(plan_repo, f"specs/{feature_name}") feat = plan_repo / "specs" / feature_name - feat.mkdir(parents=True) + feat.mkdir(parents=True, exist_ok=True) existing_content = "# My carefully authored plan\n\nDo not overwrite me.\n" (feat / "plan.md").write_text(existing_content, encoding="utf-8") @@ -234,6 +236,7 @@ def test_ps_setup_plan_does_not_match_another_features_plan(plan_repo: Path) -> """A wildcard-shaped feature path must not match a neighboring plan.""" feature_name = "001-my-feature [v2]" _write_feature_json(plan_repo, f"specs/{feature_name}") + (plan_repo / "specs" / feature_name).mkdir(parents=True) neighbor = plan_repo / "specs" / "001-my-feature v" neighbor.mkdir(parents=True) existing_content = "# Another feature's plan\n" From 4bd445905285badd53e11b5bad3bda7aa0f4f261 Mon Sep 17 00:00:00 2001 From: pengzh1 <24854675+pengzh1@users.noreply.github.com> Date: Wed, 2 Dec 2026 17:56:52 +0800 Subject: [PATCH 3/5] test(scripts): cover Windows PowerShell 5.1 and Git Bash drive paths The PowerShell port of the matrix picked pwsh whenever both hosts were installed, so the stale-pointer cases never ran under Windows PowerShell 5.1, the host CONTRIBUTING.md asks for evidence from when PowerShell behaviour changes. The matrix now has a separate windows-powershell port next to pwsh, and each host runs the full set of cases. Path assertions go through a small helper that turns the /c/... drive spelling Git for Windows Bash can emit into C:/... before Path() compares it with the fixture's C:\... spelling. Off Windows the helper is a no-op. Assisted-by: ZCode (model: GLM-5.4, autonomous) --- tests/test_feature_json_stale_pointer.py | 29 +++++++++++++++++------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/tests/test_feature_json_stale_pointer.py b/tests/test_feature_json_stale_pointer.py index 5882d45d09..240d7669d4 100644 --- a/tests/test_feature_json_stale_pointer.py +++ b/tests/test_feature_json_stale_pointer.py @@ -8,6 +8,7 @@ import json import os +import re import shutil import subprocess import sys @@ -21,15 +22,20 @@ SCRIPTS = Path(__file__).resolve().parent.parent / "scripts" HAS_PWSH = shutil.which("pwsh") is not None +# Windows PowerShell 5.1 specifically, never pwsh: a behavioural change to +# PowerShell functionality needs evidence from it (CONTRIBUTING.md). _WINDOWS_POWERSHELL = ( shutil.which("powershell.exe") or shutil.which("powershell") ) if os.name == "nt" else None -requires_pwsh = pytest.mark.skipif( - not (HAS_PWSH or _WINDOWS_POWERSHELL), reason="no PowerShell available" +requires_pwsh = pytest.mark.skipif(not HAS_PWSH, reason="pwsh not available") +requires_windows_powershell = pytest.mark.skipif( + _WINDOWS_POWERSHELL is None, reason="Windows PowerShell 5.1 not available" ) +# Each host runs the full set of cases. PORTS = [ pytest.param("bash", marks=requires_bash), - pytest.param("powershell", marks=requires_pwsh), + pytest.param("pwsh", marks=requires_pwsh), + pytest.param("windows-powershell", marks=requires_windows_powershell), pytest.param("python"), ] @@ -65,12 +71,19 @@ def _point_at(repo: Path, feature_directory: str) -> None: (repo / ".specify" / "feature.json").write_text(body + "\n", encoding="utf-8") +def _path(value: str) -> Path: + # Git for Windows Bash prints drives as /c/..., which Path() does not equate with C:\... + if os.name == "nt" and re.match(r"^/[A-Za-z]/", value): + value = f"{value[1].upper()}:{value[2:]}" + return Path(value) + + def _paths_only(repo: Path, port: str, env: dict[str, str] | None = None): scripts = repo / ".specify" / "scripts" - exe = "pwsh" if HAS_PWSH else _WINDOWS_POWERSHELL + exe = {"pwsh": "pwsh", "windows-powershell": _WINDOWS_POWERSHELL}.get(port) if port == "bash": cmd = ["bash", str(scripts / "bash" / "check-prerequisites.sh"), "--json", "--paths-only"] - elif port == "powershell": + elif exe: cmd = [exe, "-NoProfile", "-File", str(scripts / "powershell" / "check-prerequisites.ps1"), "-Json", "-PathsOnly"] else: cmd = [sys.executable, str(scripts / "python" / "check_prerequisites.py"), "--json", "--paths-only"] @@ -110,8 +123,8 @@ def test_valid_pointer_still_resolves(project: Path, port: str) -> None: assert result.returncode == 0, result.stderr payload = json.loads(result.stdout) - assert Path(payload["FEATURE_DIR"]) == feature - assert Path(payload["IMPL_PLAN"]) == feature / "plan.md" + assert _path(payload["FEATURE_DIR"]) == feature + assert _path(payload["IMPL_PLAN"]) == feature / "plan.md" @pytest.mark.parametrize("port", PORTS) @@ -132,4 +145,4 @@ def test_env_var_pointing_at_missing_dir_is_kept(project: Path, port: str) -> No assert result.returncode == 0, result.stderr payload = json.loads(result.stdout) - assert Path(payload["FEATURE_DIR"]) == project / "specs" / "999-not-created-yet" + assert _path(payload["FEATURE_DIR"]) == project / "specs" / "999-not-created-yet" From 868790427ffa92f54771de5a5fd0ab7fc8908429 Mon Sep 17 00:00:00 2001 From: pengzh1 <24854675+pengzh1@users.noreply.github.com> Date: Wed, 2 Dec 2026 23:08:03 +0800 Subject: [PATCH 4/5] test(scripts): cover a persisted pointer after a failed check-prerequisites Review asked for a regression test for the cross-session case: --paths-only disables persistence, so the existing cases never covered it. A normal check-prerequisites --json call with SPECIFY_FEATURE_DIRECTORY naming a missing directory fails but still persists that pointer; the new test then runs setup-plan without the variable and asserts it exits 1 with the missing-pointer message and creates no plan.md. A companion case covers recovery: creating the directory the persisted pointer names makes setup-plan succeed again. Both run on all four ports. Assisted-by: ZCode (model: GLM-5.4, autonomous) --- tests/test_feature_json_stale_pointer.py | 94 +++++++++++++++++++++--- 1 file changed, 84 insertions(+), 10 deletions(-) diff --git a/tests/test_feature_json_stale_pointer.py b/tests/test_feature_json_stale_pointer.py index 240d7669d4..7863b4d699 100644 --- a/tests/test_feature_json_stale_pointer.py +++ b/tests/test_feature_json_stale_pointer.py @@ -20,6 +20,7 @@ from tests.parity_helpers import clean_env, run SCRIPTS = Path(__file__).resolve().parent.parent / "scripts" +TEMPLATES = Path(__file__).resolve().parent.parent / "templates" HAS_PWSH = shutil.which("pwsh") is not None # Windows PowerShell 5.1 specifically, never pwsh: a behavioural change to @@ -48,21 +49,24 @@ @pytest.fixture def project(tmp_path: Path) -> Path: - """A git repository with the three check-prerequisites variants installed.""" + """A git repository with the check-prerequisites and setup-plan variants installed.""" repo = tmp_path / "proj" (repo / ".specify").mkdir(parents=True) ident = ["-c", "user.name=Test User", "-c", "user.email=test@example.com"] subprocess.run(["git", "init", "-q"], cwd=repo, check=True) subprocess.run(["git", *ident, "commit", "--allow-empty", "-qm", "init"], cwd=repo, check=True) for folder, names in ( - ("bash", "common.sh check-prerequisites.sh"), - ("powershell", "common.ps1 check-prerequisites.ps1"), - ("python", "common.py check_prerequisites.py"), + ("bash", "common.sh check-prerequisites.sh setup-plan.sh"), + ("powershell", "common.ps1 check-prerequisites.ps1 setup-plan.ps1"), + ("python", "common.py check_prerequisites.py setup_plan.py"), ): target = repo / ".specify" / "scripts" / folder target.mkdir(parents=True) for name in names.split(): shutil.copy(SCRIPTS / folder / name, target / name) + templates = repo / ".specify" / "templates" + templates.mkdir() + shutil.copy(TEMPLATES / "plan-template.md", templates / "plan-template.md") return repo @@ -78,16 +82,37 @@ def _path(value: str) -> Path: return Path(value) +def _ps_exe(port: str) -> str | None: + return {"pwsh": "pwsh", "windows-powershell": _WINDOWS_POWERSHELL}.get(port) + + +def _check_prereqs(repo: Path, port: str, env: dict[str, str], paths_only: bool): + scripts = repo / ".specify" / "scripts" + flag = ("--json", "--paths-only") if paths_only else ("--json",) + if port == "bash": + cmd = ["bash", str(scripts / "bash" / "check-prerequisites.sh"), *flag] + elif exe := _ps_exe(port): + ps_flag = ("-Json", "-PathsOnly") if paths_only else ("-Json",) + cmd = [exe, "-NoProfile", "-File", str(scripts / "powershell" / "check-prerequisites.ps1"), *ps_flag] + else: + cmd = [sys.executable, str(scripts / "python" / "check_prerequisites.py"), *flag] + return run(cmd, repo, env) + + def _paths_only(repo: Path, port: str, env: dict[str, str] | None = None): + return _check_prereqs(repo, port, env or clean_env(), paths_only=True) + + +def _setup_plan(repo: Path, port: str): + """Run setup-plan --json with no SPECIFY_* override in the environment.""" scripts = repo / ".specify" / "scripts" - exe = {"pwsh": "pwsh", "windows-powershell": _WINDOWS_POWERSHELL}.get(port) if port == "bash": - cmd = ["bash", str(scripts / "bash" / "check-prerequisites.sh"), "--json", "--paths-only"] - elif exe: - cmd = [exe, "-NoProfile", "-File", str(scripts / "powershell" / "check-prerequisites.ps1"), "-Json", "-PathsOnly"] + cmd = ["bash", str(scripts / "bash" / "setup-plan.sh"), "--json"] + elif exe := _ps_exe(port): + cmd = [exe, "-NoProfile", "-File", str(scripts / "powershell" / "setup-plan.ps1"), "-Json"] else: - cmd = [sys.executable, str(scripts / "python" / "check_prerequisites.py"), "--json", "--paths-only"] - return run(cmd, repo, env or clean_env()) + cmd = [sys.executable, str(scripts / "python" / "setup_plan.py"), "--json"] + return run(cmd, repo, clean_env()) @pytest.mark.parametrize("port", PORTS) @@ -146,3 +171,52 @@ def test_env_var_pointing_at_missing_dir_is_kept(project: Path, port: str) -> No assert result.returncode == 0, result.stderr payload = json.loads(result.stdout) assert _path(payload["FEATURE_DIR"]) == project / "specs" / "999-not-created-yet" + + +def _persist_later_pointer(project: Path, port: str) -> None: + """Session 1: a normal (persisting) check-prerequisites whose override names + a directory that does not exist yet. It fails, but the pointer is on disk.""" + env = clean_env() + env["SPECIFY_FEATURE_DIRECTORY"] = "specs/002-later" + first = _check_prereqs(project, port, env, paths_only=False) + assert first.returncode == 1, first.stderr + first.stdout + persisted = json.loads( + (project / ".specify" / "feature.json").read_text(encoding="utf-8") + ) + assert persisted["feature_directory"] == "specs/002-later" + + +@pytest.mark.parametrize("port", PORTS) +def test_failed_check_persists_pointer_and_blocks_setup_plan( + project: Path, port: str +) -> None: + """Session 2: without the variable, the persisted stale pointer blocks + setup-plan instead of silently recreating the directory. --paths-only + disables persistence, so only this sequence covers the cross-session case. + """ + later = project / "specs" / "002-later" + _persist_later_pointer(project, port) + + second = _setup_plan(project, port) + + assert second.returncode == 1, second.stderr + second.stdout + assert MISSING_POINTER_ERROR in second.stderr, second.stderr + assert not (later / "plan.md").exists() + + +@pytest.mark.parametrize("port", PORTS) +def test_creating_the_persisted_directory_recovers_setup_plan( + project: Path, port: str +) -> None: + """Recovery: create the directory the persisted pointer names. (Deleting + feature.json would only remove the feature context; making the pointer's + target exist keeps the session going without rewriting anything.) + """ + later = project / "specs" / "002-later" + _persist_later_pointer(project, port) + + later.mkdir(parents=True) + recovered = _setup_plan(project, port) + + assert recovered.returncode == 0, recovered.stderr + assert (later / "plan.md").is_file() From 75a2c006c334df2a75db3b57af3bb72cfa4db9f6 Mon Sep 17 00:00:00 2001 From: pengzh1 <24854675+pengzh1@users.noreply.github.com> Date: Sat, 5 Dec 2026 05:06:06 +0800 Subject: [PATCH 5/5] test(setup-tasks): create the feature directory the pointer names With the stale-pointer fix in get_feature_paths the PowerShell wildcard- neighbor test fails for the right reason: it wrote a .specify/feature.json pointer to specs/001-my-feature [v2] without ever creating that directory and relied on the old behaviour of accepting the stale pointer. Its subject is unchanged (a bracketed feature path must not accept a neighboring feature's plan) and the sibling bash tests already create the directory their pointers name, so the test now creates it too and still fails with 'plan.md not found' as intended. Assisted-by: ZCode (model: GLM-5.4, autonomous) --- tests/test_setup_tasks.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_setup_tasks.py b/tests/test_setup_tasks.py index 58a90cc3cc..5f02531e84 100644 --- a/tests/test_setup_tasks.py +++ b/tests/test_setup_tasks.py @@ -918,6 +918,8 @@ def test_setup_tasks_ps_does_not_match_wildcard_shaped_neighbor( ) -> None: """A bracketed feature path must not accept a neighboring feature's plan.""" feature_name = "001-my-feature [v2]" + # A persisted pointer must name an existing directory (#5566). + (tasks_repo / "specs" / feature_name).mkdir(parents=True) neighbor = tasks_repo / "specs" / "001-my-feature v" neighbor.mkdir(parents=True) (neighbor / "spec.md").write_text("# neighbor spec\n", encoding="utf-8")