diff --git a/packages/nx/src/plugins/project-json/build-nodes/package-json-next-to-project-json.spec.ts b/packages/nx/src/plugins/project-json/build-nodes/package-json-next-to-project-json.spec.ts index f0e3b0be23..6d7607eca9 100644 --- a/packages/nx/src/plugins/project-json/build-nodes/package-json-next-to-project-json.spec.ts +++ b/packages/nx/src/plugins/project-json/build-nodes/package-json-next-to-project-json.spec.ts @@ -18,9 +18,13 @@ describe('nx project.json plugin', () => { }; }); - it('should build projects from project.json', () => { + it('should build projects from package.json next to project.json', () => { memfs.vol.fromJSON( { + 'package.json': JSON.stringify({ + name: 'lib-a', + description: 'lib-a project description', + }), 'packages/lib-a/project.json': JSON.stringify({ name: 'lib-a', description: 'lib-a project description', @@ -85,4 +89,38 @@ describe('nx project.json plugin', () => { } `); }); + + it('should not build package manager workspace projects from package.json next to project.json', () => { + memfs.vol.fromJSON( + { + 'package.json': JSON.stringify({ + name: 'lib-a', + description: 'lib-a project description', + workspaces: ['packages/lib-a'], + }), + 'packages/lib-a/project.json': JSON.stringify({ + name: 'lib-a', + description: 'lib-a project description', + targets: { + build: { + executor: 'nx:run-commands', + options: {}, + }, + }, + }), + 'packages/lib-a/package.json': JSON.stringify({ + name: 'lib-a', + description: 'lib-a package description', + scripts: { + test: 'jest', + }, + }), + }, + '/root' + ); + + expect( + createNodesFunction('packages/lib-a/project.json', undefined, context) + ).toMatchInlineSnapshot(`{}`); + }); }); diff --git a/packages/nx/src/plugins/project-json/build-nodes/package-json-next-to-project-json.ts b/packages/nx/src/plugins/project-json/build-nodes/package-json-next-to-project-json.ts index 112c4798c5..bfdb6f5ca3 100644 --- a/packages/nx/src/plugins/project-json/build-nodes/package-json-next-to-project-json.ts +++ b/packages/nx/src/plugins/project-json/build-nodes/package-json-next-to-project-json.ts @@ -9,6 +9,7 @@ import { getTagsFromPackageJson, readTargetsFromPackageJson, } from '../../../utils/package-json'; +import { buildPackageJsonWorkspacesMatcher } from '../../package-json-workspaces'; // TODO: Remove this one day, this should not need to be done. @@ -42,8 +43,16 @@ function createProjectFromPackageJsonNextToProjectJson( workspaceRoot: string ): ProjectConfiguration | null { const root = dirname(projectJsonPath); - const packageJsonPath = join(workspaceRoot, root, 'package.json'); - if (!existsSync(packageJsonPath)) { + const relativePackageJsonPath = join(root, 'package.json'); + const packageJsonPath = join(workspaceRoot, relativePackageJsonPath); + const readJson = (f) => readJsonFile(join(workspaceRoot, f)); + + // Do not create projects for package.json files + // that are part of the package manager workspaces + // Those package.json files will be processed later on + const matcher = buildPackageJsonWorkspacesMatcher(workspaceRoot, readJson); + + if (!existsSync(packageJsonPath) || matcher(relativePackageJsonPath)) { return null; } try {