From de3068d5606b0b3703129733bfd194ce44e3e8b0 Mon Sep 17 00:00:00 2001 From: Noor-ul-ain001 Date: Mon, 29 Jun 2026 19:36:09 +0500 Subject: [PATCH 1/5] fix: fall back to feature dir basename for empty CURRENT_BRANCH (#3026) When a feature is resolved via SPECIFY_FEATURE_DIRECTORY or .specify/feature.json without SPECIFY_FEATURE set, get_current_branch() returns empty, so get_feature_paths / Get-FeaturePathsEnv emitted CURRENT_BRANCH= (empty) even though the feature directory was resolvable. Downstream scripts and agents that expect a non-empty identifier got misleading output. Fall back to the basename of the resolved feature directory when the branch is empty, in both the bash (`${feature_dir##*/}`) and PowerShell (`Split-Path -Leaf`) resolvers. An explicit SPECIFY_FEATURE still takes precedence, so this only fills the previously-empty case. Add bash + PowerShell regression tests: the basename fallback fires when SPECIFY_FEATURE is unset, and an explicit SPECIFY_FEATURE still overrides it. Co-Authored-By: Claude Opus 4.8 (1M context) --- scripts/bash/common.sh | 8 +++ scripts/powershell/common.ps1 | 8 +++ tests/test_check_prerequisites_paths_only.py | 66 ++++++++++++++++++++ 3 files changed, 82 insertions(+) diff --git a/scripts/bash/common.sh b/scripts/bash/common.sh index 70ab89b013..50f8c4f2d2 100644 --- a/scripts/bash/common.sh +++ b/scripts/bash/common.sh @@ -186,6 +186,14 @@ get_feature_paths() { return 1 fi + # When no branch context exists (no SPECIFY_FEATURE, feature resolved via + # SPECIFY_FEATURE_DIRECTORY or feature.json), fall back to the feature + # directory basename so CURRENT_BRANCH is a usable identifier rather than + # an empty, misleading value (issue #3026). + if [[ -z "$current_branch" ]]; then + current_branch="${feature_dir##*/}" + fi + # Use printf '%q' to safely quote values, preventing shell injection # via crafted branch names or paths containing special characters printf 'REPO_ROOT=%q\n' "$repo_root" diff --git a/scripts/powershell/common.ps1 b/scripts/powershell/common.ps1 index f56fc26577..7bd434497b 100644 --- a/scripts/powershell/common.ps1 +++ b/scripts/powershell/common.ps1 @@ -182,6 +182,14 @@ function Get-FeaturePathsEnv { exit 1 } + # When no branch context exists (no SPECIFY_FEATURE, feature resolved via + # SPECIFY_FEATURE_DIRECTORY or feature.json), fall back to the feature + # directory basename so CURRENT_BRANCH is a usable identifier rather than + # an empty, misleading value (issue #3026). + if (-not $currentBranch) { + $currentBranch = Split-Path -Leaf $featureDir + } + [PSCustomObject]@{ REPO_ROOT = $repoRoot CURRENT_BRANCH = $currentBranch diff --git a/tests/test_check_prerequisites_paths_only.py b/tests/test_check_prerequisites_paths_only.py index c8c2926abc..e207639efa 100644 --- a/tests/test_check_prerequisites_paths_only.py +++ b/tests/test_check_prerequisites_paths_only.py @@ -121,6 +121,50 @@ def test_paths_only_succeeds_on_spec_branch(prereq_repo: Path) -> None: assert "001-my-feature" in data.get("BRANCH", "") +@requires_bash +def test_current_branch_falls_back_to_feature_dir_basename(prereq_repo: Path) -> None: + """When no branch context exists (feature resolved from feature.json, + SPECIFY_FEATURE unset), BRANCH must fall back to the feature directory + basename instead of being emitted empty (#3026).""" + feat = prereq_repo / "specs" / "001-my-feature" + feat.mkdir(parents=True, exist_ok=True) + _write_feature_json(prereq_repo) + script = prereq_repo / ".specify" / "scripts" / "bash" / "check-prerequisites.sh" + result = subprocess.run( + ["bash", str(script), "--json", "--paths-only"], + cwd=prereq_repo, + capture_output=True, + text=True, + check=False, + env=_clean_env(), # no SPECIFY_FEATURE + ) + assert result.returncode == 0, result.stderr + data = json.loads(result.stdout) + assert data["BRANCH"] == "001-my-feature" + + +@requires_bash +def test_explicit_feature_still_overrides_basename(prereq_repo: Path) -> None: + """SPECIFY_FEATURE remains authoritative over the basename fallback (#3026).""" + feat = prereq_repo / "specs" / "001-my-feature" + feat.mkdir(parents=True, exist_ok=True) + _write_feature_json(prereq_repo) + script = prereq_repo / ".specify" / "scripts" / "bash" / "check-prerequisites.sh" + env = _clean_env() + env["SPECIFY_FEATURE"] = "my-explicit-branch" + result = subprocess.run( + ["bash", str(script), "--json", "--paths-only"], + cwd=prereq_repo, + capture_output=True, + text=True, + check=False, + env=env, + ) + assert result.returncode == 0, result.stderr + data = json.loads(result.stdout) + assert data["BRANCH"] == "my-explicit-branch" + + @requires_bash def test_paths_only_text_mode_on_non_spec_branch(prereq_repo: Path) -> None: """--paths-only without --json must return text paths from feature.json.""" @@ -189,6 +233,28 @@ def test_ps_paths_only_succeeds_on_non_spec_branch(prereq_repo: Path) -> None: assert "FEATURE_DIR" in data +@pytest.mark.skipif(not (HAS_PWSH or _WINDOWS_POWERSHELL), reason="no PowerShell available") +def test_ps_current_branch_falls_back_to_feature_dir_basename(prereq_repo: Path) -> None: + """With no SPECIFY_FEATURE, BRANCH falls back to the feature directory + basename instead of being emitted empty (#3026).""" + feat = prereq_repo / "specs" / "001-my-feature" + feat.mkdir(parents=True, exist_ok=True) + _write_feature_json(prereq_repo) + script = prereq_repo / ".specify" / "scripts" / "powershell" / "check-prerequisites.ps1" + exe = "pwsh" if HAS_PWSH else _WINDOWS_POWERSHELL + result = subprocess.run( + [exe, "-NoProfile", "-File", str(script), "-Json", "-PathsOnly"], + cwd=prereq_repo, + capture_output=True, + text=True, + check=False, + env=_clean_env(), # no SPECIFY_FEATURE + ) + assert result.returncode == 0, result.stderr + data = json.loads(result.stdout) + assert data["BRANCH"] == "001-my-feature" + + @pytest.mark.skipif(not (HAS_PWSH or _WINDOWS_POWERSHELL), reason="no PowerShell available") def test_ps_paths_only_succeeds_on_spec_branch(prereq_repo: Path) -> None: """-PathsOnly must also work when feature.json and SPECIFY_FEATURE agree.""" From 4f92ee7d40a3554e3946c6388a32ef33c12b533d Mon Sep 17 00:00:00 2001 From: Noor ul ain Date: Tue, 30 Jun 2026 00:40:28 +0500 Subject: [PATCH 2/5] Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- scripts/powershell/common.ps1 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/powershell/common.ps1 b/scripts/powershell/common.ps1 index 7bd434497b..1c91c0119f 100644 --- a/scripts/powershell/common.ps1 +++ b/scripts/powershell/common.ps1 @@ -187,7 +187,8 @@ function Get-FeaturePathsEnv { # directory basename so CURRENT_BRANCH is a usable identifier rather than # an empty, misleading value (issue #3026). if (-not $currentBranch) { - $currentBranch = Split-Path -Leaf $featureDir + $featureDirTrimmed = [System.IO.Path]::TrimEndingDirectorySeparator($featureDir) + $currentBranch = Split-Path -Leaf $featureDirTrimmed } [PSCustomObject]@{ From c5f5c8805808a32dd13c5b9d705994d956084737 Mon Sep 17 00:00:00 2001 From: Noor ul ain Date: Tue, 30 Jun 2026 05:17:56 +0500 Subject: [PATCH 3/5] Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- scripts/bash/common.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/bash/common.sh b/scripts/bash/common.sh index 50f8c4f2d2..88d80327a2 100644 --- a/scripts/bash/common.sh +++ b/scripts/bash/common.sh @@ -191,7 +191,8 @@ get_feature_paths() { # directory basename so CURRENT_BRANCH is a usable identifier rather than # an empty, misleading value (issue #3026). if [[ -z "$current_branch" ]]; then - current_branch="${feature_dir##*/}" + local feature_dir_trimmed="${feature_dir%/}" + current_branch="${feature_dir_trimmed##*/}" fi # Use printf '%q' to safely quote values, preventing shell injection From 534a0ec48ccd874b29b50dcb36e32b5f46c71345 Mon Sep 17 00:00:00 2001 From: Noor ul ain Date: Tue, 30 Jun 2026 17:15:46 +0500 Subject: [PATCH 4/5] Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- tests/test_check_prerequisites_paths_only.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/tests/test_check_prerequisites_paths_only.py b/tests/test_check_prerequisites_paths_only.py index e207639efa..129c3a474a 100644 --- a/tests/test_check_prerequisites_paths_only.py +++ b/tests/test_check_prerequisites_paths_only.py @@ -234,12 +234,18 @@ def test_ps_paths_only_succeeds_on_non_spec_branch(prereq_repo: Path) -> None: @pytest.mark.skipif(not (HAS_PWSH or _WINDOWS_POWERSHELL), reason="no PowerShell available") -def test_ps_current_branch_falls_back_to_feature_dir_basename(prereq_repo: Path) -> None: +@pytest.mark.parametrize("use_env_var", [False, True], ids=["feature_json", "env_var"]) +def test_ps_current_branch_falls_back_to_feature_dir_basename(prereq_repo: Path, use_env_var: bool) -> None: """With no SPECIFY_FEATURE, BRANCH falls back to the feature directory - basename instead of being emitted empty (#3026).""" + basename (from feature.json or SPECIFY_FEATURE_DIRECTORY) instead of being + emitted empty (#3026).""" feat = prereq_repo / "specs" / "001-my-feature" feat.mkdir(parents=True, exist_ok=True) - _write_feature_json(prereq_repo) + env = _clean_env() # no SPECIFY_FEATURE + if use_env_var: + env["SPECIFY_FEATURE_DIRECTORY"] = "specs/001-my-feature" + else: + _write_feature_json(prereq_repo) script = prereq_repo / ".specify" / "scripts" / "powershell" / "check-prerequisites.ps1" exe = "pwsh" if HAS_PWSH else _WINDOWS_POWERSHELL result = subprocess.run( @@ -248,13 +254,12 @@ def test_ps_current_branch_falls_back_to_feature_dir_basename(prereq_repo: Path) capture_output=True, text=True, check=False, - env=_clean_env(), # no SPECIFY_FEATURE + env=env, ) assert result.returncode == 0, result.stderr data = json.loads(result.stdout) assert data["BRANCH"] == "001-my-feature" - @pytest.mark.skipif(not (HAS_PWSH or _WINDOWS_POWERSHELL), reason="no PowerShell available") def test_ps_paths_only_succeeds_on_spec_branch(prereq_repo: Path) -> None: """-PathsOnly must also work when feature.json and SPECIFY_FEATURE agree.""" From 380afbf2c467f5b2d2f08e5e9ae291a0fb022307 Mon Sep 17 00:00:00 2001 From: Noor ul ain Date: Tue, 30 Jun 2026 18:40:57 +0500 Subject: [PATCH 5/5] Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- tests/test_check_prerequisites_paths_only.py | 23 +++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/tests/test_check_prerequisites_paths_only.py b/tests/test_check_prerequisites_paths_only.py index 129c3a474a..ad3d0a3e58 100644 --- a/tests/test_check_prerequisites_paths_only.py +++ b/tests/test_check_prerequisites_paths_only.py @@ -234,14 +234,26 @@ def test_ps_paths_only_succeeds_on_non_spec_branch(prereq_repo: Path) -> None: @pytest.mark.skipif(not (HAS_PWSH or _WINDOWS_POWERSHELL), reason="no PowerShell available") -@pytest.mark.parametrize("use_env_var", [False, True], ids=["feature_json", "env_var"]) -def test_ps_current_branch_falls_back_to_feature_dir_basename(prereq_repo: Path, use_env_var: bool) -> None: +@pytest.mark.parametrize( + ("use_env_var", "specify_feature", "expected_branch"), + [ + (False, None, "001-my-feature"), + (True, None, "001-my-feature"), + (False, "my-explicit-branch", "my-explicit-branch"), + ], + ids=["feature_json", "env_var", "explicit_feature"], +) +def test_ps_current_branch_falls_back_to_feature_dir_basename( + prereq_repo: Path, use_env_var: bool, specify_feature: str | None, expected_branch: str +) -> None: """With no SPECIFY_FEATURE, BRANCH falls back to the feature directory basename (from feature.json or SPECIFY_FEATURE_DIRECTORY) instead of being - emitted empty (#3026).""" + emitted empty. If SPECIFY_FEATURE is set, it remains authoritative (#3026).""" feat = prereq_repo / "specs" / "001-my-feature" feat.mkdir(parents=True, exist_ok=True) - env = _clean_env() # no SPECIFY_FEATURE + env = _clean_env() + if specify_feature: + env["SPECIFY_FEATURE"] = specify_feature if use_env_var: env["SPECIFY_FEATURE_DIRECTORY"] = "specs/001-my-feature" else: @@ -258,8 +270,7 @@ def test_ps_current_branch_falls_back_to_feature_dir_basename(prereq_repo: Path, ) assert result.returncode == 0, result.stderr data = json.loads(result.stdout) - assert data["BRANCH"] == "001-my-feature" - + assert data["BRANCH"] == expected_branch @pytest.mark.skipif(not (HAS_PWSH or _WINDOWS_POWERSHELL), reason="no PowerShell available") def test_ps_paths_only_succeeds_on_spec_branch(prereq_repo: Path) -> None: """-PathsOnly must also work when feature.json and SPECIFY_FEATURE agree."""