[patch] update sandboxed-module for Yarn PnP compatibility and add mongodb-legacy type definitions (#33983)

GitOrigin-RevId: 8f1e9a4e4b4b5fbf3a770951a070b5a259abdcee
This commit is contained in:
Anna Claire Fields
2026-06-04 15:04:35 +02:00
committed by Copybot
parent 44dee7592a
commit 3fcd133198
3 changed files with 50 additions and 3 deletions
@@ -1,5 +1,5 @@
diff --git a/lib/sandboxed_module.js b/lib/sandboxed_module.js diff --git a/lib/sandboxed_module.js b/lib/sandboxed_module.js
index 1cd6743fe221cbe91ea92fea3707ed07a8a2ded3..46889217d96d5534a206549ae7bd97100e41c3e4 100644 index 1cd6743..4718b97 100644
--- a/lib/sandboxed_module.js --- a/lib/sandboxed_module.js
+++ b/lib/sandboxed_module.js +++ b/lib/sandboxed_module.js
@@ -4,7 +4,7 @@ var Module = require('module'); @@ -4,7 +4,7 @@ var Module = require('module');
@@ -11,3 +11,47 @@ index 1cd6743fe221cbe91ea92fea3707ed07a8a2ded3..46889217d96d5534a206549ae7bd9710
var parent = module.parent; var parent = module.parent;
var globalOptions = {}; var globalOptions = {};
var registeredBuiltInSourceTransformers = ['coffee']; var registeredBuiltInSourceTransformers = ['coffee'];
@@ -157,12 +157,20 @@ SandboxedModule.prototype._createRecursiveRequireProxy = function() {
var cache = Object.create(null);
var required = this._getRequires();
for (var key in required) {
- var injectedFilename = requireLike(this.filename).resolve(key);
- cache[injectedFilename] = required[key];
+ // Under Yarn PnP, resolution from a transitive dependency's context may fail
+ // for packages not declared in that dependency's package.json. Silently skip
+ // cache pre-population on failure; the mock will still be injected via the
+ // inject map in requireInterceptor or resolved via RecursiveRequireProxy fallback.
+ try {
+ var injectedFilename = requireLike(this.filename).resolve(key);
+ cache[injectedFilename] = required[key];
+ } catch (e) {}
}
cache[this.filename] = this.exports;
var globals = this.globals;
+ // Store the top-level module's filename for PnP fallback resolution
+ var topLevelFilename = this.filename;
var options;
if(!this._options.sourceTransformersSingleOnly && this._options.sourceTransformers){
options = {
@@ -208,8 +216,18 @@ SandboxedModule.prototype._createRecursiveRequireProxy = function() {
if (request in cache) return cache[request];
return require(request);
}
- // cached modules
- var requestedFilename = requireLike(this.filename).resolve(request);
+ // Resolve the requested module filename.
+ // Under Yarn PnP, packages can only resolve their declared dependencies.
+ // When sandboxed-module loads a transitive dependency, the resolution context
+ // may not have access to all needed packages. Fall back to resolving from
+ // the top-level module's context (the module under test).
+ var requestedFilename;
+ try {
+ requestedFilename = requireLike(this.filename).resolve(request);
+ } catch (e) {
+ if (this.filename === topLevelFilename) throw e;
+ requestedFilename = requireLike(topLevelFilename).resolve(request);
+ }
if (requestedFilename in cache) return cache[requestedFilename];
var sandboxedModule = createInnerSandboxedModule(requestedFilename)
return sandboxedModule.exports;
+3
View File
@@ -0,0 +1,3 @@
declare module 'mongodb-legacy' {
export * from 'mongodb'
}
+2 -2
View File
@@ -30147,11 +30147,11 @@ __metadata:
"sandboxed-module@patch:sandboxed-module@npm%3A2.0.4#~/.yarn/patches/sandboxed-module-npm-2.0.4-f8b45aacc9.patch": "sandboxed-module@patch:sandboxed-module@npm%3A2.0.4#~/.yarn/patches/sandboxed-module-npm-2.0.4-f8b45aacc9.patch":
version: 2.0.4 version: 2.0.4
resolution: "sandboxed-module@patch:sandboxed-module@npm%3A2.0.4#~/.yarn/patches/sandboxed-module-npm-2.0.4-f8b45aacc9.patch::version=2.0.4&hash=affca8" resolution: "sandboxed-module@patch:sandboxed-module@npm%3A2.0.4#~/.yarn/patches/sandboxed-module-npm-2.0.4-f8b45aacc9.patch::version=2.0.4&hash=3f2e79"
dependencies: dependencies:
require-like: "npm:0.1.2" require-like: "npm:0.1.2"
stack-trace: "npm:0.0.9" stack-trace: "npm:0.0.9"
checksum: 10c0/d4855ed62bce32db648d2862a3f84c57794cbc9ca3a4627da8dca8d4a1b10f49c1f9a280654edfc6237fbf32f3fbe7a1c0449a2306109dd22cc3c4451e86d541 checksum: 10c0/5d1e020ea14bb6d02c1806fc98922a9b69d2e011dbb9d6e9f644b94c4ecbf422603ed0d0f9cb8bada6e7e7d847bcbaa7ee1c02710fb0f09992bdebe83deab404
languageName: node languageName: node
linkType: hard linkType: hard