Repository navigation
fix: refresh local npm dependencies during in-source builds #983
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
Open
buffbiryani
wants to merge
5
commits into
aws:develop
Choose a base branch
from
buffbiryani:fix/local-npm-dependency-refresh
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
352332d
fix: refresh local npm dependencies during in-source builds
buffbiryani 55b4dd8
Address review feedback for local npm dependency refresh
buffbiryani a77e6f0
Fix local dependency refresh in npm workspaces
buffbiryani 529d8b8
Merge remote-tracking branch 'origin/develop' into fix/local-npm-depe…
buffbiryani 692c254
refactor: separate npm workspace support into follow-up contribution
buffbiryani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
tests/integration/workflows/nodejs_npm/test_local_dependency_refresh.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| import json | ||
| import tempfile | ||
| from pathlib import Path | ||
| from unittest import TestCase | ||
|
|
||
| from parameterized import parameterized | ||
|
|
||
| from aws_lambda_builders.builder import LambdaBuilder | ||
| from aws_lambda_builders.supported_runtimes import NODEJS_RUNTIMES | ||
| from aws_lambda_builders.workflows.nodejs_npm.npm import SubprocessNpm | ||
| from aws_lambda_builders.workflows.nodejs_npm.utils import ( | ||
| EXPERIMENTAL_FLAG_NODEJS_MONOREPO, | ||
| OSUtils, | ||
| ) | ||
|
|
||
|
|
||
| class TestLocalDependencyRefresh(TestCase): | ||
|
|
||
| @parameterized.expand( | ||
| [ | ||
| ( | ||
| f"{runtime}_{path_type}_{workflow}", | ||
| runtime, | ||
| spec, | ||
| workflow == "install", | ||
| ) | ||
| for runtime in NODEJS_RUNTIMES | ||
| for path_type, spec in [ | ||
| ("file", "file:../local-dependency"), | ||
| ("bare", "../local-dependency"), | ||
| ] | ||
| for workflow in ["update", "install"] | ||
| ] | ||
| ) | ||
| def test_build_in_source_refreshes_local_dependency(self, _name, runtime, spec, use_lockfile): | ||
| with tempfile.TemporaryDirectory() as temp: | ||
| root = Path(temp) | ||
| source_dir = root / "function" | ||
| local_dep = root / "local-dependency" | ||
| artifacts_dir = root / "artifacts" | ||
| scratch_dir = root / "scratch" | ||
|
|
||
| for directory in ( | ||
| source_dir, | ||
| local_dep, | ||
| artifacts_dir, | ||
| scratch_dir, | ||
| ): | ||
| directory.mkdir() | ||
|
|
||
| (local_dep / "package.json").write_text( | ||
| json.dumps( | ||
| { | ||
| "name": "local-dependency", | ||
| "version": "1.0.0", | ||
| "main": "index.js", | ||
| } | ||
| ), | ||
| encoding="utf-8", | ||
| ) | ||
|
|
||
| dependency_source = local_dep / "index.js" | ||
|
|
||
| original = "module.exports = 'original';\\n" | ||
| updated = "module.exports = 'updated';\\n" | ||
|
|
||
| dependency_source.write_text(original, encoding="utf-8") | ||
|
|
||
| manifest = source_dir / "package.json" | ||
| manifest.write_text( | ||
| json.dumps( | ||
| { | ||
| "name": "test-function", | ||
| "version": "1.0.0", | ||
| "dependencies": {"local-dependency": spec}, | ||
| } | ||
| ), | ||
| encoding="utf-8", | ||
| ) | ||
|
|
||
| flags = [] | ||
| lockfile = source_dir / "package-lock.json" | ||
| original_lockfile = None | ||
|
|
||
| if use_lockfile: | ||
| SubprocessNpm(OSUtils()).run( | ||
| [ | ||
| "install", | ||
| "--package-lock-only", | ||
| "--install-links", | ||
| "--no-audit", | ||
| "--no-fund", | ||
| ], | ||
| cwd=str(source_dir), | ||
| ) | ||
|
|
||
| original_lockfile = lockfile.read_bytes() | ||
| flags = [EXPERIMENTAL_FLAG_NODEJS_MONOREPO] | ||
|
|
||
| builder = LambdaBuilder( | ||
| language="nodejs", | ||
| dependency_manager="npm", | ||
| application_framework=None, | ||
| ) | ||
|
|
||
| def build(): | ||
| builder.build( | ||
| str(source_dir), | ||
| str(artifacts_dir), | ||
| str(scratch_dir), | ||
| str(manifest), | ||
| runtime=runtime, | ||
| build_in_source=True, | ||
| experimental_flags=flags, | ||
| ) | ||
|
|
||
| build() | ||
|
|
||
| installed = source_dir / "node_modules" / "local-dependency" / "index.js" | ||
|
|
||
| artifact = artifacts_dir / "node_modules" / "local-dependency" / "index.js" | ||
|
|
||
| self.assertEqual(installed.read_text(encoding="utf-8"), original) | ||
| self.assertEqual(artifact.read_text(encoding="utf-8"), original) | ||
|
|
||
| dependency_source.write_text(updated, encoding="utf-8") | ||
|
|
||
| build() | ||
|
|
||
| self.assertEqual(dependency_source.read_text(encoding="utf-8"), updated) | ||
| self.assertEqual(installed.read_text(encoding="utf-8"), updated) | ||
| self.assertEqual(artifact.read_text(encoding="utf-8"), updated) | ||
|
|
||
| if use_lockfile: | ||
| self.assertEqual(lockfile.read_bytes(), original_lockfile) |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[BUG] Local dependencies hoisted to an npm workspace root are never refreshed
_refresh_local_dependenciesreads onlyinstall_dir/package.jsonand looks for installed copies only atinstall_dir/node_modules/<name>. If that directory is missing, the dependency is skipped.This misses both workspace cases:
node_modules.workflow.pysays so in a comment and links them throughresolve_project_root/NodejsNpmLinkDependencyClosureAction. Because nothing exists beside the function, theos.path.isdir(installed)check skips the dependency, and the stale copy at the root stays in place.prepare_shared_project. Hereinstall_diris the project root, so only the root manifest is read.file:or bare-path dependencies that member packages declare, and that npm hoists into the rootnode_modules, are never considered.Either way the original stale-dependency bug (aws/aws-sam-cli#7546) still happens in monorepos, even though the PR description says hoisted dependencies are supported. None of the new tests cover a workspace layout.
A fix would resolve the npm project root (as
workflow.pyalready does withsubprocess_npm.resolve_project_root) and look for installed copies there. The shared-preparation case also needs the member manifests to be read.