Repository navigation
fix(create-blocks-app): ignore node_modules when adding AWS Blocks to an existing project - #1506
Conversation
… 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 detectedLatest commit: ef5f4e2 The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
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 |
osama-rizk
left a comment
There was a problem hiding this comment.
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 anode_modulesignore 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:
- Should an explicit
!node_modulescount as the user's decision and be left alone? Right now the CLI appendsnode_modulesafter it, which quietly reverses that choice for any dependency added later. - Would you be open to biasing
ignoresRootDirectorytowards adding the line? A duplicatenode_modulesline 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.
… 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.
|
Replies to the review summary:
|
osama-rizk
left a comment
There was a problem hiding this comment.
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_modulesas the last matching line is now respected: nothing is added and the note prints. Checked with!node_modules,node_modulesthen!node_modules,!node_modules/,!/node_modules, and a CRLF (Windows line ending) variant.node_modules,!node_modules,node_modulesstill counts as ignored.- Only literal forms count now.
[!n]ode_modules,n*x*s, indentednode_modules,node_modules # deps,\!node_modules,#node_modulesand\#node_modulesall get the line added, andgit check-ignoreconfirms 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-ignorefor 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_modulesfollowed by!node_modules/*, which git still ignores.
No open questions.
…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
left a comment
There was a problem hiding this comment.
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\tguards 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_modulesthen!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 againstgit check-ignorein 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.
Problem
npx @aws-blocks/create-blocks-app . --yesin a project that has no.gitignorecreates one with the AWS Blocks entries and the secret patterns, but no line fornode_modules. The CLI adds dependencies topackage.json, so the nextnpm installcreatesnode_modules, and the nextgit add -Astages it.Minimal reproduction:
The same happens when the project has a
.gitignorethat never needed anode_modulesline (for example a site of plain files that had no dependencies before). New apps from a template are not affected: every templategitignorelistsnode_modules.Issue #, if available:
Changes
addBlocksWorkspace(shared by the existing-project and the Amplify Gen 2 paths) now appends a# Dependenciessection withnode_modulesto.gitignorewhen the file does not exist or when none of its lines ignoresnode_modulesat the project root..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 stagednode_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.✓ Modified .gitignore (+…)output with the others..gitignore.@aws-blocks/create-blocks-apppatch.Validation
Added to
packages/create-blocks-app/src/templates-security-defaults.test.ts(all run the built CLI against a temporary project and askgit check-ignore):ignores node_modules in …: a project with no.gitignore, one whose.gitignorehas nonode_modulesline, an Amplify Gen 2 project, one that only ignores a nestednode_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 templategitignore, 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 afternpm ci:npm run build,npm run lint,npm run lint:deps: passnpm run check:api: pass, the API reports are unchangedcheck:exports-consistency,check:runtime-guard,check:client-user-agent,scripts/sink-guards): passnpm test(every workspace): 13,152 tests, 1 failure, 10 skipped. The failure isdist/secret-cli.test.jsof@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/hostingrun 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 skippednpm test -w packages/create-blocks-app(the package this change touches): 374 tests, 0 failures, within the full run aboveReview follow-up (
1be1a6d32):npm run build,npm run lint:depsandnpm 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
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.