LykosAI/StabilityMatrix · error · IOException
Directory already exists and overwrite parameter is false.
Error message
Directory already exists and overwrite parameter is false.
What it means
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.
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.
Example fix
// before
Junction.Create(link, target, overwrite: false); // throws on rerun
// after: idempotent creation
if (!Junction.Exists(link)) {
Junction.Create(link, target, overwrite: true);
} Defensive patterns
Strategy: validation
Validate before calling
if (Directory.Exists(junctionPoint)) {
// already there: verify it points at the right target or skip
return; // idempotent path
}
Junction.Create(junctionPoint, targetDir, overwrite: true); Try / catch
try { Junction.Create(link, target, overwrite: false); }
catch (IOException ex) when (ex.Message.Contains("already exists")) { /* skip or recreate */ } Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: "already exists" / EEXIST / FileAlreadyExistsException: what the 'file already exists' error means and how to fix it — this error's family across 37 libraries.
Related errors
- Failed to delete junction point
- Target path does not exist or is not a directory
- Path is not a junction point.
- Unable to delete junction point.
- Failed to delete junction point
AI-assisted analysis of LykosAI/StabilityMatrix@af93d6ef57 (2026-09-12).
Data as JSON: /api/errors/7c2c18d63b77f800.
Report an issue: GitHub.
Appendix: source
Thrown at StabilityMatrix.Core/ReparsePoints/Junction.cs:55
/// </summary>
/// <param name="junctionPoint">The junction point path</param>
/// <param name="targetDir">The target directory (Must already exist)</param>
/// <param name="overwrite">If true overwrites an existing reparse point or empty directory</param>
/// <exception cref="IOException">Thrown when the junction point could not be created or when
/// an existing directory was found and <paramref name="overwrite" /> if false</exception>
public static void Create(string junctionPoint, string targetDir, bool overwrite)
{
targetDir = Path.GetFullPath(targetDir);
if (!Directory.Exists(targetDir))
{
throw new IOException("Target path does not exist or is not a directory");
}
if (Directory.Exists(junctionPoint))
{
if (!overwrite)
throw new IOException("Directory already exists and overwrite parameter is false.");
}
else
{
Directory.CreateDirectory(junctionPoint);
}
using var fileHandle = OpenReparsePoint(junctionPoint, Win32FileAccess.GenericWrite);
var targetDirBytes = Encoding.Unicode.GetBytes(
NonInterpretedPathPrefix + Path.GetFullPath(targetDir));
var reparseDataBuffer = new ReparseDataBuffer
{
ReparseTag = (uint) DeviceIoControlCode.ReparseTagMountPoint,
ReparseDataLength = Convert.ToUInt16(targetDirBytes.Length + 12),
SubstituteNameOffset = 0,
SubstituteNameLength = Convert.ToUInt16(targetDirBytes.Length),
PrintNameOffset = Convert.ToUInt16(targetDirBytes.Length + 2),
PrintNameLength = 0,View on GitHub (pinned to af93d6ef57)