From 8d4c635b5707e0d7cb34a78aed873796a4df7656 Mon Sep 17 00:00:00 2001 From: Jonathan Goldwasser Date: Thu, 16 Jul 2020 12:14:03 +0200 Subject: [PATCH] fix(lambda-nodejs): parcel tries to install @babel/core (#9067) 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* --- packages/@aws-cdk/aws-lambda-nodejs/lib/bundling.ts | 3 ++- packages/@aws-cdk/aws-lambda-nodejs/parcel/Dockerfile | 3 ++- packages/@aws-cdk/aws-lambda-nodejs/test/bundling.test.ts | 5 +++-- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/packages/@aws-cdk/aws-lambda-nodejs/lib/bundling.ts b/packages/@aws-cdk/aws-lambda-nodejs/lib/bundling.ts index fb57878099afc..392726c75298d 100644 --- a/packages/@aws-cdk/aws-lambda-nodejs/lib/bundling.ts +++ b/packages/@aws-cdk/aws-lambda-nodejs/lib/bundling.ts @@ -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'] diff --git a/packages/@aws-cdk/aws-lambda-nodejs/parcel/Dockerfile b/packages/@aws-cdk/aws-lambda-nodejs/parcel/Dockerfile index 0b91c0df6f600..51f1c720c247e 100644 --- a/packages/@aws-cdk/aws-lambda-nodejs/parcel/Dockerfile +++ b/packages/@aws-cdk/aws-lambda-nodejs/parcel/Dockerfile @@ -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" ] diff --git a/packages/@aws-cdk/aws-lambda-nodejs/test/bundling.test.ts b/packages/@aws-cdk/aws-lambda-nodejs/test/bundling.test.ts index 4ded56d78c049..cc7dac2261023 100644 --- a/packages/@aws-cdk/aws-lambda-nodejs/test/bundling.test.ts +++ b/packages/@aws-cdk/aws-lambda-nodejs/test/bundling.test.ts @@ -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', ], }), }); @@ -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', ], }), });