Skip to content

Commit

Permalink
Fix path resolution for relative SARIF artifact locations relative to…
Browse files Browse the repository at this point in the history
… %SRCROOT% (#615)

* Make sure path is normalized

* Updated link comment

---------

Co-authored-by: Chris Huynh <chhuynh@microsoft.com>
  • Loading branch information
chrishuynhc and Chris Huynh authored Apr 3, 2024
1 parent 4818b47 commit 0033272
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/Sarif.Viewer.VisualStudio.Core/CodeAnalysisResultManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,10 @@ private ResolveEmbeddedFileDialogResult PromptForEmbeddedFile(string sarifLogFil
/// <returns>The common suffix of the two file paths.</returns>
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;
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 0033272

Please sign in to comment.