Skip to content

Commit 6390944

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: angular#21310
1 parent 62b8568 commit 6390944

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(alanagius4): 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
@@ -137,6 +137,24 @@ describe('readJsonWorkpace Parsing', () => {
137137
/version specifier not found/,
138138
);
139139
});
140+
141+
it('warns on missing root property in a project', async () => {
142+
const host = createTestHost(stripIndent`
143+
{
144+
"version": 1,
145+
"projects": {
146+
"foo": {}
147+
}
148+
}
149+
`);
150+
151+
const consoleWarnSpy = spyOn(console, 'warn').and.callFake(() => undefined);
152+
await expectAsync(readJsonWorkspace('', host));
153+
154+
expect(consoleWarnSpy).toHaveBeenCalledWith(
155+
`Project "foo" is missing a required property "root". This will become an error in the next major version.`,
156+
);
157+
});
140158
});
141159

142160
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)