Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(lambda-nodejs): esbuild define parameters are incorrectly encoded #14065

Merged
merged 6 commits into from
Apr 12, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 2 additions & 0 deletions packages/@aws-cdk/aws-lambda-nodejs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ new lambda.NodejsFunction(this, 'my-handler', {
},
define: { // Replace strings during build time
'process.env.API_KEY': JSON.stringify('xxx-xxxx-xxx'),
'process.env.PRODUCTION': JSON.stringify(true),
'process.env.NUMBER': JSON.stringify(123),
},
logLevel: LogLevel.SILENT, // defaults to LogLevel.WARNING
keepNames: true, // defaults to false
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-lambda-nodejs/lib/bundling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export class Bundling implements cdk.BundlingOptions {
...this.props.sourceMap ? ['--sourcemap'] : [],
...this.externals.map(external => `--external:${external}`),
...loaders.map(([ext, name]) => `--loader:${ext}=${name}`),
...defines.map(([key, value]) => `--define:${key}=${value}`),
...defines.map(([key, value]) => `--define:${key}=${JSON.stringify(value)}`),
...this.props.logLevel ? [`--log-level=${this.props.logLevel}`] : [],
...this.props.keepNames ? ['--keep-names'] : [],
...this.relativeTsconfigPath ? [`--tsconfig=${pathJoin(inputDir, this.relativeTsconfigPath)}`] : [],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`esbuild bundling with esbuild options 1`] = `
"(() => {
// test/integ-handlers/define.ts
function handler() {
return [
\\"VALUE\\",
true,
7777,
'this is a \\"test\\"'
];
}
})();
"
`;
11 changes: 9 additions & 2 deletions packages/@aws-cdk/aws-lambda-nodejs/test/bundling.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,15 @@ test('esbuild bundling with esbuild options', () => {
footer: '/* comments */',
forceDockerBundling: true,
define: {
'DEBUG': 'true',
'process.env.KEY': JSON.stringify('VALUE'),
'process.env.BOOL': 'true',
'process.env.NUMBER': '7777',
'process.env.STRING': JSON.stringify('this is a "test"'),
},
});

// Correctly bundles with esbuild
const defineInstructions = '--define:process.env.KEY="\\"VALUE\\"" --define:process.env.BOOL="true" --define:process.env.NUMBER="7777" --define:process.env.STRING="\\"this is a \\\\\\"test\\\\\\"\\""';
expect(Code.fromAsset).toHaveBeenCalledWith(path.dirname(depsLockFilePath), {
assetHashType: AssetHashType.OUTPUT,
bundling: expect.objectContaining({
Expand All @@ -184,13 +187,17 @@ test('esbuild bundling with esbuild options', () => {
'npx esbuild --bundle "/asset-input/lib/handler.ts"',
'--target=es2020 --platform=node --outfile="/asset-output/index.js"',
'--minify --sourcemap --external:aws-sdk --loader:.png=dataurl',
'--define:DEBUG=true --define:process.env.KEY="VALUE"',
defineInstructions,
'--log-level=silent --keep-names --tsconfig=/asset-input/lib/custom-tsconfig.ts',
'--metafile=/asset-output/index.meta.json --banner=\'/* comments */\' --footer=\'/* comments */\'',
].join(' '),
],
}),
});

// Make sure that the define instructions are working as expected with the esbuild CLI
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you think we really need to run esbuild here in the test? Isn't it enough to ensure that the right command line args are passed?

Copy link
Contributor Author

@skyrpex skyrpex Apr 9, 2021

Choose a reason for hiding this comment

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

Well, I got it right this time and reverse-engineered the define parameters' expectation (it's a hell plenty of escape slashes). It should be stable now and the test is not exactly necessary, but it's proof that it works now.

Let me know if you want me to remove it 👍🏻

Copy link
Contributor

Choose a reason for hiding this comment

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

I like that this is doing an end to end test

const bundleProcess = util.exec('bash', ['-c', `npx esbuild --bundle ${`${__dirname}/integ-handlers/define.ts`} ${defineInstructions}`]);
expect(bundleProcess.stdout.toString()).toMatchSnapshot();
});

test('Detects yarn.lock', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function handler() {
return [
process.env.KEY,
process.env.BOOL,
process.env.NUMBER,
process.env.STRING,
];
}