Skip to content

Commit 6449a75

Browse files
committed
fix(@angular/cli): use default project when project path is ambiguous
1 parent c638af2 commit 6449a75

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

packages/angular/cli/models/schematic-command.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,21 @@ export abstract class SchematicCommand extends Command {
128128

129129
workflow.registry.addSmartDefaultProvider('projectName', (_schema: JsonObject) => {
130130
if (this._workspace) {
131+
try {
131132
return this._workspace.getProjectByPath(normalize(process.cwd()))
132133
|| this._workspace.getDefaultProjectName();
134+
} catch (e) {
135+
if (e instanceof experimental.workspace.AmbiguousProjectPathException) {
136+
this.logger.warn(tags.oneLine`
137+
Two or more projects are using identical roots.
138+
Unable to determine project using current working directory.
139+
Using default workspace project instead.
140+
`);
141+
142+
return this._workspace.getDefaultProjectName();
143+
}
144+
throw e;
145+
}
133146
}
134147

135148
return undefined;
@@ -346,7 +359,7 @@ export abstract class SchematicCommand extends Command {
346359

347360
private _cleanDefaults<T, K extends keyof T>(defaults: T, undefinedOptions: string[]): T {
348361
(Object.keys(defaults) as K[])
349-
.filter(key => !undefinedOptions.map(strings.camelize).includes(key))
362+
.filter(key => !undefinedOptions.map(strings.camelize).includes(key as string))
350363
.forEach(key => {
351364
delete defaults[key];
352365
});

packages/angular/cli/utilities/config.ts

+15-4
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,22 @@ export function validateWorkspace(json: JsonObject) {
140140
return true;
141141
}
142142

143+
function getProjectByCwd(workspace: experimental.workspace.Workspace): string | null {
144+
try {
145+
return workspace.getProjectByPath(normalize(process.cwd()));
146+
} catch (e) {
147+
if (e instanceof experimental.workspace.AmbiguousProjectPathException) {
148+
return workspace.getDefaultProjectName();
149+
}
150+
throw e;
151+
}
152+
}
153+
143154
export function getPackageManager(): string {
144155
let workspace = getWorkspace('local');
145156

146157
if (workspace) {
147-
const project = workspace.getProjectByPath(normalize(process.cwd()));
158+
const project = getProjectByCwd(workspace);
148159
if (project && workspace.getProjectCli(project)) {
149160
const value = workspace.getProjectCli(project)['packageManager'];
150161
if (typeof value == 'string') {
@@ -258,7 +269,7 @@ export function getDefaultSchematicCollection(): string {
258269
let workspace = getWorkspace('local');
259270

260271
if (workspace) {
261-
const project = workspace.getProjectByPath(normalize(process.cwd()));
272+
const project = getProjectByCwd(workspace);
262273
if (project && workspace.getProjectCli(project)) {
263274
const value = workspace.getProjectCli(project)['defaultCollection'];
264275
if (typeof value == 'string') {
@@ -319,7 +330,7 @@ export function getSchematicDefaults(
319330
}
320331
}
321332

322-
project = project || workspace.getProjectByPath(normalize(process.cwd()));
333+
project = project || getProjectByCwd(workspace);
323334
if (project && workspace.getProjectSchematics(project)) {
324335
const schematicObject = workspace.getProjectSchematics(project)[fullName];
325336
if (schematicObject) {
@@ -339,7 +350,7 @@ export function isWarningEnabled(warning: string): boolean {
339350
let workspace = getWorkspace('local');
340351

341352
if (workspace) {
342-
const project = workspace.getProjectByPath(normalize(process.cwd()));
353+
const project = getProjectByCwd(workspace);
343354
if (project && workspace.getProjectCli(project)) {
344355
const warnings = workspace.getProjectCli(project)['warnings'];
345356
if (typeof warnings == 'object' && !Array.isArray(warnings)) {

0 commit comments

Comments
 (0)