Skip to content

Commit 2500f34

Browse files
committed
fix(@angular-devkit/core): provide actionable warning when a workspace project has missing root property
The `root` property is required in a workspace project. Now we issue an actionable warning message when this is missing. Note: this will become an error in the next major version. Closes: #21310 (cherry picked from commit 624e0b0)
1 parent 1785505 commit 2500f34

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

packages/angular_devkit/core/src/workspace/json/reader.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,10 @@ export async function readJsonWorkspace(
6060
// TODO: Diagnostic reporting support
6161
throw new Error(message);
6262
},
63-
warn(_message, _node) {
63+
warn(message, _node) {
6464
// TODO: Diagnostic reporting support
65+
// eslint-disable-next-line no-console
66+
console.warn(message);
6567
},
6668
};
6769

@@ -167,6 +169,13 @@ function parseProject(
167169
}
168170

169171
const projectNodeValue = getNodeValue(projectNode);
172+
if (!('root' in projectNodeValue)) {
173+
// TODO(alan-agius4): change this to error in v15.
174+
context.warn(
175+
`Project "${projectName}" is missing a required property "root". This will become an error in the next major version.`,
176+
projectNodeValue,
177+
);
178+
}
170179

171180
for (const [name, value] of Object.entries<JsonValue>(projectNodeValue)) {
172181
switch (name) {

packages/angular_devkit/core/src/workspace/json/reader_spec.ts

+18
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,24 @@ describe('readJsonWorkpace Parsing', () => {
143143
expect(e.message).toContain('version specifier not found');
144144
}
145145
});
146+
147+
it('warns on missing root property in a project', async () => {
148+
const host = createTestHost(stripIndent`
149+
{
150+
"version": 1,
151+
"projects": {
152+
"foo": {}
153+
}
154+
}
155+
`);
156+
157+
const consoleWarnSpy = spyOn(console, 'warn').and.callFake(() => undefined);
158+
await expectAsync(readJsonWorkspace('', host));
159+
160+
expect(consoleWarnSpy).toHaveBeenCalledWith(
161+
`Project "foo" is missing a required property "root". This will become an error in the next major version.`,
162+
);
163+
});
146164
});
147165

148166
describe('JSON WorkspaceDefinition Tracks Workspace Changes', () => {

packages/schematics/angular/utility/workspace_spec.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import { getWorkspace as readWorkspace, updateWorkspace, writeWorkspace } from '
1212
const TEST_WORKSPACE_CONTENT = JSON.stringify({
1313
version: 1,
1414
projects: {
15-
'test': {},
15+
test: {
16+
root: '',
17+
},
1618
},
1719
});
1820

0 commit comments

Comments
 (0)