Skip to content

Commit

Permalink
fix(@angular-devkit/core): provide actionable warning when a workspac…
Browse files Browse the repository at this point in the history
…e 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)
  • Loading branch information
alan-agius4 committed Jul 8, 2022
1 parent 1785505 commit 2500f34
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
11 changes: 10 additions & 1 deletion packages/angular_devkit/core/src/workspace/json/reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
};

Expand Down Expand Up @@ -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) {
Expand Down
18 changes: 18 additions & 0 deletions packages/angular_devkit/core/src/workspace/json/reader_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,24 @@ describe('readJsonWorkpace Parsing', () => {
expect(e.message).toContain('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', () => {
Expand Down
4 changes: 3 additions & 1 deletion packages/schematics/angular/utility/workspace_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import { getWorkspace as readWorkspace, updateWorkspace, writeWorkspace } from '
const TEST_WORKSPACE_CONTENT = JSON.stringify({
version: 1,
projects: {
'test': {},
test: {
root: '',
},
},
});

Expand Down

0 comments on commit 2500f34

Please sign in to comment.