{"record":{"id":"ab8c3d75ab983f2e","repo":"microsoft/semantic-kernel","slug":"the-filename-must-have-a-png-extension","errorCode":null,"errorMessage":"The filename must have a .png extension.","messagePattern":"The filename must have a \\.png extension\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"warning","filePath":"dotnet/samples/GettingStartedWithProcesses/Utilities/MermaidRenderer.cs","lineNumber":25,"sourceCode":"\n/// <summary>\n/// Renders Mermaid diagrams to images using Puppeteer-Sharp.\n/// </summary>\npublic static class MermaidRenderer\n{\n    /// <summary>\n    /// Generates a Mermaid diagram image from the provided Mermaid code.\n    /// </summary>\n    /// <param name=\"mermaidCode\"></param>\n    /// <param name=\"filenameOrPath\"></param>\n    /// <returns></returns>\n    /// <exception cref=\"InvalidOperationException\"></exception>\n    public static async Task<string> GenerateMermaidImageAsync(string mermaidCode, string filenameOrPath)\n    {\n        // Ensure the filename has the correct .png extension\n        if (!filenameOrPath.EndsWith(\".png\", StringComparison.OrdinalIgnoreCase))\n        {\n            throw new ArgumentException(\"The filename must have a .png extension.\", nameof(filenameOrPath));\n        }\n\n        string outputFilePath;\n\n        // Check if the user provided an absolute path\n        if (Path.IsPathRooted(filenameOrPath))\n        {\n            // Use the provided absolute path\n            outputFilePath = filenameOrPath;\n\n            // Ensure the directory exists\n            string directoryPath = Path.GetDirectoryName(outputFilePath)\n                ?? throw new InvalidOperationException(\"Could not determine the directory path.\");\n            if (!Directory.Exists(directoryPath))\n            {\n                throw new DirectoryNotFoundException($\"The directory '{directoryPath}' does not exist.\");\n            }\n        }","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/samples/GettingStartedWithProcesses/Utilities/MermaidRenderer.cs#L7-L43","documentation":"Thrown by GenerateMermaidImageAsync when the supplied filenameOrPath does not end with '.png'. The renderer uses Puppeteer-Sharp's screenshot API which writes a PNG, so a non-PNG filename is treated as a programmer error via ArgumentException on the parameter.","triggerScenarios":"Calling MermaidRenderer.GenerateMermaidImageAsync(code, filename) with a filename lacking the .png extension (e.g. 'diagram.jpg', 'out', 'diagram').","commonSituations":"Caller forgets the extension, builds the filename from user input, or passes a different image format expected to be supported.","solutions":["Append '.png' to the filename before calling.","Normalize user-supplied filenames: if (!path.EndsWith(\".png\")) path += \".png\".","Document that only PNG output is supported."],"exampleFix":"// before\nawait MermaidRenderer.GenerateMermaidImageAsync(code, \"diagram\");\n// after\nstring file = filename.EndsWith(\".png\", StringComparison.OrdinalIgnoreCase) ? filename : filename + \".png\";\nawait MermaidRenderer.GenerateMermaidImageAsync(code, file);","handlingStrategy":"validation","validationCode":"if (!filename.EndsWith(\".png\", StringComparison.OrdinalIgnoreCase))\n    filename += \".png\";\nawait MermaidRenderer.GenerateMermaidImageAsync(code, filename);","typeGuard":"bool IsValidPngPath(string p) => p.EndsWith(\".png\", StringComparison.OrdinalIgnoreCase);","tryCatchPattern":"try { await MermaidRenderer.GenerateMermaidImageAsync(code, EnsurePng(filename)); }\ncatch (ArgumentException ex) when (ex.ParamName == nameof(filename)) { /* append .png and retry */ }","preventionTips":["Normalize filenames through a helper that enforces the .png extension.","Reject non-image extensions at the UI/input boundary before calling the renderer.","Document that only PNG output is supported."],"tags":["mermaid","validation","filesystem","argument"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}