From ba1641d7fdce898bc59313afcbf39a574316eb6e Mon Sep 17 00:00:00 2001 From: Colum Ferry Date: Thu, 19 Dec 2024 16:04:04 +0000 Subject: [PATCH] fix(bundling): ensure vitest timestamp files are ignored (#29418) ## Current Behavior Vitest TS config files produce timestamp temp files during project graph creation which are cleaned up. However, the creation and deletion of these files triggers the daemon to recalculate graph. It ends up in a loop. The vite timestamp files were already added to the gitignore to prevent this. ## Expected Behavior Add vitest timestamp files to gitignore --- .../__snapshots__/application.spec.ts.snap | 6 +- packages/vite/migrations.json | 5 ++ .../vite/src/generators/init/init.spec.ts | 7 ++- .../add-vite-temp-files-to-git-ignore.spec.ts | 13 +++-- ...dd-vitest-temp-files-to-git-ignore.spec.ts | 56 +++++++++++++++++++ .../add-vitest-temp-files-to-git-ignore.ts | 26 +++++++++ .../utils/add-vite-temp-files-to-gitignore.ts | 13 +++++ 7 files changed, 116 insertions(+), 10 deletions(-) create mode 100644 packages/vite/src/migrations/update-20-3-0/add-vitest-temp-files-to-git-ignore.spec.ts create mode 100644 packages/vite/src/migrations/update-20-3-0/add-vitest-temp-files-to-git-ignore.ts diff --git a/packages/nuxt/src/generators/application/__snapshots__/application.spec.ts.snap b/packages/nuxt/src/generators/application/__snapshots__/application.spec.ts.snap index 78d84c22e25a2..e454561c696dc 100644 --- a/packages/nuxt/src/generators/application/__snapshots__/application.spec.ts.snap +++ b/packages/nuxt/src/generators/application/__snapshots__/application.spec.ts.snap @@ -7,7 +7,8 @@ exports[`app generated files content - as-provided - my-app general application .nuxt .nitro .cache -vite.config.*.timestamp*" +vite.config.*.timestamp* +vitest.config.*.timestamp*" `; exports[`app generated files content - as-provided - my-app general application should add the nuxt and vitest plugins 1`] = ` @@ -366,7 +367,8 @@ exports[`app generated files content - as-provided - myApp general application s .nuxt .nitro .cache -vite.config.*.timestamp*" +vite.config.*.timestamp* +vitest.config.*.timestamp*" `; exports[`app generated files content - as-provided - myApp general application should add the nuxt and vitest plugins 1`] = ` diff --git a/packages/vite/migrations.json b/packages/vite/migrations.json index 3307720b80906..3fa187b455d84 100644 --- a/packages/vite/migrations.json +++ b/packages/vite/migrations.json @@ -29,6 +29,11 @@ "version": "20.0.6-beta.0", "description": "Add gitignore entry for temporary vite config files and remove previous incorrect glob.", "implementation": "./src/migrations/update-20-0-4/add-vite-temp-files-to-git-ignore" + }, + "update-20-3-0": { + "version": "20.3.0-beta.2", + "description": "Add gitignore entry for temporary vitest config files.", + "implementation": "./src/migrations/update-20-3-0/add-vitest-temp-files-to-git-ignore" } }, "packageJsonUpdates": { diff --git a/packages/vite/src/generators/init/init.spec.ts b/packages/vite/src/generators/init/init.spec.ts index 5e590a6d778b1..b7eaf1a845008 100644 --- a/packages/vite/src/generators/init/init.spec.ts +++ b/packages/vite/src/generators/init/init.spec.ts @@ -137,8 +137,9 @@ describe('@nx/vite:init', () => { await initGenerator(tree, {}); // ASSERT - expect(tree.read('.gitignore', 'utf-8')).toMatchInlineSnapshot( - `"vite.config.*.timestamp*"` - ); + expect(tree.read('.gitignore', 'utf-8')).toMatchInlineSnapshot(` + "vite.config.*.timestamp* + vitest.config.*.timestamp*" + `); }); }); diff --git a/packages/vite/src/migrations/update-20-0-4/add-vite-temp-files-to-git-ignore.spec.ts b/packages/vite/src/migrations/update-20-0-4/add-vite-temp-files-to-git-ignore.spec.ts index 7a007c9f95091..a1fbbf0ede685 100644 --- a/packages/vite/src/migrations/update-20-0-4/add-vite-temp-files-to-git-ignore.spec.ts +++ b/packages/vite/src/migrations/update-20-0-4/add-vite-temp-files-to-git-ignore.spec.ts @@ -13,7 +13,8 @@ describe('addViteTempFilesToGitIgnore', () => { // ASSERT expect(tree.read('.gitignore', 'utf-8')).toMatchInlineSnapshot(` ".idea - vite.config.*.timestamp*" + vite.config.*.timestamp* + vitest.config.*.timestamp*" `); }); @@ -33,7 +34,8 @@ describe('addViteTempFilesToGitIgnore', () => { expect(tree.read('.gitignore', 'utf-8')).toMatchInlineSnapshot(` ".idea - vite.config.*.timestamp*" + vite.config.*.timestamp* + vitest.config.*.timestamp*" `); }); @@ -46,8 +48,9 @@ describe('addViteTempFilesToGitIgnore', () => { addViteTempFilesToGitIgnore(tree); // ASSERT - expect(tree.read('.gitignore', 'utf-8')).toMatchInlineSnapshot( - `"vite.config.*.timestamp*"` - ); + expect(tree.read('.gitignore', 'utf-8')).toMatchInlineSnapshot(` + "vite.config.*.timestamp* + vitest.config.*.timestamp*" + `); }); }); diff --git a/packages/vite/src/migrations/update-20-3-0/add-vitest-temp-files-to-git-ignore.spec.ts b/packages/vite/src/migrations/update-20-3-0/add-vitest-temp-files-to-git-ignore.spec.ts new file mode 100644 index 0000000000000..8fcc9a841aa8c --- /dev/null +++ b/packages/vite/src/migrations/update-20-3-0/add-vitest-temp-files-to-git-ignore.spec.ts @@ -0,0 +1,56 @@ +import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; +import addViteTempFilesToGitIgnore from './add-vitest-temp-files-to-git-ignore'; + +describe('addViteTempFilesToGitIgnore', () => { + it('should update an existing .gitignore file to add the glob correctly', () => { + // ARRANGE + const tree = createTreeWithEmptyWorkspace(); + tree.write('.gitignore', '.idea'); + + // ACT + addViteTempFilesToGitIgnore(tree); + + // ASSERT + expect(tree.read('.gitignore', 'utf-8')).toMatchInlineSnapshot(` + ".idea + vite.config.*.timestamp* + vitest.config.*.timestamp*" + `); + }); + + it('should update an existing .gitignore file and remove incorrect glob and add the glob correctly', () => { + // ARRANGE + const tree = createTreeWithEmptyWorkspace(); + tree.write( + '.gitignore', + `.idea + **/vitest.config.{js,ts,mjs,mts,cjs,cts}.timestamp*` + ); + + // ACT + addViteTempFilesToGitIgnore(tree); + + // ASSERT + expect(tree.read('.gitignore', 'utf-8')).toMatchInlineSnapshot(` + ".idea + + vite.config.*.timestamp* + vitest.config.*.timestamp*" + `); + }); + + it('should write a new .gitignore file to add the glob correctly', () => { + // ARRANGE + const tree = createTreeWithEmptyWorkspace(); + tree.delete('.gitignore'); + + // ACT + addViteTempFilesToGitIgnore(tree); + + // ASSERT + expect(tree.read('.gitignore', 'utf-8')).toMatchInlineSnapshot(` + "vite.config.*.timestamp* + vitest.config.*.timestamp*" + `); + }); +}); diff --git a/packages/vite/src/migrations/update-20-3-0/add-vitest-temp-files-to-git-ignore.ts b/packages/vite/src/migrations/update-20-3-0/add-vitest-temp-files-to-git-ignore.ts new file mode 100644 index 0000000000000..0a6d6def75a68 --- /dev/null +++ b/packages/vite/src/migrations/update-20-3-0/add-vitest-temp-files-to-git-ignore.ts @@ -0,0 +1,26 @@ +import { Tree } from '@nx/devkit'; +import { addViteTempFilesToGitIgnore as _addViteTempFilesToGitIgnore } from '../../utils/add-vite-temp-files-to-gitignore'; + +export default function addViteTempFilesToGitIgnore(tree: Tree) { + // need to check if .gitignore exists before adding to it + // then need to check if it contains the following pattern + // **/vite.config.{js,ts,mjs,mts,cjs,cts}.timestamp* + // if it does, remove just this pattern + if (tree.exists('.gitignore')) { + const gitIgnoreContents = tree.read('.gitignore', 'utf-8'); + if ( + gitIgnoreContents.includes( + '**/vitest.config.{js,ts,mjs,mts,cjs,cts}.timestamp*' + ) + ) { + tree.write( + '.gitignore', + gitIgnoreContents.replace( + '**/vitest.config.{js,ts,mjs,mts,cjs,cts}.timestamp*', + '' + ) + ); + } + } + _addViteTempFilesToGitIgnore(tree); +} diff --git a/packages/vite/src/utils/add-vite-temp-files-to-gitignore.ts b/packages/vite/src/utils/add-vite-temp-files-to-gitignore.ts index e3d603042f112..fd49446f450be 100644 --- a/packages/vite/src/utils/add-vite-temp-files-to-gitignore.ts +++ b/packages/vite/src/utils/add-vite-temp-files-to-gitignore.ts @@ -13,4 +13,17 @@ export function addViteTempFilesToGitIgnore(tree: Tree) { } else { tree.write('.gitignore', newGitIgnoreContents); } + + newGitIgnoreContents = `vitest.config.*.timestamp*`; + if (tree.exists('.gitignore')) { + const gitIgnoreContents = tree.read('.gitignore', 'utf-8'); + if (!gitIgnoreContents.includes(newGitIgnoreContents)) { + newGitIgnoreContents = stripIndents`${gitIgnoreContents} + ${newGitIgnoreContents}`; + + tree.write('.gitignore', newGitIgnoreContents); + } + } else { + tree.write('.gitignore', newGitIgnoreContents); + } }