From ee13ea0f6b5706e4d73f9c7737b281634f24f32b Mon Sep 17 00:00:00 2001 From: Paolo Tranquilli Date: Wed, 13 May 2026 11:35:02 +0200 Subject: [PATCH] Harden `_relative_path` for Windows and mixed-form inputs Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- python/extractor/semmle/logging.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/python/extractor/semmle/logging.py b/python/extractor/semmle/logging.py index 6f31bff4f39..31805040bf6 100644 --- a/python/extractor/semmle/logging.py +++ b/python/extractor/semmle/logging.py @@ -366,11 +366,16 @@ def _get_source_root(): def _relative_path(path): """Make a path relative to the source root for use in diagnostic locations. If the path is not under the source root, return it unchanged.""" - source_root = _get_source_root() - relpath = os.path.relpath(path, source_root) + source_root = os.path.abspath(_get_source_root()) + abs_path = os.path.abspath(path) + try: + relpath = os.path.relpath(abs_path, source_root) + except ValueError: + # On Windows, relpath raises ValueError for paths on different drives + return path if relpath.startswith(os.pardir): return path - return relpath + return relpath.replace(os.sep, "/") def syntax_error_message(exception, unit): diag_path = _relative_path(unit.path)