angular/angular-cli · error · CannotCreateFileException

Cannot create file "${path}".

Error message

Cannot create file "${path}".

What it means

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.

Source

Thrown at packages/angular_devkit/schematics/src/tree/null.ts:109

  // Change content of host files.
  beginUpdate(path: string): never {
    throw new FileDoesNotExistException(path);
  }
  commitUpdate(record: UpdateRecorder): never {
    throw new FileDoesNotExistException(
      record instanceof UpdateRecorderBase ? record.path : '<unknown>',
    );
  }

  // Change structure of the host.
  copy(path: string, _to: string): never {
    throw new FileDoesNotExistException(path);
  }
  delete(path: string): never {
    throw new FileDoesNotExistException(path);
  }
  create(path: string, _content: Buffer | string): never {
    throw new CannotCreateFileException(path);
  }
  rename(path: string, _to: string): never {
    throw new FileDoesNotExistException(path);
  }
  overwrite(path: string, _content: Buffer | string): never {
    throw new FileDoesNotExistException(path);
  }

  apply(_action: Action, _strategy?: MergeStrategy): void {}
  get actions(): Action[] {
    return [];
  }
}

View on GitHub (pinned to bb72145f9a)

Solutions

  1. Run the schematic with a proper workspace host so the Tree is a real HostTree.
  2. Check the tree instance type/existence before creating files and throw a descriptive SchematicsException.
  3. In tests, use an in-memory HostTree instead of NullTree.

Example fix

// before
tree.create('/src/app/new-file.ts', content);
// after
if (!tree.root || tree.root.subfiles === undefined && tree instanceof NullTree) {
  throw new SchematicsException('Cannot create files: no workspace tree available');
}
tree.create('/src/app/new-file.ts', content);
Defensive patterns

Strategy: try-catch

Validate before calling

if (!tree.exists(path)) { tree.create(path, content); } else { tree.overwrite(path, content); }

Type guard

function canCreate(t: Tree, p: string): boolean {
  return !(t instanceof NullTree) && !t.exists(p);
}

Try / catch

try {
  tree.create(path, content);
} catch (e) {
  if (e instanceof CannotCreateFileException || e instanceof FileDoesNotExistException) {
    throw new SchematicsException(`Cannot create ${path}: no writable tree`);
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling tree.create(path, content) on a NullTree, directly or from template/component generation rules.

Common situations: Generating files in a schematic that runs outside a valid workspace; NullTree injected due to context creation failure; test stubs.

Related errors


AI-assisted analysis of angular/angular-cli@bb72145f9a (2026-08-30). Data as JSON: /api/errors/dae89183297f3322. Report an issue: GitHub.