From 2500f34a401c2ffb03b1dfa41299d91ddebe787e Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Thu, 7 Jul 2022 13:45:55 +0000 Subject: [PATCH] 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 624e0b0ec6d74d87914a2385cc42f0108beebbcc) --- .../core/src/workspace/json/reader.ts | 11 ++++++++++- .../core/src/workspace/json/reader_spec.ts | 18 ++++++++++++++++++ .../angular/utility/workspace_spec.ts | 4 +++- 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/packages/angular_devkit/core/src/workspace/json/reader.ts b/packages/angular_devkit/core/src/workspace/json/reader.ts index d780145636db..b0e4fbb6d17c 100644 --- a/packages/angular_devkit/core/src/workspace/json/reader.ts +++ b/packages/angular_devkit/core/src/workspace/json/reader.ts @@ -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(projectNodeValue)) { switch (name) { diff --git a/packages/angular_devkit/core/src/workspace/json/reader_spec.ts b/packages/angular_devkit/core/src/workspace/json/reader_spec.ts index 1ac13b474700..4b0af1043513 100644 --- a/packages/angular_devkit/core/src/workspace/json/reader_spec.ts +++ b/packages/angular_devkit/core/src/workspace/json/reader_spec.ts @@ -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', () => { diff --git a/packages/schematics/angular/utility/workspace_spec.ts b/packages/schematics/angular/utility/workspace_spec.ts index f5b9978719a6..56f6a14b9c59 100644 --- a/packages/schematics/angular/utility/workspace_spec.ts +++ b/packages/schematics/angular/utility/workspace_spec.ts @@ -12,7 +12,9 @@ import { getWorkspace as readWorkspace, updateWorkspace, writeWorkspace } from ' const TEST_WORKSPACE_CONTENT = JSON.stringify({ version: 1, projects: { - 'test': {}, + test: { + root: '', + }, }, });