angular/components · error · SchematicsException
Could not find file for path: ${path}
Error message
Could not find file for path: ${path} What it means
parseSourceFile reads a file from the schematic Tree and converts it to a ts.SourceFile. If host.read(path) returns no buffer, the file doesn't exist in the virtual workspace tree, so it throws SchematicsException. The migration/schematic cannot analyze a source file that isn't in the workspace.
Source
Thrown at src/cdk/schematics/utils/ast.ts:23
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import {SchematicsException, Tree} from '@angular-devkit/schematics';
import {Schema as ComponentOptions} from '@schematics/angular/component/schema';
import {InsertChange} from '@schematics/angular/utility/change';
import {ProjectDefinition, readWorkspace} from '@schematics/angular/utility';
import {findModuleFromOptions as internalFindModule} from '@schematics/angular/utility/find-module';
import {addImportToModule} from '@schematics/angular/utility/ast-utils';
import {getAppModulePath} from '@schematics/angular/utility/ng-ast-utils';
import * as ts from 'typescript';
import {getProjectMainFile} from './project-main-file';
/** Reads file given path and returns TypeScript source file. */
export function parseSourceFile(host: Tree, path: string): ts.SourceFile {
const buffer = host.read(path);
if (!buffer) {
throw new SchematicsException(`Could not find file for path: ${path}`);
}
return ts.createSourceFile(path, buffer.toString(), ts.ScriptTarget.Latest, true);
}
/** Import and add module to root app module. */
export function addModuleImportToRootModule(
host: Tree,
moduleName: string,
src: string,
project: ProjectDefinition,
) {
const modulePath = getAppModulePath(host, getProjectMainFile(project));
addModuleImportToModule(host, modulePath, moduleName, src);
}
/**
* Import and add module to specific module path.
* @param host the tree we are updatingView on GitHub (pinned to 0411926e7d)
Solutions
- Verify the target project has a valid main file at the expected path; correct the project's build options 'main' entry in angular.json/project.json.
- Ensure the schematic targets the right project (pass --project=<name> if the default project is wrong).
- Check the path casing and that the file exists in the workspace before running the migration.
- If your entry point is non-standard, restructure or add the expected main file.
Example fix
// before (assuming src/main.ts always exists)
const source = parseSourceFile(tree, '/src/main.ts');
// after (guard first)
const mainPath = getProjectMainFile(project);
if (!tree.exists(mainPath)) {
throw new SchematicsException(`Main file not found at ${mainPath}`);
}
const source = parseSourceFile(tree, mainPath); Defensive patterns
Strategy: validation
Validate before calling
// In a schematic rule, before parsing:
if (!tree.exists(mainPath)) {
throw new SchematicsException(`Main file missing at ${mainPath}; check the project's build 'main' option`);
} Type guard
function fileExistsInTree(tree: Tree, path: string): boolean {
return tree.exists(path) && tree.get(path)?.size > 0;
} Try / catch
try {
const source = parseSourceFile(tree, mainPath);
} catch (e) {
if (e instanceof SchematicsException && /Could not find file/.test(e.message)) {
context.logger.error(`Skipping: ${e.message}`);
return; // or rethrow depending on whether the file is required
}
throw e;
} Prevention
- Confirm the project's build options 'main' path resolves to an existing file before migrating.
- Pass --project explicitly when the workspace has multiple projects.
- Check file path casing (case-sensitive filesystems) and keep the main file committed.
When it happens
Trigger: Calling parseSourceFile(tree, path) where tree.read(path) returns null — the path doesn't exist in the Tree (wrong path casing, file outside the workspace, or a main file path resolved incorrectly via getProjectMainFile).
Common situations: Running a migration on a non-standard workspace where src/main.ts doesn't exist (custom entry point, Nx layout, different naming), an empty or misconfigured project target in angular.json so getProjectMainFile resolves a wrong path, or the file was deleted before the schematic ran.
Related errors
- Module not found: ${modulePath}
- Could not read Angular module file: ${modulePath}
- File ${modulePath} does not exist.
- Could not read file for path: ${htmlFilePath}
- Unable to read directory in virtual file system host. This m
AI-assisted analysis of angular/components@0411926e7d (2026-08-31).
Data as JSON: /api/errors/9058bd42b4eaef2e.
Report an issue: GitHub.