{"record":{"id":"7c2c18d63b77f800","repo":"LykosAI/StabilityMatrix","slug":"directory-already-exists-and-overwrite-parameter-is-false","errorCode":null,"errorMessage":"Directory already exists and overwrite parameter is false.","messagePattern":"Directory already exists and overwrite parameter is false\\.","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"StabilityMatrix.Core/ReparsePoints/Junction.cs","lineNumber":55,"sourceCode":"    /// </summary>\n    /// <param name=\"junctionPoint\">The junction point path</param>\n    /// <param name=\"targetDir\">The target directory (Must already exist)</param>\n    /// <param name=\"overwrite\">If true overwrites an existing reparse point or empty directory</param>\n    /// <exception cref=\"IOException\">Thrown when the junction point could not be created or when\n    /// an existing directory was found and <paramref name=\"overwrite\" /> if false</exception>\n    public static void Create(string junctionPoint, string targetDir, bool overwrite)\n    {\n        targetDir = Path.GetFullPath(targetDir);\n\n        if (!Directory.Exists(targetDir))\n        {\n            throw new IOException(\"Target path does not exist or is not a directory\");\n        }\n\n        if (Directory.Exists(junctionPoint))\n        {\n            if (!overwrite)\n                throw new IOException(\"Directory already exists and overwrite parameter is false.\");\n        }\n        else\n        {\n            Directory.CreateDirectory(junctionPoint);\n        }\n\n        using var fileHandle = OpenReparsePoint(junctionPoint, Win32FileAccess.GenericWrite);\n        var targetDirBytes = Encoding.Unicode.GetBytes(\n            NonInterpretedPathPrefix + Path.GetFullPath(targetDir));\n\n        var reparseDataBuffer = new ReparseDataBuffer\n        {\n            ReparseTag = (uint) DeviceIoControlCode.ReparseTagMountPoint,\n            ReparseDataLength = Convert.ToUInt16(targetDirBytes.Length + 12),\n            SubstituteNameOffset = 0,\n            SubstituteNameLength = Convert.ToUInt16(targetDirBytes.Length),\n            PrintNameOffset = Convert.ToUInt16(targetDirBytes.Length + 2),\n            PrintNameLength = 0,","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/LykosAI/StabilityMatrix/blob/af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002/StabilityMatrix.Core/ReparsePoints/Junction.cs#L37-L73","documentation":"Junction.Create throws IOException(\"Directory already exists and overwrite parameter is false.\") when the junctionPoint path already exists as a directory and the caller passed overwrite=false. The library refuses to clobber an existing directory (possibly an existing junction or real folder) unless explicitly told to overwrite.","triggerScenarios":"Calling Junction.Create(junctionPoint, targetDir, overwrite: false) where junctionPoint already exists — e.g. re-running setup on an environment where the junction was created previously, or a leftover real directory occupies the link path.","commonSituations":"Idempotency bugs in install/setup code that re-creates junctions each run; previous failed runs leaving the junction behind; a user-created folder at the same path; migrations re-running over existing links.","solutions":["Pass overwrite: true if replacing the existing junction is intended.","Check whether the existing junction already points at the right target and skip creation if so (Junction.Exists / read the reparse target).","Delete the stale junction (Junction.Delete) before re-creating when it points at the wrong target.","If the path holds a real directory with data, choose a different junctionPoint or move the data first."],"exampleFix":"// before\nJunction.Create(link, target, overwrite: false); // throws on rerun\n// after: idempotent creation\nif (!Junction.Exists(link)) {\n    Junction.Create(link, target, overwrite: true);\n}","handlingStrategy":"validation","validationCode":"if (Directory.Exists(junctionPoint)) {\n    // already there: verify it points at the right target or skip\n    return; // idempotent path\n}\nJunction.Create(junctionPoint, targetDir, overwrite: true);","typeGuard":null,"tryCatchPattern":"try { Junction.Create(link, target, overwrite: false); }\ncatch (IOException ex) when (ex.Message.Contains(\"already exists\")) { /* skip or recreate */ }","preventionTips":["Make junction creation idempotent: check Junction.Exists first.","Use overwrite:true only when you intend to replace the existing link.","Clean up stale junctions (Junction.Delete) in uninstall/cleanup flows."],"tags":["filesystem","junction","windows","already-exists"],"backgroundTag":"file-already-exists","analyzedSha":"af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002","analyzedAt":"2026-09-12T19:02:43.389Z","contentChangedAt":"2026-09-12T19:02:43.389Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}