Skip to content

Commit 4b7935f

Browse files
SLVS-1652 Add try/catch in SolutionWorkspaceService VsHierarchy enumeration
1 parent ffbee65 commit 4b7935f

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

src/Integration.UnitTests/LocalServices/SolutionWorkspaceServiceTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public void MefCtor_CheckIsExported()
3636
{
3737
MefTestHelpers.CheckTypeCanBeImported<SolutionWorkspaceService, ISolutionWorkspaceService>(
3838
MefTestHelpers.CreateExport<ISolutionInfoProvider>(),
39+
MefTestHelpers.CreateExport<ILogger>(),
3940
MefTestHelpers.CreateExport<SVsServiceProvider>());
4041
}
4142

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

5859
var threadHandler = new NoOpThreadHandler();
5960

60-
var testSubject = new SolutionWorkspaceService(solutionInfoProvider, serviceProvider, threadHandler);
61+
var testSubject = new SolutionWorkspaceService(solutionInfoProvider, new TestLogger(), serviceProvider, threadHandler);
6162

6263
var result = testSubject.IsSolutionWorkSpace();
6364

src/Integration/LocalServices/SolutionWorkspaceService.cs

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,18 @@ namespace SonarLint.VisualStudio.Integration
3838
public class SolutionWorkspaceService : ISolutionWorkspaceService
3939
{
4040
private readonly ISolutionInfoProvider solutionInfoProvider;
41+
private readonly ILogger log;
4142
private readonly IServiceProvider serviceProvider;
4243
private readonly IThreadHandling threadHandling;
4344

4445
[ImportingConstructor]
45-
public SolutionWorkspaceService(ISolutionInfoProvider solutionInfoProvider, [Import(typeof(SVsServiceProvider))] IServiceProvider serviceProvider)
46-
: this(solutionInfoProvider, serviceProvider, ThreadHandling.Instance) { }
46+
public SolutionWorkspaceService(ISolutionInfoProvider solutionInfoProvider, ILogger log, [Import(typeof(SVsServiceProvider))] IServiceProvider serviceProvider)
47+
: this(solutionInfoProvider, log, serviceProvider, ThreadHandling.Instance) { }
4748

48-
internal SolutionWorkspaceService(ISolutionInfoProvider solutionInfoProvider, IServiceProvider serviceProvider, IThreadHandling threadHandling)
49+
internal SolutionWorkspaceService(ISolutionInfoProvider solutionInfoProvider, ILogger log, IServiceProvider serviceProvider, IThreadHandling threadHandling)
4950
{
5051
this.solutionInfoProvider = solutionInfoProvider;
52+
this.log = log;
5153
this.serviceProvider = serviceProvider;
5254
this.threadHandling = threadHandling;
5355
}
@@ -76,7 +78,7 @@ private IReadOnlyCollection<string> GetAllFilesInSolution(IVsSolution solution)
7678
.Where(x => !x.Contains("\\.nuget\\"))
7779
.Where(x => !x.Contains("\\node_modules\\"))
7880
.ToHashSet(StringComparer.InvariantCultureIgnoreCase)); // move filtering closer to path extraction to avoid processing unnecessary items)
79-
81+
8082
return result;
8183
}
8284

@@ -149,24 +151,36 @@ private string GetProjectFilePath(IVsProject project)
149151
}
150152

151153
[ExcludeFromCodeCoverage]
152-
private static VSConstants.VSITEMID FirstChild(IVsHierarchy hierarchy, VSConstants.VSITEMID rootID)
154+
private VSConstants.VSITEMID FirstChild(IVsHierarchy hierarchy, VSConstants.VSITEMID rootID)
153155
{
154-
hierarchy.GetProperty((uint)rootID, (int)__VSHPROPID.VSHPROPID_FirstChild, out var childIDObj);
155-
if (childIDObj != null)
156+
try
157+
{
158+
if (hierarchy.GetProperty((uint)rootID, (int)__VSHPROPID.VSHPROPID_FirstChild, out var childIDObj) == VSConstants.S_OK && childIDObj != null)
159+
{
160+
return (VSConstants.VSITEMID)(int)childIDObj;
161+
}
162+
}
163+
catch (Exception e)
156164
{
157-
return (VSConstants.VSITEMID)(int)childIDObj;
165+
log.LogVerbose(e.ToString());
158166
}
159167

160168
return VSConstants.VSITEMID.Nil;
161169
}
162170

163171
[ExcludeFromCodeCoverage]
164-
private static VSConstants.VSITEMID NextSibling(IVsHierarchy hierarchy, VSConstants.VSITEMID firstID)
172+
private VSConstants.VSITEMID NextSibling(IVsHierarchy hierarchy, VSConstants.VSITEMID firstID)
165173
{
166-
hierarchy.GetProperty((uint)firstID, (int)__VSHPROPID.VSHPROPID_NextSibling, out var siblingIDObj);
167-
if (siblingIDObj != null)
174+
try
175+
{
176+
if (hierarchy.GetProperty((uint)firstID, (int)__VSHPROPID.VSHPROPID_NextSibling, out var siblingIDObj) == VSConstants.S_OK && siblingIDObj != null)
177+
{
178+
return (VSConstants.VSITEMID)(int)siblingIDObj;
179+
}
180+
}
181+
catch (Exception e)
168182
{
169-
return (VSConstants.VSITEMID)(int)siblingIDObj;
183+
log.LogVerbose(e.ToString());
170184
}
171185

172186
return VSConstants.VSITEMID.Nil;

0 commit comments

Comments
 (0)