angular/angular-cli · error · FileAlreadyExistException

File already exist.

Error message

File already exist.

What it means

RecordHost's create() (wrapped in write workflow) explicitly rejects creations when the path already exists in the underlying host: `if (super._exists(path)) throw new FileAlreadyExistException(path)`. Record hosts record changes over a delegate host, so even files that exist only in the delegate (not yet recorded) count as existing and block create().

Source

Thrown at packages/angular_devkit/core/src/virtual-fs/host/record.ts:197

          ({
            kind: 'overwrite',
            path,
            content: this._read(path),
          }) as CordHostRecord,
      ),
    ];
  }

  /**
   * Specialized version of {@link CordHost#write} which forces the creation of a file whether it
   * exists or not.
   * @param {} path
   * @param {FileBuffer} content
   * @returns {Observable<void>}
   */
  create(path: Path, content: FileBuffer): Observable<void> {
    if (super._exists(path)) {
      throw new FileAlreadyExistException(path);
    }

    if (this._filesToDelete.has(path)) {
      this._filesToDelete.delete(path);
      this._filesToOverwrite.add(path);
    } else {
      this._filesToCreate.add(path);
    }

    return super.write(path, content);
  }

  overwrite(path: Path, content: FileBuffer): Observable<void> {
    return this.isDirectory(path).pipe(
      switchMap((isDir) => {
        if (isDir) {
          return throwError(new PathIsDirectoryException(path));
        }

View on GitHub (pinned to bb72145f9a)

Solutions

  1. Use host.write(path, content) (or Record's overwrite path) instead of create() when the file may already exist.
  2. Check host.exists(path) first and route to create() only for genuinely new files.
  3. Delete the existing file (host.delete(path)) before calling create() if replacement is intended.
  4. Make the generation idempotent: track created files in the record and skip them on repeat runs.

Example fix

// before
host.create(normalize('src/app/new.component.ts'), content); // throws if it exists
// after
const p = normalize('src/app/new.component.ts');
if (host.exists(p)) {
  host.write(p, content); // overwrite
} else {
  host.create(p, content);
}
Defensive patterns

Strategy: validation

Validate before calling

import { normalize, Path } from '@angular-devkit/core';

function createOrOverwrite(host: { exists(p: Path): boolean; create(p: Path, b: Buffer): void; write(p: Path, b: Buffer): void }, p: Path, content: Buffer): void {
  const abs = normalize(p);
  if (host.exists(abs)) {
    host.write(abs, content);
  } else {
    host.create(abs, content);
  }
}

Try / catch

import { FileAlreadyExistException } from '@angular-devkit/core';

try {
  host.create(path, content);
} catch (e) {
  if (e instanceof FileAlreadyExistException) {
    host.write(path, content); // overwrite via the record
  } else {
    throw e;
  }
}

Prevention

When it happens

Trigger: Calling host.create(path, content) on a path that already exists in the delegate host or in the record; calling create() twice for the same path; using create() where write() (which allows overwrite) was intended; re-running a non-idempotent schematic that creates the same file on a second pass.

Common situations: Schematics generating files into a project where the file already exists (common with `ng generate` re-runs); code that should use overwrite (create + write or write()) but calls create(); retry logic replaying a partially completed run against the same recorded tree.

Related errors


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