Skip to content

Commit 0fbd2f8

Browse files
authored
fix(core): ensure project with name undefined is not created (#23097)
* fix(core): ensure workspaces config is updated when daemon running * fix(core): properly error when projects have no name but with a package.json
1 parent 5ded713 commit 0fbd2f8

File tree

3 files changed

+38
-27
lines changed

3 files changed

+38
-27
lines changed

packages/nx/src/generators/utils/project-configuration.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,7 @@ function readAndCombineAllProjectConfigurations(tree: Tree): {
200200
readJson(tree, p, { expectComments: true })
201201
),
202202
];
203-
const projectGlobPatterns = configurationGlobs([
204-
ProjectJsonProjectsPlugin,
205-
{ createNodes: packageJsonWorkspacesCreateNodes },
206-
]);
207-
const globbedFiles = globWithWorkspaceContext(tree.root, projectGlobPatterns);
203+
const globbedFiles = globWithWorkspaceContext(tree.root, patterns);
208204
const createdFiles = findCreatedProjectFiles(tree, patterns);
209205
const deletedFiles = findDeletedProjectFiles(tree, patterns);
210206
const projectFiles = [...globbedFiles, ...createdFiles].filter(

packages/nx/src/plugins/package-json-workspaces/create-nodes.ts

+34-22
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,50 @@ import {
1414
readTargetsFromPackageJson,
1515
} from '../../utils/package-json';
1616
import { joinPathFragments } from '../../utils/path';
17-
import { workspaceRoot } from '../../utils/workspace-root';
1817
import { CreateNodes } from '../../project-graph/plugins';
1918

20-
const readJson = (f) => readJsonFile(join(workspaceRoot, f));
21-
const patterns = getGlobPatternsFromPackageManagerWorkspaces(
22-
workspaceRoot,
23-
readJson
24-
);
25-
26-
const negativePatterns = patterns.filter((p) => p.startsWith('!'));
27-
const positivePatterns = patterns.filter((p) => !p.startsWith('!'));
28-
if (
29-
// There are some negative patterns
30-
negativePatterns.length > 0 &&
31-
// No positive patterns
32-
(positivePatterns.length === 0 ||
33-
// Or only a single positive pattern that is the default coming from root package
34-
(positivePatterns.length === 1 && positivePatterns[0] === 'package.json'))
35-
) {
36-
positivePatterns.push('**/package.json');
37-
}
3819
export const createNodes: CreateNodes = [
39-
combineGlobPatterns(positivePatterns),
20+
combineGlobPatterns('package.json', '**/package.json'),
4021
(p, _, { workspaceRoot }) => {
41-
if (!negativePatterns.some((negative) => minimatch(p, negative))) {
22+
const readJson = (f) => readJsonFile(join(workspaceRoot, f));
23+
const matcher = buildPackageJsonWorkspacesMatcher(workspaceRoot, readJson);
24+
25+
if (matcher(p)) {
4226
return createNodeFromPackageJson(p, workspaceRoot);
4327
}
44-
// A negative pattern matched, so we should not create a node for this package.json
28+
// The given package.json is not part of the workspaces configuration.
4529
return {};
4630
},
4731
];
4832

33+
export function buildPackageJsonWorkspacesMatcher(
34+
workspaceRoot: string,
35+
readJson: (string) => any
36+
) {
37+
const patterns = getGlobPatternsFromPackageManagerWorkspaces(
38+
workspaceRoot,
39+
readJson
40+
);
41+
42+
const negativePatterns = patterns.filter((p) => p.startsWith('!'));
43+
const positivePatterns = patterns.filter((p) => !p.startsWith('!'));
44+
45+
if (
46+
// There are some negative patterns
47+
negativePatterns.length > 0 &&
48+
// No positive patterns
49+
(positivePatterns.length === 0 ||
50+
// Or only a single positive pattern that is the default coming from root package
51+
(positivePatterns.length === 1 && positivePatterns[0] === 'package.json'))
52+
) {
53+
positivePatterns.push('**/package.json');
54+
}
55+
56+
return (p: string) =>
57+
positivePatterns.some((positive) => minimatch(p, positive)) &&
58+
!negativePatterns.some((negative) => minimatch(p, negative));
59+
}
60+
4961
export function createNodeFromPackageJson(pkgJsonPath: string, root: string) {
5062
const json: PackageJson = readJsonFile(join(root, pkgJsonPath));
5163
const project = buildProjectConfigurationFromPackageJson(

packages/nx/src/project-graph/utils/project-configuration-utils.ts

+3
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,9 @@ export function readProjectConfigurationsFromRootMap(
525525
if (!configuration.name) {
526526
try {
527527
const { name } = readJsonFile(join(root, 'package.json'));
528+
if (!name) {
529+
throw new Error("No name found for project at '" + root + "'.");
530+
}
528531
configuration.name = name;
529532
} catch {
530533
projectRootsWithNoName.push(root);

0 commit comments

Comments
 (0)