{"record":{"id":"dae89183297f3322","repo":"angular/angular-cli","slug":"cannot-create-file-path","errorCode":null,"errorMessage":"Cannot create file \"${path}\".","messagePattern":"Cannot create file \"(.+?)\"\\.","errorType":"exception","errorClass":"CannotCreateFileException","httpStatus":null,"severity":"error","filePath":"packages/angular_devkit/schematics/src/tree/null.ts","lineNumber":109,"sourceCode":"  // Change content of host files.\n  beginUpdate(path: string): never {\n    throw new FileDoesNotExistException(path);\n  }\n  commitUpdate(record: UpdateRecorder): never {\n    throw new FileDoesNotExistException(\n      record instanceof UpdateRecorderBase ? record.path : '<unknown>',\n    );\n  }\n\n  // Change structure of the host.\n  copy(path: string, _to: string): never {\n    throw new FileDoesNotExistException(path);\n  }\n  delete(path: string): never {\n    throw new FileDoesNotExistException(path);\n  }\n  create(path: string, _content: Buffer | string): never {\n    throw new CannotCreateFileException(path);\n  }\n  rename(path: string, _to: string): never {\n    throw new FileDoesNotExistException(path);\n  }\n  overwrite(path: string, _content: Buffer | string): never {\n    throw new FileDoesNotExistException(path);\n  }\n\n  apply(_action: Action, _strategy?: MergeStrategy): void {}\n  get actions(): Action[] {\n    return [];\n  }\n}\n","sourceCodeStart":91,"sourceCodeEnd":123,"githubUrl":"https://github.com/angular/angular-cli/blob/bb72145f9ab45aee29f523236b3a25cd0813a841/packages/angular_devkit/schematics/src/tree/null.ts#L91-L123","documentation":"NullTree.create always throws CannotCreateFileException — a distinct exception from FileDoesNotExistException — because file creation is unsupported on the null tree. It means new-file creation was attempted where no real host exists.","triggerScenarios":"Calling tree.create(path, content) on a NullTree, directly or from template/component generation rules.","commonSituations":"Generating files in a schematic that runs outside a valid workspace; NullTree injected due to context creation failure; test stubs.","solutions":["Run the schematic with a proper workspace host so the Tree is a real HostTree.","Check the tree instance type/existence before creating files and throw a descriptive SchematicsException.","In tests, use an in-memory HostTree instead of NullTree."],"exampleFix":"// before\ntree.create('/src/app/new-file.ts', content);\n// after\nif (!tree.root || tree.root.subfiles === undefined && tree instanceof NullTree) {\n  throw new SchematicsException('Cannot create files: no workspace tree available');\n}\ntree.create('/src/app/new-file.ts', content);","handlingStrategy":"try-catch","validationCode":"if (!tree.exists(path)) { tree.create(path, content); } else { tree.overwrite(path, content); }","typeGuard":"function canCreate(t: Tree, p: string): boolean {\n  return !(t instanceof NullTree) && !t.exists(p);\n}","tryCatchPattern":"try {\n  tree.create(path, content);\n} catch (e) {\n  if (e instanceof CannotCreateFileException || e instanceof FileDoesNotExistException) {\n    throw new SchematicsException(`Cannot create ${path}: no writable tree`);\n  }\n  throw e;\n}","preventionTips":["Branch create vs overwrite on tree.exists(path)","Confirm the schematic runs inside a valid workspace","Use in-memory HostTree in tests","Surface CannotCreateFileException as a user-facing SchematicsException"],"tags":["schematics","tree","cannot-create-file","null-tree"],"backgroundTag":"cannot-create-file","analyzedSha":"bb72145f9ab45aee29f523236b3a25cd0813a841","analyzedAt":"2026-08-30T02:47:34.745Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}