Skip to content

fix(create-blocks-app): ignore node_modules when adding AWS Blocks to an existing project - #1506

Merged
xyos merged 4 commits into
aws:mainfrom
xyos:fix/create-blocks-app-gitignore-node-modules
Nov 18, 2026
Merged

xyos merged 4 commits into
aws:mainfrom
xyos:fix/create-blocks-app-gitignore-node-modules

Conversation

@xyos

@xyos xyos commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Problem

npx @aws-blocks/create-blocks-app . --yes in a project that has no .gitignore creates one with the AWS Blocks entries and the secret patterns, but no line for node_modules. The CLI adds dependencies to package.json, so the next npm install creates node_modules, and the next git add -A stages it.

Minimal reproduction:

mkdir site && cd site && git init -q
echo '{"name":"site","version":"1.0.0"}' > package.json
npx @aws-blocks/create-blocks-app . --yes
git check-ignore node_modules/x   # prints nothing, exit 1

The same happens when the project has a .gitignore that never needed a node_modules line (for example a site of plain files that had no dependencies before). New apps from a template are not affected: every template gitignore lists node_modules.

Issue #, if available:

Changes

  • addBlocksWorkspace (shared by the existing-project and the Amplify Gen 2 paths) now appends a # Dependencies section with node_modules to .gitignore when the file does not exist or when none of its lines ignores node_modules at the project root.
  • Choice for an existing .gitignore: the line is added there too, not only when the file is created. The CLI is what introduces the dependencies, and a missing line leads to the same staged node_modules. A line counts as present only in a literal form: node_modules, node_modules/, /node_modules, node_modules/*, node_modules/**, **/node_modules, or a bare *. Any other pattern (globs, character classes, leading spaces) gets the line added, because a duplicate line does no harm. When the last matching line is !node_modules, the CLI adds nothing and prints a note, so an explicit un-ignore (vendored dependencies) is respected. A line for a nested path does not count.
  • The added line is listed in the ✓ Modified .gitignore (+…) output with the others.
  • README: the "Add to Existing Project" section now says what is added to .gitignore.
  • Changeset: @aws-blocks/create-blocks-app patch.

Validation

Added to packages/create-blocks-app/src/templates-security-defaults.test.ts (all run the built CLI against a temporary project and ask git check-ignore):

  • ignores node_modules in …: a project with no .gitignore, one whose .gitignore has no node_modules line, an Amplify Gen 2 project, one that only ignores a nested node_modules, and one that un-ignores it after ignoring it. These fail without the fix.
  • adds no node_modules line when the existing .gitignore has …: node_modules, node_modules/, /node_modules, /node_modules/, **/node_modules; the file keeps exactly its one line.
  • template "<name>" ignores node_modules: every template gitignore, so the fresh-project path and the existing-project path stay in step.

The contributor checklist on this commit (1d0770953), run locally on Node.js 22.23.1 after npm ci:

  • npm run build, npm run lint, npm run lint:deps: pass
  • npm run check:api: pass, the API reports are unchanged
  • The repository guards of the pull request workflow (check:exports-consistency, check:runtime-guard, check:client-user-agent, scripts/sink-guards): pass
  • npm test (every workspace): 13,152 tests, 1 failure, 10 skipped. The failure is dist/secret-cli.test.js of @aws-blocks/hosting, in a package this change does not touch: "Unable to deserialize cloned data due to invalid or unsupported version", raised by the test runner while it read the file's output, not by an assertion (test(hosting,blocks): keep CLI and adapter output off the test runner's stdout #1330 describes the cause). npm test -w @aws-blocks/hosting run again on the same commit: pass, 1,827 tests, 0 failures (attempt 4; the same error came up in the 3 attempts before it)
  • npm run test:e2e:local: pass, 557 tests, 0 failures, 6 skipped
  • npm test -w packages/create-blocks-app (the package this change touches): 374 tests, 0 failures, within the full run above

Review follow-up (1be1a6d32): npm run build, npm run lint:deps and npm test -w packages/create-blocks-app (381 tests, 0 failures) pass. New cases cover !node_modules, [!n]ode_modules, n*x*s, a leading-space line, node_modules/*, node_modules/** and a CRLF file.

Checklist

  • PR description included
  • Tests are changed or added
  • Relevant documentation is changed or added (and PR referenced)

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

… an existing project

Adding AWS Blocks to a project with no .gitignore created one with the
AWS Blocks entries and the secret patterns but no node_modules line. The
CLI adds dependencies, so the next npm install created node_modules and
the next `git add -A` staged it.

addBlocksWorkspace now appends node_modules when the .gitignore is new or
when none of its lines ignores node_modules at the project root. A line
such as node_modules, node_modules/, /node_modules or **/node_modules
counts as present, so nothing is duplicated; a later !node_modules or a
line for a nested path does not. The templates already ignore it; a test
now checks every template so both paths stay in step.

Validation: npm run build, npm run lint, npm run lint:deps, npm test -w packages/create-blocks-app (374 pass)
@changeset-bot

changeset-bot Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: ef5f4e2

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@aws-blocks/create-blocks-app Patch
@aws-blocks/blocks Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@xyos
xyos marked this pull request as ready for review November 16, 2026 11:36
@xyos
xyos requested a review from a team as a code owner November 16, 2026 11:36

@osama-rizk osama-rizk 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.

This adds a node_modules line to .gitignore when AWS Blocks is added to an existing or Amplify Gen 2 project, and only when no existing line already covers it. I'm leaving a comment rather than requesting changes: the main paths behave correctly and I found nothing blocking. Two behaviors are worth a second look before merge: the deliberate !node_modules case, and which way the coverage heuristic fails.

What's good:

  • It extends the existing patch flow from #1139 and #1301 instead of building a second one. The added lines go through the same append step, keep CRLF line endings, and show up in the Modified .gitignore (+…) output. I ran it against a CRLF file and the result stayed CRLF throughout.
  • Running it twice is safe. After the first run the file has a covering line, so no duplicate is written. (A real second run stops earlier anyway, at aws-blocks/ already exists.)
  • Applying "last matching line wins" and skipping nested paths matches how git really behaves. Both rules have tests. I broke each one in dist/ by hand, and the matching test failed both times.
  • The template parity test (template "<name>" ignores node_modules) is a good guard that keeps the new-app and existing-project paths in step.
  • I checked the history of packages/create-blocks-app (git log -S node_modules, plus #1139, #1301, #593 and #1207). Nothing there ever added and then removed a node_modules ignore line, so this isn't bringing back something that was reverted.

What needs work: 0 must-fix · 2 should-fix · 1 minor · 2 nits. Details are in the inline comments.

Open questions for the author:

  1. Should an explicit !node_modules count as the user's decision and be left alone? Right now the CLI appends node_modules after it, which quietly reverses that choice for any dependency added later.
  2. Would you be open to biasing ignoresRootDirectory towards adding the line? A duplicate node_modules line does no harm. A line wrongly treated as covering it leaves the original bug in place.

Cross-cutting: I checked the edge cases you listed, using the built CLI and git check-ignore. These all behave correctly: no .gitignore, a file without a trailing newline, CRLF, node_modules/*, node_modules/**, **/node_modules/, * followed by !package.json, and a mid-line #. /**/node_modules adds a duplicate line, which is harmless. The cases that leave node_modules unignored are in the inline comment on line 343. The full package test run on Node.js 22 passes all the .gitignore suites. The only failures were in tests that need packages/blocks built or that hit the macOS /private/var path, and none of them is related to this change.

Comment thread packages/create-blocks-app/src/index.ts Resolved
Comment thread packages/create-blocks-app/src/index.ts Resolved
Comment thread packages/create-blocks-app/src/index.ts Resolved
Comment thread packages/create-blocks-app/src/index.ts Resolved
… a literal one covers it

A .gitignore whose last matching line is !node_modules (vendored
dependencies) now keeps that choice: the CLI adds no node_modules line and
prints a note. Coverage now counts only literal lines (node_modules,
node_modules/, /node_modules, node_modules/*, node_modules/**,
**/node_modules, or *) read without trimming leading spaces, so a pattern
such as [!n]ode_modules, n*x*s or "  node_modules" gets the line added; a
duplicate line is harmless, a wrong match is not. The plan preview now
mentions node_modules.
@xyos

xyos commented Nov 16, 2026

Copy link
Copy Markdown
Contributor Author

Replies to the review summary:

  • Q1 (explicit !node_modules): Yes. If the last matching line is !node_modules, the CLI now leaves the file alone and prints a note (fixed in 1be1a6d).

  • Q2 (bias towards adding): Yes. The CLI now treats only literal forms as covering node_modules and adds the line for anything else, because a duplicate line does no harm (fixed in 1be1a6d).

osama-rizk
osama-rizk previously approved these changes Nov 16, 2026

@osama-rizk osama-rizk 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.

Since the last review: 4 resolved · 2 new · 0 still open (1 answered and settled)

Thanks for the quick turnaround. The three-state check is a clear improvement, and the literal-only matching now errs in the right direction. Approving; the two new items below are small follow-ups, not blockers.

Resolved

  • !node_modules as the last matching line is now respected: nothing is added and the note prints. Checked with !node_modules, node_modules then !node_modules, !node_modules/, !/node_modules, and a CRLF (Windows line ending) variant. node_modules, !node_modules, node_modules still counts as ignored.
  • Only literal forms count now. [!n]ode_modules, n*x*s, indented node_modules, node_modules # deps, \!node_modules, #node_modules and \#node_modules all get the line added, and git check-ignore confirms each one ends up ignored.
  • Both plan-preview strings mention node_modules, and the preview test pins the new text.
  • The no-duplicate list now covers node_modules/*, node_modules/** and CRLF. I also checked /node_modules/, **/node_modules/**, /*, */ and **/*.

Settled

  • git check-ignore for the monorepo case: agreed. Per-user excludes aren't shared with teammates, so a duplicate line is the safer outcome.

What I verified on 1be1a6d: the package builds, and templates-security-defaults.test.js passes 113/113. I ran an edge-case matrix of about 35 .gitignore shapes through the built CLI (command-line interface) and compared each result with git check-ignore before and after. I also broke the code on purpose three ways: treating un-ignored as "add", loosening pattern === name, and loosening the * check. The tests catch all three. The secret-exception path (mayIgnore) is unchanged.

Follow-ups (non-blocking, details inline)

  • 1 minor: a trailing tab is trimmed, but git keeps it, so node_modules<TAB> counts as covering when git doesn't ignore anything.
  • 1 nit: the "un-ignored" note also prints for node_modules followed by !node_modules/*, which git still ignores.

No open questions.

Comment thread packages/create-blocks-app/src/index.ts Resolved
Comment thread packages/create-blocks-app/src/index.ts Resolved
@xyos
xyos requested a review from osama-rizk November 16, 2026 13:25
…nes, and keep !node_modules/* from counting as un-ignored

Git strips trailing spaces but not tabs, so a line of node_modules followed
by a tab doesn't ignore node_modules; it no longer counts as covering it.
Git can't re-include files under an excluded directory, so !node_modules/*
after node_modules leaves it ignored, and the CLI no longer prints that the
.gitignore un-ignores it.
…ents apart in the .gitignore check

!node_modules/* after node_modules/* leaves the directory included, so git
re-includes its contents: that now counts as an explicit un-ignore, as
!node_modules does. After node_modules it still changes nothing, because git
can't re-include files under an excluded directory. A negation that reverses
no earlier line no longer counts either, so the line is added.

@osama-rizk osama-rizk 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.

Since the last review: 2 resolved · 0 new · 0 still open

The push dismissed my approval at 1be1a6d, and the two commits before it are unchanged. Re-approving at the new head after reading the two new commits.

Resolved:

  • Only trailing spaces are stripped now, not tabs, matching git. A trailing tab stays part of the pattern, and the test for node_modules\t guards it. The strip is a loop rather than / +$/, so there is no quadratic backtracking on long runs of spaces.
  • The directory and its contents are tracked apart. node_modules/* then !node_modules/* counts as un-ignored. node_modules then !node_modules/* stays ignored, because git can't re-include files under an excluded directory. A lone !node_modules/* is no choice either way. Each case is checked against git check-ignore in the tests, not only against the function's own answer.

I also walked node_modules/* followed by !node_modules by hand: the function says ignored, and git agrees, since the contents rule still matches.

CI: the only red jobs are Local Unit Tests (shard 3) and the two summary jobs that wait on it. The failing tests are all in the auth-oidc template checks (favicon, watch.exclude, TEST_URL/BLOCKS_API_URL, setTimeout), which this PR doesn't touch. That template landed on main in #932 and was aligned with the template checks in #1596, so a rerun on current main should clear it. Every other shard is green.

@xyos
xyos merged commit c3aecc5 into aws:main Nov 18, 2026
42 of 45 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants