Repository navigation
fix: refresh local npm dependencies during in-source builds - #983
buffbiryani wants to merge 5 commits into
Conversation
bnusunny
left a comment
There was a problem hiding this comment.
Thanks for tracking this down. I reproduced aws/aws-sam-cli#7546 against develop and confirmed this branch fixes the file: case. The guards around deleting things (realpath checks, symlink skip, name regex, protecting sources) look solid.
-
Bare relative paths are missed. npm treats
"dep": "../dep"(also./,/,~/) as a directory dependency, just likefile:../dep._refresh_local_dependenciesonly matchesspec.startswith("file:"), so this form stays stale. I checked: with../dep, the second in-source build still ships the oldindex.js. Please normalise both forms with one helper and use it fordependenciesand forprotected_sources. Add a parametrized unit case for the bare form. -
The other in-source install path isn't covered. With
experimentalNodejsMonorepoand a lockfile, in-source builds useNodejsNpmInstallAction(npm install --install-links), notNodejsNpmUpdateAction. If that path has the same staleness, put the refresh in a shared helper onNodejsNpmInstallOrUpdateBaseActionand call it from both actions. If it doesn't, add a comment saying why only update needs it. -
Integration test style. The new file is a bare pytest function pinned to
nodejs20.x. Other tests intests/integration/workflows/nodejs_npm/areTestCaseclasses parameterized overNODEJS_RUNTIMES. Please fold this intotest_nodejs_npm.py, or at least parameterize it the same way, so it follows the runtime matrix. -
Nits:
_read_local_dependenciesreads and parsespackage.jsontwice. Read it once and derive both views."\x00" not in specis applied only toprotected_sources, not to the loop._paths_overlapcasefolds on every OS. That's fine, since it errs toward not deleting, but say so in the docstring.
Local run on this branch (node 24, npm 11): tests/unit/workflows/nodejs_npm plus the new integration test, 139 passed. CI is green.
|
Thanks for the detailed review! I appreciate the feedback. I'll address the requested changes, investigate the monorepo workflow, and update the PR once everything has been tested. |
There was a problem hiding this comment.
✅ Lambda Tooling PR Reviewer: No high-confidence issues found.
1 lower-confidence observation: these passed verification at a lower score, so check before acting
1. [BUG] aws_lambda_builders/workflows/nodejs_npm/actions.py:217: Refresh misses local dependencies that npm hoists to the workspace root
_refresh_local_dependencies only looks for the installed copy under <install_dir>/node_modules/<name>. In an npm workspaces build, install_dir is the member directory, but npm hoists dependencies, including file: ones, into the root's node_modules. The os.path.isdir(installed) check is then False, so the stale copy at the root is never removed. The shared install has a similar gap: prepare_shared_project reads only the root package.json, so local dependencies declared in a member's manifest are never checked. To confirm, build a workspace member that depends on file:../shared twice, changing shared between the two builds.
|
Thanks again for the detailed feedback! I've addressed all the requested changes and investigated the automated reviewer's workspace-hoisting observation. The updated implementation now supports both file-prefixed and bare local paths, both in-source installation workflows, and hoisted dependencies in individual workspace builds and shared project preparation. Final validation:
I've also synchronized the branch with the latest develop changes. The PR is ready for another review. Thanks for your time! |
bnusunny
left a comment
There was a problem hiding this comment.
Thanks for the quick turnaround. The earlier points are addressed: bare paths are handled, both in-source install paths refresh, the integration test runs across the Node.js runtimes, and the nits are fixed.
Please split the npm workspace support into its own PR.
The single-project fix for aws/aws-sam-cli#7546 was about 100 lines. The workspace handling adds about 200 more (_workspace_root, _workspace_metadata, _workspace_member*, _workspace_installed_copy, _workspace_refresh_candidates, _refresh_workspace_dependencies). It parses npm's hidden lockfile (node_modules/.package-lock.json), reproduces npm's staleness rule for it, and runs an extra npm pkg get name --workspaces --json on every build. That is a separate change with a larger surface and its own failure modes, and it's the part currently failing CI. Splitting it lets the original fix merge now, and the workspace part can follow with its own review.
Why CI fails (workspace test, npm 11). The three Ubuntu npm 11 jobs (node 20, 24, 26) each report 24 failed, 282 passed, all in test_workspace_refreshes_hoisted_local_dependency_*_file_* at line 196:
self.assertFalse(installed.parent.is_symlink())
AssertionError: True is not false
npm 11 changed how --install-links treats a file: dependency declared by a workspace member. In the test's setup step:
| npm | "file:../shared" |
"../shared" |
|---|---|---|
| 8.19.4, 9.9.4, 10.9.11 | copied | copied |
| 11.6.0, 11.10.0, 11.19.0 | symlink (lockfile {"resolved": "shared", "link": true}) |
copied |
So the test's precondition (a hoisted copy) doesn't hold on npm 11, and it fails before the refresh logic runs. The refresh code itself handles this correctly, since a symlinked install is skipped and is never stale. In the follow-up PR, the test should accept both outcomes: either assert only that the artifact is updated after the second build, or skip the is_symlink check for file: cases on npm 11+ with a comment.
Two small things for the workspace PR:
- When
_refresh_workspace_dependenciesfails closed, log the reason at debug level. _workspace_metadataignores the hidden lockfile when it's older thannode_modules, so any manual change undernode_modulessilently disables the refresh until the nextnpm install. That mirrors npm's rule, but it deserves a comment.
|
Thanks! I've separated the workspace support into its own branch and narrowed this PR to the original single-project fix. The updated implementation passed all 154 Node.js unit tests and all 24 regression tests under both npm 10 and npm 11, along with Ruff and Black. I've also preserved the workspace changes, including the npm 11 symlink compatibility fix, for the follow-up PR. I'll address the debug logging and hidden lockfile documentation there. Thanks again for your help! |
| installed = os.path.join(modules_dir, *name.split("/")) | ||
|
|
||
| if not os.path.isdir(installed) or os.path.islink(installed): | ||
| continue |
There was a problem hiding this comment.
[BUG] Local dependencies hoisted to an npm workspace root are never refreshed
_refresh_local_dependencies reads only install_dir/package.json and looks for installed copies only at install_dir/node_modules/<name>. If that directory is missing, the dependency is skipped.
This misses both workspace cases:
- Building one workspace member in source. npm hoists the member's dependencies into the workspace root's
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.py already does with subprocess_npm.resolve_project_root) and look for installed copies there. The shared-preparation case also needs the member manifests to be read.
Issue #, if available: aws/aws-sam-cli#7546
Description of changes
Fixes an issue where local npm dependencies remain stale during in-source builds after their source files are modified.
Workspace support has been separated into a follow-up branch as requested during review.
Description of how you validated changes
Checklist
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.