Skip to content

Commit 86c5819

Browse files
vsavkinFrozenPandaz
authored andcommitted
fix(core): correctly derive the name of the project based on package.json without name
(cherry picked from commit 67e032d)
1 parent 444af2d commit 86c5819

File tree

12 files changed

+106
-119
lines changed

12 files changed

+106
-119
lines changed

e2e/add-nx-to-monorepo/src/add-nx-to-monorepo.test.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ import {
1111
import { Workspaces } from 'nx/src/config/workspaces';
1212

1313
describe('add-nx-to-monorepo', () => {
14-
const packageManagerCommand = getPackageManagerCommand({
14+
const pmc = getPackageManagerCommand({
1515
packageManager: getSelectedPackageManager(),
16-
}).runUninstalledPackage;
16+
});
1717

1818
it('should not throw', () => {
19-
if (packageManagerCommand) {
19+
if (pmc.runUninstalledPackage) {
2020
// Arrange
2121
createNonNxProjectDirectory();
22+
runCommand(pmc.install);
2223
updateFile(
2324
'packages/package-a/package.json',
2425
JSON.stringify({
@@ -42,17 +43,20 @@ describe('add-nx-to-monorepo', () => {
4243

4344
// Act
4445
const output = runCommand(
45-
`${packageManagerCommand} add-nx-to-monorepo@${getPublishedVersion()} -y`
46+
`${
47+
pmc.runUninstalledPackage
48+
} add-nx-to-monorepo@${getPublishedVersion()} -y`
4649
);
4750
// Assert
4851
expect(output).toContain('🎉 Done!');
4952
}
5053
});
5154

5255
it('should build', () => {
53-
if (packageManagerCommand) {
56+
if (pmc.runUninstalledPackage) {
5457
// Arrange
5558
createNonNxProjectDirectory();
59+
runCommand(pmc.runUninstalledPackage);
5660
updateFile(
5761
'packages/package-a/package.json',
5862
JSON.stringify({
@@ -65,7 +69,9 @@ describe('add-nx-to-monorepo', () => {
6569

6670
// Act
6771
runCommand(
68-
`${packageManagerCommand} add-nx-to-monorepo@${getPublishedVersion()} -y`
72+
`${
73+
pmc.runUninstalledPackage
74+
} add-nx-to-monorepo@${getPublishedVersion()} -y`
6975
);
7076
const output = runCLI('build package-a');
7177
// Assert

e2e/nx-init/src/nx-init.test.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import {
1010
} from '@nrwl/e2e/utils';
1111

1212
describe('nx init', () => {
13-
const packageManagerCommand = getPackageManagerCommand({
13+
const pmc = getPackageManagerCommand({
1414
packageManager: getSelectedPackageManager(),
15-
}).runUninstalledPackage;
15+
});
1616

1717
afterEach(() => cleanupProject());
1818

@@ -28,8 +28,10 @@ describe('nx init', () => {
2828
})
2929
);
3030

31-
const output = runCommand(`${packageManagerCommand} nx init -y`);
32-
expect(output).toContain('Done!');
31+
runCommand(pmc.install);
32+
33+
const output = runCommand(`${pmc.runUninstalledPackage} nx init -y`);
34+
expect(output).toContain('Enabled computation caching');
3335

3436
expect(runCLI('run package:echo')).toContain('123');
3537
renameFile('nx.json', 'nx.json.old');

e2e/nx-init/src/nx-project-graph.test.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,16 @@ describe('project graph creation', () => {
1313
afterEach(() => cleanupProject());
1414

1515
it('should correctly build the nxdeps.json containing files for the project', () => {
16-
// ARRANGE
1716
const libName = uniq('mylib');
18-
1917
runCLI(`generate @nrwl/workspace:lib ${libName}`);
2018

21-
// ACT
22-
2319
runCLI(`graph --file=graph.json`);
24-
25-
// ASSERT
2620
const { graph: graphJson } = readJson('graph.json');
2721

2822
expect(graphJson.nodes[libName].data.files.length).toBeGreaterThan(0);
2923
});
3024

3125
it("should correctly build the nxdeps.json containing files for the project when root is ''", () => {
32-
// ARRANGE
3326
const libName = uniq('mylib');
3427

3528
runCLI(`generate @nrwl/workspace:lib ${libName}`);
@@ -38,18 +31,13 @@ describe('project graph creation', () => {
3831
root: '',
3932
}));
4033

41-
// ACT
42-
4334
runCLI(`graph --file=graph.json`);
4435

45-
// ASSERT
46-
4736
const { graph: graphJson } = readJson('graph.json');
4837
expect(graphJson.nodes[libName].data.files.length).toBeGreaterThan(0);
4938
});
5039

5140
it("should correctly build the graph.json containing files for the project when root is '' and for project that do not have root as ''", () => {
52-
// ARRANGE
5341
const libName = uniq('mylib');
5442
const secondLibName = uniq('mysecondlib');
5543

@@ -60,12 +48,8 @@ describe('project graph creation', () => {
6048
root: '',
6149
}));
6250

63-
// ACT
64-
6551
runCLI(`graph --file=graph.json`);
6652

67-
// ASSERT
68-
6953
const { graph: graphJson } = readJson('graph.json');
7054
expect(graphJson.nodes[libName].data.files.length).toBeGreaterThan(0);
7155
expect(graphJson.nodes[secondLibName].data.files.length).toBeGreaterThan(0);

e2e/nx-misc/src/extras.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ describe('Extra Nx Misc Tests', () => {
189189
expect(resultArgs).toContain('camel: d');
190190
}, 120000);
191191

192-
it('ttt should fail when a process exits non-zero', async () => {
192+
it('should fail when a process exits non-zero', async () => {
193193
updateProjectConfig(mylib, (config) => {
194194
config.targets.error = {
195195
executor: 'nx:run-commands',

e2e/nx-misc/src/workspace.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ import {
1515
tmpProjPath,
1616
readResolvedWorkspaceConfiguration,
1717
removeFile,
18+
getPackageManagerCommand,
19+
getSelectedPackageManager,
20+
runCommand,
1821
} from '@nrwl/e2e/utils';
1922

2023
let proj: string;
@@ -112,6 +115,18 @@ describe('Workspace Tests', () => {
112115

113116
runCLI(`generate @nrwl/workspace:npm-package ${npmPackage}`);
114117

118+
updateFile('package.json', (content) => {
119+
const json = JSON.parse(content);
120+
json.workspaces = ['libs/*'];
121+
return JSON.stringify(json);
122+
});
123+
124+
const pmc = getPackageManagerCommand({
125+
packageManager: getSelectedPackageManager(),
126+
});
127+
128+
runCommand(pmc.install);
129+
115130
const result = runCLI(`test ${npmPackage}`);
116131
expect(result).toContain('Hello World');
117132
});

e2e/utils/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ export function createNonNxProjectDirectory(name = uniq('proj')) {
128128
'package.json',
129129
JSON.stringify({
130130
name,
131+
workspaces: ['packages/*'],
131132
})
132133
);
133134
}
@@ -857,6 +858,7 @@ export function getPackageManagerCommand({
857858
runNx: string;
858859
runNxSilent: string;
859860
runUninstalledPackage: string;
861+
install: string;
860862
addProd: string;
861863
addDev: string;
862864
list: string;
@@ -873,6 +875,7 @@ export function getPackageManagerCommand({
873875
runNx: `npx nx`,
874876
runNxSilent: `npx nx`,
875877
runUninstalledPackage: `npx --yes`,
878+
install: 'npm install',
876879
addProd: `npm install --legacy-peer-deps`,
877880
addDev: `npm install --legacy-peer-deps -D`,
878881
list: 'npm ls --depth 10',
@@ -884,6 +887,7 @@ export function getPackageManagerCommand({
884887
runNx: `yarn nx`,
885888
runNxSilent: `yarn --silent nx`,
886889
runUninstalledPackage: 'npx --yes',
890+
install: 'yarn',
887891
addProd: `yarn add`,
888892
addDev: `yarn add -D`,
889893
list: 'npm ls --depth 10',
@@ -895,6 +899,7 @@ export function getPackageManagerCommand({
895899
runNx: `pnpm exec nx`,
896900
runNxSilent: `pnpm exec nx`,
897901
runUninstalledPackage: 'pnpm dlx',
902+
install: 'pnpm i',
898903
addProd: `pnpm add`,
899904
addDev: `pnpm add -D`,
900905
list: 'npm ls --depth 10',

packages/add-nx-to-monorepo/src/add-nx-to-monorepo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ function printFinalMessage() {
306306
output.success({
307307
title: `🎉 Done!`,
308308
bodyLines: [
309-
`- Enabled Computation caching!`,
309+
`- Enabled computation caching!`,
310310
`- Run "${
311311
getPackageManagerCommand().exec
312312
} nx run-many --target=build" to run the build script for every project in the monorepo.`,

packages/nx/src/command-line/affected.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export async function affected(
5757
if (apps.length) {
5858
output.warn({
5959
title:
60-
'Deprecated: Use "nx print-affected --type=app --select=projects" instead. This command will be removed in v15.',
60+
'Deprecated: Use "nx print-affected --type=app --select=projects" instead. This command will be removed in v16.',
6161
});
6262
output.log({
6363
title: 'Affected apps:',
@@ -77,7 +77,7 @@ export async function affected(
7777
if (libs.length) {
7878
output.warn({
7979
title:
80-
'Deprecated: Use "nx print-affected --type=lib --select=projects" instead. This command will be removed in v15.',
80+
'Deprecated: Use "nx print-affected --type=lib --select=projects" instead. This command will be removed in v16.',
8181
});
8282
output.log({
8383
title: 'Affected libs:',

packages/nx/src/config/workspaces.spec.ts

Lines changed: 8 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ describe('Workspaces', () => {
8282
'workspace.json': JSON.stringify({
8383
projects: { 'lib1-workspace': 'libs/lib1' },
8484
}),
85+
'package.json': JSON.stringify({
86+
workspaces: ['**/package.json'],
87+
}),
8588
},
8689
'/root'
8790
);
@@ -93,44 +96,11 @@ describe('Workspaces', () => {
9396
expect(projects['lib1']).toBeUndefined();
9497
expect(projects.lib2).toEqual(lib2Config);
9598
expect(projects['domain-lib3']).toEqual(domainPackageConfig);
96-
expect(projects['domain-lib4']).toEqual(domainLibConfig);
99+
expect(projects['lib4']).toEqual(domainLibConfig);
97100
});
98101
});
99102

100103
describe('to project name', () => {
101-
it('should trim default directories from beginning', () => {
102-
const appResults = toProjectName(
103-
'apps/directory/my-app/project.json',
104-
null
105-
);
106-
const libResults = toProjectName(
107-
'libs/directory/my-lib/project.json',
108-
null
109-
);
110-
expect(appResults).toEqual('directory-my-app');
111-
expect(libResults).toEqual('directory-my-lib');
112-
});
113-
114-
it('should trim custom directories from beginning', () => {
115-
const nxJson: NxJsonConfiguration = {
116-
npmScope: '',
117-
workspaceLayout: {
118-
appsDir: 'my-apps',
119-
libsDir: 'packages',
120-
},
121-
};
122-
const appResults = toProjectName(
123-
'my-apps/directory/my-app/project.json',
124-
nxJson
125-
);
126-
const libResults = toProjectName(
127-
'packages/directory/my-lib/project.json',
128-
nxJson
129-
);
130-
expect(appResults).toEqual('directory-my-app');
131-
expect(libResults).toEqual('directory-my-lib');
132-
});
133-
134104
it('should lowercase names', () => {
135105
const nxJson: NxJsonConfiguration = {
136106
npmScope: '',
@@ -139,16 +109,10 @@ describe('Workspaces', () => {
139109
libsDir: 'packages',
140110
},
141111
};
142-
const appResults = toProjectName(
143-
'my-apps/directory/my-app/package.json',
144-
nxJson
145-
);
146-
const libResults = toProjectName(
147-
'packages/directory/MyLib/package.json',
148-
nxJson
149-
);
150-
expect(appResults).toEqual('directory-my-app');
151-
expect(libResults).toEqual('directory-mylib');
112+
const appResults = toProjectName('my-apps/directory/my-app/package.json');
113+
const libResults = toProjectName('packages/directory/MyLib/package.json');
114+
expect(appResults).toEqual('my-app');
115+
expect(libResults).toEqual('mylib');
152116
});
153117

154118
it('should use the workspace globs in package.json', () => {

0 commit comments

Comments
 (0)