Skip to content

fix(cli): ESM-compatible child_process import + execSync injection in git-staged.ts#79

Merged
Looted merged 2 commits intomodularize_improve_rules_improve_guidancefrom
copilot/sub-pr-78
Mar 17, 2026
Merged

fix(cli): ESM-compatible child_process import + execSync injection in git-staged.ts#79
Looted merged 2 commits intomodularize_improve_rules_improve_guidancefrom
copilot/sub-pr-78

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 17, 2026

import child_process from "node:child_process" is invalid in Node ESM — node:child_process has no default export, throwing at runtime in a "type": "module" package.

Changes

  • Named import: import { execSync } from "node:child_process" replaces the broken default import
  • Dependency injection: getStagedFiles now accepts an optional exec: ExecFn = execSync parameter; all existing callers with no arguments are unaffected
  • Test cleanup: removed require() cache mutation in favor of passing a mock exec directly — getStagedFiles(mockExec)
  • Path sanitization fix: extracted escapePath() helper that escapes backslashes before double-quotes (previously only quotes were escaped, leaving paths with backslashes vulnerable to shell interpretation)
// Before — invalid ESM default import
import child_process from "node:child_process";
child_process.execSync(cmd, { encoding: "utf8" });

// After — named import + injectable exec
import { execSync } from "node:child_process";
export function getStagedFiles(exec: ExecFn = execSync): StagedFile[] {  }

// Test — no more require() cache mutation
const files = getStagedFiles((cmd) => mockOutputs[cmd] ?? "");

📍 Connect Copilot coding agent with Jira, Azure Boards or Linear to delegate work to Copilot in one click without leaving your project management tool.

…taged.ts

Co-authored-by: Looted <6255880+Looted@users.noreply.github.com>
Copilot AI changed the title [WIP] [WIP] Address feedback on modularizing improve rules and guidance fix(cli): ESM-compatible child_process import + execSync injection in git-staged.ts Mar 17, 2026
Copilot AI requested a review from Looted March 17, 2026 17:31
@Looted Looted marked this pull request as ready for review March 17, 2026 18:38
Copilot AI review requested due to automatic review settings March 17, 2026 18:38
@Looted Looted merged commit 2f8d8a0 into modularize_improve_rules_improve_guidance Mar 17, 2026
Copy link
Copy Markdown

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

This PR updates the CLI traceability “git-staged” utilities to be more testable and ESM-compatible by injecting the git execution function instead of relying on mutating the child_process module cache.

Changes:

  • Switch git-staged.ts from a default child_process import to a named execSync import and allow dependency injection of the exec function.
  • Add escapePath() and use it when building git diff / git show commands.
  • Update the Bun test to pass a mock exec function into getStagedFiles().

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
packages/cli/src/traceability/git-staged.ts Refactors git command execution to accept an injected exec function and updates path escaping/quoting.
packages/cli/tests/traceability/traceability.test.ts Updates staged-files test to use exec injection instead of mutating the module cache.

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

Comment on lines +18 to +22
type ExecFn = (cmd: string, opts: { encoding: "utf8" }) => string;

function runGit(cmd: string, exec: ExecFn): string {
try {
return child_process.execSync(cmd, { encoding: "utf8" });
return exec(cmd, { encoding: "utf8" });
}

function escapePath(p: string): string {
return p.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
@Looted Looted deleted the copilot/sub-pr-78 branch March 27, 2026 14:22
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.

3 participants