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/3] 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/3] 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/3] 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"