{"record":{"id":"d3549133ff467fae","repo":"angular/components","slug":"cannot-read-filepath-because-it-does-not-exis","errorCode":null,"errorMessage":"Cannot read \"${filePath}\" because it does not exist.","messagePattern":"Cannot read \"(.+?)\" because it does not exist\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/cdk/schematics/testing/file-content.ts","lineNumber":16,"sourceCode":"/**\n * @license\n * Copyright Google LLC All Rights Reserved.\n *\n * 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 {Tree} from '@angular-devkit/schematics';\n\n/** Gets the content of a specified file from a schematic tree. */\nexport function getFileContent(tree: Tree, filePath: string): string {\n  const contentBuffer = tree.read(filePath);\n\n  if (!contentBuffer) {\n    throw new Error(`Cannot read \"${filePath}\" because it does not exist.`);\n  }\n\n  return contentBuffer.toString();\n}\n","sourceCodeStart":1,"sourceCodeEnd":21,"githubUrl":"https://github.com/angular/components/blob/0411926e7d8ae06b32236ec1048a888cfad5abf2/src/cdk/schematics/testing/file-content.ts#L1-L21","documentation":"getFileContent is a schematic-testing helper that reads a file from a schematic Tree. Tree.read returns undefined/falsy when the path doesn't exist in the virtual file system, and the helper surfaces that as this explicit error naming the path.","triggerScenarios":"Calling getFileContent(tree, 'src/app/app.module.ts') (or helpers packageJson/module/component/test that use it) during a schematic test when the file was never created by the rule under test, or the path string is wrong/relative-to-wrong-root.","commonSituations":"Schematic renamed or moved a file so the expected path doesn't exist; testing a rule that only writes some files; path typo or missing leading directory; asserting against files outside the tree root.","solutions":["Verify the path exists in the tree before reading: if (!tree.exists(filePath)) handle or fail with a clear message.","Fix the file path (check the schematic's output paths; use tree.files to list what exists).","Ensure the schematic under test actually creates the file (run/apply the rule before asserting).","In tests, use tree.readDir or log tree.files to debug which files were generated."],"exampleFix":"// before\nconst content = getFileContent(tree, '/src/app/old.module.ts');\n// after\nconst path = '/src/app/app.module.ts';\nif (!tree.exists(path)) {\n  throw new Error(`Expected ${path}; tree has: ${tree.files.join(', ')}`);\n}\nconst content = getFileContent(tree, path);","handlingStrategy":"validation","validationCode":"if (!tree.exists(filePath)) {\n  throw new Error(`Expected file missing from schematic tree: ${filePath}`);\n}\nconst content = getFileContent(tree, filePath);","typeGuard":null,"tryCatchPattern":"try {\n  const content = getFileContent(tree, path);\n} catch (e) {\n  if ((e as Error).message.startsWith('Cannot read')) {\n    console.error(`Tree files: ${tree.files.join(', ')}`);\n  }\n  throw e;\n}","preventionTips":["Call tree.exists before reading any schematic file.","Debug with tree.files / tree.readDir to see what the rule produced.","Centralize expected paths in constants to avoid typos."],"tags":["angular","schematics","testing","file-io"],"backgroundTag":"file-not-found-in-tree","analyzedSha":"0411926e7d8ae06b32236ec1048a888cfad5abf2","analyzedAt":"2026-08-31T11:58:23.400Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}