angular/angular · error · SchematicsException

Cannot run service migration outside of the current project.

Error message

Cannot run service migration outside of the current project.

What it means

The service migration schematic (migrating providedIn: 'root' services, etc. as part of the standalone/inject family) applies the same cwd escape guard as the inject migration: a truthy options.path starting with '..' is rejected so files outside the current project are never touched.

Source

Thrown at packages/core/schematics/ng-generate/service-migration/index.ts:29

import ts from 'typescript';

import {normalizePath} from '../../utils/change_tracker';
import {canMigrateFile, createMigrationProgram} from '../../utils/typescript/compiler_host';

import {migrateFile} from './migration';
import {getProjectTsConfigPaths} from '../../utils/project_tsconfig_paths';

interface Options {
  path: string;
}

export function migrate(options: Options): Rule {
  return async (tree: Tree, context: SchematicContext) => {
    const basePath = process.cwd();
    let pathToMigrate: string | undefined;
    if (options.path) {
      if (options.path.startsWith('..')) {
        throw new SchematicsException(
          'Cannot run service migration outside of the current project.',
        );
      }
      pathToMigrate = normalizePath(join(basePath, options.path));
    }

    const {buildPaths, testPaths} = await getProjectTsConfigPaths(tree);
    const allPaths = [...buildPaths, ...testPaths];

    if (!allPaths.length) {
      context.logger.warn('Could not find any tsconfig file. Cannot run the service migration.');
      return;
    }

    let sourceFilesCount = 0;

    for (const tsconfigPath of allPaths) {
      const program = createMigrationProgram(tree, tsconfigPath, basePath);

View on GitHub (pinned to 51cb07e980)

Solutions

  1. Run the command from the target project's root directory.
  2. Use a --path that does not start with '..' (e.g. ./src or ./projects/shared).
  3. Run a separate invocation inside each workspace of a monorepo.

Example fix

# before
ng generate service-migration --path ../shared

# after (from the workspace root)
ng generate service-migration --path ./projects/shared
Defensive patterns

Strategy: validation

Validate before calling

function assertSafeMigrationPath(pathOpt?: string): void {
  if (pathOpt && pathOpt.startsWith('..')) {
    throw new Error(`Refusing migration path outside project: ${pathOpt}`);
  }
}

Prevention

When it happens

Trigger: Running ng generate service-migration --path ../lib or any --path value whose first characters are '..'. Only a truthy --path is checked; omitting the flag skips the guard.

Common situations: Running the migration from a nested folder while pointing at a parent directory; trying to cover a shared library from the app workspace; copied CI commands with hardcoded relative paths.

Related errors


AI-assisted analysis of angular/angular@51cb07e980 (2026-08-22). Data as JSON: /api/errors/3765263f9154a75e. Report an issue: GitHub.