{"record":{"id":"3b8a951ef252b70e","repo":"angular/angular-cli","slug":"file-already-exist-3b8a95","errorCode":null,"errorMessage":"File already exist.","messagePattern":"File already exist\\.","errorType":"exception","errorClass":"FileAlreadyExistException","httpStatus":null,"severity":"error","filePath":"packages/angular_devkit/core/src/virtual-fs/host/record.ts","lineNumber":197,"sourceCode":"          ({\n            kind: 'overwrite',\n            path,\n            content: this._read(path),\n          }) as CordHostRecord,\n      ),\n    ];\n  }\n\n  /**\n   * Specialized version of {@link CordHost#write} which forces the creation of a file whether it\n   * exists or not.\n   * @param {} path\n   * @param {FileBuffer} content\n   * @returns {Observable<void>}\n   */\n  create(path: Path, content: FileBuffer): Observable<void> {\n    if (super._exists(path)) {\n      throw new FileAlreadyExistException(path);\n    }\n\n    if (this._filesToDelete.has(path)) {\n      this._filesToDelete.delete(path);\n      this._filesToOverwrite.add(path);\n    } else {\n      this._filesToCreate.add(path);\n    }\n\n    return super.write(path, content);\n  }\n\n  overwrite(path: Path, content: FileBuffer): Observable<void> {\n    return this.isDirectory(path).pipe(\n      switchMap((isDir) => {\n        if (isDir) {\n          return throwError(new PathIsDirectoryException(path));\n        }","sourceCodeStart":179,"sourceCodeEnd":215,"githubUrl":"https://github.com/angular/angular-cli/blob/bb72145f9ab45aee29f523236b3a25cd0813a841/packages/angular_devkit/core/src/virtual-fs/host/record.ts#L179-L215","documentation":"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().","triggerScenarios":"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.","commonSituations":"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.","solutions":["Use host.write(path, content) (or Record's overwrite path) instead of create() when the file may already exist.","Check host.exists(path) first and route to create() only for genuinely new files.","Delete the existing file (host.delete(path)) before calling create() if replacement is intended.","Make the generation idempotent: track created files in the record and skip them on repeat runs."],"exampleFix":"// before\nhost.create(normalize('src/app/new.component.ts'), content); // throws if it exists\n// after\nconst p = normalize('src/app/new.component.ts');\nif (host.exists(p)) {\n  host.write(p, content); // overwrite\n} else {\n  host.create(p, content);\n}","handlingStrategy":"validation","validationCode":"import { normalize, Path } from '@angular-devkit/core';\n\nfunction createOrOverwrite(host: { exists(p: Path): boolean; create(p: Path, b: Buffer): void; write(p: Path, b: Buffer): void }, p: Path, content: Buffer): void {\n  const abs = normalize(p);\n  if (host.exists(abs)) {\n    host.write(abs, content);\n  } else {\n    host.create(abs, content);\n  }\n}","typeGuard":null,"tryCatchPattern":"import { FileAlreadyExistException } from '@angular-devkit/core';\n\ntry {\n  host.create(path, content);\n} catch (e) {\n  if (e instanceof FileAlreadyExistException) {\n    host.write(path, content); // overwrite via the record\n  } else {\n    throw e;\n  }\n}","preventionTips":["Prefer write() over create() whenever the file may already exist in the delegate host.","Call exists() before create() and route to overwrite logic on true.","Make generation steps idempotent — skip or overwrite files created in earlier runs.","Remember RecordHost.create() also sees files existing only in the underlying delegate host, not just in the record."],"tags":["virtual-fs","file-system","angular-devkit","conflict"],"backgroundTag":"file-already-exists","analyzedSha":"bb72145f9ab45aee29f523236b3a25cd0813a841","analyzedAt":"2026-08-30T02:47:34.745Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}