From e404d9a0daf35909e767716242ed726933b036b9 Mon Sep 17 00:00:00 2001 From: Daniel Bruce Date: Thu, 15 Oct 2026 20:54:46 +0000 Subject: [PATCH] chore: remove proxyquire-bun-shim.cjs and runner flags --- .mocharc.cjs | 3 - bin/proxyquire-bun-shim.cjs | 189 ------------------ bin/run-test.cjs | 39 +--- .../gapic-node-processing/.mocharc.js | 4 - core/packages/gax/.mocharc.js | 5 - core/packages/gaxios/.mocharc.js | 4 - core/packages/gcp-metadata/.mocharc.js | 4 - .../google-auth-library-nodejs/.mocharc.js | 4 - .../nodejs-googleapis-common/.mocharc.js | 4 - core/packages/nodejs-proto-files/.mocharc.js | 4 - .../proto3-json-serializer-nodejs/.mocharc.js | 4 - core/packages/retry-request/.mocharc.js | 4 - core/packages/teeny-request/.mocharc.js | 4 - 13 files changed, 4 insertions(+), 268 deletions(-) delete mode 100644 bin/proxyquire-bun-shim.cjs diff --git a/.mocharc.cjs b/.mocharc.cjs index 2180a5a3fd63..596132144ad4 100644 --- a/.mocharc.cjs +++ b/.mocharc.cjs @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -const path = require('path'); - const config = { "enable-source-maps": true, "throw-deprecation": true, @@ -28,7 +26,6 @@ if (process.env.MOCHA_PARALLEL === 'false' || typeof Bun !== 'undefined') { config.parallel = false; } if (typeof Bun !== 'undefined') { - config.require = [path.resolve(__dirname, 'bin/proxyquire-bun-shim.cjs')]; config.exit = true; } if (process.env.MOCHA_REPORTER) { diff --git a/bin/proxyquire-bun-shim.cjs b/bin/proxyquire-bun-shim.cjs deleted file mode 100644 index c048250f2029..000000000000 --- a/bin/proxyquire-bun-shim.cjs +++ /dev/null @@ -1,189 +0,0 @@ -// Copyright 2026 Google LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// https://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -// A drop-in `proxyquire` replacement for the Bun runtime, built on the two -// seams Bun supports (`Module.prototype.require` dispatch + `require.cache`), -// loaded when running unit tests with `--proxyquire-shim` under Bun so no test -// files need to change. Completely inert when running under Node.js (`typeof Bun === 'undefined'`). -'use strict'; - -const Module = require('module'); -const path = require('path'); - -if ( - typeof Bun !== 'undefined' && - !globalThis.__GOOGLE_CLOUD_BUN_PROXYQUIRE_SHIM__ -) { - const enableProxyquireShim = - process.env.BUN_ENABLE_PROXYQUIRE_SHIM === 'true' || - process.env.BUN_PROXYQUIRE_SHIM === 'true'; - - if (enableProxyquireShim) { - globalThis.__GOOGLE_CLOUD_BUN_PROXYQUIRE_SHIM__ = true; - - const origRequire = Module.prototype.require; - const hasOwn = (o, k) => - o !== null && - typeof o === 'object' && - Object.prototype.hasOwnProperty.call(o, k); - const frames = []; - - // Mirror of proxyquire's Proxyquire.prototype._resolveModule: resolve - // `request` from `baseFile`'s directory; on failure keep bare specifiers as - // they are and fall back to a plain path.resolve for relative ones. - function resolveFrom(baseFile, request) { - try { - return require.resolve(request, {paths: [path.dirname(baseFile)]}); - } catch { - if (request[0] !== '.') return request; - return path.resolve(path.dirname(baseFile), request); - } - } - - function isGlobalStub(stub) { - return hasOwn(stub, '@global') || hasOwn(stub, '@runtimeGlobal'); - } - - function applyStub(self, id, stub, noCallThru) { - if (stub === null) { - const e = new Error("Cannot find module '" + id + "'"); - e.code = 'MODULE_NOT_FOUND'; - throw e; - } - const skip = hasOwn(stub, '@noCallThru') ? stub['@noCallThru'] : noCallThru; - if (!skip) { - let real; - try { - real = origRequire.call(self, id); - } catch { - real = undefined; - } - if (real && (typeof real === 'object' || typeof real === 'function')) { - for (const k of Object.keys(real)) { - if (!(k in stub)) stub[k] = real[k]; - } - } - } - return stub; - } - - // proxyquire's _disableModuleCache: drop just the SUT, restore afterwards. - function disableModuleCache(id) { - const cache = require.cache; - const saved = cache[id]; - delete cache[id]; - return function restore(preserve) { - delete cache[id]; - if (saved && preserve) cache[id] = saved; - }; - } - - // proxyquire's _disableGlobalCache: empty the entire cache so that an - // already-loaded intermediate is re-executed and its require() calls can be - // intercepted. Native (.node) modules are kept. - function disableGlobalCache(sut) { - const cache = require.cache; - const saved = Object.create(null); - for (const id of Object.keys(cache)) { - if (/\.node$/.test(id)) continue; - saved[id] = cache[id]; - delete cache[id]; - } - return function restore(preserve) { - for (const id of Object.keys(cache)) { - if (/\.node$/.test(id)) continue; - delete cache[id]; - } - if (preserve) { - for (const id of Object.keys(saved)) cache[id] = saved[id]; - } else { - for (const id of Object.keys(saved)) { - if (id !== sut) cache[id] = saved[id]; - } - } - }; - } - - Module.prototype.require = function (id) { - if (id === 'proxyquire') { - return makeProxyquire(this); - } - const fr = frames[frames.length - 1]; - if (fr && this && this.filename) { - const isSut = this.filename === fr.sut; - if (isSut || fr.containsGlobal) { - let found = false; - let stub; - if (Object.prototype.hasOwnProperty.call(fr.stubs, id)) { - found = true; - stub = fr.stubs[id]; - } else { - const resolved = resolveFrom(this.filename, id); - if (Object.prototype.hasOwnProperty.call(fr.resolved, resolved)) { - found = true; - stub = fr.resolved[resolved]; - } - } - if (found && (isSut || isGlobalStub(stub))) { - return applyStub(this, id, stub, fr.noCallThru); - } - } - } - return origRequire.apply(this, arguments); - }; - - function makeProxyquire(parent) { - let noCallThru = false; - let preserveCache = true; - const fn = function (request, stubs) { - const sut = Module._resolveFilename(request, parent); - stubs = stubs || {}; - const resolved = {}; - let containsGlobal = false; - for (const k of Object.keys(stubs)) { - if (isGlobalStub(stubs[k])) containsGlobal = true; - resolved[resolveFrom(sut, k)] = stubs[k]; - } - const restore = containsGlobal - ? disableGlobalCache(sut) - : disableModuleCache(sut); - frames.push({sut, stubs, resolved, noCallThru, containsGlobal}); - try { - return origRequire.call(parent, request); - } finally { - frames.pop(); - restore(preserveCache); - } - }; - fn.load = fn; - fn.noCallThru = function () { - noCallThru = true; - return fn; - }; - fn.callThru = function () { - noCallThru = false; - return fn; - }; - fn.noPreserveCache = function () { - preserveCache = false; - return fn; - }; - fn.preserveCache = function () { - preserveCache = true; - return fn; - }; - return fn; - } - } -} diff --git a/bin/run-test.cjs b/bin/run-test.cjs index 7f31b6e3df0d..da7497dd16d4 100755 --- a/bin/run-test.cjs +++ b/bin/run-test.cjs @@ -20,10 +20,9 @@ // coverage and Mocha worker-thread parallelism (`parallel: true`). // - Under Bun (`typeof Bun !== 'undefined'` or `JS_RUNTIME === 'bun'`): // Skips `c8` (which relies on Node's `NODE_V8_COVERAGE`), disables Mocha -// worker-thread parallelism (`--no-parallel`), preloads the Bun `proxyquire` -// shim (`bin/proxyquire-bun-shim.cjs`) when `--proxyquire-shim` is passed, -// and loads `mocha/bin/mocha.js` directly in-process so `#!/usr/bin/env node` -// shebangs never switch back to Node.js. +// worker-thread parallelism (`--no-parallel`), and loads `mocha/bin/mocha.js` +// directly in-process so `#!/usr/bin/env node` shebangs never switch back to +// Node.js. 'use strict'; const {spawnSync} = require('child_process'); @@ -32,26 +31,7 @@ const fs = require('fs'); const rawArgs = process.argv.slice(2); const noC8 = rawArgs.includes('--no-c8'); - -const SHIM_FLAGS = [ - { - flag: '--proxyquire-shim', - envIn: 'BUN_PROXYQUIRE_SHIM', - envOut: 'BUN_ENABLE_PROXYQUIRE_SHIM', - }, -]; - -const shimFlagSet = new Set(SHIM_FLAGS.map(s => s.flag)); -const shimEnvVars = {}; -for (const {flag, envIn, envOut} of SHIM_FLAGS) { - const enabled = - rawArgs.includes(flag) || - process.env[envIn] === 'true' || - process.env[envOut] === 'true'; - shimEnvVars[envOut] = enabled ? 'true' : 'false'; -} - -const args = rawArgs.filter(a => a !== '--no-c8' && !shimFlagSet.has(a)); +const args = rawArgs.filter(a => a !== '--no-c8'); const isBunRuntime = typeof Bun !== 'undefined'; const isBunUserAgent = /^bun\//i.test(process.env.npm_config_user_agent || ''); @@ -650,7 +630,6 @@ if (require.main === module) { if (wantsBunRuntime) { process.env.MOCHA_PARALLEL = 'false'; - Object.assign(process.env, shimEnvVars); // If Bun was requested (via JS_RUNTIME=bun or `bun run test`) but this // script was launched via Node.js, re-exec under the `bun` binary. @@ -660,7 +639,6 @@ if (require.main === module) { env: { ...process.env, MOCHA_PARALLEL: 'false', - ...shimEnvVars, }, }); if (res.error) { @@ -678,15 +656,6 @@ if (require.main === module) { mochaArgs.unshift('--no-parallel'); } - // Only preload the proxyquire shim when `--proxyquire-shim` is requested. - if (shimEnvVars.BUN_ENABLE_PROXYQUIRE_SHIM === 'true') { - const shimPath = path.resolve(__dirname, 'proxyquire-bun-shim.cjs'); - require(shimPath); - if (!mochaArgs.includes(shimPath)) { - mochaArgs.unshift('--require', shimPath); - } - } - if (!mochaArgs.includes('--exit')) { mochaArgs.unshift('--exit'); } diff --git a/core/packages/gapic-node-processing/.mocharc.js b/core/packages/gapic-node-processing/.mocharc.js index d02f15ced556..992e0b1d7d98 100644 --- a/core/packages/gapic-node-processing/.mocharc.js +++ b/core/packages/gapic-node-processing/.mocharc.js @@ -11,7 +11,6 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. -const path = require('path'); const config = { "enable-source-maps": true, "throw-deprecation": true, @@ -24,9 +23,6 @@ if (process.env.MOCHA_THROW_DEPRECATION === 'false') { if (process.env.MOCHA_PARALLEL === 'false' || typeof Bun !== 'undefined') { config.parallel = false; } -if (typeof Bun !== 'undefined') { - config.require = [path.resolve(__dirname, '../../../bin/proxyquire-bun-shim.cjs')]; -} if (process.env.MOCHA_REPORTER) { config.reporter = process.env.MOCHA_REPORTER; } diff --git a/core/packages/gax/.mocharc.js b/core/packages/gax/.mocharc.js index 289497a73f00..f269fd0cdbdd 100644 --- a/core/packages/gax/.mocharc.js +++ b/core/packages/gax/.mocharc.js @@ -11,7 +11,6 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. -const path = require('path'); const config = { "enable-source-maps": true, "throw-deprecation": true, @@ -24,10 +23,6 @@ if (process.env.MOCHA_THROW_DEPRECATION === 'false') { if (process.env.MOCHA_PARALLEL === 'false' || typeof Bun !== 'undefined') { config.parallel = false; } -if (typeof Bun !== 'undefined') { - // Preload Bun compatibility shim for proxyquire and nock when running under Bun - config.require = [path.resolve(__dirname, '../../../bin/proxyquire-bun-shim.cjs')]; -} if (process.env.MOCHA_REPORTER) { config.reporter = process.env.MOCHA_REPORTER; } diff --git a/core/packages/gaxios/.mocharc.js b/core/packages/gaxios/.mocharc.js index 5e32d443dd98..f269fd0cdbdd 100644 --- a/core/packages/gaxios/.mocharc.js +++ b/core/packages/gaxios/.mocharc.js @@ -11,7 +11,6 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. -const path = require('path'); const config = { "enable-source-maps": true, "throw-deprecation": true, @@ -24,9 +23,6 @@ if (process.env.MOCHA_THROW_DEPRECATION === 'false') { if (process.env.MOCHA_PARALLEL === 'false' || typeof Bun !== 'undefined') { config.parallel = false; } -if (typeof Bun !== 'undefined') { - config.require = [path.resolve(__dirname, '../../../bin/proxyquire-bun-shim.cjs')]; -} if (process.env.MOCHA_REPORTER) { config.reporter = process.env.MOCHA_REPORTER; } diff --git a/core/packages/gcp-metadata/.mocharc.js b/core/packages/gcp-metadata/.mocharc.js index 5e32d443dd98..f269fd0cdbdd 100644 --- a/core/packages/gcp-metadata/.mocharc.js +++ b/core/packages/gcp-metadata/.mocharc.js @@ -11,7 +11,6 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. -const path = require('path'); const config = { "enable-source-maps": true, "throw-deprecation": true, @@ -24,9 +23,6 @@ if (process.env.MOCHA_THROW_DEPRECATION === 'false') { if (process.env.MOCHA_PARALLEL === 'false' || typeof Bun !== 'undefined') { config.parallel = false; } -if (typeof Bun !== 'undefined') { - config.require = [path.resolve(__dirname, '../../../bin/proxyquire-bun-shim.cjs')]; -} if (process.env.MOCHA_REPORTER) { config.reporter = process.env.MOCHA_REPORTER; } diff --git a/core/packages/google-auth-library-nodejs/.mocharc.js b/core/packages/google-auth-library-nodejs/.mocharc.js index 5e32d443dd98..f269fd0cdbdd 100644 --- a/core/packages/google-auth-library-nodejs/.mocharc.js +++ b/core/packages/google-auth-library-nodejs/.mocharc.js @@ -11,7 +11,6 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. -const path = require('path'); const config = { "enable-source-maps": true, "throw-deprecation": true, @@ -24,9 +23,6 @@ if (process.env.MOCHA_THROW_DEPRECATION === 'false') { if (process.env.MOCHA_PARALLEL === 'false' || typeof Bun !== 'undefined') { config.parallel = false; } -if (typeof Bun !== 'undefined') { - config.require = [path.resolve(__dirname, '../../../bin/proxyquire-bun-shim.cjs')]; -} if (process.env.MOCHA_REPORTER) { config.reporter = process.env.MOCHA_REPORTER; } diff --git a/core/packages/nodejs-googleapis-common/.mocharc.js b/core/packages/nodejs-googleapis-common/.mocharc.js index 5e32d443dd98..f269fd0cdbdd 100644 --- a/core/packages/nodejs-googleapis-common/.mocharc.js +++ b/core/packages/nodejs-googleapis-common/.mocharc.js @@ -11,7 +11,6 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. -const path = require('path'); const config = { "enable-source-maps": true, "throw-deprecation": true, @@ -24,9 +23,6 @@ if (process.env.MOCHA_THROW_DEPRECATION === 'false') { if (process.env.MOCHA_PARALLEL === 'false' || typeof Bun !== 'undefined') { config.parallel = false; } -if (typeof Bun !== 'undefined') { - config.require = [path.resolve(__dirname, '../../../bin/proxyquire-bun-shim.cjs')]; -} if (process.env.MOCHA_REPORTER) { config.reporter = process.env.MOCHA_REPORTER; } diff --git a/core/packages/nodejs-proto-files/.mocharc.js b/core/packages/nodejs-proto-files/.mocharc.js index 5e32d443dd98..f269fd0cdbdd 100644 --- a/core/packages/nodejs-proto-files/.mocharc.js +++ b/core/packages/nodejs-proto-files/.mocharc.js @@ -11,7 +11,6 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. -const path = require('path'); const config = { "enable-source-maps": true, "throw-deprecation": true, @@ -24,9 +23,6 @@ if (process.env.MOCHA_THROW_DEPRECATION === 'false') { if (process.env.MOCHA_PARALLEL === 'false' || typeof Bun !== 'undefined') { config.parallel = false; } -if (typeof Bun !== 'undefined') { - config.require = [path.resolve(__dirname, '../../../bin/proxyquire-bun-shim.cjs')]; -} if (process.env.MOCHA_REPORTER) { config.reporter = process.env.MOCHA_REPORTER; } diff --git a/core/packages/proto3-json-serializer-nodejs/.mocharc.js b/core/packages/proto3-json-serializer-nodejs/.mocharc.js index 758e73b2dd2c..8da4969efb4d 100644 --- a/core/packages/proto3-json-serializer-nodejs/.mocharc.js +++ b/core/packages/proto3-json-serializer-nodejs/.mocharc.js @@ -11,7 +11,6 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. -const path = require('path'); const config = { "enable-source-maps": true, "throw-deprecation": true, @@ -24,9 +23,6 @@ if (process.env.MOCHA_THROW_DEPRECATION === 'false') { if (process.env.MOCHA_PARALLEL === 'false' || typeof Bun !== 'undefined') { config.parallel = false; } -if (typeof Bun !== 'undefined') { - config.require = [path.resolve(__dirname, '../../../bin/proxyquire-bun-shim.cjs')]; -} if (process.env.MOCHA_REPORTER) { config.reporter = process.env.MOCHA_REPORTER; } diff --git a/core/packages/retry-request/.mocharc.js b/core/packages/retry-request/.mocharc.js index 5e32d443dd98..f269fd0cdbdd 100644 --- a/core/packages/retry-request/.mocharc.js +++ b/core/packages/retry-request/.mocharc.js @@ -11,7 +11,6 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. -const path = require('path'); const config = { "enable-source-maps": true, "throw-deprecation": true, @@ -24,9 +23,6 @@ if (process.env.MOCHA_THROW_DEPRECATION === 'false') { if (process.env.MOCHA_PARALLEL === 'false' || typeof Bun !== 'undefined') { config.parallel = false; } -if (typeof Bun !== 'undefined') { - config.require = [path.resolve(__dirname, '../../../bin/proxyquire-bun-shim.cjs')]; -} if (process.env.MOCHA_REPORTER) { config.reporter = process.env.MOCHA_REPORTER; } diff --git a/core/packages/teeny-request/.mocharc.js b/core/packages/teeny-request/.mocharc.js index 5e32d443dd98..f269fd0cdbdd 100644 --- a/core/packages/teeny-request/.mocharc.js +++ b/core/packages/teeny-request/.mocharc.js @@ -11,7 +11,6 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. -const path = require('path'); const config = { "enable-source-maps": true, "throw-deprecation": true, @@ -24,9 +23,6 @@ if (process.env.MOCHA_THROW_DEPRECATION === 'false') { if (process.env.MOCHA_PARALLEL === 'false' || typeof Bun !== 'undefined') { config.parallel = false; } -if (typeof Bun !== 'undefined') { - config.require = [path.resolve(__dirname, '../../../bin/proxyquire-bun-shim.cjs')]; -} if (process.env.MOCHA_REPORTER) { config.reporter = process.env.MOCHA_REPORTER; }