Skip to content

feat(tools): add individual CLI flags for Bun monkey patches - #9533

Merged
danieljbruce merged 4 commits into
googleapis:mainfrom
danieljbruce:feat/bun-shim-monkey-patch-flags
Oct 7, 2026
Merged

danieljbruce merged 4 commits into
googleapis:mainfrom
danieljbruce:feat/bun-shim-monkey-patch-flags

Conversation

@danieljbruce

Copy link
Copy Markdown
Contributor

Fixes b/570079072

Summary

  • Introduces an explicit opt-in CLI flag in bin/run-test.cjs and bin/proxyquire-bun-shim.cjs for each monkey patch in the Bun test shim (all defaulting to disabled when not passed):
    • --fetch-shim (BUN_ENABLE_FETCH_SHIM) — globalThis.__googleCloudBunFetch and teeny-request CJS extension hook (b/570078219)
    • --bun-plugin-shim (BUN_ENABLE_BUN_PLUGIN_SHIM) — Bun.plugin ESM loader hook for gaxios (b/570079742)
    • --gaxios-shim (BUN_ENABLE_GAXIOS_SHIM) — patchGaxiosIfPresent CJS adapter hook (b/570124969)
    • --proxyquire-shim (BUN_ENABLE_PROXYQUIRE_SHIM) — proxyquire CJS loader and Module._cache generational cache snapshot shim (b/570076540)
    • --keypair-shim (BUN_ENABLE_KEYPAIR_SHIM) — keypair native crypto.generateKeyPairSync shim (b/570076197)
    • --require-shim (BUN_ENABLE_REQUIRE_SHIM) — Module.prototype.require / Module._load delegation shim (b/570123878)
    • --abort-signal-timeout-shim (BUN_ENABLE_ABORT_SIGNAL_TIMEOUT_SHIM) — AbortSignal.timeout message compatibility patch (b/570083851)
    • --promise-any-shim (BUN_ENABLE_PROMISE_ANY_SHIM) — Promise.any AggregateError message patch (b/570083852)
    • --crypto-verify-shim (BUN_ENABLE_CRYPTO_VERIFY_SHIM) — crypto.Verify.prototype.verify explicit-curve SPKI & JWK patch (b/570080140)
    • --assert-deep-equal-shim (BUN_ENABLE_ASSERT_DEEP_EQUAL_SHIM) — assert.deepEqual Headers comparison patch (b/570076194)
  • Configures the minimal set of flags needed in each package's package.json test script for unit tests to pass under Bun.
  • Touches ci/run_conditional_tests.sh so CI runs the full unit test suite across all shards.

@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 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
) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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));
};
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This ensures we run all the unit tests and nothing breaks.

function patchGaxiosIfPresent(res) {
if (
enableFetchShim &&
enableGaxiosShim &&

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Looks like this is just a rename

}
} catch {
// Ignore if prototype is not configurable
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

All the code above looks like it is just moved into the enableProxyquireShim if block which is correct.

} catch {
// ignore
}
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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') {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Note: This block is part of the enableFetchShim.

@danieljbruce
danieljbruce marked this pull request as ready for review October 6, 2026 15:53
@danieljbruce
danieljbruce requested review from a team as code owners October 6, 2026 15:53
@github-actions
github-actions Bot requested a review from feywind October 6, 2026 15:53
@danieljbruce
danieljbruce requested a review from quirogas October 6, 2026 21:29

@quirogas quirogas 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.

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';

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.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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) {

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.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

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.

Nit: Update the comment from core/packages/gcp-metadata to core/packages/gaxios, since gaxios is the package that uses --abort-signal-timeout-shim.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Addressed in #9553 — updated the comment to reference core/packages/gaxios.

throw err;
}
};
};

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.

Nit: The body of globalThis.__googleCloudBunFetch (lines 438–708) is missing 2 spaces of indentation after wrapping it in if (enableFetchShim).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Addressed in #9553 — indented the body of globalThis.__googleCloudBunFetch by 2 spaces.

}
}

if (enableBunPluginShim && typeof Bun.plugin === 'function') {

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.

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).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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",

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.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

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.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Addressed in #9553 — removed the temporary full-suite CI trigger comment from ci/run_conditional_tests.sh.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

@danieljbruce

Copy link
Copy Markdown
Contributor Author

Sounds good. I added a task to address them in https://b.corp.google.com/issues/571038362.

@danieljbruce
danieljbruce merged commit a5eec89 into googleapis:main Oct 7, 2026
80 checks passed
shivanee-p pushed a commit that referenced this pull request Oct 7, 2026
🤖 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>
shivanee-p added a commit that referenced this pull request Oct 7, 2026
🤖 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]>
quirogas pushed a commit that referenced this pull request Oct 7, 2026
## 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
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.

2 participants