Skip to content

fix: refresh local npm dependencies during in-source builds - #983

Open
buffbiryani wants to merge 5 commits into
aws:developfrom
buffbiryani:fix/local-npm-dependency-refresh
Open

buffbiryani wants to merge 5 commits into
aws:developfrom
buffbiryani:fix/local-npm-dependency-refresh

Conversation

@buffbiryani

@buffbiryani buffbiryani commented Nov 30, 2026 •

Copy link
Copy Markdown

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.

  • Refreshes installed copies of local directory dependencies before npm install or npm update.
  • Supports both file-prefixed and bare local dependency paths.
  • Supports both in-source installation workflows.
  • Protects original source directories, symbolic links, unrelated dependencies, and lockfiles.
  • Adds parameterized integration tests across supported Node.js runtimes.

Workspace support has been separated into a follow-up branch as requested during review.

Description of how you validated changes

  • Reproduced the original issue using SAM CLI 1.174.1 and npm 10.9.2.
  • Verified successful consecutive rebuilds using the original reporter's application.
  • Addressed the requested reviewer changes.
  • Passed 154 Node.js unit tests after separating workspace support.
  • Passed all 24 single-project regression tests under npm 10.9.2.
  • Passed all 24 single-project regression tests under npm 11.21.3.
  • Passed Ruff, Black, and Git diff checks.

Checklist

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@buffbiryani
buffbiryani requested a review from a team as a code owner November 30, 2026 06:33
@github-actions github-actions Bot added pr/external stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at. area/workflow/node_npm labels Nov 30, 2026

@bnusunny bnusunny left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

  1. Bare relative paths are missed. npm treats "dep": "../dep" (also ./, /, ~/) as a directory dependency, just like file:../dep. _refresh_local_dependencies only matches spec.startswith("file:"), so this form stays stale. I checked: with ../dep, the second in-source build still ships the old index.js. Please normalise both forms with one helper and use it for dependencies and for protected_sources. Add a parametrized unit case for the bare form.

  2. The other in-source install path isn't covered. With experimentalNodejsMonorepo and a lockfile, in-source builds use NodejsNpmInstallAction (npm install --install-links), not NodejsNpmUpdateAction. If that path has the same staleness, put the refresh in a shared helper on NodejsNpmInstallOrUpdateBaseAction and call it from both actions. If it doesn't, add a comment saying why only update needs it.

  3. Integration test style. The new file is a bare pytest function pinned to nodejs20.x. Other tests in tests/integration/workflows/nodejs_npm/ are TestCase classes parameterized over NODEJS_RUNTIMES. Please fold this into test_nodejs_npm.py, or at least parameterize it the same way, so it follows the runtime matrix.

  4. Nits:

    • _read_local_dependencies reads and parses package.json twice. Read it once and derive both views.
    • "\x00" not in spec is applied only to protected_sources, not to the loop.
    • _paths_overlap casefolds 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.

@buffbiryani

Copy link
Copy Markdown
Author

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.

@aws-sam-tooling-bot aws-sam-tooling-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ 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.

@buffbiryani

Copy link
Copy Markdown
Author

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:

  • 1,252 unit and functional tests passed
  • 306 Node.js integration tests passed
  • 64 additional subtests passed
  • 94.55% code coverage, exceeding the required 94%
  • All lint, formatting, and Git diff checks passed

I've also synchronized the branch with the latest develop changes.

The PR is ready for another review. Thanks for your time!

@bnusunny bnusunny left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_dependencies fails closed, log the reason at debug level.
  • _workspace_metadata ignores the hidden lockfile when it's older than node_modules, so any manual change under node_modules silently disables the refresh until the next npm install. That mirrors npm's rule, but it deserves a comment.

@buffbiryani

buffbiryani commented Dec 1, 2026 •

Copy link
Copy Markdown
Author

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!

@aws-sam-tooling-bot aws-sam-tooling-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review Results

Reviewed: c994d47..692c254
Files: 3
Comments: 1

Comment on lines +214 to +217
installed = os.path.join(modules_dir, *name.split("/"))

if not os.path.isdir(installed) or os.path.islink(installed):
continue

Copy link
Copy Markdown

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_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.py says so in a comment and links them through resolve_project_root / NodejsNpmLinkDependencyClosureAction. Because nothing exists beside the function, the os.path.isdir(installed) check skips the dependency, and the stale copy at the root stays in place.
  • prepare_shared_project. Here install_dir is 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 root node_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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/workflow/node_npm pr/external stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants