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

(@aws-cdk/aws-lambda-nodejs): Does not use closest/first lockfile. #15847

Closed
yo1dog opened this issue Aug 1, 2021 · 5 comments · Fixed by #16629
Closed

(@aws-cdk/aws-lambda-nodejs): Does not use closest/first lockfile. #15847

yo1dog opened this issue Aug 1, 2021 · 5 comments · Fixed by #16629
Labels
@aws-cdk/aws-lambda-nodejs bug This issue is a bug. effort/small Small work item – less than a day of effort p2

Comments

@yo1dog
Copy link
Contributor

yo1dog commented Aug 1, 2021

NodejsFunction bundling does not use the correct lockfile if a lockfile for a different package manger exists in a parent directory.

const lockFile = findUp(PackageManager.PNPM.lockFile)
?? findUp(PackageManager.YARN.lockFile)
?? findUp(PackageManager.NPM.lockFile);

This code searches the current and all parent directories for PNPM, then yarn, then NPM lock files. So even if an NPM lockfile exists in the current directory, if a PNPM or yarn lock file exists in any parent directory, CDK will use the PNPM or yarn lockfile. Meaning, if cwd was /fu/bar/baz/, /fu/yarn.lock would be used instead of /fu/bar/baz/package-lock.json.

Reproduction Steps

/home/mike/my-cdk/cdkApp.ts
/home/mike/my-cdk/package-lock.json
/home/mike/yarn.lock

Assuming process.cwd() === '/home/mike/my-cdk'

What did you expect to happen?

/home/mike/my-cdk/package-lock.json is selected as the lockfile. Bundles successfully.

What actually happened?

/home/mike/yarn.lock is selected as the lockfile. Bundling fails.

Even with the --verbose flag, the logs only provide this cryptic message: bash: yarn: command not found. In an attempt to fix, I tried installing yarn. Then the error became: error Couldn't find a package.json file in "/home/mike". At the time, I did not realize there was an errant yarn.lock file in my home directory, so this message was confusing as well.

Environment

  • CDK CLI Version : 1.116.0
  • Framework Version: 1.116.0
  • Node.js Version: v14.17.3
  • OS: Ubuntu 18.04.3 LTS
  • Language (Version): TypeScript (4.3.5)

Other

Workaround is to simply provide the depsLockFilePath param to the NodejsFunction constructor. However, as it stands, it seems one should always include this param as an unrelated lockfile outside the project could break cdk synth.

Instead of walking up directories 3 times looking for the 3 lockfiles, instead walk up once and look for all 3. This would correct the logic to use the first/closest lockfile. Something like:

const lockFile = findUp([
  PackageManager.PNPM.lockFile,
  PackageManager.YARN.lockFile,
  PackageManager.NPM.lockFile
]);

// ...

/**
 * Find a file by walking up parent directories
 */
export function findUp(names: string[], directory: string = process.cwd()): string | undefined {
  const absoluteDirectory = path.resolve(directory);

  for (const name of names) {
    const file = path.join(directory, name);
    if (fs.existsSync(file)) {
      return file;
    }
  }

  const { root } = path.parse(absoluteDirectory);
  if (absoluteDirectory === root) {
    return undefined;
  }

  return findUp(names, path.dirname(absoluteDirectory));
}

Also, enabling more diagnostic messaging around bundling with the verbose flag would be helpful. If the project root, lockfile path, etc. used were reported I would have been able to immediately identify the problem. For example, logging this.packageManager in Bundling.prototype.tryBundle.


This is 🐛 Bug Report

@yo1dog yo1dog added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Aug 1, 2021
@yo1dog
Copy link
Contributor Author

yo1dog commented Aug 1, 2021

Sorry submitted too soon by accident. It is done now.

@yo1dog yo1dog changed the title @aws-cdk/aws-lambda-nodejs: Does not find correct lockfile. @aws-cdk/aws-lambda-nodejs: Does not use closest/first lockfile. Aug 1, 2021
@yo1dog yo1dog changed the title @aws-cdk/aws-lambda-nodejs: Does not use closest/first lockfile. (@aws-cdk/aws-lambda-nodejs): Does not use closest/first lockfile. Aug 1, 2021
@nija-at
Copy link
Contributor

nija-at commented Aug 23, 2021

The correct solution here would be to stop when the first lock file is found. And if two lock files are found in the same (lowest) directory, it should error and expect depsLockFilePath to be supplied.

Since we have a workaround for this, marking it as a p2.

@nija-at nija-at added effort/small Small work item – less than a day of effort p2 and removed needs-triage This issue or PR still needs to be triaged. labels Aug 23, 2021
@bedaka
Copy link

bedaka commented Sep 22, 2021

This just took me a lot of time to debug. A random yarn.lock file that was located in a parent folder some levels above my project caused my build to fail. If there is a lock file on project level it really should be used before anything else.

@kamzil
Copy link

kamzil commented Sep 23, 2021

Had the same experience as @bedaka. To me this feels also like a potential security issue if CDK ends up using some random lock file from outside project folder.

yo1dog added a commit to yo1dog/aws-cdk that referenced this issue Sep 23, 2021
@nija-at nija-at removed their assignment Nov 25, 2021
@mergify mergify bot closed this as completed in #16629 Dec 14, 2021
mergify bot pushed a commit that referenced this issue Dec 14, 2021
fixes #15847

A bug in the automatic lockfile finding logic causes lockfiles higher in the directory tree to be used over lower/closer ones. This is because the code traverses the tree once per lockfile type in series, stopping when it finds one: https://github.com/aws/aws-cdk/blob/58fda9104ad884026d578dc0602f7d64dd533f6d/packages/%40aws-cdk/aws-lambda-nodejs/lib/function.ts#L137-L139

This updates the code to traverse the tree once looking for all the lockfile types at the same time and stop when one or more is found. If multiple are found at the same level, an error is thrown (per #15847 (comment)).

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@github-actions
Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

TikiTDO pushed a commit to TikiTDO/aws-cdk that referenced this issue Feb 21, 2022
…16629)

fixes aws#15847

A bug in the automatic lockfile finding logic causes lockfiles higher in the directory tree to be used over lower/closer ones. This is because the code traverses the tree once per lockfile type in series, stopping when it finds one: https://github.com/aws/aws-cdk/blob/58fda9104ad884026d578dc0602f7d64dd533f6d/packages/%40aws-cdk/aws-lambda-nodejs/lib/function.ts#L137-L139

This updates the code to traverse the tree once looking for all the lockfile types at the same time and stop when one or more is found. If multiple are found at the same level, an error is thrown (per aws#15847 (comment)).

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-lambda-nodejs bug This issue is a bug. effort/small Small work item – less than a day of effort p2
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants