{"record":{"id":"616fc47525338db8","repo":"LykosAI/StabilityMatrix","slug":"could-not-find-file-filepath","errorCode":null,"errorMessage":"Could not find file: {filePath}","messagePattern":"Could not find file: (.+?)","errorType":"exception","errorClass":"FileNotFoundException","httpStatus":null,"severity":"error","filePath":"StabilityMatrix.Core/Helper/FileHash.cs","lineNumber":46,"sourceCode":"                totalBytesRead += (ulong)bytesRead;\n                hashAlgorithm.TransformBlock(buffer, 0, bytesRead, null, 0);\n                progress?.Invoke(totalBytesRead);\n            }\n            hashAlgorithm.TransformFinalBlock(buffer, 0, 0);\n            var hash = hashAlgorithm.Hash;\n            if (hash == null || hash.Length == 0)\n            {\n                throw new InvalidOperationException(\"Hash algorithm did not produce a hash.\");\n            }\n            return BitConverter.ToString(hash).Replace(\"-\", string.Empty).ToLowerInvariant();\n        }\n    }\n\n    public static async Task<string> GetSha256Async(string filePath, IProgress<ProgressReport>? progress = default)\n    {\n        if (!File.Exists(filePath))\n        {\n            throw new FileNotFoundException($\"Could not find file: {filePath}\");\n        }\n\n        var totalBytes = Convert.ToUInt64(new FileInfo(filePath).Length);\n        var shared = ArrayPool<byte>.Shared;\n        var buffer = shared.Rent((int)FileTransfers.GetBufferSize(totalBytes));\n        try\n        {\n            await using var stream = File.OpenRead(filePath);\n\n            var hash = await GetHashAsync(\n                    SHA256.Create(),\n                    stream,\n                    buffer,\n                    totalBytesRead =>\n                    {\n                        progress?.Report(new ProgressReport(totalBytesRead, totalBytes, type: ProgressType.Hashing));\n                    }\n                )","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/LykosAI/StabilityMatrix/blob/af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002/StabilityMatrix.Core/Helper/FileHash.cs#L28-L64","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Verify File.Exists on the exact absolute path before hashing","Resolve relative paths against the intended base directory (e.g. the Stability Matrix library dir) rather than cwd","If the file should have been downloaded first, check the download step's success and destination","Check for case-sensitivity/misplaced separators on Linux"],"exampleFix":"// before\nvar hash = await FileHash.GetSha256Async(relativePath);\n// after\nvar fullPath = Path.Combine(SETTINGS.LibraryDir, relativePath);\nif (!File.Exists(fullPath))\n    throw new FileNotFoundException($\"Model file missing, re-download required: {fullPath}\", fullPath);\nvar hash = await FileHash.GetSha256Async(fullPath);","handlingStrategy":"validation","validationCode":"if (!File.Exists(filePath))\n    throw new FileNotFoundException($\"Verify target missing; expected at {Path.GetFullPath(filePath)}\", filePath);","typeGuard":"static bool FileReadyForHash(string path) =>\n    File.Exists(path) && new FileInfo(path).Length > 0;","tryCatchPattern":"try { return await FileHash.GetSha256Async(filePath, progress); }\ncatch (FileNotFoundException ex)\n{\n    logger.LogError(ex, \"Cannot hash missing file {Path}; trigger re-download\", filePath);\n    return null; // or start download then hash\n}","preventionTips":["Always use absolute paths resolved against a known base directory","Verify the download step completed and wrote the file before hashing","Check file existence and non-zero size before hash verification","Handle case-sensitive path differences on Linux"],"tags":["file","hash","sha256","file-not-found"],"backgroundTag":"file-not-found","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"}