{"record":{"id":"9058bd42b4eaef2e","repo":"angular/components","slug":"could-not-find-file-for-path-path","errorCode":null,"errorMessage":"Could not find file for path: ${path}","messagePattern":"Could not find file for path: (.+?)","errorType":"exception","errorClass":"SchematicsException","httpStatus":null,"severity":"error","filePath":"src/cdk/schematics/utils/ast.ts","lineNumber":23,"sourceCode":" * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://angular.dev/license\n */\n\nimport {SchematicsException, Tree} from '@angular-devkit/schematics';\nimport {Schema as ComponentOptions} from '@schematics/angular/component/schema';\nimport {InsertChange} from '@schematics/angular/utility/change';\nimport {ProjectDefinition, readWorkspace} from '@schematics/angular/utility';\nimport {findModuleFromOptions as internalFindModule} from '@schematics/angular/utility/find-module';\nimport {addImportToModule} from '@schematics/angular/utility/ast-utils';\nimport {getAppModulePath} from '@schematics/angular/utility/ng-ast-utils';\nimport * as ts from 'typescript';\nimport {getProjectMainFile} from './project-main-file';\n\n/** Reads file given path and returns TypeScript source file. */\nexport function parseSourceFile(host: Tree, path: string): ts.SourceFile {\n  const buffer = host.read(path);\n  if (!buffer) {\n    throw new SchematicsException(`Could not find file for path: ${path}`);\n  }\n  return ts.createSourceFile(path, buffer.toString(), ts.ScriptTarget.Latest, true);\n}\n\n/** Import and add module to root app module. */\nexport function addModuleImportToRootModule(\n  host: Tree,\n  moduleName: string,\n  src: string,\n  project: ProjectDefinition,\n) {\n  const modulePath = getAppModulePath(host, getProjectMainFile(project));\n  addModuleImportToModule(host, modulePath, moduleName, src);\n}\n\n/**\n * Import and add module to specific module path.\n * @param host the tree we are updating","sourceCodeStart":5,"sourceCodeEnd":41,"githubUrl":"https://github.com/angular/components/blob/0411926e7d8ae06b32236ec1048a888cfad5abf2/src/cdk/schematics/utils/ast.ts#L5-L41","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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."],"exampleFix":"// before (assuming src/main.ts always exists)\nconst source = parseSourceFile(tree, '/src/main.ts');\n// after (guard first)\nconst mainPath = getProjectMainFile(project);\nif (!tree.exists(mainPath)) {\n  throw new SchematicsException(`Main file not found at ${mainPath}`);\n}\nconst source = parseSourceFile(tree, mainPath);","handlingStrategy":"validation","validationCode":"// In a schematic rule, before parsing:\nif (!tree.exists(mainPath)) {\n  throw new SchematicsException(`Main file missing at ${mainPath}; check the project's build 'main' option`);\n}","typeGuard":"function fileExistsInTree(tree: Tree, path: string): boolean {\n  return tree.exists(path) && tree.get(path)?.size > 0;\n}","tryCatchPattern":"try {\n  const source = parseSourceFile(tree, mainPath);\n} catch (e) {\n  if (e instanceof SchematicsException && /Could not find file/.test(e.message)) {\n    context.logger.error(`Skipping: ${e.message}`);\n    return; // or rethrow depending on whether the file is required\n  }\n  throw e;\n}","preventionTips":["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."],"tags":["angular","schematics","file-not-found","typescript"],"backgroundTag":"source-file-not-found","analyzedSha":"0411926e7d8ae06b32236ec1048a888cfad5abf2","analyzedAt":"2026-08-31T11:58:23.400Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}