Skip to content

Commit

Permalink
fix: Avoid ESBuild destroying Proxy in __toESM (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielku15 authored Oct 12, 2024
1 parent acf34b9 commit c22bfb3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
35 changes: 32 additions & 3 deletions src/discoverer/evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,32 @@ export class EvaluationTestDiscoverer implements ITestDiscoverer {
function placeholder(): unknown {
return new Proxy(placeholder, {
get: (obj, target) => {
const desc = Object.getOwnPropertyDescriptor(obj, target);
if (desc && !desc.writable && !desc.configurable) {
return desc.value; // avoid invariant volation https://stackoverflow.com/q/75148897
try {
const desc = Object.getOwnPropertyDescriptor(obj, target);
if (desc && !desc.writable && !desc.configurable) {
return desc.value; // avoid invariant volation https://stackoverflow.com/q/75148897
}
return placeholder();
} catch (e) {
return placeholder();
}
},
set: () => true,
apply: () => {
return placeholder();
},
});
}

function objectPlaceholder(originalObject: any): unknown {
return new Proxy(objectPlaceholder, {
get: (_, target) => {
if (target === 'create') {
return placeholder();
} else {
return originalObject[target];
}
},
set: () => true,
});
}
Expand Down Expand Up @@ -186,6 +206,15 @@ export class EvaluationTestDiscoverer implements ITestDiscoverer {
} else if (prop in target) {
return target[prop]; // top-level `var` defined get set on the contextObj
} else if (prop in globalThis && !replacedGlobals.has(prop as string)) {
// Bug #153: ESBuild will wrap require() calls into __toESM which breaks quite some things
// we want to keep our Proxy placeholder object in all scenarios
// Due to that we provide a special proxy object which will create again placeholder proxies
// on Object.create
// https://github.com/evanw/esbuild/blob/d34e79e2a998c21bb71d57b92b0017ca11756912/internal/runtime/runtime.go#L231-L242
if (prop === 'Object') {
return objectPlaceholder((globalThis as any)[prop]);
}

return (globalThis as any)[prop];
} else {
return placeholder();
Expand Down
2 changes: 1 addition & 1 deletion src/test/integration/typescript.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ describe('typescript', () => {
expect(failed.message?.location).to.not.be.undefined;
expect(failed.message?.location?.uri.toString()).to.include('hello.test.ts');
expect(path.isAbsolute(failed.message!.location!.uri.fsPath)).to.be.true;
expect(failed.message?.location?.range.start.line).to.equal(25);
expect(failed.message?.location?.range.start.line).to.equal(29);
expect(failed.message?.location?.range.start.character).to.equal(5);
});
});
4 changes: 4 additions & 0 deletions test-workspaces/typescript/hello.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { strictEqual } from 'node:assert';
import path, { join } from 'node:path';

path.join('', '');
join('', '');

// just some typescript code which would be valid directly in Node

Expand Down

0 comments on commit c22bfb3

Please sign in to comment.