LykosAI/StabilityMatrix · error · FileNotFoundException

Could not find file

Error message

Could not find file: {filePath}

What it means

FileHash.GetSha256Async computes the SHA-256 of a file with progress reporting, but first requires the file to exist. If it doesn't, FileNotFoundException with 'Could not find file: {path}' is thrown instead of hashing.

Solutions

  1. Verify File.Exists on the exact absolute path before hashing
  2. Resolve relative paths against the intended base directory (e.g. the Stability Matrix library dir) rather than cwd
  3. If the file should have been downloaded first, check the download step's success and destination
  4. Check for case-sensitivity/misplaced separators on Linux

Example fix

// before
var hash = await FileHash.GetSha256Async(relativePath);
// after
var fullPath = Path.Combine(SETTINGS.LibraryDir, relativePath);
if (!File.Exists(fullPath))
    throw new FileNotFoundException($"Model file missing, re-download required: {fullPath}", fullPath);
var hash = await FileHash.GetSha256Async(fullPath);
Defensive patterns

Strategy: validation

Validate before calling

if (!File.Exists(filePath))
    throw new FileNotFoundException($"Verify target missing; expected at {Path.GetFullPath(filePath)}", filePath);

Type guard

static bool FileReadyForHash(string path) =>
    File.Exists(path) && new FileInfo(path).Length > 0;

Try / catch

try { return await FileHash.GetSha256Async(filePath, progress); }
catch (FileNotFoundException ex)
{
    logger.LogError(ex, "Cannot hash missing file {Path}; trigger re-download", filePath);
    return null; // or start download then hash
}

Prevention

When it happens

Trigger: Calling GetSha256Async with a relative path resolved from the wrong working directory, a path to a file that was deleted/moved, or a typo'd/never-downloaded file path.

Common situations: Verifying a downloaded model's hash before the download actually completed or landed at the expected path; using a relative path while the app's cwd differs from the library directory; verifying after an optional step skipped the download.

Understand the failure class

Background: "File not found" and ENOENT errors: why libraries can't find a file that should exist — this error's family across 50 libraries.

Related errors


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

Appendix: source

Thrown at StabilityMatrix.Core/Helper/FileHash.cs:46

                totalBytesRead += (ulong)bytesRead;
                hashAlgorithm.TransformBlock(buffer, 0, bytesRead, null, 0);
                progress?.Invoke(totalBytesRead);
            }
            hashAlgorithm.TransformFinalBlock(buffer, 0, 0);
            var hash = hashAlgorithm.Hash;
            if (hash == null || hash.Length == 0)
            {
                throw new InvalidOperationException("Hash algorithm did not produce a hash.");
            }
            return BitConverter.ToString(hash).Replace("-", string.Empty).ToLowerInvariant();
        }
    }

    public static async Task<string> GetSha256Async(string filePath, IProgress<ProgressReport>? progress = default)
    {
        if (!File.Exists(filePath))
        {
            throw new FileNotFoundException($"Could not find file: {filePath}");
        }

        var totalBytes = Convert.ToUInt64(new FileInfo(filePath).Length);
        var shared = ArrayPool<byte>.Shared;
        var buffer = shared.Rent((int)FileTransfers.GetBufferSize(totalBytes));
        try
        {
            await using var stream = File.OpenRead(filePath);

            var hash = await GetHashAsync(
                    SHA256.Create(),
                    stream,
                    buffer,
                    totalBytesRead =>
                    {
                        progress?.Report(new ProgressReport(totalBytesRead, totalBytes, type: ProgressType.Hashing));
                    }
                )

View on GitHub (pinned to af93d6ef57)