Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion packages/nx/src/generators/utils/project-configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,14 +279,29 @@ function readAndCombineAllProjectConfigurations(tree: Tree): {
if (basename(projectFile) === 'project.json') {
const json = readJson(tree, projectFile);
const config = buildProjectFromProjectJson(json, projectFile);
if (!config.name) {
try {
const packageJson = readJson<PackageJson>(
tree,
joinPathFragments(config.root, 'package.json')
);
if (packageJson.name) {
config.name = packageJson.name;
}
} catch {
// Maybe no package json, is ok.
}
config.name ??= toProjectName(projectFile);
}
mergeProjectConfigurationIntoRootMap(
rootMap,
config,
undefined,
undefined,
true
);
} else if (basename(projectFile) === 'package.json') {
}
if (basename(projectFile) === 'package.json') {
const packageJson = readJson<PackageJson>(tree, projectFile);

// We don't want to have all of the extra inferred stuff in here, as
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/plugins/package-json/create-nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ export function buildProjectConfigurationFromPackageJson(
}
}

if (!packageJson.name && projectRoot === '.') {
if (!packageJson.name && projectRoot === '.' && !packageJson.nx?.name) {
throw new Error(
'Nx requires the root package.json to specify a name if it is being used as an Nx project.'
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { dirname, join } from 'node:path';

import { ProjectConfiguration } from '../../../config/workspace-json-project-json';
import { toProjectName } from '../../../config/to-project-name';
import { readJsonFile } from '../../../utils/fileutils';
import {
createNodesFromFiles,
NxPluginV2,
} from '../../../project-graph/plugins';
import { PackageJson } from '../../../utils/package-json';

export const ProjectJsonProjectsPlugin: NxPluginV2 = {
name: 'nx/core/project-json',
Expand Down Expand Up @@ -42,21 +40,10 @@ export function buildProjectFromProjectJson(
json: Partial<ProjectConfiguration>,
path: string
): ProjectConfiguration {
const packageJsonPath = join(dirname(path), 'package.json');
const { name, root, ...rest } = json;
return {
name:
name ?? readNameFromPackageJson(packageJsonPath) ?? toProjectName(path),
name,
root: root ?? dirname(path),
...rest,
};
}

export function readNameFromPackageJson(path: string): string {
try {
const json = readJsonFile<PackageJson>(path);
return json.nx?.name ?? json.name;
} catch {
return undefined;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { workspaceRoot } from '../../utils/workspace-root';
import { minimatch } from 'minimatch';
import { join } from 'path';
import { performance } from 'perf_hooks';
import { existsSync } from 'node:fs';

import { LoadedNxPlugin } from '../plugins/loaded-nx-plugin';
import {
Expand All @@ -37,6 +38,7 @@ import {
getExecutorInformation,
parseExecutor,
} from '../../command-line/run/executor-utils';
import { toProjectName } from '../../config/to-project-name';

export type SourceInformation = [file: string | null, plugin: string];
export type ConfigurationSourceMaps = Record<
Expand Down Expand Up @@ -668,6 +670,16 @@ function validateAndNormalizeProjectRootMap(
// Strip it before returning configuration for usage.
if (project['// targets']) delete project['// targets'];

// We initially did this in the project.json plugin, but
// that resulted in project.json files without names causing
// the resulting project to change names from earlier plugins...
if (
!project.name &&
existsSync(join(workspaceRoot, project.root, 'project.json'))
) {
project.name = toProjectName(join(root, 'project.json'));
}

try {
validateProject(project, projects);
projects[project.name] = project;
Expand Down
Loading