From 404383bfcb989f6cc70a03535e06eb94b56c85b9 Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Thu, 30 Jun 2022 09:04:15 +0000 Subject: [PATCH] fix(@angular-devkit/core): provide actionable error when a workspace project has missing `root` property The `root` property is required in a workspace project. Now we issue an actionable error message when this is missing. Closes: #21310 --- .../core/src/workspace/json/reader.ts | 6 ++++++ .../core/src/workspace/json/reader_spec.ts | 15 +++++++++++++++ .../schematics/angular/utility/workspace_spec.ts | 4 +++- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/packages/angular_devkit/core/src/workspace/json/reader.ts b/packages/angular_devkit/core/src/workspace/json/reader.ts index d780145636db..24f3d03ab160 100644 --- a/packages/angular_devkit/core/src/workspace/json/reader.ts +++ b/packages/angular_devkit/core/src/workspace/json/reader.ts @@ -167,6 +167,12 @@ function parseProject( } const projectNodeValue = getNodeValue(projectNode); + if (!('root' in projectNodeValue)) { + context.error( + `Project "${projectName}" is missing a required property "root".`, + 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 d28a9b26b629..d93bf1eeaf08 100644 --- a/packages/angular_devkit/core/src/workspace/json/reader_spec.ts +++ b/packages/angular_devkit/core/src/workspace/json/reader_spec.ts @@ -137,6 +137,21 @@ describe('readJsonWorkpace Parsing', () => { /version specifier not found/, ); }); + + it('errors on missing root property in a project', async () => { + const host = createTestHost(stripIndent` + { + "version": 1, + "projects": { + "foo": {} + } + } + `); + + await expectAsync(readJsonWorkspace('', host)).toBeRejectedWithError( + /Project "foo" is missing a required property "root"/, + ); + }); }); 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: '', + }, }, });