Skip to content

fix: suppress stderr in get_current_branch.cjs when outside a git repo#19499

Merged
pelikhan merged 2 commits intomainfrom
copilot/fix-stderr-leak-git-repo-check
Mar 4, 2026
Merged

fix: suppress stderr in get_current_branch.cjs when outside a git repo#19499
pelikhan merged 2 commits intomainfrom
copilot/fix-stderr-leak-git-repo-check

Conversation

Copy link
Contributor

Copilot AI commented Mar 4, 2026

get_current_branch.cjs leaks fatal: not a git repository to stderr on every invocation from a non-git working directory, because execSync inherits stderr by default. This noise appears specifically in failure-path logs (called from handle_agent_failure.cjs), where clarity matters most.

Change

Added stdio: ["pipe", "pipe", "pipe"] to the execSync call so stderr is captured rather than inherited:

const branch = execSync("git rev-parse --abbrev-ref HEAD", {
  encoding: "utf8",
  cwd: cwd,
  stdio: ["pipe", "pipe", "pipe"],
}).trim();

The existing try/catch already handles the error and falls back to GITHUB_HEAD_REF/GITHUB_REF_NAME correctly — this makes that fallback silent.

Original prompt

This section details on the original issue you should resolve

<issue_title>get_current_branch.cjs leaks stderr when not in a git repository</issue_title>
<issue_description>## What happens

get_current_branch.cjs runs git rev-parse --abbrev-ref HEAD via execSync without suppressing stderr. When called in a context where the working directory isn't a git repository (or the .git directory is inaccessible), git prints fatal: not a git repository (or any of the parent directories): .git to stderr before the function catches the error and falls back to GITHUB_REF_NAME.

The fallback works correctly — the function returns the right branch name. But the stderr output leaks into the Actions log, adding confusing noise to failure reports.

This function is called in the failure reporting path (handle_agent_failure.cjs:742), so the stderr noise appears specifically in runs that are already failing — the worst time to add confusing log output.

What should happen

The execSync call should suppress stderr so that the fallback is silent. The function already has a try/catch; the issue is purely that stderr is inherited from the parent process rather than piped.

Where in the code

All references are to main at 99b2107.

The leaky call:

  • get_current_branch.cjs:18-23:
    const branch = execSync("git rev-parse --abbrev-ref HEAD", {
      encoding: "utf8",
      cwd: cwd,
    }).trim();
    No stdio option — defaults to "pipe" for stdout but inherits stderr from the parent process.

Call site in failure path:

  • handle_agent_failure.cjs:18const { getCurrentBranch } = require("./get_current_branch.cjs");
  • handle_agent_failure.cjs:742const currentBranch = getCurrentBranch();

Evidence

Local reproduction:

  • Set GITHUB_WORKSPACE=/tmp and GITHUB_REF_NAME=main
  • Ran the function — stderr printed fatal: not a git repository (or any of the parent directories): .git
  • Return value was correctly main (from GITHUB_REF_NAME fallback)

Source-level verification (2026-03-03):

  • Confirmed execSync at line 19 has no stdio configuration
  • Node.js execSync defaults: stdout is "pipe" (captured), stderr is inherited (printed to console)
  • The try/catch at lines 18/24 catches the thrown error but stderr has already been written

Proposed fix

Add stdio: ["pipe", "pipe", "pipe"] (or stdio: "pipe") to the execSync options:

const branch = execSync("git rev-parse --abbrev-ref HEAD", {
  encoding: "utf8",
  cwd: cwd,
  stdio: ["pipe", "pipe", "pipe"],
}).trim();

This suppresses stderr while still capturing stdout. The existing try/catch handles the error path.

Impact

Frequency: Every failure-path invocation where GITHUB_WORKSPACE doesn't point to a valid git repository.
Cost: Low — purely cosmetic noise, but it appears in failure logs where clarity matters most. One-line fix.</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix stderr leakage in get_current_branch.cjs when not in git repository fix: suppress stderr in get_current_branch.cjs when outside a git repo Mar 4, 2026
@pelikhan pelikhan marked this pull request as ready for review March 4, 2026 04:29
Copilot AI review requested due to automatic review settings March 4, 2026 04:29
@pelikhan pelikhan merged commit f0dee4d into main Mar 4, 2026
1 check passed
@pelikhan pelikhan deleted the copilot/fix-stderr-leak-git-repo-check branch March 4, 2026 04:29
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Suppresses noisy git stderr output from getCurrentBranch() when invoked outside a git repository, improving clarity in failure-path logs while keeping the existing fallback behavior.

Changes:

  • Pipe stderr for the git rev-parse --abbrev-ref HEAD execSync call to prevent fatal: not a git repository from leaking into logs.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment on lines 18 to 22
const branch = execSync("git rev-parse --abbrev-ref HEAD", {
encoding: "utf8",
cwd: cwd,
stdio: ["pipe", "pipe", "pipe"],
}).trim();
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a regression test to ensure getCurrentBranch() does not leak the underlying git failure message to stderr when cwd is not a git repo (the behavior this change is meant to fix). The repo already has get_current_branch.test.cjs, so this can be covered by spawning a node process with GITHUB_WORKSPACE set to a non-git directory and asserting the child process stderr does not contain fatal: not a git repository (while the function still returns the env fallback or throws as expected).

Copilot uses AI. Check for mistakes.
const branch = execSync("git rev-parse --abbrev-ref HEAD", {
encoding: "utf8",
cwd: cwd,
stdio: ["pipe", "pipe", "pipe"],
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency with other child_process sync calls in this repo, consider using stdio: "pipe" instead of the explicit ['pipe','pipe','pipe'] array (e.g. actions/setup/js/merge_remote_agent_github_folder.cjs:191 and actions/setup/js/push_repo_memory.cjs:130 use the string form). This is functionally equivalent here and a bit easier to scan.

Suggested change
stdio: ["pipe", "pipe", "pipe"],
stdio: "pipe",

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

get_current_branch.cjs leaks stderr when not in a git repository

3 participants