{"record":{"id":"06851ba69c368b9b","repo":"LykosAI/StabilityMatrix","slug":"pyvenv-cfg-contains-nul-bytes-expected-utf-8-ascii-path","errorCode":null,"errorMessage":"pyvenv.cfg contains NUL bytes; expected UTF-8/ASCII: {path}","messagePattern":"pyvenv\\.cfg contains NUL bytes; expected UTF-8/ASCII: (.+?)","errorType":"exception","errorClass":"InvalidDataException","httpStatus":null,"severity":"error","filePath":"StabilityMatrix.Core/Python/PyVenvCfg.cs","lineNumber":69,"sourceCode":"    /// </summary>\n    public static PyVenvCfg Load(string path)\n    {\n        var bytes = File.ReadAllBytes(path);\n\n        // pyvenv.cfg is UTF-8/ASCII; reject UTF-16 BOMs and NUL bytes, which\n        // indicate the file was read with the wrong encoding.\n        if (\n            bytes.Length >= 2\n            && ((bytes[0] == 0xFF && bytes[1] == 0xFE) || (bytes[0] == 0xFE && bytes[1] == 0xFF))\n        )\n        {\n            throw new InvalidDataException($\"pyvenv.cfg is UTF-16 encoded; expected UTF-8/ASCII: {path}\");\n        }\n\n        var content = new UTF8Encoding(false).GetString(bytes);\n        if (content.Contains('\\0'))\n        {\n            throw new InvalidDataException($\"pyvenv.cfg contains NUL bytes; expected UTF-8/ASCII: {path}\");\n        }\n\n        return Parse(content);\n    }\n\n    /// <summary>\n    /// Gets the value of the last matching key (CPython is last-wins), or null.\n    /// Setting rewrites every matching key, appending a new key when absent.\n    /// </summary>\n    public string? this[string key]\n    {\n        get\n        {\n            for (var i = _entries.Count - 1; i >= 0; i--)\n            {\n                if (_entries[i].Key is { } k && k.Equals(key, StringComparison.OrdinalIgnoreCase))\n                {\n                    return _entries[i].Value;","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/LykosAI/StabilityMatrix/blob/af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002/StabilityMatrix.Core/Python/PyVenvCfg.cs#L51-L87","documentation":"PyVenvCfg.Load also rejects pyvenv.cfg content containing NUL bytes after UTF-8 decoding. NUL bytes mean the file was written in an incompatible encoding (typically UTF-16 without a BOM, or corruption), so an InvalidDataException naming the path is thrown to avoid silently parsing garbage.","triggerScenarios":"Loading a pyvenv.cfg whose raw bytes decode (as UTF-8) with embedded '\\0' characters — UTF-16-encoded files lacking a BOM, or corrupted/truncated binary writes.","commonSituations":"UTF-16 files written without BOM by custom scripts, disk corruption, tools that wrote the config with wrong encoding settings.","solutions":["Rewrite pyvenv.cfg as UTF-8/ASCII without NUL bytes","Recreate the venv to regenerate a clean config file","Inspect the file bytes to confirm the encoding and convert with an explicit Unicode->UTF-8 conversion","Catch InvalidDataException and rebuild the environment or regenerate the config"],"exampleFix":"// before\n[IO.File]::WriteAllText($path, $content, [Text.Encoding]::Unicode)  # UTF-16, maybe no BOM\n// after\n[IO.File]::WriteAllText($path, $content, new-object Text.UTF8Encoding($false))","handlingStrategy":"validation","validationCode":"var text = new UTF8Encoding(false).GetString(File.ReadAllBytes(cfgPath));\nif (text.Contains('\\0'))\n    throw new InvalidDataException($\"{cfgPath} is not valid UTF-8/ASCII; rewrite as UTF-8\");","typeGuard":null,"tryCatchPattern":"try\n{\n    var cfg = PyVenvCfg.Load(cfgPath);\n}\ncatch (InvalidDataException ex)\n{\n    logger.LogWarning(ex, \"pyvenv.cfg contains NUL bytes; recreating venv config\");\n    RecreateVenvConfig(cfgPath);\n}","preventionTips":["Write config files with explicit UTF-8 encoding, never UTF-16","Validate file bytes after any programmatic write to pyvenv.cfg","Recreate the venv if config corruption is suspected"],"tags":["python","venv","encoding"],"backgroundTag":"file-encoding-mismatch","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"}