diff --git a/packages/nx/src/generators/utils/project-configuration.spec.ts b/packages/nx/src/generators/utils/project-configuration.spec.ts index c96e4daaf0fa6..bec512e7ba3c4 100644 --- a/packages/nx/src/generators/utils/project-configuration.spec.ts +++ b/packages/nx/src/generators/utils/project-configuration.spec.ts @@ -329,5 +329,46 @@ describe('project configuration', () => { `); expect(tree.exists('proj/project.json')).toBeFalsy(); }); + + it('should avoid writing empty nx property', () => { + writeJson(tree, 'proj/package.json', { + name: 'proj', + }); + + updateProjectConfiguration(tree, 'proj', { + root: 'proj', + }); + + const updatedProj = readProjectConfiguration(tree, 'proj'); + expect(updatedProj).toEqual({ + name: 'proj', + root: 'proj', + }); + + expect(tree.read('proj/package.json', 'utf-8')).toMatchInlineSnapshot(` + "{ + "name": "proj" + } + " + `); + expect(tree.exists('proj/project.json')).toBeFalsy(); + + // Adding tags will add nx property + updateProjectConfiguration(tree, 'proj', { + root: 'proj', + tags: ['test'], + }); + expect(tree.read('proj/package.json', 'utf-8')).toMatchInlineSnapshot(` + "{ + "name": "proj", + "nx": { + "tags": [ + "test" + ] + } + } + " + `); + }); }); }); diff --git a/packages/nx/src/generators/utils/project-configuration.ts b/packages/nx/src/generators/utils/project-configuration.ts index 5011db23a4b46..0eec94fa4515d 100644 --- a/packages/nx/src/generators/utils/project-configuration.ts +++ b/packages/nx/src/generators/utils/project-configuration.ts @@ -130,10 +130,19 @@ function updateProjectConfigurationInPackageJson( packageJson.nx = { ...packageJson.nx, ...projectConfiguration, - root: undefined, }; - writeJson(tree, packageJsonFile, packageJson); + // We don't want to ever this since it is inferred + delete packageJson.nx.root; + + // Only set `nx` property in `package.json` if it is a root project (necessary to mark it as Nx project), + // or if there are properties to be set. If it is empty, then avoid it so we don't add unnecessary boilerplate. + if ( + projectConfiguration.root === '.' || + Object.keys(packageJson.nx).length > 0 + ) { + writeJson(tree, packageJsonFile, packageJson); + } } function updateProjectConfigurationInProjectJson( @@ -245,8 +254,15 @@ function readAndCombineAllProjectConfigurations(tree: Tree): { const patterns = [ '**/project.json', 'project.json', - ...getGlobPatternsFromPackageManagerWorkspaces(tree.root, (p) => - readJson(tree, p, { expectComments: true }) + ...getGlobPatternsFromPackageManagerWorkspaces( + tree.root, + (p) => readJson(tree, p, { expectComments: true }), + (p) => { + const content = tree.read(p, 'utf-8'); + const { load } = require('@zkochan/js-yaml'); + return load(content, { filename: p }) as T; + }, + (p) => tree.exists(p) ), ]; const globbedFiles = globWithWorkspaceContextSync(tree.root, patterns); diff --git a/packages/nx/src/plugins/package-json/create-nodes.ts b/packages/nx/src/plugins/package-json/create-nodes.ts index e30202d053def..66c9128026013 100644 --- a/packages/nx/src/plugins/package-json/create-nodes.ts +++ b/packages/nx/src/plugins/package-json/create-nodes.ts @@ -237,8 +237,14 @@ export function buildProjectConfigurationFromPackageJson( */ export function getGlobPatternsFromPackageManagerWorkspaces( root: string, - readJson: (path: string) => T = (path) => - readJsonFile(join(root, path)) // making this an arg allows us to reuse in devkit + // allow overwriting these args so we can use them in devkit + readJson: (path: string) => T = ( + path: string + ) => readJsonFile(join(root, path)), + readYaml: (path: string) => T = ( + path: string + ) => readYamlFile(join(root, path)), + exists: (path: string) => boolean = (p) => existsSync(join(root, p)) ): string[] { try { const patterns: string[] = []; @@ -252,12 +258,10 @@ export function getGlobPatternsFromPackageManagerWorkspaces( ) ); - if (existsSync(join(root, 'pnpm-workspace.yaml'))) { + if (exists('pnpm-workspace.yaml')) { try { const { packages } = - readYamlFile<{ packages: string[] }>( - join(root, 'pnpm-workspace.yaml') - ) ?? {}; + readYaml<{ packages: string[] }>('pnpm-workspace.yaml') ?? {}; patterns.push(...normalizePatterns(packages || [])); } catch (e: unknown) { output.warn({