Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SLVS-1652 Add try/catch in SolutionWorkspaceService VsHierarchy enumeration #5849

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public void MefCtor_CheckIsExported()
{
MefTestHelpers.CheckTypeCanBeImported<SolutionWorkspaceService, ISolutionWorkspaceService>(
MefTestHelpers.CreateExport<ISolutionInfoProvider>(),
MefTestHelpers.CreateExport<ILogger>(),
MefTestHelpers.CreateExport<SVsServiceProvider>());
}

Expand All @@ -57,7 +58,7 @@ public void IsSolutionWorkSpace_ShouldBeOppsiteOfFolderWorkSpace(bool isFolderSp

var threadHandler = new NoOpThreadHandler();

var testSubject = new SolutionWorkspaceService(solutionInfoProvider, serviceProvider, threadHandler);
var testSubject = new SolutionWorkspaceService(solutionInfoProvider, new TestLogger(), serviceProvider, threadHandler);

var result = testSubject.IsSolutionWorkSpace();

Expand Down
38 changes: 26 additions & 12 deletions src/Integration/LocalServices/SolutionWorkspaceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,18 @@ namespace SonarLint.VisualStudio.Integration
public class SolutionWorkspaceService : ISolutionWorkspaceService
{
private readonly ISolutionInfoProvider solutionInfoProvider;
private readonly ILogger log;
private readonly IServiceProvider serviceProvider;
private readonly IThreadHandling threadHandling;

[ImportingConstructor]
public SolutionWorkspaceService(ISolutionInfoProvider solutionInfoProvider, [Import(typeof(SVsServiceProvider))] IServiceProvider serviceProvider)
: this(solutionInfoProvider, serviceProvider, ThreadHandling.Instance) { }
public SolutionWorkspaceService(ISolutionInfoProvider solutionInfoProvider, ILogger log, [Import(typeof(SVsServiceProvider))] IServiceProvider serviceProvider)
: this(solutionInfoProvider, log, serviceProvider, ThreadHandling.Instance) { }

internal SolutionWorkspaceService(ISolutionInfoProvider solutionInfoProvider, IServiceProvider serviceProvider, IThreadHandling threadHandling)
internal SolutionWorkspaceService(ISolutionInfoProvider solutionInfoProvider, ILogger log, IServiceProvider serviceProvider, IThreadHandling threadHandling)
{
this.solutionInfoProvider = solutionInfoProvider;
this.log = log;
this.serviceProvider = serviceProvider;
this.threadHandling = threadHandling;
}
Expand Down Expand Up @@ -76,7 +78,7 @@ private IReadOnlyCollection<string> GetAllFilesInSolution(IVsSolution solution)
.Where(x => !x.Contains("\\.nuget\\"))
.Where(x => !x.Contains("\\node_modules\\"))
.ToHashSet(StringComparer.InvariantCultureIgnoreCase)); // move filtering closer to path extraction to avoid processing unnecessary items)

return result;
}

Expand Down Expand Up @@ -149,24 +151,36 @@ private string GetProjectFilePath(IVsProject project)
}

[ExcludeFromCodeCoverage]
private static VSConstants.VSITEMID FirstChild(IVsHierarchy hierarchy, VSConstants.VSITEMID rootID)
private VSConstants.VSITEMID FirstChild(IVsHierarchy hierarchy, VSConstants.VSITEMID rootID)
{
hierarchy.GetProperty((uint)rootID, (int)__VSHPROPID.VSHPROPID_FirstChild, out var childIDObj);
if (childIDObj != null)
try
{
if (hierarchy.GetProperty((uint)rootID, (int)__VSHPROPID.VSHPROPID_FirstChild, out var childIDObj) == VSConstants.S_OK && childIDObj != null)
{
return (VSConstants.VSITEMID)(int)childIDObj;
}
}
catch (Exception e)
{
return (VSConstants.VSITEMID)(int)childIDObj;
log.LogVerbose(e.ToString());
}

return VSConstants.VSITEMID.Nil;
}

[ExcludeFromCodeCoverage]
private static VSConstants.VSITEMID NextSibling(IVsHierarchy hierarchy, VSConstants.VSITEMID firstID)
private VSConstants.VSITEMID NextSibling(IVsHierarchy hierarchy, VSConstants.VSITEMID firstID)
{
hierarchy.GetProperty((uint)firstID, (int)__VSHPROPID.VSHPROPID_NextSibling, out var siblingIDObj);
if (siblingIDObj != null)
try
{
if (hierarchy.GetProperty((uint)firstID, (int)__VSHPROPID.VSHPROPID_NextSibling, out var siblingIDObj) == VSConstants.S_OK && siblingIDObj != null)
{
return (VSConstants.VSITEMID)(int)siblingIDObj;
}
}
catch (Exception e)
{
return (VSConstants.VSITEMID)(int)siblingIDObj;
log.LogVerbose(e.ToString());
}

return VSConstants.VSITEMID.Nil;
Expand Down