Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(@angular-devkit/schematics): properly resolve relative schematics when executed from a nested directory #29986

Merged
merged 1 commit into from
Apr 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import { BaseException } from '@angular-devkit/core';
import { dirname, join, resolve } from 'node:path';
import { dirname, resolve } from 'node:path';
import { RuleFactory } from '../src';
import { FileSystemCollectionDesc, FileSystemSchematicDesc } from './description';
import { ExportStringRef } from './export-ref';
Expand Down Expand Up @@ -46,20 +46,14 @@ export class NodeModulesEngineHost extends FileSystemEngineHostBase {
}
}

const relativeBase = requester ? dirname(requester) : process.cwd();
let collectionPath: string | undefined = undefined;

if (name.startsWith('.')) {
name = resolve(relativeBase, name);
}

const resolveOptions = {
paths: requester ? [dirname(requester), ...(this.paths || [])] : this.paths,
};

// Try to resolve as a package
try {
const packageJsonPath = require.resolve(join(name, 'package.json'), resolveOptions);
const packageJsonPath = require.resolve(`${name}/package.json`, resolveOptions);
const { schematics } = require(packageJsonPath);

if (!schematics || typeof schematics !== 'string') {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import assert from 'node:assert';
import { join } from 'node:path';
import { ng } from '../../utils/process';
import { writeMultipleFiles, createDir } from '../../utils/fs';
import { updateJsonFile } from '../../utils/project';

export default async function () {
// setup temp collection
await createDir('./fake-schematics');
await writeMultipleFiles({
'./fake-schematics/package.json': JSON.stringify({
'schematics': './collection.json',
}),
'./fake-schematics/collection.json': JSON.stringify({
'schematics': {
'fake': {
'description': 'Fake schematic',
'schema': './fake-schema.json',
'factory': './fake',
},
},
}),
'./fake-schematics/fake-schema.json': JSON.stringify({
'$id': 'FakeSchema',
'title': 'Fake Schema',
'type': 'object',
}),
'./fake-schematics/fake.js': `
exports.default = () => (host, context) => context.logger.info('fake schematic run.');
`,
});

await updateJsonFile('angular.json', (json) => {
json.cli ??= {};
json.cli.schematicCollections = ['./fake-schematics'];
});

const { stdout: stdout1 } = await ng('generate', '--help');
assert.match(stdout1, /Fake schematic/);

const { stdout: stdout2 } = await ng('generate', 'fake');
assert.match(stdout2, /fake schematic run/);

// change cwd to a nested directory to validate the relative schematic is resolved correctly
const originalCwd = process.cwd();
try {
process.chdir(join(originalCwd, 'src/app'));
const { stdout: stdout3 } = await ng('generate', 'fake');
assert.match(stdout3, /fake schematic run/);
} finally {
process.chdir(originalCwd);
}
}