Detect and warn if duplicate lambda libraries are detected #585
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Asynchronously detects if the Lambda library is installed both in node_modules and as a Lambda library. If so, it logs a warning.
Motivation
It's common for users to install the Lambda layers as instructed by the documentation, but then then see import errors in their code, causing them to run
npm install datadog-lambda-js. Now they will have the Lambda library installed in two places, which will (1) increase package size and cold start duration, and (2) break custom metrics.We've had many customers open support tickets recently about broken metrics caused by this issue, so this PR aims to avoid this problem in the future.
Testing Guidelines
Manual tests: logs a warning as expected when installed in both places, and doesn't log any warning if only installed in the layer or node_modules.
Added unit tests.
Additional Notes
This works by using
fsand checking if the paths exist. I'm assuming the paths will always be:'/opt/nodejs/node_modules/datadog-lambda-js'path.join(process.cwd(), 'node_modules/datadog-lambda-js')We could use
process.env.NODE_PATHand search fordatadog-lambda-js/package.jsonin the paths. However, my solution is simpler and works perfectly fine. It is also faster than relying on node path because we don't have to parse the node path.Types of Changes
Check all that apply