Skip to content

Commit

Permalink
fix(core): fix create package json root parsing (#26717)
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 -->

## 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 #
  • Loading branch information
meeroslav authored Jun 27, 2024
1 parent 9aca5f3 commit acddf89
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 20 deletions.
103 changes: 85 additions & 18 deletions packages/nx/src/plugins/js/package-json/create-package-json.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import * as fs from 'fs';

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 { createPackageJson } from './create-package-json';
import * as fileutilsModule from '../../../utils/fileutils';
Expand Down Expand Up @@ -294,6 +299,7 @@ describe('createPackageJson', () => {

expect(filterUsingGlobPatternsSpy).toHaveBeenCalledTimes(1);
});

it('should exclude devDependencies from production build when local package.json is imported', () => {
jest.spyOn(configModule, 'readNxJson').mockReturnValueOnce({
namedInputs: {
Expand Down Expand Up @@ -426,12 +432,12 @@ describe('createPackageJson', () => {
'npm:@nx/devkit': {
type: 'npm',
name: 'npm:@nx/devkit',
data: { version: '16.0.0', hash: '', packageName: '@nx/devkit' },
data: { version: '16.0.3', hash: '', packageName: '@nx/devkit' },
},
'npm:nx': {
type: 'npm',
name: 'npm:nx',
data: { version: '16.0.0', hash: '', packageName: 'nx' },
data: { version: '16.0.3', hash: '', packageName: 'nx' },
},
'npm:tslib': {
type: 'npm',
Expand All @@ -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 = () => ({
dependencies: {
'@nx/devkit': '~16.0.0',
Expand All @@ -474,7 +496,13 @@ describe('createPackageJson', () => {
spies.push(
jest
.spyOn(hashModule, 'filterUsingGlobPatterns')
.mockImplementation(() => [])
.mockImplementation((root) => {
if (root === 'libs/lib1') {
return fileMap['lib1'];
} else {
return fileMap['app1'];
}
})
);
});

Expand Down Expand Up @@ -503,9 +531,13 @@ describe('createPackageJson', () => {
})
);

expect(createPackageJson('app1', graph, { root: '' })).toEqual({
expect(createPackageJson('app1', graph, { root: '' }, fileMap)).toEqual({
name: 'app1',
version: '0.0.1',
dependencies: {
'@nx/devkit': '16.0.3',
nx: '16.0.3',
},
});
});

Expand All @@ -531,12 +563,23 @@ describe('createPackageJson', () => {
);

expect(
createPackageJson('app1', graph, {
root: '',
})
createPackageJson(
'app1',
graph,
{
root: '',
},
fileMap
)
).toEqual({
name: 'app1',
version: '0.0.1',
name: 'other-name',
version: '1.2.3',
dependencies: {
'@nx/devkit': '16.0.3',
nx: '16.0.3',
random: '1.0.0',
typescript: '^4.8.4',
},
});
});

Expand All @@ -552,12 +595,21 @@ describe('createPackageJson', () => {
);

expect(
createPackageJson('lib1', graph, {
root: '',
})
createPackageJson(
'lib1',
graph,
{
root: '',
},
fileMap
)
).toEqual({
name: 'lib1',
version: '0.0.1',
dependencies: {
'@nx/devkit': '~16.0.0',
tslib: '~2.4.0',
},
});
});

Expand All @@ -583,13 +635,28 @@ describe('createPackageJson', () => {
);

expect(
createPackageJson('lib1', graph, {
root: '',
})
createPackageJson(
'lib1',
graph,
{
root: '',
},
fileMap
)
).toEqual({
name: 'lib1',
version: '0.0.1',
name: 'other-name',
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 };
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function createPackageJson(
const isLibrary = projectNode.type === 'lib';

const rootPackageJson = readJsonFile(
`${options.root || workspaceRoot}/package.json`
join(options.root ?? workspaceRoot, 'package.json')
);

const npmDeps = findProjectsNpmDependencies(
Expand All @@ -65,7 +65,7 @@ export function createPackageJson(
version: '0.0.1',
};
const projectPackageJsonPath = join(
options.root || workspaceRoot,
options.root ?? workspaceRoot,
projectNode.data.root,
'package.json'
);
Expand Down

0 comments on commit acddf89

Please sign in to comment.