Enhancing file read robustness (#682)

If a file cannot be read using the project's defined encoding, 
use charset-normalizer to detect the likely encoding
This commit is contained in:
玖亖伍 authored and GitHub committed 2025-10-20 21:36:32 +02:00
1 parent f45c22045a
commit d88e2dd180
2 files changed
+17 -5

No files matched your search

+3 -3
View File
@@ -13,6 +13,7 @@ from serena.util.file_system import GitignoreParser, match_path
from solidlsp import SolidLanguageServer
from solidlsp.ls_config import Language, LanguageServerConfig
from solidlsp.ls_logger import LanguageServerLogger
from solidlsp.ls_utils import FileUtils
from solidlsp.settings import SolidLSPSettings
log = logging.getLogger(__name__)
@@ -138,9 +139,8 @@ class Project:
:return: the content of the file
"""
abs_path = Path(self.project_root) / relative_path
if not abs_path.exists():
raise FileNotFoundError(f"File not found: {abs_path}")
return abs_path.read_text(encoding=self.project_config.encoding)
ls_logger = LanguageServerLogger(log_level=log.level)
return FileUtils.read_file(ls_logger, str(abs_path), self.project_config.encoding)
def get_ignore_spec(self) -> pathspec.PathSpec:
"""
+14 -2
View File
@@ -13,6 +13,7 @@ import zipfile
from enum import Enum
from pathlib import Path, PurePath
import charset_normalizer
import requests
from solidlsp.ls_exceptions import SolidLSPException
@@ -173,8 +174,19 @@ class FileUtils:
logger.log(f"File read '{file_path}' failed: File does not exist.", logging.ERROR)
raise SolidLSPException(f"File read '{file_path}' failed: File does not exist.")
try:
with open(file_path, encoding=encoding) as inp_file:
return inp_file.read()
try:
with open(file_path, encoding=encoding) as inp_file:
return inp_file.read()
except UnicodeDecodeError as ude:
results = charset_normalizer.from_path(file_path)
match = results.best()
if match:
logger.log(
f"Could not decode {file_path} with encoding='{encoding}'; using best match '{match.encoding}' instead",
logging.WARNING,
)
return match.raw.decode(match.encoding)
raise ude
except Exception as exc:
logger.log(f"File read '{file_path}' failed to read with encoding '{encoding}': {exc}", logging.ERROR)
raise SolidLSPException("File read failed.") from None