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): paths with spaces break esbuild #13312

Merged
merged 3 commits into from
Feb 28, 2021
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
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'),
],
}),
});
});