internal bool TryResolveFilePathFromSolution()

in src/Sarif.Viewer.VisualStudio.Core/CodeAnalysisResultManager.cs [776:824]


        internal bool TryResolveFilePathFromSolution(string solutionPath, string pathFromLogFile, IFileSystem fileSystem, out string resolvedPath)
        {
            resolvedPath = null;
            if (string.IsNullOrWhiteSpace(solutionPath))
            {
                return false;
            }

            try
            {
                solutionPath = fileSystem.FileExists(solutionPath) ? Path.GetDirectoryName(solutionPath) : solutionPath;
                if (!fileSystem.DirectoryExists(solutionPath))
                {
                    return false;
                }

                pathFromLogFile = pathFromLogFile.Replace('/', '\\');
                string fileToSearch = Path.GetFileName(pathFromLogFile);
                IEnumerable<string> searchResults = fileSystem.DirectoryEnumerateFiles(solutionPath, fileToSearch, SearchOption.AllDirectories);
                searchResults = searchResults.Where(path => path.EndsWith(pathFromLogFile, StringComparison.OrdinalIgnoreCase));

                // if path like "\AssemblyInfo.cs" it may exists in many projects.
                // Here try to find a unique file matching the path,
                // if more than 1 files match the path, we cannot decide which file to select, need manual intervention
                IEnumerator<string> searchResultsEnumerator = searchResults.GetEnumerator();
                if (searchResultsEnumerator.MoveNext())
                {
                    string currentResolvedPath = searchResultsEnumerator.Current;

                    // If there is another entry, the user must pick one.
                    if (searchResultsEnumerator.MoveNext())
                    {
                        return false;
                    }

                    resolvedPath = currentResolvedPath;
                    return true;
                }
            }
            catch (Exception ex) when (ex is ArgumentException ||
                                       ex is IOException ||
                                       ex is UnauthorizedAccessException)
            {
                // do not throw exception so that it keeps trying to resolve path the next
                Trace.WriteLine($"{nameof(TryResolveFilePathFromSolution)} threw exception: {ex}");
            }

            return false;
        }