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
46 changes: 0 additions & 46 deletions src/EditorFeatures/Test/Diagnostics/AnalyzerLoadFailureTests.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static ImmutableArray<DiagnosticData> ToDiagnosticData(this ImmutableArra
foreach (var diagnostic in projectDiagnostics)
{
var document = solution.GetDocument(diagnostic.Location.SourceTree);
var data = (document != null) ? DiagnosticData.Create(diagnostic, document) : DiagnosticData.Create(solution, diagnostic, project);
var data = document != null ? DiagnosticData.Create(diagnostic, document) : DiagnosticData.Create(diagnostic, project);
result.Add(data);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Diagnostics;
Expand Down Expand Up @@ -88,25 +89,26 @@ await client.TryInvokeAsync<IRemoteEditAndContinueService>(
return new EmitSolutionUpdateResults.Data()
{
ModuleUpdates = new ModuleUpdates(ModuleUpdateStatus.RestartRequired, []),
Diagnostics = GetInternalErrorDiagnosticData(solution, e),
Diagnostics = GetInternalErrorDiagnosticData(e.Message),
RudeEdits = [],
SyntaxError = null,
ProjectsToRebuild = [],
ProjectsToRestart = [],
};
}
}

private static ImmutableArray<DiagnosticData> GetInternalErrorDiagnosticData(Solution solution, Exception e)
{
var descriptor = EditAndContinueDiagnosticDescriptors.GetDescriptor(RudeEditKind.InternalError);
ImmutableArray<DiagnosticData> GetInternalErrorDiagnosticData(string message)
{
var descriptor = EditAndContinueDiagnosticDescriptors.GetDescriptor(RudeEditKind.InternalError);

var diagnostic = Diagnostic.Create(
descriptor,
Location.None,
string.Format(descriptor.MessageFormat.ToString(), "", e.Message));
var firstProject = solution.GetProject(runningProjects.FirstOrDefault()) ?? solution.Projects.First();
var diagnostic = Diagnostic.Create(
descriptor,
Location.None,
string.Format(descriptor.MessageFormat.ToString(), "", message));

return [DiagnosticData.Create(solution, diagnostic, project: null)];
return [DiagnosticData.Create(diagnostic, firstProject)];
}
}

public async ValueTask CommitSolutionUpdateAsync(CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public async ValueTask<ImmutableArray<DiagnosticData>> GetDocumentDiagnosticsAsy
if (client == null)
{
var diagnostics = await GetLocalService().GetDocumentDiagnosticsAsync(document, activeStatementSpanProvider, cancellationToken).ConfigureAwait(false);
return diagnostics.SelectAsArray(diagnostic => DiagnosticData.Create(document.Project.Solution, diagnostic, document.Project));
return diagnostics.SelectAsArray(diagnostic => DiagnosticData.Create(diagnostic, document));
}

var diagnosticData = await client.TryInvokeAsync<IRemoteEditAndContinueService, ImmutableArray<DiagnosticData>>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ internal static IEnumerable<string> InspectDiagnostics(ImmutableArray<Diagnostic
=> actual.Select(InspectDiagnostic);

internal static string InspectDiagnostic(DiagnosticData diagnostic)
=> $"{(string.IsNullOrWhiteSpace(diagnostic.DataLocation.MappedFileSpan.Path) ? diagnostic.ProjectId!.ToString() : diagnostic.DataLocation.MappedFileSpan.ToString())}: {diagnostic.Severity} {diagnostic.Id}: {diagnostic.Message}";
=> $"{(string.IsNullOrWhiteSpace(diagnostic.DataLocation.MappedFileSpan.Path) ? diagnostic.ProjectId.ToString() : diagnostic.DataLocation.MappedFileSpan.ToString())}: {diagnostic.Severity} {diagnostic.Id}: {diagnostic.Message}";

internal static Guid ReadModuleVersionId(Stream stream)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,8 @@ namespace Microsoft.CodeAnalysis.Diagnostics;

internal sealed partial class DocumentAnalysisExecutor
{
// These are the error codes of the compiler warnings.
// Keep the ids the same so that de-duplication against compiler errors
// works in the error list (after a build).
internal const string WRN_AnalyzerCannotBeCreatedIdCS = "CS8032";
internal const string WRN_AnalyzerCannotBeCreatedIdVB = "BC42376";
internal const string WRN_NoAnalyzerInAssemblyIdCS = "CS8033";
internal const string WRN_NoAnalyzerInAssemblyIdVB = "BC42377";
internal const string WRN_UnableToLoadAnalyzerIdCS = "CS8034";
internal const string WRN_UnableToLoadAnalyzerIdVB = "BC42378";
internal const string WRN_AnalyzerReferencesNetFrameworkIdCS = "CS8850";
internal const string WRN_AnalyzerReferencesNetFrameworkIdVB = "BC42503";
internal const string WRN_AnalyzerReferencesNewerCompilerIdCS = "CS9057";
internal const string WRN_AnalyzerReferencesNewerCompilerIdVB = "BC42506";

// Shared with Compiler
internal const string AnalyzerExceptionDiagnosticId = "AD0001";
internal const string AnalyzerDriverExceptionDiagnosticId = "AD0002";

// IDE only errors
internal const string WRN_AnalyzerCannotBeCreatedId = "AD1000";
internal const string WRN_NoAnalyzerInAssemblyId = "AD1001";
internal const string WRN_UnableToLoadAnalyzerId = "AD1002";
internal const string WRN_AnalyzerReferencesNetFrameworkId = "AD1003";
internal const string WRN_AnalyzerReferencesNewerCompilerId = "AD1004";
public const string AnalyzerExceptionDiagnosticId = "AD0001";

private const string AnalyzerExceptionDiagnosticCategory = "Intellisense";

Expand Down Expand Up @@ -68,62 +46,6 @@ internal static Diagnostic CreateAnalyzerExceptionDiagnostic(DiagnosticAnalyzer
return Diagnostic.Create(descriptor, Location.None, analyzerName, e.GetType(), e.Message);
}

public static DiagnosticData CreateAnalyzerLoadFailureDiagnostic(AnalyzerLoadFailureEventArgs e, string fullPath, ProjectId? projectId, string? language)
{
static string GetLanguageSpecificId(string? language, string noLanguageId, string csharpId, string vbId)
=> language == null ? noLanguageId : (language == LanguageNames.CSharp) ? csharpId : vbId;

string id, message;

switch (e.ErrorCode)
{
case AnalyzerLoadFailureEventArgs.FailureErrorCode.UnableToLoadAnalyzer:
id = GetLanguageSpecificId(language, WRN_UnableToLoadAnalyzerId, WRN_UnableToLoadAnalyzerIdCS, WRN_UnableToLoadAnalyzerIdVB);
message = string.Format(FeaturesResources.Unable_to_load_Analyzer_assembly_0_colon_1, fullPath, e.Message);
break;

case AnalyzerLoadFailureEventArgs.FailureErrorCode.UnableToCreateAnalyzer:
id = GetLanguageSpecificId(language, WRN_AnalyzerCannotBeCreatedId, WRN_AnalyzerCannotBeCreatedIdCS, WRN_AnalyzerCannotBeCreatedIdVB);
message = string.Format(FeaturesResources.An_instance_of_analyzer_0_cannot_be_created_from_1_colon_2, e.TypeName, fullPath, e.Message);
break;

case AnalyzerLoadFailureEventArgs.FailureErrorCode.NoAnalyzers:
id = GetLanguageSpecificId(language, WRN_NoAnalyzerInAssemblyId, WRN_NoAnalyzerInAssemblyIdCS, WRN_NoAnalyzerInAssemblyIdVB);
message = string.Format(FeaturesResources.The_assembly_0_does_not_contain_any_analyzers, fullPath);
break;

case AnalyzerLoadFailureEventArgs.FailureErrorCode.ReferencesFramework:
id = GetLanguageSpecificId(language, WRN_AnalyzerReferencesNetFrameworkId, WRN_AnalyzerReferencesNetFrameworkIdCS, WRN_AnalyzerReferencesNetFrameworkIdVB);
message = string.Format(FeaturesResources.The_assembly_0_containing_type_1_references_NET_Framework, fullPath, e.TypeName);
break;

case AnalyzerLoadFailureEventArgs.FailureErrorCode.ReferencesNewerCompiler:
id = GetLanguageSpecificId(language, WRN_AnalyzerReferencesNewerCompilerId, WRN_AnalyzerReferencesNewerCompilerIdCS, WRN_AnalyzerReferencesNewerCompilerIdVB);
message = string.Format(FeaturesResources.The_assembly_0_references_compiler_version_1_newer_than_2, fullPath, e.ReferencedCompilerVersion, typeof(AnalyzerLoadFailureEventArgs).Assembly.GetName().Version);
break;

default:
throw ExceptionUtilities.UnexpectedValue(e.ErrorCode);
}

var description = e.Exception.CreateDiagnosticDescription();

return new DiagnosticData(
id,
FeaturesResources.Roslyn_HostError,
message,
severity: DiagnosticSeverity.Warning,
defaultSeverity: DiagnosticSeverity.Warning,
isEnabledByDefault: true,
warningLevel: 0,
customTags: [],
properties: ImmutableDictionary<string, string?>.Empty,
projectId: projectId,
location: new DiagnosticDataLocation(new FileLinePositionSpan(fullPath, span: default)),
description: description,
language: language);
}

/// <summary>
/// Return true if the given <paramref name="analyzer"/> is not suppressed for the given project.
/// NOTE: This API is intended to be used only for performance optimization.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ group data by data.DocumentId into documentData
// diagnostics not associated with a document:
sources.AddRange(
from data in applyDiagnostics
where data.DocumentId == null && data.ProjectId != null
where data.DocumentId == null
group data by data.ProjectId into projectData
let project = solution.GetProject(projectData.Key)
where project != null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ private static void SerializeDiagnosticData(DiagnosticData diagnosticData, Objec
writer.WriteString(diagnosticData.Properties[key]);
}

if (diagnosticData.ProjectId != null)
writer.WriteGuid(diagnosticData.ProjectId.Id);
writer.WriteGuid(diagnosticData.ProjectId.Id);

WriteDiagnosticDataLocation(diagnosticData.DataLocation, writer);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1253,9 +1253,9 @@ public async Task EditAndContinue(bool useVSDiagnostics, bool mutatingLspWorkspa
await OpenDocumentAsync(testLspServer, openDocument);

var projectDiagnostic = CreateDiagnostic("ENC_PROJECT", project: project);
var openDocumentDiagnostic1 = CreateDiagnostic("ENC_OPEN_DOC1", openDocument);
var openDocumentDiagnostic2 = await CreateDiagnostic("ENC_OPEN_DOC2", openDocument).ToDiagnosticAsync(project, CancellationToken.None);
var closedDocumentDiagnostic = CreateDiagnostic("ENC_CLOSED_DOC", closedDocument);
var openDocumentDiagnostic1 = CreateDocumentDiagnostic("ENC_OPEN_DOC1", openDocument);
var openDocumentDiagnostic2 = await CreateDocumentDiagnostic("ENC_OPEN_DOC2", openDocument).ToDiagnosticAsync(project, CancellationToken.None);
var closedDocumentDiagnostic = CreateDocumentDiagnostic("ENC_CLOSED_DOC", closedDocument);

encSessionState.IsSessionActive = true;
encSessionState.ApplyChangesDiagnostics = [projectDiagnostic, openDocumentDiagnostic1, closedDocumentDiagnostic];
Expand Down Expand Up @@ -1314,7 +1314,10 @@ public async Task EditAndContinue(bool useVSDiagnostics, bool mutatingLspWorkspa
testLspServer, useVSDiagnostics, previousResults: CreateDiagnosticParamsFromPreviousReports(workspaceResults2), includeTaskListItems: false, category: PullDiagnosticCategories.EditAndContinue);
AssertEx.Equal([], workspaceResults3.Select(Inspect));

static DiagnosticData CreateDiagnostic(string id, Document? document = null, Project? project = null)
static DiagnosticData CreateDocumentDiagnostic(string id, Document document)
=> CreateDiagnostic(id, document.Project, document);

static DiagnosticData CreateDiagnostic(string id, Project project, Document? document = null)
{
return new(
id,
Expand All @@ -1324,12 +1327,12 @@ static DiagnosticData CreateDiagnostic(string id, Document? document = null, Pro
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
warningLevel: 0,
projectId: project?.Id,
projectId: project.Id,
customTags: [],
properties: ImmutableDictionary<string, string?>.Empty,
location: new DiagnosticDataLocation(new FileLinePositionSpan("file", span: default), document?.Id),
additionalLocations: [],
language: (project ?? document!.Project).Language);
language: project.Language);
}

static string Inspect(TestDiagnosticResult result)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -541,24 +541,12 @@ private async Task<ImmutableDictionary<Document, ImmutableArray<Diagnostic>>> Ge

private async Task<ImmutableDictionary<Project, ImmutableArray<Diagnostic>>> GetProjectDiagnosticsToFixAsync(IEnumerable<DiagnosticData> diagnosticsToFix, Func<Project, bool> shouldFixInProject, bool filterStaleDiagnostics, CancellationToken cancellationToken)
{
var builder = ImmutableDictionary.CreateBuilder<ProjectId, List<DiagnosticData>>();
using var _ = CodeAnalysis.PooledObjects.PooledDictionary<ProjectId, CodeAnalysis.PooledObjects.ArrayBuilder<DiagnosticData>>.GetInstance(out var builder);
foreach (var diagnosticData in diagnosticsToFix.Where(IsProjectDiagnostic))
{
RoslynDebug.AssertNotNull(diagnosticData.ProjectId);

if (!builder.TryGetValue(diagnosticData.ProjectId, out var diagnosticsPerProject))
{
diagnosticsPerProject = [];
builder[diagnosticData.ProjectId] = diagnosticsPerProject;
}

diagnosticsPerProject.Add(diagnosticData);
}
builder.MultiAdd(diagnosticData.ProjectId, diagnosticData);

if (builder.Count == 0)
{
return ImmutableDictionary<Project, ImmutableArray<Diagnostic>>.Empty;
}

var finalBuilder = ImmutableDictionary.CreateBuilder<Project, ImmutableArray<Diagnostic>>();
var latestDiagnosticsToFix = filterStaleDiagnostics ? new HashSet<DiagnosticData>() : null;
Expand Down Expand Up @@ -599,6 +587,6 @@ private async Task<ImmutableDictionary<Project, ImmutableArray<Diagnostic>>> Get
return finalBuilder.ToImmutableDictionary();

// Local functions
static bool IsProjectDiagnostic(DiagnosticData d) => d.DataLocation == null && d.ProjectId != null;
static bool IsProjectDiagnostic(DiagnosticData d) => d.DataLocation == null;
}
}
Loading
Loading