{"record":{"id":"990b7f83b1fdcd14","repo":"angular/angular-cli","slug":"file-already-exist","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/memory.ts","lineNumber":214,"sourceCode":"    path = this._toAbsolute(path);\n    if (this._isDirectory(path)) {\n      for (const [cachePath] of this._cache.entries()) {\n        if (cachePath.startsWith(path + NormalizedSep) || cachePath === path) {\n          this._cache.delete(cachePath);\n        }\n      }\n    } else {\n      this._cache.delete(path);\n    }\n    this._updateWatchers(path, HostWatchEventType.Deleted);\n  }\n  protected _rename(from: Path, to: Path): void {\n    from = this._toAbsolute(from);\n    to = this._toAbsolute(to);\n    if (!this._cache.has(from)) {\n      throw new FileDoesNotExistException(from);\n    } else if (this._cache.has(to)) {\n      throw new FileAlreadyExistException(to);\n    }\n\n    if (this._isDirectory(from)) {\n      for (const path of this._cache.keys()) {\n        if (path.startsWith(from + NormalizedSep)) {\n          const content = this._cache.get(path);\n          if (content) {\n            // We don't need to clone or extract the content, since we're moving files.\n            this._cache.set(join(to, NormalizedSep, path.slice(from.length)), content);\n          }\n        }\n      }\n    } else {\n      const content = this._cache.get(from);\n      if (content) {\n        const fragments = split(to);\n        const newDirectories: Path[] = [];\n        let curr: Path = normalize('/');","sourceCodeStart":196,"sourceCodeEnd":232,"githubUrl":"https://github.com/angular/angular-cli/blob/bb72145f9ab45aee29f523236b3a25cd0813a841/packages/angular_devkit/core/src/virtual-fs/host/memory.ts#L196-L232","documentation":"After confirming the source exists, _rename checks the destination: if `this._cache.has(to)` is true it throws FileAlreadyExistException. The memory host never silently overwrites an existing entry on rename, forcing callers to resolve collisions explicitly.","triggerScenarios":"Calling host.rename(from, to) where a file or directory already occupies `to` (e.g. from a previous write or rename); rerunning an idempotent operation without cleanup so the destination was created in an earlier run of the same host.","commonSituations":"Upgrade/migration schematics renaming 'a.ts' to 'b.ts' where 'b.ts' already exists; collision when two generated files map to the same target name; repeated execution of a one-shot rename within a long-lived builder process.","solutions":["Delete the existing destination first (host.delete(to)) if overwriting is intended, then rename.","Rename the destination to a backup path before the move, or pick a unique destination name (suffix/timestamp).","Check host.exists(to) beforehand and branch: skip, overwrite, or generate an alternate name.","Make the operation idempotent by tracking completed renames so reruns don't collide."],"exampleFix":"// before\nhost.rename(from, to); // throws if to exists\n// after\nif (host.exists(to)) {\n  host.delete(to); // or rename to a backup first\n}\nhost.rename(from, to);","handlingStrategy":"validation","validationCode":"import { normalize, Path } from '@angular-devkit/core';\n\nfunction renameSafe(host: { exists(p: Path): boolean; delete(p: Path): void; rename(f: Path, t: Path): void }, from: Path, to: Path): void {\n  const dest = normalize(to);\n  if (host.exists(dest)) {\n    host.delete(dest); // or back it up first\n  }\n  host.rename(normalize(from), dest);\n}","typeGuard":null,"tryCatchPattern":"import { FileAlreadyExistException } from '@angular-devkit/core';\n\ntry {\n  host.rename(from, to);\n} catch (e) {\n  if (e instanceof FileAlreadyExistException) {\n    host.delete(e.path);\n    host.rename(from, to);\n  } else {\n    throw e;\n  }\n}","preventionTips":["Check exists() on the destination before renaming and decide overwrite vs. unique-name policy up front.","Back up conflicting destinations instead of hard-deleting when content matters.","Make rename operations idempotent so repeated runs don't collide with themselves."],"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-30T08:17:16.595Z"}