Skip to content

Commit

Permalink
fix(@angular/cli): resolve relative schematic from angular.json ins…
Browse files Browse the repository at this point in the history
…tead of current working directory

Relative schematics referenced in `angular.json` `schematicCollections` and `defaultCollection` were always resolved from the current working directory, which is not correct and caused the collection not to be resolved when the this is different from the location of the workspace config.

Closes #23136
  • Loading branch information
alan-agius4 authored and clydin committed May 18, 2022
1 parent b9a8e91 commit e20964c
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions packages/angular/cli/models/schematic-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
} from '@angular-devkit/schematics/tools';
import * as inquirer from 'inquirer';
import * as systemPath from 'path';
import { resolve } from 'path';
import { colors } from '../utilities/color';
import { getProjectByCwd, getSchematicDefaults, getWorkspace } from '../utilities/config';
import { parseJsonSchemaToOptions } from '../utilities/json-schema';
Expand Down Expand Up @@ -373,20 +374,26 @@ export abstract class SchematicCommand<
}

protected async getDefaultSchematicCollection(): Promise<string> {
// Resolve relative collections from the location of `angular.json`
const resolveRelativeCollection = (collectionName: string) =>
collectionName.charAt(0) === '.'
? resolve(this.context.root, collectionName)
: collectionName;

let workspace = await getWorkspace('local');

if (workspace) {
const project = getProjectByCwd(workspace);
if (project && workspace.getProjectCli(project)) {
const value = workspace.getProjectCli(project)['defaultCollection'];
if (typeof value == 'string') {
return value;
return resolveRelativeCollection(value);
}
}
if (workspace.getCli()) {
const value = workspace.getCli()['defaultCollection'];
if (typeof value == 'string') {
return value;
return resolveRelativeCollection(value);
}
}
}
Expand All @@ -395,7 +402,7 @@ export abstract class SchematicCommand<
if (workspace && workspace.getCli()) {
const value = workspace.getCli()['defaultCollection'];
if (typeof value == 'string') {
return value;
return resolveRelativeCollection(value);
}
}

Expand Down

0 comments on commit e20964c

Please sign in to comment.