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
This commit is contained in:
Alan Agius 2022-07-07 13:45:55 +00:00 committed by Alan Agius
parent 3d6ed0caf3
commit 624e0b0ec6
3 changed files with 31 additions and 2 deletions

View File

@ -60,8 +60,10 @@ export async function readJsonWorkspace(
// TODO: Diagnostic reporting support
throw new Error(message);
},
warn(_message, _node) {
warn(message, _node) {
// TODO: Diagnostic reporting support
// eslint-disable-next-line no-console
console.warn(message);
},
};
@ -167,6 +169,13 @@ function parseProject(
}
const projectNodeValue = getNodeValue(projectNode);
if (!('root' in projectNodeValue)) {
// TODO(alan-agius4): change this to error in v15.
context.warn(
`Project "${projectName}" is missing a required property "root". This will become an error in the next major version.`,
projectNodeValue,
);
}
for (const [name, value] of Object.entries<JsonValue>(projectNodeValue)) {
switch (name) {

View File

@ -137,6 +137,24 @@ describe('readJsonWorkpace Parsing', () => {
/version specifier not found/,
);
});
it('warns on missing root property in a project', async () => {
const host = createTestHost(stripIndent`
{
"version": 1,
"projects": {
"foo": {}
}
}
`);
const consoleWarnSpy = spyOn(console, 'warn').and.callFake(() => undefined);
await expectAsync(readJsonWorkspace('', host));
expect(consoleWarnSpy).toHaveBeenCalledWith(
`Project "foo" is missing a required property "root". This will become an error in the next major version.`,
);
});
});
describe('JSON WorkspaceDefinition Tracks Workspace Changes', () => {

View File

@ -12,7 +12,9 @@ import { getWorkspace as readWorkspace, updateWorkspace, writeWorkspace } from '
const TEST_WORKSPACE_CONTENT = JSON.stringify({
version: 1,
projects: {
'test': {},
test: {
root: '',
},
},
});