Skip to content

Commit ee13ea0

Browse files
redsun82Copilot
andcommitted
Harden _relative_path for Windows and mixed-form inputs
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent d287925 commit ee13ea0

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

python/extractor/semmle/logging.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -366,11 +366,16 @@ def _get_source_root():
366366
def _relative_path(path):
367367
"""Make a path relative to the source root for use in diagnostic locations.
368368
If the path is not under the source root, return it unchanged."""
369-
source_root = _get_source_root()
370-
relpath = os.path.relpath(path, source_root)
369+
source_root = os.path.abspath(_get_source_root())
370+
abs_path = os.path.abspath(path)
371+
try:
372+
relpath = os.path.relpath(abs_path, source_root)
373+
except ValueError:
374+
# On Windows, relpath raises ValueError for paths on different drives
375+
return path
371376
if relpath.startswith(os.pardir):
372377
return path
373-
return relpath
378+
return relpath.replace(os.sep, "/")
374379

375380
def syntax_error_message(exception, unit):
376381
diag_path = _relative_path(unit.path)

0 commit comments

Comments
 (0)