Skip to content

Fix instruction generation error: Dynamic require of "util" is not supported#52

Merged
digitarald merged 3 commits intomicrosoft:mainfrom
nicolehaugen:copilot/respective-grasshopper
Mar 15, 2026
Merged

Fix instruction generation error: Dynamic require of "util" is not supported#52
digitarald merged 3 commits intomicrosoft:mainfrom
nicolehaugen:copilot/respective-grasshopper

Conversation

@nicolehaugen
Copy link
Copy Markdown
Member

Summary

Repro:

1.) Run: agentrc instructions
2.) Error:
Generating instructions...
[agentrc:copilot] trying override AGENTRC_COPILOT_CLI_PATH=C:\Users\nicolela\AppData\Roaming\Code\User\globalStorage\github.copilot-chat\copilotCli\copilot.bat
[agentrc:copilot] override CLI is compatible
[agentrc:copilot] validating CLI compatibility with C:\Users\nicolela\AppData\Roaming\Code\User\globalStorage\github.copilot-chat\copilotCli\copilot.bat
Error: Failed to generate instructions with Copilot SDK. Ensure the Copilot CLI is installed (copilot --version) and logged in. Dynamic require of "util" is not supported

Fix: Add createRequire banner to tsup.config.ts

Problem

When CJS dependencies (e.g. vscode-jsonrpc) are bundled into an ESM output, calls to require() inside those dependencies fail at runtime with ReferenceError: require is not defined. ESM does not provide a global require — it only exists in CommonJS.

Solution

Add a banner.js entry in the tsup config that injects a shim at the top of the output bundle. This shim uses Node's built-in module package to create a real require function.

Why this works

  • import { createRequire } from "module" — imports the factory from Node's built-in module package.
  • createRequire(import.meta.url) — creates a CJS-compatible require() function anchored to the bundle's file path.
  • Bundled CJS code that calls require("util"), require("path"), etc. now resolves normally.
  • The #!/usr/bin/env node shebang is included so the bundle is directly executable as a CLI.

This pull request updates the Copilot SDK client creation logic to improve compatibility with Windows and better handle cases where the CLI cannot be executed directly. The main changes clarify when to bypass the SDK-managed process and use the external server mode, specifically for .bat/.cmd shims and npx wrappers.

Checklist

  • [NA] Tests added or updated
  • Lint/typecheck pass
  • [NA] Docs updated if needed

nicolehaugen and others added 3 commits March 12, 2026 16:47
…p.config.ts

Co-authored-by: nicolehaugen <10600161+nicolehaugen@users.noreply.github.com>
Co-authored-by: nicolehaugen <10600161+nicolehaugen@users.noreply.github.com>
Copilot AI review requested due to automatic review settings March 12, 2026 23:49
Copy link
Copy Markdown
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

Fixes a runtime error (Dynamic require of "util" is not supported) when CJS dependencies like vscode-jsonrpc are bundled into the ESM output. The bundled CJS code calls require() which doesn't exist in ESM context.

Changes:

  • Adds a createRequire shim in the tsup banner to provide a real require function for bundled CJS dependencies
  • Preserves the shebang line for CLI executability

@digitarald digitarald merged commit ba735d9 into microsoft:main Mar 15, 2026
13 of 14 checks passed
github-actions Bot pushed a commit that referenced this pull request Mar 15, 2026
- Add [Unreleased] section documenting features from PRs #55, #60, #53, #52:
  - instructions --dry-run flag
  - VS Code batch instructions and multi-root workspace support
  - Enhanced .NET/F# detection with framework parsing
  - Windows .bat/.cmd spawn EINVAL fix
  - ESM/CJS createRequire banner fix
  - Model default updated to claude-sonnet-4.6
  - TUI key binding changes (R for readiness, N for nested area)
  - Terminology de-branding
- Fix stale claude-sonnet-4.5 reference in [2.0.0] section

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.

4 participants