Skip to content

Commit

Permalink
fix(lambda-nodejs): paths with spaces break esbuild (#13312)
Browse files Browse the repository at this point in the history
Problem: Paths with spaces break ESBuild on Windows.
Solution: Add double quotes around the input paths for the ESBuild command.
Testing: Updated unit tests and confirmed in my own package this fix works.

Closes #13311

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
GEMISIS committed Feb 28, 2021
1 parent 1fcdb6d commit f983fbb
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 28 deletions.
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-lambda-nodejs/lib/bundling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,10 @@ export class Bundling implements cdk.BundlingOptions {

const esbuildCommand: string = [
npx, 'esbuild',
'--bundle', pathJoin(inputDir, this.relativeEntryPath).replace(/(\s+)/g, '\\$1'),
'--bundle', `"${pathJoin(inputDir, this.relativeEntryPath)}"`,
`--target=${this.props.target ?? toTarget(this.props.runtime)}`,
'--platform=node',
`--outfile=${pathJoin(outputDir, 'index.js')}`,
`--outfile="${pathJoin(outputDir, 'index.js')}"`,
...this.props.minify ? ['--minify'] : [],
...this.props.sourceMap ? ['--sourcemap'] : [],
...this.externals.map(external => `--external:${external}`),
Expand Down
32 changes: 6 additions & 26 deletions packages/@aws-cdk/aws-lambda-nodejs/test/bundling.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ test('esbuild bundling in Docker', () => {
},
command: [
'bash', '-c',
'npx esbuild --bundle /asset-input/lib/handler.ts --target=node12 --platform=node --outfile=/asset-output/index.js --external:aws-sdk --loader:.png=dataurl',
'npx esbuild --bundle "/asset-input/lib/handler.ts" --target=node12 --platform=node --outfile="/asset-output/index.js" --external:aws-sdk --loader:.png=dataurl',
],
workingDirectory: '/',
}),
Expand All @@ -74,7 +74,7 @@ test('esbuild bundling with handler named index.ts', () => {
bundling: expect.objectContaining({
command: [
'bash', '-c',
'npx esbuild --bundle /asset-input/lib/index.ts --target=node12 --platform=node --outfile=/asset-output/index.js --external:aws-sdk',
'npx esbuild --bundle "/asset-input/lib/index.ts" --target=node12 --platform=node --outfile="/asset-output/index.js" --external:aws-sdk',
],
}),
});
Expand All @@ -94,7 +94,7 @@ test('esbuild bundling with tsx handler', () => {
bundling: expect.objectContaining({
command: [
'bash', '-c',
'npx esbuild --bundle /asset-input/lib/handler.tsx --target=node12 --platform=node --outfile=/asset-output/index.js --external:aws-sdk',
'npx esbuild --bundle "/asset-input/lib/handler.tsx" --target=node12 --platform=node --outfile="/asset-output/index.js" --external:aws-sdk',
],
}),
});
Expand Down Expand Up @@ -139,7 +139,7 @@ test('esbuild bundling with externals and dependencies', () => {
command: [
'bash', '-c',
[
'npx esbuild --bundle /asset-input/test/bundling.test.js --target=node12 --platform=node --outfile=/asset-output/index.js --external:abc --external:delay',
'npx esbuild --bundle "/asset-input/test/bundling.test.js" --target=node12 --platform=node --outfile="/asset-output/index.js" --external:abc --external:delay',
`echo \'{\"dependencies\":{\"delay\":\"${delayVersion}\"}}\' > /asset-output/package.json`,
'cp /asset-input/package-lock.json /asset-output/package-lock.json',
'cd /asset-output',
Expand Down Expand Up @@ -181,8 +181,8 @@ test('esbuild bundling with esbuild options', () => {
command: [
'bash', '-c',
[
'npx esbuild --bundle /asset-input/lib/handler.ts',
'--target=es2020 --platform=node --outfile=/asset-output/index.js',
'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"',
'--log-level=silent --keep-names --tsconfig=/asset-input/lib/custom-tsconfig.ts',
Expand Down Expand Up @@ -334,23 +334,3 @@ test('with command hooks', () => {
}),
});
});

test('escapes spaces in path', () => {
Bundling.bundle({
entry: '/project/lib/my cool lambda/handler.ts',
depsLockFilePath,
runtime: Runtime.NODEJS_12_X,
forceDockerBundling: true,
});

// Correctly bundles with esbuild
expect(Code.fromAsset).toHaveBeenCalledWith(path.dirname(depsLockFilePath), {
assetHashType: AssetHashType.OUTPUT,
bundling: expect.objectContaining({
command: [
'bash', '-c',
expect.stringContaining('lib/my\\ cool\\ lambda/handler.ts'),
],
}),
});
});

0 comments on commit f983fbb

Please sign in to comment.