Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): ensure project with name undefined is not created #23097

Merged
merged 2 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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