fix(vcs): surface git's stderr on GitCommandError #5620
ClosedIAmJSD(GitHub profile, opens in a new tab)wants to merge 7 commits into
main fromrefs/pull/5620/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:S
- packages/contracts/src/git.ts lines 343 to 344OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -330,8 +330,15 @@330330 stdoutLength: Schema.optional(Schema.Number),331331 stderrLength: Schema.optional(Schema.Number),332332 outputLength: Schema.optional(Schema.Number),333+ /**334+ * Tail of git's stderr, truncated and with credential-bearing URLs335+ * redacted. Without it a failed git command is undiagnosable from outside336+ * the process: the lengths above say how much git wrote, never what.337+ */338+ stderrTail: Schema.optional(Schema.String),333339 detail: Schema.String,334340 cause: Schema.optional(Schema.Defect()),335341 }) {336342 override get message(): string {337- return `Git command failed in ${this.operation} (${this.cwd}): ${this.detail}`;343+ const summary = `Git command failed in ${this.operation} (${this.cwd}): ${this.detail}`;344+ return this.stderrTail === undefined ? summary : `${summary}\n${this.stderrTail}`;packages/contracts/src/git.ts lines 333 to 338OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -330,3 +330,9 @@330330 stdoutLength: Schema.optional(Schema.Number),331331 stderrLength: Schema.optional(Schema.Number),332332 outputLength: Schema.optional(Schema.Number),333+ /**334+ * Tail of git's stderr, truncated and with credential-bearing URLs335+ * redacted. Without it a failed git command is undiagnosable from outside336+ * the process: the lengths above say how much git wrote, never what.337+ */338+ stderrTail: Schema.optional(Schema.String),apps/server/src/vcs/GitVcsDriverCore.ts lines 408 to 414OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -396,3 +396,19 @@396396 return refName.length > 0 ? refName : null;397397 }398398399+/**400+ * Tail of git's stderr for `GitCommandError.stderrTail`.401+ *402+ * Truncated from the end, because git puts the reason on its last lines and a403+ * long transfer log in front of it. Credential-bearing URLs are redacted:404+ * git echoes back the remote it was handed, which may embed a token.405+ */406+const GIT_STDERR_TAIL_LIMIT = 2000;407+408+function gitStderrTail(stderr: string): string | undefined {409+ const redacted = stderr.replace(/\/\/([^/@:\s]+):([^@\s]+)@/g, "//$1:***@").trim();410+ if (redacted.length === 0) return undefined;411+ return redacted.length > GIT_STDERR_TAIL_LIMIT412+ ? `…${redacted.slice(-GIT_STDERR_TAIL_LIMIT)}`413+ : redacted;414+}
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:S
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:M
- apps/server/src/vcs/GitVcsDriverCore.ts lines 481 to 489Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -398,0 +407,21 @@407+ * nor the server log says why git exited non-zero.408+ *409+ * Logging it at debug keeps the diagnostic reachable when someone goes looking,410+ * without putting unbounded, possibly secret-bearing output into a value that411+ * crosses RPC, UI, and persistence boundaries.412+ */413+const logFailedGitCommandOutput = (414+ command: {415+ readonly operation: string;416+ readonly cwd: string;417+ readonly args: ReadonlyArray<string>;418+ },419+ exitCode: number | null,420+ stdout: string,421+ stderr: string,422+): Effect.Effect<void> =>423+ Effect.logDebug(424+ `GitVcsDriver.commandFailed: ${command.operation} in ${command.cwd} ` +425+ `exited ${exitCode ?? "null"} (${command.args.length} arguments)`,426+ { stdout, stderr },427+ );
- apps/server/src/vcs/GitVcsDriverCore.ts line 856OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -794,9 +846,11 @@794846 yield* trace2Monitor.flush;795847796848 if (!input.allowNonZeroExit && exitCode !== 0) {849+ yield* logFailedGitCommandOutput(commandInput, exitCode, stdout.text, stderr.text);797850 return yield* new GitCommandError({798851 ...gitCommandContext(commandInput),799852 detail: "Git command exited with a non-zero status.",800853 exitCode,801854 stdoutLength: stdout.text.length,802855 stderrLength: stderr.text.length,856+ cause: stderr.text,
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:M
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/server/src/vcs/GitVcsDriverCore.ts lines 410 to 423Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -396,3 +397,19 @@396397 return refName.length > 0 ? refName : null;397398 }398399400+/**401+ * Normalized category for a failing git command.402+ *403+ * Reuses the vocabulary `VcsProcess.classifyNonZeroExit` already established,404+ * so the reason a command failed survives as a bounded, non-secret-bearing405+ * value. Reading stderr to produce it is safe; keeping stderr is not, so the406+ * text is read here and goes no further.407+ */408+const classifyGitFailure = (stderr: string): VcsProcessExitFailureKind => {409+ const normalized = stderr.toLowerCase();410+ if (411+ normalized.includes("permission denied") ||412+ normalized.includes("authentication failed") ||413+ normalized.includes("could not read username") ||414+ normalized.includes("access rights")415+ ) {
- apps/server/src/vcs/GitVcsDriverCore.ts line 459Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -398,0 +421,21 @@421+ return "command-failed";422+};423+424+/**425+ * Caller-facing `detail` for a failing git command.426+ *427+ * Mirrors `VcsProcessExitError.fromProcessExit`: the classification is what428+ * makes the failure actionable, so it is turned into a fixed sentence rather429+ * than left for the caller to infer from an exit code. Every branch returns a430+ * constant — nothing derived from git's output crosses this boundary.431+ */432+const detailForGitFailure = (failureKind: VcsProcessExitFailureKind, fallback: string): string => {433+ switch (failureKind) {434+ case "authentication":435+ return "Git authentication failed. Check the credentials available to this host for the remote.";436+ case "not-found":437+ return "Git could not find the requested repository, remote, or ref.";438+ case "command-failed":439+ return fallback;440+ }441+};
- apps/server/src/vcs/GitVcsDriverCore.ts line 437Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -398,0 +412,21 @@412+ normalized.includes("could not read username") ||413+ normalized.includes("could not read password") ||414+ normalized.includes("invalid username or password") ||415+ normalized.includes("authentication required") ||416+ // "...make sure you have the correct access rights and the repository exists."417+ normalized.includes("access rights") ||418+ // ssh names the methods it tried: "Permission denied (publickey)." A bare419+ // "permission denied" is a local filesystem error — `git init` into an420+ // unwritable directory reaches this function too, and telling someone to421+ // check their remote credentials would bury the actual problem.422+ normalized.includes("permission denied (") ||423+ // GitHub over https: "remote: Permission to owner/repo.git denied to user."424+ /remote:[^\n]*permission to [^\n]*denied/.test(normalized)425+ ) {426+ return "authentication";427+ }428+ if (normalized.includes("not found") || normalized.includes("does not exist")) {429+ return "not-found";430+ }431+ return "command-failed";432+};apps/server/src/vcs/GitVcsDriverCore.ts line 437Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -398,0 +412,21 @@412+ normalized.includes("could not read username") ||413+ normalized.includes("could not read password") ||414+ normalized.includes("invalid username or password") ||415+ normalized.includes("authentication required") ||416+ // "...make sure you have the correct access rights and the repository exists."417+ normalized.includes("access rights") ||418+ // ssh names the methods it tried: "Permission denied (publickey)." A bare419+ // "permission denied" is a local filesystem error — `git init` into an420+ // unwritable directory reaches this function too, and telling someone to421+ // check their remote credentials would bury the actual problem.422+ normalized.includes("permission denied (") ||423+ // GitHub over https: "remote: Permission to owner/repo.git denied to user."424+ /remote:[^\n]*permission to [^\n]*denied/.test(normalized)425+ ) {426+ return "authentication";427+ }428+ if (normalized.includes("not found") || normalized.includes("does not exist")) {429+ return "not-found";430+ }431+ return "command-failed";432+};
macroscopeapp[bot](GitHub profile, opens in a new tab) approved these changes
t3dotgg(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.


Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID:
d29a81b9-43e8-424c-bbdc-ba6b35fa4b4cYou can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share