Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/aws-cdk-lib/aws-lambda-nodejs/lib/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,5 +292,8 @@ function findDefiningFile(scope: Construct): string {
throw new ValidationError('Cannot find defining file.', scope);
}

return sites[definingIndex].getFileName();
// Fixes issue #21630.
// ESM modules return a 'file://' prefix to the filenames, this should be removed for
// compatibility with the NodeJS filesystem functions.
return sites[definingIndex].getFileName().replace(/^file:\/\//, '');
}
28 changes: 22 additions & 6 deletions packages/aws-cdk-lib/aws-lambda-nodejs/test/function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ let stack: Stack;
beforeEach(() => {
stack = new Stack();
jest.clearAllMocks();
// pretend the calling file is in a fake file path
mockCallsites.mockImplementation(() => [
{ getFunctionName: () => 'NodejsFunction' },
{ getFileName: () => bockPath`function.test.ts` },
]);
});

// We MUST use a fake file system here.
Expand All @@ -57,12 +62,6 @@ bockfs({
});
const bockPath = bockfs.workingDirectory('/home/project');

// pretend the calling file is in a fake file path
mockCallsites.mockImplementation(() => [
{ getFunctionName: () => 'NodejsFunction' },
{ getFileName: () => bockPath`function.test.ts` },
]);

afterAll(() => {
bockfs.restore();
});
Expand Down Expand Up @@ -231,6 +230,23 @@ test('throws when entry is not js/ts', () => {
})).toThrow(/Only JavaScript or TypeScript entry files are supported/);
});

test('NodejsFunction with .js handler in an ESM package', () => {
// pretend the calling file is in a fake file path
// In ESM, callsites are prepended with 'file://'
mockCallsites.mockImplementation(() => [
{ getFunctionName: () => 'NodejsFunction' },
{ getFileName: () => `file://${bockPath`function.test.ts`}` },
]);

// WHEN
new NodejsFunction(stack, 'handler2');

// THEN
expect(Bundling.bundle).toHaveBeenCalledWith(stack, expect.objectContaining({
entry: expect.stringContaining('function.test.handler2.js'), // Automatically finds .ts handler file
}));
});

test('accepts tsx', () => {
const entry = bockPath`handler.tsx`;

Expand Down
Loading