fix(core): fix create package json root parsing (#26717)

<!-- 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 -->

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

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

Fixes #
This commit is contained in:
Miroslav Jonaš 2024-06-27 12:12:28 +02:00 committed by GitHub
parent 9aca5f30f2
commit acddf894a4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 87 additions and 20 deletions

View File

@ -1,7 +1,12 @@
import * as fs from 'fs'; import * as fs from 'fs';
import * as configModule from '../../../config/configuration'; import * as configModule from '../../../config/configuration';
import { ProjectGraph } from '../../../config/project-graph'; import {
FileData,
FileDataDependency,
ProjectFileMap,
ProjectGraph,
} from '../../../config/project-graph';
import * as hashModule from '../../../hasher/task-hasher'; import * as hashModule from '../../../hasher/task-hasher';
import { createPackageJson } from './create-package-json'; import { createPackageJson } from './create-package-json';
import * as fileutilsModule from '../../../utils/fileutils'; import * as fileutilsModule from '../../../utils/fileutils';
@ -294,6 +299,7 @@ describe('createPackageJson', () => {
expect(filterUsingGlobPatternsSpy).toHaveBeenCalledTimes(1); expect(filterUsingGlobPatternsSpy).toHaveBeenCalledTimes(1);
}); });
it('should exclude devDependencies from production build when local package.json is imported', () => { it('should exclude devDependencies from production build when local package.json is imported', () => {
jest.spyOn(configModule, 'readNxJson').mockReturnValueOnce({ jest.spyOn(configModule, 'readNxJson').mockReturnValueOnce({
namedInputs: { namedInputs: {
@ -426,12 +432,12 @@ describe('createPackageJson', () => {
'npm:@nx/devkit': { 'npm:@nx/devkit': {
type: 'npm', type: 'npm',
name: 'npm:@nx/devkit', name: 'npm:@nx/devkit',
data: { version: '16.0.0', hash: '', packageName: '@nx/devkit' }, data: { version: '16.0.3', hash: '', packageName: '@nx/devkit' },
}, },
'npm:nx': { 'npm:nx': {
type: 'npm', type: 'npm',
name: 'npm:nx', name: 'npm:nx',
data: { version: '16.0.0', hash: '', packageName: 'nx' }, data: { version: '16.0.3', hash: '', packageName: 'nx' },
}, },
'npm:tslib': { 'npm:tslib': {
type: 'npm', type: 'npm',
@ -450,6 +456,22 @@ describe('createPackageJson', () => {
}, },
}; };
const fileMap: ProjectFileMap = {
app1: [
createFile(`apps/app1/src/main.ts`, [
'npm:@nx/devkit',
'npm:typecript',
]),
],
lib1: [
createFile(`libs/lib1/src/main.ts`, [
'npm:@nx/devkit',
'npm:typecript',
'npm:tslib',
]),
],
};
const rootPackageJson = () => ({ const rootPackageJson = () => ({
dependencies: { dependencies: {
'@nx/devkit': '~16.0.0', '@nx/devkit': '~16.0.0',
@ -474,7 +496,13 @@ describe('createPackageJson', () => {
spies.push( spies.push(
jest jest
.spyOn(hashModule, 'filterUsingGlobPatterns') .spyOn(hashModule, 'filterUsingGlobPatterns')
.mockImplementation(() => []) .mockImplementation((root) => {
if (root === 'libs/lib1') {
return fileMap['lib1'];
} else {
return fileMap['app1'];
}
})
); );
}); });
@ -503,9 +531,13 @@ describe('createPackageJson', () => {
}) })
); );
expect(createPackageJson('app1', graph, { root: '' })).toEqual({ expect(createPackageJson('app1', graph, { root: '' }, fileMap)).toEqual({
name: 'app1', name: 'app1',
version: '0.0.1', version: '0.0.1',
dependencies: {
'@nx/devkit': '16.0.3',
nx: '16.0.3',
},
}); });
}); });
@ -531,12 +563,23 @@ describe('createPackageJson', () => {
); );
expect( expect(
createPackageJson('app1', graph, { createPackageJson(
root: '', 'app1',
}) graph,
{
root: '',
},
fileMap
)
).toEqual({ ).toEqual({
name: 'app1', name: 'other-name',
version: '0.0.1', version: '1.2.3',
dependencies: {
'@nx/devkit': '16.0.3',
nx: '16.0.3',
random: '1.0.0',
typescript: '^4.8.4',
},
}); });
}); });
@ -552,12 +595,21 @@ describe('createPackageJson', () => {
); );
expect( expect(
createPackageJson('lib1', graph, { createPackageJson(
root: '', 'lib1',
}) graph,
{
root: '',
},
fileMap
)
).toEqual({ ).toEqual({
name: 'lib1', name: 'lib1',
version: '0.0.1', version: '0.0.1',
dependencies: {
'@nx/devkit': '~16.0.0',
tslib: '~2.4.0',
},
}); });
}); });
@ -583,13 +635,28 @@ describe('createPackageJson', () => {
); );
expect( expect(
createPackageJson('lib1', graph, { createPackageJson(
root: '', 'lib1',
}) graph,
{
root: '',
},
fileMap
)
).toEqual({ ).toEqual({
name: 'lib1', name: 'other-name',
version: '0.0.1', version: '1.2.3',
dependencies: {
'@nx/devkit': '~16.0.0',
tslib: '~2.4.0',
random: '1.0.0',
typescript: '^4.8.4',
},
}); });
}); });
}); });
}); });
function createFile(f: string, deps?: FileDataDependency[]): FileData {
return { file: f, hash: '', deps };
}

View File

@ -44,7 +44,7 @@ export function createPackageJson(
const isLibrary = projectNode.type === 'lib'; const isLibrary = projectNode.type === 'lib';
const rootPackageJson = readJsonFile( const rootPackageJson = readJsonFile(
`${options.root || workspaceRoot}/package.json` join(options.root ?? workspaceRoot, 'package.json')
); );
const npmDeps = findProjectsNpmDependencies( const npmDeps = findProjectsNpmDependencies(
@ -65,7 +65,7 @@ export function createPackageJson(
version: '0.0.1', version: '0.0.1',
}; };
const projectPackageJsonPath = join( const projectPackageJsonPath = join(
options.root || workspaceRoot, options.root ?? workspaceRoot,
projectNode.data.root, projectNode.data.root,
'package.json' 'package.json'
); );