Skip to content

Commit

Permalink
SLVS-1652 Add try/catch in SolutionWorkspaceService VsHierarchy enume…
Browse files Browse the repository at this point in the history
…ration
  • Loading branch information
georgii-borovinskikh-sonarsource authored and vnaskos-sonar committed Nov 25, 2024
1 parent ffbee65 commit 4b7935f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
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

0 comments on commit 4b7935f

Please sign in to comment.