diff --git a/src/Sarif.Viewer.VisualStudio.Core/CodeAnalysisResultManager.cs b/src/Sarif.Viewer.VisualStudio.Core/CodeAnalysisResultManager.cs index 950ce00a..a45b150a 100644 --- a/src/Sarif.Viewer.VisualStudio.Core/CodeAnalysisResultManager.cs +++ b/src/Sarif.Viewer.VisualStudio.Core/CodeAnalysisResultManager.cs @@ -826,6 +826,10 @@ private ResolveEmbeddedFileDialogResult PromptForEmbeddedFile(string sarifLogFil /// The common suffix of the two file paths. private static string GetCommonSuffix(string firstPath, string secondPath) { + // Normalize file paths before doing comparison. + firstPath = NormalizeFilePath(firstPath); + secondPath = NormalizeFilePath(secondPath); + string commonSuffix = null; int firstSuffixOffset = firstPath.Length; @@ -1084,9 +1088,15 @@ internal bool TryResolveFilePathFromUriBasePaths(string uriBaseId, string pathFr pathFromLogFile = new Uri(originalBaseUri, pathFromLogFile).LocalPath; } + // Note: _fileSystem.FileExists is a wrapper around File.Exists. + // If File.Exists is passed a relative path, which is possible in this scenario, + // it is interpreted as relative to the current working directory. + // https://learn.microsoft.com/en-us/dotnet/api/system.io.file.exists?view=netframework-4.8#remarks if (this._fileSystem.FileExists(pathFromLogFile)) { - resolvedPath = pathFromLogFile; + // If the path is rooted, return as is. + // Otherwise if relative, make sure to return the fully resolved and normalized path combined with the current directory. + resolvedPath = Path.IsPathRooted(pathFromLogFile) ? pathFromLogFile : NormalizeFilePath(Path.Combine(this._fileSystem.EnvironmentCurrentDirectory, pathFromLogFile)); return true; } } @@ -1197,7 +1207,7 @@ internal bool SaveResolvedPathToUriBaseMapping(string uriBaseId, string original } } - string commonSuffix = GetCommonSuffix(NormalizeFilePath(pathFromLogFile), resolvedPath); + string commonSuffix = GetCommonSuffix(pathFromLogFile, resolvedPath); if (commonSuffix == null) { return false;