Skip to content

fix(core): normalize proxy-agent esbuild interop for environment proxy resolution - #29401

Merged
DavidAPierce merged 12 commits into
google-gemini:mainfrom
diegogodinezr:fix/proxy-agent-bundling-interop
Sep 21, 2026
Merged

DavidAPierce merged 12 commits into
google-gemini:mainfrom
diegogodinezr:fix/proxy-agent-bundling-interop

Conversation

@diegogodinezr

Copy link
Copy Markdown
Contributor

Summary

Normalizes the CJS/ESM interop handling and export structures for https-proxy-agent and http-proxy-agent within the esbuild bundle pipeline to ensure consistent constructor resolution across static, dynamic, named, and default imports.

Details

  • Patch Normalization: Updated packages/cli/src/patches/https-proxy-agent.ts and http-proxy-agent.ts to safely normalize constructor resolution (raw.HttpsProxyAgent || raw.default || raw), export both named and default constructors, and attach self-referential properties on the constructor function (HttpsProxyAgent.HttpsProxyAgent and HttpsProxyAgent.default) for full cross-module compatibility.
  • Shared Aliases: Moved https-proxy-agent and http-proxy-agent patch aliases into commonAliases in esbuild.config.js so that all build configurations (cliConfig, workerConfig, a2aServerConfig) resolve proxy modules consistently.
  • Test Coverage: Added tests in scripts/tests/proxy-agent-bundle.test.ts to validate named and default export resolution, constructability, and instantiation with HTTP_PROXY / HTTPS_PROXY environment variables set.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update

Related Issues

Closes #26533

How to Validate

  1. Run bundle proxy-agent shape tests:
    npx vitest run scripts/tests/proxy-agent-bundle.test.ts
  2. Run full scripts test suite:
    npm run test:scripts
  3. Run sea-launch test suite:
    npm run test:sea-launch
  4. Run core package unit tests:
    npm test -w @google/gemini-cli-core -- src/core/contentGenerator.test.ts
  5. Run bundle and typecheck/lint:
    npm run bundle
    npm run typecheck
    npm run lint

Pre-Merge Checklist

  • Updated relevant documentation and README (if needed)
  • Added/updated tests (if needed)
  • Noted breaking changes (if any)
  • Validated on required platforms/methods:
    • MacOS
      • npm run
      • npx
      • Docker
      • Podman
      • Seatbelt
    • Windows
      • npm run
      • npx
      • Docker
    • Linux
      • npm run
      • npx
      • Docker

…y resolution

Normalizes the CJS/ESM interop handling and export structures for https-proxy-agent and http-proxy-agent within the esbuild bundle pipeline.

- Export both named and default constructors from the patch shims.
- Attach self-referential constructor properties for cross-module compatibility.
- Share proxy-agent patch aliases across all bundle configurations in commonAliases.
- Add test coverage in scripts/tests/proxy-agent-bundle.test.ts for proxy resolution.

Closes google-gemini#26533
@diegogodinezr
diegogodinezr requested a review from a team as a code owner September 18, 2026 20:18
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses issues with CJS/ESM interop for proxy agents within the esbuild bundle pipeline. By normalizing how these modules are exported and consumed, it ensures that developers can reliably use both named and default imports without encountering runtime resolution errors. The changes also centralize the patch configuration to maintain consistency across various build targets.

Highlights

  • Proxy Agent Interop Normalization: Updated the https-proxy-agent and http-proxy-agent patches to ensure consistent constructor resolution across different import styles (static, dynamic, named, and default) by normalizing the export structure.
  • Centralized Build Aliases: Moved proxy agent patch aliases into commonAliases within esbuild.config.js to ensure consistent module resolution across all build configurations.
  • Enhanced Test Coverage: Expanded scripts/tests/proxy-agent-bundle.test.ts to include comprehensive validation for named/default exports, constructability, and environment variable resolution.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@github-actions github-actions Bot added the size/m A medium sized PR label Sep 18, 2026
@github-actions

github-actions Bot commented Sep 18, 2026 •

Copy link
Copy Markdown

📊 PR Size: size/L

  • Lines changed: 256
  • Additions: +232
  • Deletions: -24
  • Files changed: 4

@diegogodinezr

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the http-proxy-agent and https-proxy-agent patches to improve CJS/ESM interop, moves their aliases to common aliases in esbuild.config.js, and expands the test suite to verify static/dynamic imports and proxy environment variables. The review feedback identifies a potential runtime TypeError in strict mode caused by directly mutating the imported constructor functions. To prevent crashes, it is recommended to use Object.defineProperty wrapped in a try-catch block to safely assign the self-referential properties.

Comment thread packages/cli/src/patches/http-proxy-agent.ts Outdated
Comment thread packages/cli/src/patches/https-proxy-agent.ts Outdated

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the proxy agent aliases in esbuild.config.js to be shared under commonAliases, updates the http-proxy-agent and https-proxy-agent patches to support robust ESM/CJS interop, and expands the test suite to verify static/dynamic imports and environment-based proxy instantiation. The review feedback suggests further strengthening the interop resolution in both patches to handle cases where mod.default is an object containing the named exports rather than the constructor function itself.

Comment thread packages/cli/src/patches/http-proxy-agent.ts Outdated
Comment thread packages/cli/src/patches/https-proxy-agent.ts Outdated
@gemini-cli gemini-cli Bot added priority/p1 Important and should be addressed in the near term. area/core Issues related to User Interface, OS Support, Core Functionality labels Sep 18, 2026
…roperty definitions in proxy patches

Address review feedback:
- Handle edge cases where default export is a module namespace containing named proxy agent constructors.
- Use Object.defineProperty with try-catch to defensively define self-referential properties without mutating frozen objects.

Related to google-gemini#26533
@github-actions github-actions Bot added the size/l A large sized PR label Sep 18, 2026
@diegogodinezr

Copy link
Copy Markdown
Contributor Author

Applied requested review updates:

  • Added fallback constructor resolution in packages/cli/src/patches/https-proxy-agent.ts and http-proxy-agent.ts to handle cases where default resolves to a module namespace object containing named constructors.
  • Replaced direct property mutation with Object.defineProperty wrapped in a try-catch block to ensure safe definition without errors if constructors or namespace objects are non-writable/frozen.
  • Re-ran all validation suites (npx vitest run scripts/tests/proxy-agent-bundle.test.ts, npm run bundle, npm run typecheck, npm run lint, and npm test -w @google/gemini-cli-core).

@diegogodinezr

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the proxy agent patches for http-proxy-agent and https-proxy-agent to improve ESM/CJS interop handling during bundling, moving their aliases to commonAliases in esbuild.config.js and adding comprehensive tests. The feedback highlights a potential runtime safety issue in both patches where resolvedCtor is initialized with the raw namespace object, which could lead to a TypeError if none of the interop conditions match. It is recommended to initialize the constructor as undefined and fall back to a dummy class that throws a clear error upon instantiation.

Comment thread packages/cli/src/patches/http-proxy-agent.ts Outdated
Comment thread packages/cli/src/patches/https-proxy-agent.ts Outdated
diegogodinezr and others added 2 commits September 18, 2026 14:56
…be resolved

Address follow-up review feedback:
- Initialize resolvedCtor as undefined rather than prematurely casting rawProxyAgent.
- Fallback to a stub class that throws an informative Error on instantiation if no constructor can be resolved.

Related to google-gemini#26533
@diegogodinezr

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the http-proxy-agent and https-proxy-agent patches to improve ESM/CJS interop and robustly resolve constructors under different import styles. It also updates the esbuild configuration to apply these aliases globally and expands the test suite to verify static/dynamic imports and proxy environment variables. Feedback on the PR suggests using a Proxy object instead of mutating the resolved constructor directly to avoid side-effects and handle potentially frozen constructors more safely.

Comment thread packages/cli/src/patches/http-proxy-agent.ts Outdated
@diegogodinezr

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the http-proxy-agent and https-proxy-agent patches to use a Proxy-based wrapper, ensuring robust CommonJS/ESM interop and self-referential property support. It also moves these aliases to the common configuration in esbuild.config.js and expands the test suite to cover static/dynamic imports and proxy environment variables. The review feedback recommends omitting the receiver argument from Reflect.get in both proxy patches to avoid potential this-binding issues or runtime errors with private static members.

Comment thread packages/cli/src/patches/http-proxy-agent.ts
Comment thread packages/cli/src/patches/https-proxy-agent.ts
@diegogodinezr

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the http-proxy-agent and https-proxy-agent patches to improve CommonJS/ESM interop and adds comprehensive bundling tests. Specifically, it moves the proxy agent aliases to commonAliases in esbuild.config.js, wraps the resolved constructors in a Proxy to support both default and named exports, and expands the test suite to verify these behaviors under various import styles and environment configurations. The feedback recommends passing the receiver argument to Reflect.get in the Proxy get traps of both patch files to ensure the correct this context is preserved during property access.

Comment thread packages/cli/src/patches/http-proxy-agent.ts Outdated
Comment thread packages/cli/src/patches/https-proxy-agent.ts Outdated
diegogodinezr and others added 2 commits September 21, 2026 10:59
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@diegogodinezr

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the 'http-proxy-agent' and 'https-proxy-agent' patches to improve ESM/CJS interop and adds comprehensive bundling and environment variable tests. Feedback highlights a potential issue in both patches where using 'Reflect.get' with the proxy as the receiver can break static getters or methods on the class constructor; the reviewer suggests binding retrieved functions to the target constructor instead.

Comment thread packages/cli/src/patches/http-proxy-agent.ts
Comment thread packages/cli/src/patches/https-proxy-agent.ts
diegogodinezr and others added 2 commits September 21, 2026 11:02
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@diegogodinezr

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the http-proxy-agent and https-proxy-agent patches to improve ESM/CJS interop by dynamically resolving constructors and wrapping them in a Proxy, alongside updating the esbuild configuration and expanding bundle tests. However, the Proxy implementation in both patches binds all retrieved functions to the target, which strips their prototype property and breaks subclassing. It is recommended to use standard receiver-forwarding with Reflect.get(target, prop, receiver) instead.

Comment thread packages/cli/src/patches/http-proxy-agent.ts
Comment thread packages/cli/src/patches/https-proxy-agent.ts
@diegogodinezr

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request updates the esbuild configuration to use custom patches for http-proxy-agent and https-proxy-agent to improve ESM/CJS interop. It also enhances the corresponding test suite to verify both static and dynamic imports, as well as proxy agent instantiation when environment variables are set. There are no review comments to address.

@DavidAPierce
DavidAPierce added this pull request to the merge queue Sep 21, 2026
Merged via the queue into google-gemini:main with commit 04e39e5 Sep 21, 2026
33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/core Issues related to User Interface, OS Support, Core Functionality priority/p1 Important and should be addressed in the near term. size/l A large sized PR size/m A medium sized PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: "HttpsProxyAgent is not a constructor" crash when using Vertex AI with HTTP_PROXY environment variables

2 participants