Skip to content

Commit

Permalink
fix(core): ensure project with name undefined is not created (#23097)
Browse files Browse the repository at this point in the history
* fix(core): ensure workspaces config is updated when daemon running

* fix(core): properly error when projects have no name but with a package.json
  • Loading branch information
AgentEnder committed May 2, 2024
1 parent 5ded713 commit 0fbd2f8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 27 deletions.
6 changes: 1 addition & 5 deletions packages/nx/src/generators/utils/project-configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,7 @@ function readAndCombineAllProjectConfigurations(tree: Tree): {
readJson(tree, p, { expectComments: true })
),
];
const projectGlobPatterns = configurationGlobs([
ProjectJsonProjectsPlugin,
{ createNodes: packageJsonWorkspacesCreateNodes },
]);
const globbedFiles = globWithWorkspaceContext(tree.root, projectGlobPatterns);
const globbedFiles = globWithWorkspaceContext(tree.root, patterns);
const createdFiles = findCreatedProjectFiles(tree, patterns);
const deletedFiles = findDeletedProjectFiles(tree, patterns);
const projectFiles = [...globbedFiles, ...createdFiles].filter(
Expand Down
56 changes: 34 additions & 22 deletions packages/nx/src/plugins/package-json-workspaces/create-nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,50 @@ import {
readTargetsFromPackageJson,
} from '../../utils/package-json';
import { joinPathFragments } from '../../utils/path';
import { workspaceRoot } from '../../utils/workspace-root';
import { CreateNodes } from '../../project-graph/plugins';

const readJson = (f) => readJsonFile(join(workspaceRoot, f));
const patterns = getGlobPatternsFromPackageManagerWorkspaces(
workspaceRoot,
readJson
);

const negativePatterns = patterns.filter((p) => p.startsWith('!'));
const positivePatterns = patterns.filter((p) => !p.startsWith('!'));
if (
// There are some negative patterns
negativePatterns.length > 0 &&
// No positive patterns
(positivePatterns.length === 0 ||
// Or only a single positive pattern that is the default coming from root package
(positivePatterns.length === 1 && positivePatterns[0] === 'package.json'))
) {
positivePatterns.push('**/package.json');
}
export const createNodes: CreateNodes = [
combineGlobPatterns(positivePatterns),
combineGlobPatterns('package.json', '**/package.json'),
(p, _, { workspaceRoot }) => {
if (!negativePatterns.some((negative) => minimatch(p, negative))) {
const readJson = (f) => readJsonFile(join(workspaceRoot, f));
const matcher = buildPackageJsonWorkspacesMatcher(workspaceRoot, readJson);

if (matcher(p)) {
return createNodeFromPackageJson(p, workspaceRoot);
}
// A negative pattern matched, so we should not create a node for this package.json
// The given package.json is not part of the workspaces configuration.
return {};
},
];

export function buildPackageJsonWorkspacesMatcher(
workspaceRoot: string,
readJson: (string) => any
) {
const patterns = getGlobPatternsFromPackageManagerWorkspaces(
workspaceRoot,
readJson
);

const negativePatterns = patterns.filter((p) => p.startsWith('!'));
const positivePatterns = patterns.filter((p) => !p.startsWith('!'));

if (
// There are some negative patterns
negativePatterns.length > 0 &&
// No positive patterns
(positivePatterns.length === 0 ||
// Or only a single positive pattern that is the default coming from root package
(positivePatterns.length === 1 && positivePatterns[0] === 'package.json'))
) {
positivePatterns.push('**/package.json');
}

return (p: string) =>
positivePatterns.some((positive) => minimatch(p, positive)) &&
!negativePatterns.some((negative) => minimatch(p, negative));
}

export function createNodeFromPackageJson(pkgJsonPath: string, root: string) {
const json: PackageJson = readJsonFile(join(root, pkgJsonPath));
const project = buildProjectConfigurationFromPackageJson(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,9 @@ export function readProjectConfigurationsFromRootMap(
if (!configuration.name) {
try {
const { name } = readJsonFile(join(root, 'package.json'));
if (!name) {
throw new Error("No name found for project at '" + root + "'.");
}
configuration.name = name;
} catch {
projectRootsWithNoName.push(root);
Expand Down

0 comments on commit 0fbd2f8

Please sign in to comment.