Skip to content

Commit

Permalink
fix(lambda-nodejs): parcel tries to install @babel/core (#9067)
Browse files Browse the repository at this point in the history
When Parcel cannot find `@babel/core` in the search paths for node
modules it tries to install it. This overwrites the lock file and
`node_modules`.

Add the `--no-autoinstall` flag to prevent Parcel from installing any
missing dependency and switch to a local install in the Docker image at
the root (`/`) of the filesystem. This way all dependencies in
`/node_modules` will be in the search paths of `/asset-input`.

This was not detected in this repo or in the init template because
`@babel/core` is a dependency of `jest`.

Closes #9032


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
jogold committed Jul 16, 2020
1 parent 375335e commit 8d4c635
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
3 changes: 2 additions & 1 deletion packages/@aws-cdk/aws-lambda-nodejs/lib/bundling.ts
Expand Up @@ -154,9 +154,10 @@ export class Bundling {
// Entry file path relative to container path
const containerEntryPath = path.join(cdk.AssetStaging.BUNDLING_INPUT_DIR, path.relative(projectRoot, path.resolve(options.entry)));
const parcelCommand = [
'parcel',
'$(node -p "require.resolve(\'parcel\')")', // Parcel is not globally installed, find its "bin"
'build', containerEntryPath.replace(/\\/g, '/'), // Always use POSIX paths in the container
'--target', 'cdk-lambda',
'--no-autoinstall',
'--no-scope-hoist',
...options.cacheDir
? ['--cache-dir', '/parcel-cache']
Expand Down
3 changes: 2 additions & 1 deletion packages/@aws-cdk/aws-lambda-nodejs/parcel/Dockerfile
Expand Up @@ -13,7 +13,8 @@ RUN mkdir /npm-cache && \
RUN npm install --global yarn

# Install parcel 2 (fix the version since it's still in beta)
# install at "/" so that node_modules will be in the path for /asset-input
ARG PARCEL_VERSION=2.0.0-beta.1
RUN yarn global add parcel@$PARCEL_VERSION
RUN cd / && npm install parcel@$PARCEL_VERSION

CMD [ "parcel" ]
5 changes: 3 additions & 2 deletions packages/@aws-cdk/aws-lambda-nodejs/test/bundling.test.ts
Expand Up @@ -45,7 +45,8 @@ test('Parcel bundling', () => {
volumes: [{ containerPath: '/parcel-cache', hostPath: '/cache-dir' }],
workingDirectory: '/asset-input/folder',
command: [
'bash', '-c', 'parcel build /asset-input/folder/entry.ts --target cdk-lambda --no-scope-hoist --cache-dir /parcel-cache',
'bash', '-c',
'$(node -p "require.resolve(\'parcel\')") build /asset-input/folder/entry.ts --target cdk-lambda --no-autoinstall --no-scope-hoist --cache-dir /parcel-cache',
],
}),
});
Expand Down Expand Up @@ -106,7 +107,7 @@ test('Parcel bundling with externals and dependencies', () => {
bundling: expect.objectContaining({
command: [
'bash', '-c',
'parcel build /asset-input/folder/entry.ts --target cdk-lambda --no-scope-hoist && mv /asset-input/.package.json /asset-output/package.json && cd /asset-output && npm install',
'$(node -p "require.resolve(\'parcel\')") build /asset-input/folder/entry.ts --target cdk-lambda --no-autoinstall --no-scope-hoist && mv /asset-input/.package.json /asset-output/package.json && cd /asset-output && npm install',
],
}),
});
Expand Down

0 comments on commit 8d4c635

Please sign in to comment.