Repository navigation
feat(tools): add individual CLI flags for Bun monkey patches - #9533
Conversation
…le minimal flags per package
There was a problem hiding this comment.
Code Review
This pull request introduces fine-grained control over the Bun monkey-patch shims by replacing the single global fetch shim flag with individual environment variables and CLI flags for each shim (such as proxyquire, keypair, gaxios, promise-any, crypto-verify, and assert-deep-equal). The proxyquire-bun-shim.cjs and run-test.cjs files have been updated to conditionally apply these shims based on the newly introduced flags, and the test scripts across various package configurations have been updated to pass the specific shim flags they require. There are no review comments, and I have no additional feedback to provide.
| enableProxyquireShim || | ||
| enableKeypairShim || | ||
| enableGaxiosShim | ||
| ) { |
There was a problem hiding this comment.
I really like this particular change because it means we can pull out this entire Module.prototype.require block when all 4 shims have been addressed.
| } | ||
| return patchGaxiosIfPresent(origRequire.apply(this, arguments)); | ||
| }; | ||
| } |
There was a problem hiding this comment.
It looks like this section is wrapped correctly.
| # `-e` enables the script to automatically fail when a command fails | ||
| # `-o pipefail` sets the exit code to the rightmost comment to exit | ||
| # with a non-zero | ||
| # Trigger full unit test suite in CI to verify Bun monkey-patch shim flags across all packages. |
There was a problem hiding this comment.
This ensures we run all the unit tests and nothing breaks.
| function patchGaxiosIfPresent(res) { | ||
| if ( | ||
| enableFetchShim && | ||
| enableGaxiosShim && |
There was a problem hiding this comment.
Looks like this is just a rename
| } | ||
| } catch { | ||
| // Ignore if prototype is not configurable | ||
| } |
There was a problem hiding this comment.
All the code above looks like it is just moved into the enableProxyquireShim if block which is correct.
| } catch { | ||
| // ignore | ||
| } | ||
| } |
There was a problem hiding this comment.
The sections above introduce the enableAssertDeepEqualShim, enableCryptoVerifyShim and enablePromiseAnyShim blocks. Looks like everything is a one to one map
| } | ||
| } | ||
|
|
||
| if (enableBunPluginShim && typeof Bun.plugin === 'function') { |
There was a problem hiding this comment.
Note: This block is part of the enableFetchShim.
quirogas
left a comment
There was a problem hiding this comment.
LGTM, Feel free to merge and address any comments in a follow up. they are mostly Nits.
|
|
||
| const origRequire = Module.prototype.require; | ||
|
|
||
| const enableFetchShim = process.env.BUN_ENABLE_FETCH_SHIM === 'true'; |
There was a problem hiding this comment.
Nit: bin/run-test.cjs accepts both BUN_<NAME>_SHIM=true and BUN_ENABLE_<NAME>_SHIM=true, but this file only checks BUN_ENABLE_<NAME>_SHIM === 'true'. Checking both here too would make the env vars work even when Mocha is run directly without bin/run-test.cjs.
There was a problem hiding this comment.
Addressed in #9553 — updated bin/proxyquire-bun-shim.cjs to check both BUN_ENABLE_SHIM === 'true' and BUN_SHIM === 'true' for all 10 shim flags.
| function createCacheGeneration(initialEntries = {}) { | ||
| const map = Object.assign(Object.create(null), initialEntries); | ||
| let detached = false; | ||
| if (enableProxyquireShim) { |
There was a problem hiding this comment.
Nit: makeProxyquire never reassigns Module._cache = ... (only mockery in handwritten/storage does). In a follow-up, we could gate this createCacheGeneration proxy under enableRequireShim (or remove the unused mockery calls in storage/test/resumable-upload.ts) so the 14 --proxyquire-shim packages don't need a Proxy on require.cache.
There was a problem hiding this comment.
Addressed in #9553 — gated the createCacheGeneration Proxy on Module._cache and require.cache under enableRequireShim instead of enableProxyquireShim.
| @@ -272,6 +292,7 @@ if ( | |||
| // uses the exact V8 message string ('The operation was aborted due to timeout') | |||
| // asserted by core/packages/gcp-metadata unit tests. | |||
There was a problem hiding this comment.
Nit: Update the comment from core/packages/gcp-metadata to core/packages/gaxios, since gaxios is the package that uses --abort-signal-timeout-shim.
There was a problem hiding this comment.
Addressed in #9553 — updated the comment to reference core/packages/gaxios.
| throw err; | ||
| } | ||
| }; | ||
| }; |
There was a problem hiding this comment.
Nit: The body of globalThis.__googleCloudBunFetch (lines 438–708) is missing 2 spaces of indentation after wrapping it in if (enableFetchShim).
There was a problem hiding this comment.
Addressed in #9553 — indented the body of globalThis.__googleCloudBunFetch by 2 spaces.
| } | ||
| } | ||
|
|
||
| if (enableBunPluginShim && typeof Bun.plugin === 'function') { |
There was a problem hiding this comment.
Nit: --bun-plugin-shim (line 741) and --gaxios-shim (line 769) both call globalThis.__googleCloudBunFetch(...a), which is only defined inside if (enableFetchShim) (line 435). If someone enables --gaxios-shim or --bun-plugin-shim without --fetch-shim, it will throw TypeError: globalThis.__googleCloudBunFetch is not a function. We can initialize globalThis.__googleCloudBunFetch when enableFetchShim || enableBunPluginShim || enableGaxiosShim is true, and keep Module._extensions['.js'] (lines 710–727, which only patches teeny-request) inside if (enableFetchShim).
There was a problem hiding this comment.
Addressed in #9553 — globalThis.__googleCloudBunFetch is now initialized when enableFetchShim || enableBunPluginShim || enableGaxiosShim is true, while Module._extensions['.js'] remains gated under enableFetchShim.
| @@ -37,7 +37,7 @@ | |||
| "system-test": "node ../../bin/run-test.cjs build/system-test --timeout 1600000", | |||
| "observability-test": "node ../../bin/run-test.cjs build/observability-test --timeout 1600000", | |||
There was a problem hiding this comment.
Nit: Several tests in build/observability-test use proxyquire (which is why "test" on line 40 includes --proxyquire-shim). Should we also add --proxyquire-shim to "observability-test" here so it works if run standalone under Bun?
There was a problem hiding this comment.
Addressed in #9553 — added --proxyquire-shim to the observability-test script in handwritten/spanner/package.json.
| # `-e` enables the script to automatically fail when a command fails | ||
| # `-o pipefail` sets the exit code to the rightmost comment to exit | ||
| # with a non-zero | ||
| # Trigger full unit test suite in CI to verify Bun monkey-patch shim flags across all packages. |
There was a problem hiding this comment.
Nit: Just a reminder to remove this trigger comment if you don't want the final merge commit to re-run all 5 shards, though keeping it is also fine if we want full-suite CI on merge.
There was a problem hiding this comment.
Addressed in #9553 — removed the temporary full-suite CI trigger comment from ci/run_conditional_tests.sh.
There was a problem hiding this comment.
Note that all the tests will run any time run_conditional_tests.sh is changed so leaving this comment here won't force all the tests to run on all of our PRs. This is just a handy tool to get all the unit tests back to a stable passing state.
|
Sounds good. I added a task to address them in https://b.corp.google.com/issues/571038362. |
🤖 I have created a release *beep* *boop* --- ## [8.3.0](storage-v8.2.1...storage-v8.3.0) (2026-10-07) ### Features * **tools:** Add individual CLI flags for Bun monkey patches ([#9533](#9533)) ([a5eec89](a5eec89)) --- This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please). Co-authored-by: release-please[bot] <55107282+release-please[bot]@users.noreply.github.com>
🤖 I have created a release *beep* *boop* --- <details><summary>google-gax: 6.12.0</summary> ## [6.12.0](google-gax-v6.11.1...google-gax-v6.12.0) (2026-10-07) ### Features * **tools:** Add individual CLI flags for Bun monkey patches ([#9533](#9533)) ([a5eec89](a5eec89)) </details> <details><summary>gaxios: 8.2.0</summary> ## [8.2.0](gaxios-v8.1.0...gaxios-v8.2.0) (2026-10-07) ### Features * **tools:** Add individual CLI flags for Bun monkey patches ([#9533](#9533)) ([a5eec89](a5eec89)) </details> <details><summary>gcp-metadata: 9.1.0</summary> ## [9.1.0](gcp-metadata-v9.0.4...gcp-metadata-v9.1.0) (2026-10-07) ### Features * **tools:** Add individual CLI flags for Bun monkey patches ([#9533](#9533)) ([a5eec89](a5eec89)) </details> <details><summary>google-auth-library: 11.2.0</summary> ## [11.2.0](google-auth-library-v11.1.0...google-auth-library-v11.2.0) (2026-10-07) ### Features * **tools:** Add individual CLI flags for Bun monkey patches ([#9533](#9533)) ([a5eec89](a5eec89)) </details> <details><summary>googleapis-common: 9.2.0</summary> ## [9.2.0](googleapis-common-v9.1.0...googleapis-common-v9.2.0) (2026-10-07) ### Features * **tools:** Add individual CLI flags for Bun monkey patches ([#9533](#9533)) ([a5eec89](a5eec89)) </details> <details><summary>test-utils: 0.2.1</summary> ## [0.2.1](test-utils-v0.2.0...test-utils-v0.2.1) (2026-10-07) ### Bug Fixes * **test-utils:** Make google-test-utils a private workspace package ([#9525](#9525)) ([be25b6f](be25b6f)) </details> <details><summary>bigquery: 9.2.0</summary> ## [9.2.0](bigquery-v9.1.0...bigquery-v9.2.0) (2026-10-07) ### Features * **tools:** Add individual CLI flags for Bun monkey patches ([#9533](#9533)) ([a5eec89](a5eec89)) </details> <details><summary>bigtable: 7.4.0</summary> ## [7.4.0](bigtable-v7.3.0...bigtable-v7.4.0) (2026-10-07) ### Features * **tools:** Add individual CLI flags for Bun monkey patches ([#9533](#9533)) ([a5eec89](a5eec89)) </details> <details><summary>datastore: 11.2.0</summary> ## [11.2.0](datastore-v11.1.0...datastore-v11.2.0) (2026-10-07) ### Features * **tools:** Add individual CLI flags for Bun monkey patches ([#9533](#9533)) ([a5eec89](a5eec89)) </details> <details><summary>error-reporting: 4.2.0</summary> ## [4.2.0](error-reporting-v4.1.0...error-reporting-v4.2.0) (2026-10-07) ### Features * **tools:** Add individual CLI flags for Bun monkey patches ([#9533](#9533)) ([a5eec89](a5eec89)) </details> <details><summary>dns: 6.2.0</summary> ## [6.2.0](dns-v6.1.0...dns-v6.2.0) (2026-10-07) ### Features * **tools:** Add individual CLI flags for Bun monkey patches ([#9533](#9533)) ([a5eec89](a5eec89)) </details> <details><summary>logging: 12.2.0</summary> ## [12.2.0](logging-v12.1.0...logging-v12.2.0) (2026-10-07) ### Features * **tools:** Add individual CLI flags for Bun monkey patches ([#9533](#9533)) ([a5eec89](a5eec89)) </details> <details><summary>logging-bunyan: 6.2.0</summary> ## [6.2.0](logging-bunyan-v6.1.0...logging-bunyan-v6.2.0) (2026-10-07) ### Features * **tools:** Add individual CLI flags for Bun monkey patches ([#9533](#9533)) ([a5eec89](a5eec89)) </details> <details><summary>logging-winston: 7.2.0</summary> ## [7.2.0](logging-winston-v7.1.0...logging-winston-v7.2.0) (2026-10-07) ### Features * **tools:** Add individual CLI flags for Bun monkey patches ([#9533](#9533)) ([a5eec89](a5eec89)) </details> <details><summary>pubsub: 6.2.0</summary> ## [6.2.0](pubsub-v6.1.1...pubsub-v6.2.0) (2026-10-07) ### Features * **tools:** Add individual CLI flags for Bun monkey patches ([#9533](#9533)) ([a5eec89](a5eec89)) ### Bug Fixes * **pubsub:** Avoid invoking callback inside promise handler in getClient_ ([#9544](#9544)) ([752d1d7](752d1d7)) * **pubsub:** Pause underlying pull streams and skip keepalive teardown while paused ([#9521](#9521)) ([f863ecc](f863ecc)) </details> <details><summary>translate: 10.2.0</summary> ## [10.2.0](translate-v10.1.1...translate-v10.2.0) (2026-10-07) ### Features * **tools:** Add individual CLI flags for Bun monkey patches ([#9533](#9533)) ([a5eec89](a5eec89)) </details> --- This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please). Co-authored-by: release-please[bot] <55107282+release-please[bot]@users.noreply.github.com> Co-authored-by: Shivanee Persaud <[email protected]>
## Description Addresses all 7 follow-up review comments from #9533 in the Bun test shim and package test scripts. ## Impact Improves environment variable consistency, avoids unnecessary `Proxy` wrapping of `require.cache` for packages that only use `--proxyquire-shim`, prevents `TypeError: globalThis.__googleCloudBunFetch is not a function` when `--bun-plugin-shim` or `--gaxios-shim` is enabled without `--fetch-shim`, enables standalone execution of Spanner's `observability-test` under Bun, and removes the temporary full-suite CI trigger comment from `ci/run_conditional_tests.sh`. ## Changes - Check both `BUN_ENABLE_<NAME>_SHIM === 'true'` and `BUN_<NAME>_SHIM === 'true'` in `bin/proxyquire-bun-shim.cjs` so the short environment variable names also work when Mocha is invoked directly without `bin/run-test.cjs`. - Gate the `createCacheGeneration` `Proxy` on `Module._cache` and `require.cache` under `enableRequireShim` instead of `enableProxyquireShim`, since only `mockery` in `handwritten/storage` reassigns `Module._cache = ...`. - Update the `AbortSignal.timeout` comment in `bin/proxyquire-bun-shim.cjs` to reference `core/packages/gaxios` instead of `core/packages/gcp-metadata`. - Initialize `globalThis.__googleCloudBunFetch` whenever `enableFetchShim || enableBunPluginShim || enableGaxiosShim` is true (keeping the `teeny-request` `Module._extensions['.js']` hook gated under `enableFetchShim`), and indent the body of `globalThis.__googleCloudBunFetch` by 2 spaces. - Add `--proxyquire-shim` to the `observability-test` script in `handwritten/spanner/package.json`. - Remove the temporary full-suite CI trigger comment from `ci/run_conditional_tests.sh`. ## Testing - Verified in Bun that `BUN_<NAME>_SHIM=true` environment variables activate their respective shims when loading `bin/proxyquire-bun-shim.cjs` directly. - Verified in Bun that `globalThis.__googleCloudBunFetch` is defined when either `BUN_GAXIOS_SHIM=true` or `BUN_PLUGIN_SHIM=true` is enabled without `BUN_FETCH_SHIM=true`. - Verified in Bun that `Module._cache` / `require.cache` generational cache proxying is enabled under `BUN_REQUIRE_SHIM=true`. ## Alternatives - We considered removing `mockery` from `handwritten/storage/test/resumable-upload.ts` instead of gating `createCacheGeneration` under `enableRequireShim`, but gating `createCacheGeneration` under `enableRequireShim` avoids modifying storage test dependencies while still eliminating the `require.cache` `Proxy` for the other 13 `--proxyquire-shim` packages. - Not merging this PR would leave `--bun-plugin-shim` and `--gaxios-shim` dependent on `--fetch-shim` being passed simultaneously and would cause every subsequent PR to run all 5 CI unit test shards due to the trigger comment in `ci/run_conditional_tests.sh`. Fixes b/571038362
Fixes b/570079072
Summary
bin/run-test.cjsandbin/proxyquire-bun-shim.cjsfor each monkey patch in the Bun test shim (all defaulting to disabled when not passed):--fetch-shim(BUN_ENABLE_FETCH_SHIM) —globalThis.__googleCloudBunFetchandteeny-requestCJS extension hook (b/570078219)--bun-plugin-shim(BUN_ENABLE_BUN_PLUGIN_SHIM) —Bun.pluginESM loader hook forgaxios(b/570079742)--gaxios-shim(BUN_ENABLE_GAXIOS_SHIM) —patchGaxiosIfPresentCJS adapter hook (b/570124969)--proxyquire-shim(BUN_ENABLE_PROXYQUIRE_SHIM) —proxyquireCJS loader andModule._cachegenerational cache snapshot shim (b/570076540)--keypair-shim(BUN_ENABLE_KEYPAIR_SHIM) —keypairnativecrypto.generateKeyPairSyncshim (b/570076197)--require-shim(BUN_ENABLE_REQUIRE_SHIM) —Module.prototype.require/Module._loaddelegation shim (b/570123878)--abort-signal-timeout-shim(BUN_ENABLE_ABORT_SIGNAL_TIMEOUT_SHIM) —AbortSignal.timeoutmessage compatibility patch (b/570083851)--promise-any-shim(BUN_ENABLE_PROMISE_ANY_SHIM) —Promise.anyAggregateErrormessage patch (b/570083852)--crypto-verify-shim(BUN_ENABLE_CRYPTO_VERIFY_SHIM) —crypto.Verify.prototype.verifyexplicit-curve SPKI & JWK patch (b/570080140)--assert-deep-equal-shim(BUN_ENABLE_ASSERT_DEEP_EQUAL_SHIM) —assert.deepEqualHeaderscomparison patch (b/570076194)package.jsontestscript for unit tests to pass under Bun.ci/run_conditional_tests.shso CI runs the full unit test suite across all shards.