diff --git a/src/multilspy/multilspy_utils.py b/src/multilspy/multilspy_utils.py index f7365b5f..7c46d37d 100644 --- a/src/multilspy/multilspy_utils.py +++ b/src/multilspy/multilspy_utils.py @@ -117,7 +117,10 @@ class FileUtils: """ Reads the file at the given path and returns the contents as a string. """ - encodings = ["utf-8-sig", "utf-16"] + if not os.path.exists(file_path): + logger.log(f"File read '{file_path}' failed: File does not exist.", logging.ERROR) + raise MultilspyException(f"File read '{file_path}' failed: File does not exist.") + encodings = ["utf-8-sig", "utf-16", "utf-8", "latin-1"] try: for encoding in encodings: try: @@ -125,6 +128,9 @@ class FileUtils: return inp_file.read() except UnicodeError: continue + # Try system default encoding as a last resort + with open(file_path, "r") as inp_file: + return inp_file.read() except Exception as exc: logger.log(f"File read '{file_path}' failed: {exc}", logging.ERROR) raise MultilspyException("File read failed.") from None