Skip to content

Commit

Permalink
fix(core): do not create projects twice from package.json (#26700)
Browse files Browse the repository at this point in the history
<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->

NPM scripts show up twice in the project details view.


![image](https://github.com/nrwl/nx/assets/8104246/98c55a03-7e71-430f-9fc3-de91ce180e26)


## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

NPM scripts only show up once in the project details view.


![image](https://github.com/nrwl/nx/assets/8104246/27787794-a8ef-40be-b00a-c9b1d26182f2)


## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
  • Loading branch information
FrozenPandaz authored Jun 26, 2024
1 parent a0e8f83 commit c2c6a13
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ describe('nx project.json plugin', () => {
};
});

it('should build projects from project.json', () => {
it('should build projects from package.json next to project.json', () => {
memfs.vol.fromJSON(
{
'package.json': JSON.stringify({
name: 'lib-a',
description: 'lib-a project description',
}),
'packages/lib-a/project.json': JSON.stringify({
name: 'lib-a',
description: 'lib-a project description',
Expand Down Expand Up @@ -85,4 +89,38 @@ describe('nx project.json plugin', () => {
}
`);
});

it('should not build package manager workspace projects from package.json next to project.json', () => {
memfs.vol.fromJSON(
{
'package.json': JSON.stringify({
name: 'lib-a',
description: 'lib-a project description',
workspaces: ['packages/lib-a'],
}),
'packages/lib-a/project.json': JSON.stringify({
name: 'lib-a',
description: 'lib-a project description',
targets: {
build: {
executor: 'nx:run-commands',
options: {},
},
},
}),
'packages/lib-a/package.json': JSON.stringify({
name: 'lib-a',
description: 'lib-a package description',
scripts: {
test: 'jest',
},
}),
},
'/root'
);

expect(
createNodesFunction('packages/lib-a/project.json', undefined, context)
).toMatchInlineSnapshot(`{}`);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
getTagsFromPackageJson,
readTargetsFromPackageJson,
} from '../../../utils/package-json';
import { buildPackageJsonWorkspacesMatcher } from '../../package-json-workspaces';

// TODO: Remove this one day, this should not need to be done.

Expand Down Expand Up @@ -42,8 +43,16 @@ function createProjectFromPackageJsonNextToProjectJson(
workspaceRoot: string
): ProjectConfiguration | null {
const root = dirname(projectJsonPath);
const packageJsonPath = join(workspaceRoot, root, 'package.json');
if (!existsSync(packageJsonPath)) {
const relativePackageJsonPath = join(root, 'package.json');
const packageJsonPath = join(workspaceRoot, relativePackageJsonPath);
const readJson = (f) => readJsonFile(join(workspaceRoot, f));

// Do not create projects for package.json files
// that are part of the package manager workspaces
// Those package.json files will be processed later on
const matcher = buildPackageJsonWorkspacesMatcher(workspaceRoot, readJson);

if (!existsSync(packageJsonPath) || matcher(relativePackageJsonPath)) {
return null;
}
try {
Expand Down

0 comments on commit c2c6a13

Please sign in to comment.