LykosAI/StabilityMatrix · error · DirectoryNotFoundException

Directory not found

Error message

Directory not found: {FullPath}

What it means

DirectoryPath.ThrowIfNotExists is an internal guard used by DirectoryPath copy/delete operations. When the directory on disk does not exist it throws DirectoryNotFoundException with the full path. CopyTo/CopyToAsync require an existing source directory to enumerate its contents.

Solutions

  1. Check directoryPath.Exists before calling CopyTo/CopyToAsync
  2. Create the source directory (or correct the stored path) if it should exist
  3. Catch DirectoryNotFoundException and prompt the user to locate the folder
  4. Re-run the operation after the folder is restored

Example fix

// before
sourceDir.CopyTo(destDir);
// after
if (!sourceDir.Exists) { Logger.Warn("Source dir missing: {Path}", sourceDir.FullPath); return; }
sourceDir.CopyTo(destDir);
Defensive patterns

Strategy: validation

Validate before calling

if (!sourceDir.Exists)
    throw new DirectoryNotFoundException($"Source missing: {sourceDir.FullPath}");

Type guard

static bool IsExistingDir(DirectoryPath? d) => d is not null && d.Exists;

Try / catch

try { sourceDir.CopyTo(destDir); }
catch (DirectoryNotFoundException ex) { Logger.Warn("Copy skipped: {Msg}", ex.Message); }

Prevention

When it happens

Trigger: Calling DirectoryPath.CopyTo(destinationDir) or CopyToAsync when the source DirectoryPath does not exist — path never created, wrong casing/drive, or deleted between listing and copy.

Common situations: Copying preset/workspace folders whose location moved; migrating data directories before first launch when the folder is not yet initialized.

Related errors


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

Appendix: source

Thrown at StabilityMatrix.Core/Models/FileInterfaces/DirectoryPath.cs:145

    /// Deletes the directory.
    /// </summary>
    /// <param name="recursive">Whether to delete subdirectories and files.</param>
    public void Delete(bool recursive) => Info.Delete(recursive);

    /// <summary>
    /// Deletes the directory asynchronously.
    /// </summary>
    public Task DeleteAsync(bool recursive) => Task.Run(() => Delete(recursive));

    void IPathObject.Delete() => Info.Delete(true);

    Task IPathObject.DeleteAsync() => DeleteAsync(true);

    private void ThrowIfNotExists()
    {
        if (!Exists)
        {
            throw new DirectoryNotFoundException($"Directory not found: {FullPath}");
        }
    }

    public void CopyTo(DirectoryPath destinationDir, bool recursive = true)
    {
        ThrowIfNotExists();

        // Cache directories before we start copying
        var dirs = EnumerateDirectories().ToList();

        destinationDir.Create();

        // Get the files in the source directory and copy to the destination directory
        foreach (var file in EnumerateFiles())
        {
            var targetFilePath = destinationDir.JoinFile(file.Name);
            file.CopyTo(targetFilePath);
        }

View on GitHub (pinned to af93d6ef57)