diff --git a/packages/nx/src/generators/utils/project-configuration.ts b/packages/nx/src/generators/utils/project-configuration.ts index 21bfaeab4d435..c8ab3c12e597e 100644 --- a/packages/nx/src/generators/utils/project-configuration.ts +++ b/packages/nx/src/generators/utils/project-configuration.ts @@ -465,6 +465,7 @@ function findDeletedProjects(tree: Tree) { } let staticFSWorkspace: RawProjectsConfigurations; +let cachedTree: Tree; function readRawWorkspaceJson(tree: Tree): RawProjectsConfigurations { const path = getWorkspacePath(tree); if (path && tree.exists(path)) { @@ -477,13 +478,14 @@ function readRawWorkspaceJson(tree: Tree): RawProjectsConfigurations { findCreatedProjects(tree), (file) => readJson(tree, file) ).projects; - // We already have built a cache - if (!staticFSWorkspace) { + // We already have built a cache but need to confirm it's the same tree + if (!staticFSWorkspace || tree !== cachedTree) { staticFSWorkspace = buildWorkspaceConfigurationFromGlobs( nxJson, [...globForProjectFiles(tree.root, nxJson)], (file) => readJson(tree, file) ); + cachedTree = tree; } const projects = { ...staticFSWorkspace.projects, ...createdProjects }; findDeletedProjects(tree).forEach((file) => { @@ -498,10 +500,11 @@ function readRawWorkspaceJson(tree: Tree): RawProjectsConfigurations { delete projects[matchingStaticProject[0]]; } }); - return { + staticFSWorkspace = { ...staticFSWorkspace, projects, }; + return staticFSWorkspace; } }