LykosAI/StabilityMatrix · error · IOException

Target path does not exist or is not a directory

Error message

Target path does not exist or is not a directory

What it means

Junction.Create (Windows NTFS junction helper) throws IOException("Target path does not exist or is not a directory") when the junction's target directory, after Path.GetFullPath normalization, does not exist or is a file rather than a directory. A junction must point at an existing directory, so the library validates the target before creating the reparse point.

Solutions

  1. Create the target directory first: Directory.CreateDirectory(targetDir) before Junction.Create.
  2. Verify the target path spelling and that it resolves to the intended absolute directory (print Path.GetFullPath(targetDir)).
  3. Ensure the target is a directory, not a file; move/choose a different target if it's a file.
  4. Check that no concurrent cleanup deleted the target between creation and junctioning.

Example fix

// before
Junction.Create(link, target, overwrite: true);
// after
target = Path.GetFullPath(target);
Directory.CreateDirectory(target); // idempotent
Junction.Create(link, target, overwrite: true);
Defensive patterns

Strategy: validation

Validate before calling

targetDir = Path.GetFullPath(targetDir);
if (!Directory.Exists(targetDir)) {
    Directory.CreateDirectory(targetDir); // or validate and fail early
}

Try / catch

try { Junction.Create(link, target, overwrite: true); }
catch (IOException ex) { Logger.Error($"junction target invalid: {ex.Message}"); throw; }

Prevention

When it happens

Trigger: Calling Junction.Create(junctionPoint, targetDir, overwrite) where targetDir was never created, was deleted before the call, contains a typo, or points at a file.

Common situations: Ordering bugs where the target is created later than the junction; relative paths resolved against an unexpected working directory; case/spacing typos in the target path; target removed by a cleanup step.

Related errors


AI-assisted analysis of LykosAI/StabilityMatrix@af93d6ef57 (2026-09-12). Data as JSON: /api/errors/7def5ed7b4809bd7. Report an issue: GitHub.

Appendix: source

Thrown at StabilityMatrix.Core/ReparsePoints/Junction.cs:49

        [In] IntPtr lpInBuffer, uint nInBufferSize,
        IntPtr lpOutBuffer, uint nOutBufferSize,
        [Out] out uint lpBytesReturned, IntPtr lpOverlapped);
    
    /// <summary>
    /// Creates a junction point from the specified directory to the specified target directory.
    /// </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
        {

View on GitHub (pinned to af93d6ef57)