diff --git a/extensions/catalog.json b/extensions/catalog.json index a4bc32ceb0..5d12b4af6b 100644 --- a/extensions/catalog.json +++ b/extensions/catalog.json @@ -66,7 +66,7 @@ "github": { "name": "GitHub Integration", "id": "github", - "version": "1.0.6", + "version": "1.0.7", "description": "GitHub platform integration for Spec Kit - create GitHub issues from a feature's task list", "author": "spec-kit-core", "repository": "https://github.com/github/spec-kit", diff --git a/extensions/github/extension.yml b/extensions/github/extension.yml index 4faf3428a5..aadce984b9 100644 --- a/extensions/github/extension.yml +++ b/extensions/github/extension.yml @@ -3,7 +3,7 @@ schema_version: "1.0" extension: id: github name: "GitHub Integration" - version: "1.0.6" + version: "1.0.7" description: "GitHub platform integration for Spec Kit - create GitHub issues from a feature's task list" author: spec-kit-core repository: https://github.com/github/spec-kit diff --git a/extensions/github/scripts/bash/resolve-tasks.sh b/extensions/github/scripts/bash/resolve-tasks.sh index 4e6c874cf2..91b60e0de9 100755 --- a/extensions/github/scripts/bash/resolve-tasks.sh +++ b/extensions/github/scripts/bash/resolve-tasks.sh @@ -152,7 +152,9 @@ read_feature_json_feature_directory() { fi fi if [[ -z "$_fd" ]] && command -v python3 >/dev/null 2>&1; then - 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 + # Read and write UTF-8 explicitly, as core does: Windows defaults both + # the file and the piped stdout to the ANSI code page. + if ! _fd=$(python3 -c "import json,sys; d=json.load(open(sys.argv[1], encoding='utf-8')); v=d.get('feature_directory'); sys.stdout.buffer.write(str(v).encode('utf-8') if v else b'')" "$fj" 2>/dev/null); then _fd='' fi fi diff --git a/scripts/bash/common.sh b/scripts/bash/common.sh index 55bcffa25c..9ea5646f29 100644 --- a/scripts/bash/common.sh +++ b/scripts/bash/common.sh @@ -112,7 +112,10 @@ read_feature_json_feature_directory() { fi if [[ -z "$_fd" ]] && command -v python3 >/dev/null 2>&1; then # Use Python so pretty-printed/multi-line JSON still parses correctly. - if ! _fd=$(python3 -c "import json,sys; d=json.load(open(sys.argv[1], encoding='utf-8')); v=d.get('feature_directory'); print(v if v else '')" "$fj" 2>/dev/null); then + # Write UTF-8 bytes instead of print(): on Windows a piped stdout uses + # the ANSI code page, which re-encodes non-ASCII names in that code + # page instead of UTF-8 (cp1252 turns "é" into the single byte 0xE9). + if ! _fd=$(python3 -c "import json,sys; d=json.load(open(sys.argv[1], encoding='utf-8')); v=d.get('feature_directory'); sys.stdout.buffer.write(str(v).encode('utf-8') if v else b'')" "$fj" 2>/dev/null); then _fd='' fi fi diff --git a/tests/extensions/github/test_github_extension.py b/tests/extensions/github/test_github_extension.py index eb79415e07..7a5b4d6cfc 100644 --- a/tests/extensions/github/test_github_extension.py +++ b/tests/extensions/github/test_github_extension.py @@ -31,6 +31,7 @@ HAS_PWSH, POWERSHELL_EXE, WINDOWS_POWERSHELL, + make_python3_path_shim, ) @@ -157,7 +158,7 @@ def test_manifest_validates(self): m = ExtensionManifest(EXT_DIR / "extension.yml") assert m.id == "github" - assert m.version == "1.0.6" + assert m.version == "1.0.7" assert [c["name"] for c in m.commands] == [COMMAND_NAME] def test_manifest_does_not_restore_legacy_alias(self): @@ -1071,6 +1072,57 @@ def test_errors_when_tasks_md_is_missing(self, tmp_path: Path): assert result.returncode == 1 assert "tasks.md not found" in result.stderr + @pytest.mark.parametrize("ensure_ascii", [False, True], ids=["raw-utf8", "ascii-escaped"]) + @pytest.mark.parametrize( + ("code_page", "name"), + [("cp1252", "001-café"), ("gbk", "001-功能")], + ids=["cp1252-latin", "gbk-cjk"], + ) + def test_feature_json_survives_a_legacy_stdout( + self, tmp_path: Path, code_page: str, name: str, ensure_ascii: bool + ): + """Regression: the python3 feature.json parser printed in the code page. + + Without jq, ``read_feature_json_feature_directory`` parses feature.json + with python3, whose piped stdout uses the ANSI code page on Windows + (cp1252, or GBK on Simplified Chinese systems), so the directory + reached bash in that encoding instead of UTF-8. A raw UTF-8 + feature.json also checks that the parser reads the file as UTF-8: + without ``encoding='utf-8'``, Windows decodes it in the code page too. + """ + project = tmp_path / "project" + feature = project / "specs" / name + feature.mkdir(parents=True) + (project / ".specify").mkdir() + (project / ".specify" / "feature.json").write_text( + json.dumps({"feature_directory": f"specs/{name}"}, ensure_ascii=ensure_ascii), + encoding="utf-8", + ) + (feature / "plan.md").write_text("# Plan\n", encoding="utf-8") + (feature / "tasks.md").write_text("- [ ] T001 Task\n", encoding="utf-8") + + # A working python3 and a failing jq, so python3 parses feature.json + # even on runners that have jq. + shim_dir = make_python3_path_shim(tmp_path / "bin") + jq_stub = shim_dir / "jq" + jq_stub.write_text("#!/bin/sh\nexit 1\n", encoding="utf-8", newline="\n") + jq_stub.chmod(0o755) + env = { + **_resolver_env(), + "PATH": f"{shim_dir}{os.pathsep}{os.environ.get('PATH', '')}", + "PYTHONIOENCODING": code_page, + } + result = subprocess.run( + ["bash", str(SH_SCRIPT), "--json"], + cwd=project, + capture_output=True, + env=env, + ) + + assert result.returncode == 0, result.stderr.decode("utf-8", "replace") + payload = json.loads(result.stdout.decode("utf-8")) + assert Path(payload["FEATURE_DIR"]).name == name + # -- Override contract, across all three twins -------------------------------- diff --git a/tests/test_setup_plan_feature_json.py b/tests/test_setup_plan_feature_json.py index ded0c8a5f9..ce22ca3550 100644 --- a/tests/test_setup_plan_feature_json.py +++ b/tests/test_setup_plan_feature_json.py @@ -9,6 +9,7 @@ import pytest from tests.conftest import requires_bash +from tests.parity_helpers import make_python3_path_shim PROJECT_ROOT = Path(__file__).resolve().parent.parent COMMON_SH = PROJECT_ROOT / "scripts" / "bash" / "common.sh" @@ -191,6 +192,67 @@ def test_setup_plan_survives_broken_python3_stub(plan_repo: Path) -> None: assert (feat / "plan.md").is_file() +@requires_bash +@pytest.mark.parametrize("ensure_ascii", [False, True], ids=["raw-utf8", "ascii-escaped"]) +@pytest.mark.parametrize( + ("code_page", "name"), + [("cp1252", "001-café"), ("gbk", "001-添加用户")], + ids=["cp1252-latin", "gbk-cjk"], +) +def test_setup_plan_reads_non_ascii_feature_json_through_a_legacy_stdout( + plan_repo: Path, tmp_path: Path, code_page: str, name: str, ensure_ascii: bool +) -> None: + """The python3 feature.json parser must hand bash UTF-8, not the code page. + + On Windows a piped stdout uses the ANSI code page (cp1252 on Western + systems, GBK on Simplified Chinese ones) rather than UTF-8. Without jq, + printing the parsed path re-encoded it in that code page ("é" became the + single byte 0xE9), so setup-plan.sh reported, and on some platforms + created, a different directory from the one feature.json names. + """ + subprocess.run( + ["git", "checkout", "-q", "-b", "feature/my-feature-branch"], + cwd=plan_repo, + check=True, + ) + feat = plan_repo / "specs" / name + feat.mkdir(parents=True) + (feat / "spec.md").write_text("# spec\n", encoding="utf-8") + (plan_repo / ".specify" / "feature.json").write_text( + json.dumps({"feature_directory": f"specs/{name}"}, ensure_ascii=ensure_ascii), + encoding="utf-8", + ) + + # A working python3 (Windows venvs only ship python.exe) and a failing jq, + # so feature.json is parsed by python3 even on runners that have jq. + stub_dir = make_python3_path_shim(tmp_path / "bin") + jq_stub = stub_dir / "jq" + jq_stub.write_text("#!/bin/sh\nexit 1\n", encoding="utf-8", newline="\n") + jq_stub.chmod(0o755) + env = _clean_env() + env["PATH"] = f"{stub_dir}{os.pathsep}{env.get('PATH', '')}" + env["PYTHONIOENCODING"] = code_page + + script = plan_repo / ".specify" / "scripts" / "bash" / "setup-plan.sh" + result = subprocess.run( + ["bash", str(script)], + cwd=plan_repo, + capture_output=True, + check=False, + env=env, + ) + assert result.returncode == 0, result.stderr.decode("utf-8", "replace") + assert (feat / "plan.md").is_file() + assert [p.name for p in (plan_repo / "specs").iterdir()] == [name] + # The reported paths must be UTF-8 too: agents read them from this output. + feature_dir_line = next( + line + for line in result.stdout.decode("utf-8").splitlines() + if line.startswith("FEATURE_DIR: ") + ) + assert feature_dir_line.endswith(f"/specs/{name}") + + @requires_bash def test_setup_plan_numbered_branch_works_with_feature_json( plan_repo: Path,