diff --git a/src/serena/project.py b/src/serena/project.py index 146c5354..bd444b03 100644 --- a/src/serena/project.py +++ b/src/serena/project.py @@ -139,8 +139,7 @@ class Project: :return: the content of the file """ abs_path = Path(self.project_root) / relative_path - ls_logger = LanguageServerLogger(log_level=log.level) - return FileUtils.read_file(ls_logger, str(abs_path), self.project_config.encoding) + return FileUtils.read_file(str(abs_path), self.project_config.encoding) def get_ignore_spec(self) -> pathspec.PathSpec: """ diff --git a/src/solidlsp/ls.py b/src/solidlsp/ls.py index d4a9c111..849b1256 100644 --- a/src/solidlsp/ls.py +++ b/src/solidlsp/ls.py @@ -392,7 +392,7 @@ class SolidLanguageServer(ABC): yield self.open_file_buffers[uri] self.open_file_buffers[uri].ref_count -= 1 else: - contents = FileUtils.read_file(self.logger, absolute_file_path, self._encoding) + contents = FileUtils.read_file(absolute_file_path, self._encoding) version = 0 self.open_file_buffers[uri] = LSPFileBuffer(uri, contents, version, self.language_id, 1) @@ -1409,7 +1409,7 @@ class SolidLanguageServer(ABC): # checking if the line is empty, unfortunately ugly and duplicating code, but I don't want to refactor with self.open_file(relative_file_path): absolute_file_path = str(PurePath(self.repository_root_path, relative_file_path)) - content = FileUtils.read_file(self.logger, absolute_file_path, self._encoding) + content = FileUtils.read_file(absolute_file_path, self._encoding) if content.split("\n")[line].strip() == "": self.logger.log( f"Passing empty lines to request_container_symbol is currently not supported, {relative_file_path=}, {line=}", diff --git a/src/solidlsp/ls_utils.py b/src/solidlsp/ls_utils.py index 12fd0ddb..0cec5994 100644 --- a/src/solidlsp/ls_utils.py +++ b/src/solidlsp/ls_utils.py @@ -20,6 +20,8 @@ from solidlsp.ls_exceptions import SolidLSPException from solidlsp.ls_logger import LanguageServerLogger from solidlsp.ls_types import UnifiedSymbolInformation +log = logging.getLogger(__name__) + class InvalidTextLocationError(Exception): pass @@ -166,13 +168,16 @@ class FileUtils: """ @staticmethod - def read_file(logger: LanguageServerLogger, file_path: str, encoding: str) -> str: + def read_file(file_path: str, encoding: str) -> str: """ - Reads the file at the given path and returns the contents as a string. + Reads the file at the given path using the given encoding and returns the contents as a string. + If decoding fails, tries to detect the encoding using charset_normalizer. + + Raises FileNotFoundError if the file does not exist. """ if not os.path.exists(file_path): - 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.") + log.error(f"Failed to read '{file_path}': File does not exist.") + raise FileNotFoundError(f"File read '{file_path}' failed: File does not exist.") try: try: with open(file_path, encoding=encoding) as inp_file: @@ -181,15 +186,14 @@ class FileUtils: results = charset_normalizer.from_path(file_path) match = results.best() if match: - logger.log( + log.warning( 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 + log.error(f"Failed to read '{file_path}' with encoding '{encoding}': {exc}") + raise exc @staticmethod def download_file(logger: LanguageServerLogger, url: str, target_path: str) -> None: