From 22f93ad139a032c857c3c2281eada3411d9ebf87 Mon Sep 17 00:00:00 2001 From: Jonathan Cammisuli Date: Tue, 15 Oct 2024 14:14:06 -0400 Subject: [PATCH] fix(core): add migration to set `useLegacyCache` by default --- packages/nx/migrations.json | 5 ++ .../update-20-0-1/use-legacy-cache.spec.ts | 59 +++++++++++++++++++ .../update-20-0-1/use-legacy-cache.ts | 19 ++++++ 3 files changed, 83 insertions(+) create mode 100644 packages/nx/src/migrations/update-20-0-1/use-legacy-cache.spec.ts create mode 100644 packages/nx/src/migrations/update-20-0-1/use-legacy-cache.ts diff --git a/packages/nx/migrations.json b/packages/nx/migrations.json index ac2d45987cb7d..c1cff40ad5fca 100644 --- a/packages/nx/migrations.json +++ b/packages/nx/migrations.json @@ -57,6 +57,11 @@ "version": "20.0.0-beta.7", "description": "Migration for v20.0.0-beta.7", "implementation": "./src/migrations/update-20-0-0/move-use-daemon-process" + }, + "use-legacy-cache": { + "version": "20.0.1", + "description": "Set `useLegacyCache` to true for migrating workspaces", + "implementation": "./src/migrations/update-20-0-1/use-legacy-cache" } } } diff --git a/packages/nx/src/migrations/update-20-0-1/use-legacy-cache.spec.ts b/packages/nx/src/migrations/update-20-0-1/use-legacy-cache.spec.ts new file mode 100644 index 0000000000000..af3e9a8a1e806 --- /dev/null +++ b/packages/nx/src/migrations/update-20-0-1/use-legacy-cache.spec.ts @@ -0,0 +1,59 @@ +import { createTreeWithEmptyWorkspace } from '../../generators/testing-utils/create-tree-with-empty-workspace'; +import { Tree } from '../../generators/tree'; + +import update from './use-legacy-cache'; +import { readNxJson, updateNxJson } from '../../generators/utils/nx-json'; + +describe('use legacy cache', () => { + let tree: Tree; + + beforeEach(() => { + tree = createTreeWithEmptyWorkspace(); + }); + + it('should add `useLegacyCache` on migrating workspaces that did not have `enableDbCache`', async () => { + await update(tree); + + expect(readNxJson(tree)).toMatchInlineSnapshot(` + { + "affected": { + "defaultBase": "main", + }, + "targetDefaults": { + "build": { + "cache": true, + }, + "lint": { + "cache": true, + }, + }, + "useLegacyCache": true, + } + `); + }); + it('should not add `useLegacyCache` on migrating workspaces that did have `enableDbCache`', async () => { + const nxJson = readNxJson(tree); + updateNxJson(tree, { + enableDbCache: true, + ...nxJson, + } as any); + + await update(tree); + + expect(readNxJson(tree)).toMatchInlineSnapshot(` + { + "affected": { + "defaultBase": "main", + }, + "targetDefaults": { + "build": { + "cache": true, + }, + "lint": { + "cache": true, + }, + }, + } + `); + }); +}); diff --git a/packages/nx/src/migrations/update-20-0-1/use-legacy-cache.ts b/packages/nx/src/migrations/update-20-0-1/use-legacy-cache.ts new file mode 100644 index 0000000000000..89e6eb80c8fce --- /dev/null +++ b/packages/nx/src/migrations/update-20-0-1/use-legacy-cache.ts @@ -0,0 +1,19 @@ +import { Tree } from '../../generators/tree'; +import { formatChangedFilesWithPrettierIfAvailable } from '../../generators/internal-utils/format-changed-files-with-prettier-if-available'; +import { readNxJson, updateNxJson } from '../../generators/utils/nx-json'; +import { NxJsonConfiguration } from '../../config/nx-json'; + +export default async function update(tree: Tree) { + const nxJson = readNxJson(tree) as NxJsonConfiguration; + + // If workspaces had `enableDbCache` we can just delete the property as the db cache is enabled by default in nx v20 + if ((nxJson as any).enableDbCache) { + delete (nxJson as any).enableDbCache; + } else { + nxJson.useLegacyCache = true; + } + + updateNxJson(tree, nxJson); + + await formatChangedFilesWithPrettierIfAvailable(tree); +}