Skip to content
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 @@ -12,22 +12,17 @@
namespace Microsoft.CodeAnalysis.Diagnostics;

[ExportWorkspaceServiceFactory(typeof(IBuildOnlyDiagnosticsService), ServiceLayer.Default), Shared]
internal sealed class BuildOnlyDiagnosticsServiceFactory : IWorkspaceServiceFactory
[method: ImportingConstructor]
[method: Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
internal sealed class BuildOnlyDiagnosticsServiceFactory() : IWorkspaceServiceFactory
{
[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public BuildOnlyDiagnosticsServiceFactory()
{
}

public IWorkspaceService CreateService(HostWorkspaceServices workspaceServices)
=> new BuildOnlyDiagnosticsService(workspaceServices.Workspace);

private sealed class BuildOnlyDiagnosticsService : IBuildOnlyDiagnosticsService
{
private readonly object _gate = new();
private readonly Dictionary<DocumentId, ImmutableArray<DiagnosticData>> _documentDiagnostics = [];
private readonly Dictionary<ProjectId, ImmutableArray<DiagnosticData>> _projectDiagnostics = [];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

data in this was never read in any product code. only in a single test. remove it, and updated codepaths affected by it.


public BuildOnlyDiagnosticsService(Workspace workspace)
{
Expand Down Expand Up @@ -61,18 +56,12 @@ private void OnWorkspaceChanged(object? sender, WorkspaceChangeEventArgs e)
}
}

public void AddBuildOnlyDiagnostics(Solution solution, ProjectId? projectId, DocumentId? documentId, ImmutableArray<DiagnosticData> diagnostics)
public void AddBuildOnlyDiagnostics(DocumentId documentId, ImmutableArray<DiagnosticData> diagnostics)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since nothing was reading the project side, we only need to call this for teh document side of things.

{
lock (_gate)
{
if (documentId != null)
{
_documentDiagnostics[documentId] = diagnostics;
}
else if (projectId != null)
{
_projectDiagnostics[projectId] = diagnostics;
}
}
}

Expand All @@ -81,7 +70,6 @@ private void ClearAllDiagnostics()
lock (_gate)
{
_documentDiagnostics.Clear();
_projectDiagnostics.Clear();
}
}

Expand All @@ -103,43 +91,24 @@ private void ClearDiagnostics(Project? project)

lock (_gate)
{
_projectDiagnostics.Remove(project.Id);
foreach (var documentId in project.DocumentIds)
_documentDiagnostics.Remove(documentId);
}
}

public void ClearBuildOnlyDiagnostics(Solution solution, ProjectId? projectId, DocumentId? documentId)
public void ClearBuildOnlyDiagnostics(Project project, DocumentId? documentId)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

passing in a project-id here meant 'clear out project diags, and all diags for any document in this project'. So, maintaining that logic here.

{
if (documentId != null)
ClearDiagnostics(documentId);
else
ClearDiagnostics(solution.GetProject(projectId));
ClearDiagnostics(project);
}

public ImmutableArray<DiagnosticData> GetBuildOnlyDiagnostics(DocumentId documentId)
{
lock (_gate)
{
if (_documentDiagnostics.TryGetValue(documentId, out var diagnostics))
{
return diagnostics;
}

return [];
}
}

public ImmutableArray<DiagnosticData> GetBuildOnlyDiagnostics(ProjectId projectId)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only called in a single test method, no other product code called this.

{
lock (_gate)
{
if (_projectDiagnostics.TryGetValue(projectId, out var diagnostics))
{
return diagnostics;
}

return [];
return _documentDiagnostics.TryGetValue(documentId, out var diagnostics) ? diagnostics : [];
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ namespace Microsoft.CodeAnalysis.Diagnostics;
/// </summary>
internal interface IBuildOnlyDiagnosticsService : IWorkspaceService
{
void AddBuildOnlyDiagnostics(Solution solution, ProjectId? projectId, DocumentId? documentId, ImmutableArray<DiagnosticData> diagnostics);
void AddBuildOnlyDiagnostics(DocumentId documentId, ImmutableArray<DiagnosticData> diagnostics);

void ClearBuildOnlyDiagnostics(Solution solution, ProjectId? projectId, DocumentId? documentId);
void ClearBuildOnlyDiagnostics(Project project, DocumentId? documentId);

ImmutableArray<DiagnosticData> GetBuildOnlyDiagnostics(DocumentId documentId);

ImmutableArray<DiagnosticData> GetBuildOnlyDiagnostics(ProjectId projectId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,8 @@
using Microsoft.CodeAnalysis.SolutionCrawler;
using Roslyn.Utilities;

#pragma warning disable CA1200 // Avoid using cref tags with a prefix

namespace Microsoft.VisualStudio.LanguageServices.Implementation.TaskList;

using ProjectErrorMap = ImmutableDictionary<ProjectId, ImmutableArray<DiagnosticData>>;

/// <summary>
/// Diagnostic source for warnings and errors reported from explicit build command invocations in Visual Studio.
/// VS workspaces calls into us when a build is invoked or completed in Visual Studio.
Expand Down Expand Up @@ -520,13 +516,16 @@ private void ProcessAndRaiseDiagnosticsUpdated(ImmutableArray<DiagnosticsUpdated
{
if (args.Kind == DiagnosticsUpdatedKind.DiagnosticsCreated)
{
RoslynDebug.AssertNotNull(args.Solution);
_buildOnlyDiagnosticsService.AddBuildOnlyDiagnostics(args.Solution, args.ProjectId, args.DocumentId, args.Diagnostics);
Contract.ThrowIfNull(args.Solution);
if (args.DocumentId != null)
_buildOnlyDiagnosticsService.AddBuildOnlyDiagnostics(args.DocumentId, args.Diagnostics);
}
else if (args.Kind == DiagnosticsUpdatedKind.DiagnosticsRemoved)
{
RoslynDebug.AssertNotNull(args.Solution);
_buildOnlyDiagnosticsService.ClearBuildOnlyDiagnostics(args.Solution, args.ProjectId, args.DocumentId);
Contract.ThrowIfNull(args.Solution);
var project = args.Solution.GetProject(args.ProjectId);
if (project != null)
_buildOnlyDiagnosticsService.ClearBuildOnlyDiagnostics(project, args.DocumentId);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ Namespace Microsoft.VisualStudio.LanguageServices.UnitTests.Diagnostics

Dim buildOnlyDiagnosticService = workspace.Services.GetRequiredService(Of IBuildOnlyDiagnosticsService)
Assert.Empty(buildOnlyDiagnosticService.GetBuildOnlyDiagnostics(project.DocumentIds.First()))
Assert.Empty(buildOnlyDiagnosticService.GetBuildOnlyDiagnostics(project.Id))

Dim diagnostics = source.GetBuildErrors()
Assert.Equal(2, diagnostics.Length)
Expand Down