Skip to content

Commit 769758d

Browse files
authored
fix(devkit): do not update unknown properties to workspace configuration (#6388)
1 parent 56e30d7 commit 769758d

File tree

2 files changed

+152
-92
lines changed

2 files changed

+152
-92
lines changed

packages/devkit/src/generators/project-configuration.spec.ts

Lines changed: 127 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import {
55
updateProjectConfiguration,
66
removeProjectConfiguration,
77
getProjects,
8+
readWorkspaceConfiguration,
9+
WorkspaceConfiguration,
10+
updateWorkspaceConfiguration,
811
} from './project-configuration';
912
import { createTreeWithEmptyWorkspace } from '../tests/create-tree-with-empty-workspace';
1013
import { ProjectConfiguration } from '@nrwl/tao/src/shared/workspace';
@@ -23,115 +26,149 @@ describe('project configuration', () => {
2326
tree = createTreeWithEmptyWorkspace();
2427
});
2528

26-
it('should create project.json file when adding a project if standalone is true', () => {
27-
addProjectConfiguration(tree, 'test', baseTestProjectConfig, true);
29+
describe('addProjectConfiguration', () => {
30+
it('should create project.json file when adding a project if standalone is true', () => {
31+
addProjectConfiguration(tree, 'test', baseTestProjectConfig, true);
2832

29-
expect(tree.exists('libs/test/project.json')).toBeTruthy();
30-
});
33+
expect(tree.exists('libs/test/project.json')).toBeTruthy();
34+
});
3135

32-
it('should create project.json file if all other apps in the workspace use project.json', () => {
33-
addProjectConfiguration(
34-
tree,
35-
'project-a',
36-
{
36+
it('should create project.json file if all other apps in the workspace use project.json', () => {
37+
addProjectConfiguration(
38+
tree,
39+
'project-a',
40+
{
41+
root: 'apps/project-a',
42+
targets: {},
43+
},
44+
true
45+
);
46+
addProjectConfiguration(
47+
tree,
48+
'project-b',
49+
{
50+
root: 'apps/project-b',
51+
targets: {},
52+
},
53+
true
54+
);
55+
expect(tree.exists('apps/project-b/project.json')).toBeTruthy();
56+
});
57+
58+
it("should not create project.json file if any other app in the workspace doesn't use project.json", () => {
59+
addProjectConfiguration(tree, 'project-a', {
3760
root: 'apps/project-a',
3861
targets: {},
39-
},
40-
true
41-
);
42-
addProjectConfiguration(
43-
tree,
44-
'project-b',
45-
{
62+
});
63+
addProjectConfiguration(tree, 'project-b', {
4664
root: 'apps/project-b',
4765
targets: {},
48-
},
49-
true
50-
);
51-
expect(tree.exists('apps/project-b/project.json')).toBeTruthy();
52-
});
66+
});
67+
expect(tree.exists('apps/project-a/project.json')).toBeFalsy();
68+
expect(tree.exists('apps/project-b/project.json')).toBeFalsy();
69+
});
70+
71+
it('should not create project.json file when adding a project if standalone is false', () => {
72+
addProjectConfiguration(tree, 'test', baseTestProjectConfig, false);
5373

54-
it("should not create project.json file if any other app in the workspace doesn't use project.json", () => {
55-
addProjectConfiguration(tree, 'project-a', {
56-
root: 'apps/project-a',
57-
targets: {},
74+
expect(tree.exists('libs/test/project.json')).toBeFalsy();
5875
});
59-
addProjectConfiguration(tree, 'project-b', {
60-
root: 'apps/project-b',
61-
targets: {},
76+
77+
it('should be able to read from standalone projects', () => {
78+
tree.write(
79+
'libs/test/project.json',
80+
JSON.stringify(baseTestProjectConfig, null, 2)
81+
);
82+
tree.write(
83+
'workspace.json',
84+
JSON.stringify(
85+
{
86+
projects: {
87+
test: 'libs/test',
88+
},
89+
},
90+
null,
91+
2
92+
)
93+
);
94+
95+
const projectConfig = readProjectConfiguration(tree, 'test');
96+
97+
expect(projectConfig).toEqual(baseTestProjectConfig);
6298
});
63-
expect(tree.exists('apps/project-a/project.json')).toBeFalsy();
64-
expect(tree.exists('apps/project-b/project.json')).toBeFalsy();
65-
});
6699

67-
it('should not create project.json file when adding a project if standalone is false', () => {
68-
addProjectConfiguration(tree, 'test', baseTestProjectConfig, false);
100+
it('should update project.json file when updating a project', () => {
101+
addProjectConfiguration(tree, 'test', baseTestProjectConfig, true);
102+
const expectedProjectConfig = {
103+
...baseTestProjectConfig,
104+
targets: { build: { executor: '' } },
105+
};
106+
updateProjectConfiguration(tree, 'test', expectedProjectConfig);
107+
108+
expect(readJson(tree, 'libs/test/project.json')).toEqual(
109+
expectedProjectConfig
110+
);
111+
});
69112

70-
expect(tree.exists('libs/test/project.json')).toBeFalsy();
71-
});
113+
it('should update workspace.json file when updating an inline project', () => {
114+
addProjectConfiguration(tree, 'test', baseTestProjectConfig, false);
115+
const expectedProjectConfig = {
116+
...baseTestProjectConfig,
117+
targets: { build: { executor: '' } },
118+
};
119+
updateProjectConfiguration(tree, 'test', expectedProjectConfig);
120+
121+
expect(readJson(tree, 'workspace.json').projects.test).toEqual(
122+
expectedProjectConfig
123+
);
124+
});
72125

73-
it('should be able to read from standalone projects', () => {
74-
tree.write(
75-
'libs/test/project.json',
76-
JSON.stringify(baseTestProjectConfig, null, 2)
77-
);
78-
tree.write(
79-
'workspace.json',
80-
JSON.stringify(
81-
{
82-
projects: {
83-
test: 'libs/test',
84-
},
85-
},
86-
null,
87-
2
88-
)
89-
);
126+
it('should remove project.json file when removing project configuration', () => {
127+
addProjectConfiguration(tree, 'test', baseTestProjectConfig, true);
128+
removeProjectConfiguration(tree, 'test');
90129

91-
const projectConfig = readProjectConfiguration(tree, 'test');
130+
expect(readJson(tree, 'workspace.json').projects.test).toBeUndefined();
131+
expect(tree.exists('test/project.json')).toBeFalsy();
132+
});
92133

93-
expect(projectConfig).toEqual(baseTestProjectConfig);
134+
it('should support workspaces with standalone and inline projects', () => {
135+
addProjectConfiguration(tree, 'test', baseTestProjectConfig, true);
136+
addProjectConfiguration(tree, 'test2', baseTestProjectConfig, false);
137+
const configurations = getProjects(tree);
138+
expect(configurations.get('test')).toEqual(baseTestProjectConfig);
139+
expect(configurations.get('test2')).toEqual(baseTestProjectConfig);
140+
});
94141
});
95142

96-
it('should update project.json file when updating a project', () => {
97-
addProjectConfiguration(tree, 'test', baseTestProjectConfig, true);
98-
const expectedProjectConfig = {
99-
...baseTestProjectConfig,
100-
targets: { build: { executor: '' } },
101-
};
102-
updateProjectConfiguration(tree, 'test', expectedProjectConfig);
103-
104-
expect(readJson(tree, 'libs/test/project.json')).toEqual(
105-
expectedProjectConfig
106-
);
107-
});
143+
describe('updateWorkspaceConfiguration', () => {
144+
let workspaceConfiguration: WorkspaceConfiguration;
145+
beforeEach(() => {
146+
workspaceConfiguration = readWorkspaceConfiguration(tree);
147+
});
108148

109-
it('should update workspace.json file when updating an inline project', () => {
110-
addProjectConfiguration(tree, 'test', baseTestProjectConfig, false);
111-
const expectedProjectConfig = {
112-
...baseTestProjectConfig,
113-
targets: { build: { executor: '' } },
114-
};
115-
updateProjectConfiguration(tree, 'test', expectedProjectConfig);
116-
117-
expect(readJson(tree, 'workspace.json').projects.test).toEqual(
118-
expectedProjectConfig
119-
);
120-
});
149+
it('should update properties in workspace.json', () => {
150+
workspaceConfiguration.version = 3;
121151

122-
it('should remove project.json file when removing project configuration', () => {
123-
addProjectConfiguration(tree, 'test', baseTestProjectConfig, true);
124-
removeProjectConfiguration(tree, 'test');
152+
updateWorkspaceConfiguration(tree, workspaceConfiguration);
125153

126-
expect(readJson(tree, 'workspace.json').projects.test).toBeUndefined();
127-
expect(tree.exists('test/project.json')).toBeFalsy();
128-
});
154+
expect(readJson(tree, 'workspace.json').version).toEqual(3);
155+
});
156+
157+
it('should update properties in nx.json', () => {
158+
workspaceConfiguration.npmScope = 'new-npmScope';
129159

130-
it('should support workspaces with standalone and inline projects', () => {
131-
addProjectConfiguration(tree, 'test', baseTestProjectConfig, true);
132-
addProjectConfiguration(tree, 'test2', baseTestProjectConfig, false);
133-
const configurations = getProjects(tree);
134-
expect(configurations.get('test')).toEqual(baseTestProjectConfig);
135-
expect(configurations.get('test2')).toEqual(baseTestProjectConfig);
160+
updateWorkspaceConfiguration(tree, workspaceConfiguration);
161+
162+
expect(readJson(tree, 'nx.json').npmScope).toEqual('new-npmScope');
163+
});
164+
165+
it('should not update unknown properties', () => {
166+
workspaceConfiguration['$schema'] = 'schema';
167+
168+
updateWorkspaceConfiguration(tree, workspaceConfiguration);
169+
170+
expect(readJson(tree, 'workspace.json').$schema).not.toBeDefined();
171+
expect(readJson(tree, 'nx.json').$schema).not.toBeDefined();
172+
});
136173
});
137174
});

packages/devkit/src/generators/project-configuration.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,37 @@ export function updateWorkspaceConfiguration(
126126
host: Tree,
127127
workspaceConfig: WorkspaceConfiguration
128128
): void {
129-
const { version, cli, defaultProject, generators, ...nxJson } =
130-
workspaceConfig;
129+
const {
130+
// Angular Json Properties
131+
version,
132+
cli,
133+
defaultProject,
134+
generators,
135+
136+
// Nx Json Properties
137+
implicitDependencies,
138+
plugins,
139+
npmScope,
140+
targetDependencies,
141+
workspaceLayout,
142+
tasksRunnerOptions,
143+
affected,
144+
} = workspaceConfig;
131145
const workspace: Omit<Required<WorkspaceJsonConfiguration>, 'projects'> = {
132146
version,
133147
cli,
134148
defaultProject,
135149
generators,
136150
};
151+
const nxJson: Omit<Required<NxJsonConfiguration>, 'projects'> = {
152+
implicitDependencies,
153+
plugins,
154+
npmScope,
155+
targetDependencies,
156+
workspaceLayout,
157+
tasksRunnerOptions,
158+
affected,
159+
};
137160

138161
updateJson<WorkspaceJsonConfiguration>(
139162
host,

0 commit comments

Comments
 (0)