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

Merged
merged 1 commit into from
Jun 27, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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