Skip to content

Commit 5a901a6

Browse files
fix(scripts): fall through to grep/sed when python3 is a broken stub in feature.json parser (github#3312)
* fix(scripts): fall through to grep/sed when python3 is a broken stub in feature.json parser read_feature_json_feature_directory picked its json parser by availability (if jq / elif python3 / else grep-sed). on windows `python3` usually resolves to the microsoft store app execution alias stub: it satisfies `command -v` but fails at runtime (exit 49). the elif selected it, the runtime failure was swallowed to _fd='', and the grep/sed last resort was never reached - so a valid .specify/feature.json read as empty and every setup-plan / setup-tasks / check-prerequisites call errored with "Feature directory not found" right after a successful `specify init --script sh`. change selection from availability to parse success: try jq, then python3 only if still empty, then grep/sed only if still empty. a parser that exists but produces nothing now falls through instead of terminating the chain. the write path (_persist_feature_json) already uses jq-or-printf with no python3, so it was unaffected; only the read path needed this. add a regression test that puts a python3 stub (exit 49, like the store alias) first on PATH and asserts setup-plan.sh still resolves the feature via the grep/sed fallback. fixes github#3304 * test: shadow jq so the broken-python3 fallback test actually exercises it the test claimed it dropped jq so the parser chain would reach python3 and then grep/sed, but it only prepended the python3 stub dir to PATH. on a runner with jq installed, read_feature_json_feature_directory parses via jq and never reaches the fallback the test is meant to cover. add a failing jq stub alongside the python3 stub so the chain is forced through jq -> python3 -> grep/sed regardless of what the runner has installed.
1 parent 94c7ec2 commit 5a901a6

2 files changed

Lines changed: 76 additions & 2 deletions

File tree

‎scripts/bash/common.sh‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,26 @@ read_feature_json_feature_directory() {
9797
local fj="$repo_root/.specify/feature.json"
9898
[[ -f "$fj" ]] || { printf '%s' ''; return 0; }
9999

100+
# Try parsers in order (jq -> python3 -> grep/sed), falling through on
101+
# failure. Selection is by *parse success*, not mere availability: on
102+
# Windows `python3` commonly resolves to the Microsoft Store App Execution
103+
# Alias stub, which passes `command -v` but fails at runtime (exit 49), so
104+
# an availability-gated `elif` would pick python3, swallow its failure, and
105+
# never reach the grep/sed fallback -- leaving feature.json unreadable even
106+
# though it is valid (issue #3304).
100107
local _fd=''
101108
if command -v jq >/dev/null 2>&1; then
102109
if ! _fd=$(jq -r '.feature_directory // empty' "$fj" 2>/dev/null); then
103110
_fd=''
104111
fi
105-
elif command -v python3 >/dev/null 2>&1; then
112+
fi
113+
if [[ -z "$_fd" ]] && command -v python3 >/dev/null 2>&1; then
106114
# Use Python so pretty-printed/multi-line JSON still parses correctly.
107115
if ! _fd=$(python3 -c "import json,sys; d=json.load(open(sys.argv[1])); v=d.get('feature_directory'); print(v if v else '')" "$fj" 2>/dev/null); then
108116
_fd=''
109117
fi
110-
else
118+
fi
119+
if [[ -z "$_fd" ]]; then
111120
# Last-resort single-line grep/sed fallback. The `|| true` guards against
112121
# grep returning 1 (no match) aborting under `set -e` / `pipefail`.
113122
_fd=$( { grep -E '"feature_directory"[[:space:]]*:' "$fj" 2>/dev/null || true; } \

‎tests/test_setup_plan_feature_json.py‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,71 @@ def test_setup_plan_errors_without_feature_context(plan_repo: Path) -> None:
126126
assert "Feature directory not found" in result.stderr
127127

128128

129+
@requires_bash
130+
def test_setup_plan_survives_broken_python3_stub(plan_repo: Path) -> None:
131+
"""A `python3` on PATH that exists but fails at runtime must not defeat
132+
feature.json parsing.
133+
134+
On Windows `python3` typically resolves to the Microsoft Store App Execution
135+
Alias stub: it satisfies `command -v python3` yet exits non-zero at runtime.
136+
The parser must fall through to the grep/sed fallback on that failure instead
137+
of selecting python3 by mere availability and swallowing its error (#3304).
138+
"""
139+
subprocess.run(
140+
["git", "checkout", "-q", "-b", "feature/my-feature-branch"],
141+
cwd=plan_repo,
142+
check=True,
143+
)
144+
feat = plan_repo / "specs" / "001-tiny-notes-app"
145+
feat.mkdir(parents=True)
146+
(feat / "spec.md").write_text("# spec\n", encoding="utf-8")
147+
_write_feature_json(plan_repo, "specs/001-tiny-notes-app")
148+
149+
# A stub python3 that mimics the Windows Store alias: on PATH, exits 49.
150+
stub_dir = plan_repo / "_stubbin"
151+
stub_dir.mkdir()
152+
stub = stub_dir / "python3"
153+
stub.write_text(
154+
"#!/bin/sh\n"
155+
'echo "Python was not found; run without arguments to install from the '
156+
'Microsoft Store" >&2\n'
157+
"exit 49\n",
158+
encoding="utf-8",
159+
)
160+
stub.chmod(0o755)
161+
162+
# A stub jq that shadows any real jq on PATH and also fails, so the parser
163+
# cannot short-circuit on jq and must reach the broken python3 stub and then
164+
# fall through to grep/sed. Without this, a runner that has jq installed
165+
# would parse feature.json via jq and never exercise the fallback this test
166+
# is meant to cover.
167+
jq_stub = stub_dir / "jq"
168+
jq_stub.write_text(
169+
"#!/bin/sh\n"
170+
'echo "jq: simulated failure" >&2\n'
171+
"exit 1\n",
172+
encoding="utf-8",
173+
)
174+
jq_stub.chmod(0o755)
175+
176+
env = _clean_env()
177+
# Prepend the stub dir so the failing jq and python3 stubs take precedence
178+
# over any real ones; PATH still needs the real bash utilities for grep/sed.
179+
env["PATH"] = f"{stub_dir}{os.pathsep}{env.get('PATH', '')}"
180+
181+
script = plan_repo / ".specify" / "scripts" / "bash" / "setup-plan.sh"
182+
result = subprocess.run(
183+
["bash", str(script)],
184+
cwd=plan_repo,
185+
capture_output=True,
186+
text=True,
187+
check=False,
188+
env=env,
189+
)
190+
assert result.returncode == 0, result.stderr + result.stdout
191+
assert (feat / "plan.md").is_file()
192+
193+
129194
@requires_bash
130195
def test_setup_plan_numbered_branch_works_with_feature_json(
131196
plan_repo: Path,

0 commit comments

Comments
 (0)