{"record":{"id":"156c95e64dd658bd","repo":"ungoogled-software/ungoogled-chromium","slug":"unable-to-decode-with-any-encoding-path","errorCode":null,"errorMessage":"Unable to decode with any encoding: {path}","messagePattern":"Unable to decode with any encoding: (.+?)","errorType":"exception","errorClass":"UnicodeDecodeError","httpStatus":null,"severity":"error","filePath":"utils/domain_substitution.py","lineNumber":109,"sourceCode":"    \"\"\"\n    if not os.access(path, os.W_OK):\n        # If the patch cannot be written to, it cannot be opened for updating\n        print(str(path) + \" cannot be opened for writing! Adding write permission...\")\n        path.chmod(path.stat().st_mode | stat.S_IWUSR)\n    with path.open('r+b') as input_file:\n        original_content = input_file.read()\n        if not original_content:\n            return (None, None)\n        content = None\n        encoding = None\n        for encoding in TREE_ENCODINGS:\n            try:\n                content = original_content.decode(encoding)\n                break\n            except UnicodeDecodeError:\n                continue\n        if not content:\n            raise UnicodeDecodeError(f'Unable to decode with any encoding: {path}')\n        file_subs = 0\n        for regex_pair in regex_iter:\n            content, sub_count = regex_pair.pattern.subn(regex_pair.replacement, content)\n            file_subs += sub_count\n        if file_subs > 0:\n            substituted_content = content.encode(encoding)\n            input_file.seek(0)\n            input_file.write(content.encode(encoding))\n            input_file.truncate()\n            return (zlib.crc32(substituted_content), original_content)\n        return (None, None)\n\n\ndef _validate_file_index(index_file, resolved_tree, cache_index_files):\n    \"\"\"\n    Validation of file index and hashes against the source tree.\n        Updates cache_index_files\n","sourceCodeStart":91,"sourceCodeEnd":127,"githubUrl":"https://github.com/ungoogled-software/ungoogled-chromium/blob/f85e84a480e2e17c103de7013de94320f9a2ba39/utils/domain_substitution.py#L91-L127","documentation":"_substitute_path() attempts to decode each file with a list of candidate encodings (e.g. UTF-8, then Latin-1). If decoding fails for every encoding, `content` stays unset/empty and a UnicodeDecodeError is raised with this message naming the file.","triggerScenarios":"apply_substitution() iterating the file index reaches a file whose bytes are not valid in any of the configured encodings — a binary file or a file in an unsupported encoding matched by the regex list.","commonSituations":"Binary resources (images, webp, wasm) ending up in the domain-substitution file list; files saved in UTF-16 or another encoding not in the ENCODING_LIST; corrupted downloads.","solutions":["Remove the offending path from the file list (files list) used by apply_substitution()","Add the file's actual encoding to the encoding list used for decoding","Re-obtain the file — it may be corrupted or truncated","If the file should be binary, exclude it from domain substitution"],"exampleFix":"# before: binary file in substitution list\ndomsub_apply(source_tree, regexes, files_list)\n# after: filter binary files out first\nfiles = [f for f in files_list if not (source_tree / f).suffix in {'.png', '.webp', '.wasm'}]\ndomsub_apply(source_tree, regexes, files)","handlingStrategy":"try-catch","validationCode":"for f in file_list:\n    data = (source_tree / f).read_bytes()\n    if b'\\x00' in data[:1024]:\n        raise ValueError(f'binary file in substitution list: {f}')","typeGuard":null,"tryCatchPattern":"try:\n    apply_substitution(tree, regexes, files)\nexcept UnicodeDecodeError as e:\n    log.error('Undecodable file, exclude it from the substitution list: %s', e)\n    raise","preventionTips":["Exclude binary file types from the substitution file list","Ensure files are saved in an encoding included in the decoding list","Re-download corrupted files instead of substituting them"],"tags":["encoding","unicode","decoding","substitution"],"backgroundTag":"unicode-decode-error","analyzedSha":"f85e84a480e2e17c103de7013de94320f9a2ba39","analyzedAt":"2026-08-29T10:07:17.606Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}