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 @@ -69,10 +69,7 @@ public async ValueTask<RazorCodeDocument> GetGeneratedOutputAsync(CancellationTo
return _codeDocument;
}

// TODO: What happens if we can't get the document? (https://github.com/dotnet/razor/issues/11522)
var document = await ProjectSnapshot.GetCodeDocumentAsync(this, cancellationToken).ConfigureAwait(false)
?? throw new InvalidOperationException("Could not get the code document");

var document = await ProjectSnapshot.GetRequiredCodeDocumentAsync(this, cancellationToken).ConfigureAwait(false);
return InterlockedOperations.Initialize(ref _codeDocument, document);
}

Expand All @@ -95,9 +92,7 @@ public async ValueTask<SourceGeneratedDocument> GetGeneratedDocumentAsync(Cancel
return _generatedDocument;
}

var generatedDocument = await ProjectSnapshot.GetGeneratedDocumentAsync(this, cancellationToken).ConfigureAwait(false)
?? throw new InvalidOperationException("Could not get the generated document"); // TODO: what happens if we can't get the generated document?

var generatedDocument = await ProjectSnapshot.GetRequiredGeneratedDocumentAsync(this, cancellationToken).ConfigureAwait(false);
return InterlockedOperations.Initialize(ref _generatedDocument, generatedDocument);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
Expand Down Expand Up @@ -61,7 +60,7 @@ public IEnumerable<string> DocumentFilePaths

public async ValueTask<ImmutableArray<TagHelperDescriptor>> GetTagHelpersAsync(CancellationToken cancellationToken)
{
var generatorResult = await GetRazorGeneratorResultAsync(cancellationToken).ConfigureAwait(false);
var generatorResult = await GetRazorGeneratorResultAsync(throwIfNotFound: false, cancellationToken).ConfigureAwait(false);
if (generatorResult is null)
return [];

Expand Down Expand Up @@ -141,30 +140,24 @@ public bool TryGetDocument(string filePath, [NotNullWhen(true)] out IDocumentSna
return false;
}

internal async Task<RazorCodeDocument?> GetCodeDocumentAsync(IDocumentSnapshot documentSnapshot, CancellationToken cancellationToken)
internal async Task<RazorCodeDocument> GetRequiredCodeDocumentAsync(IDocumentSnapshot documentSnapshot, CancellationToken cancellationToken)
{
var generatorResult = await GetRazorGeneratorResultAsync(cancellationToken).ConfigureAwait(false);
if (generatorResult is null)
{
return null;
}
var generatorResult = await GetRazorGeneratorResultAsync(throwIfNotFound: true, cancellationToken).ConfigureAwait(false);

return generatorResult.GetCodeDocument(documentSnapshot.FilePath);
return generatorResult.AssumeNotNull().GetCodeDocument(documentSnapshot.FilePath)
?? throw new InvalidOperationException(SR.FormatGenerator_run_result_did_not_contain_a_code_document(documentSnapshot.FilePath));
}

internal async Task<SourceGeneratedDocument?> GetGeneratedDocumentAsync(IDocumentSnapshot documentSnapshot, CancellationToken cancellationToken)
internal async Task<SourceGeneratedDocument> GetRequiredGeneratedDocumentAsync(IDocumentSnapshot documentSnapshot, CancellationToken cancellationToken)
{
var generatorResult = await GetRazorGeneratorResultAsync(cancellationToken).ConfigureAwait(false);
if (generatorResult is null)
{
return null;
}
var generatorResult = await GetRazorGeneratorResultAsync(throwIfNotFound: true, cancellationToken).ConfigureAwait(false);

var hintName = generatorResult.GetHintName(documentSnapshot.FilePath);
var hintName = generatorResult.AssumeNotNull().GetHintName(documentSnapshot.FilePath);

var generatedDocument = await _project.TryGetSourceGeneratedDocumentFromHintNameAsync(hintName, cancellationToken).ConfigureAwait(false);

return generatedDocument ?? throw new InvalidOperationException("Couldn't get the source generated document for a hint name that we got from the generator?");
return generatedDocument
?? throw new InvalidOperationException(SR.FormatCouldnt_get_the_source_generated_document_for_hint_name(hintName));
}

public async Task<RazorCodeDocument?> TryGetCodeDocumentFromGeneratedDocumentUriAsync(Uri generatedDocumentUri, CancellationToken cancellationToken)
Expand All @@ -179,7 +172,7 @@ public bool TryGetDocument(string filePath, [NotNullWhen(true)] out IDocumentSna

public async Task<RazorCodeDocument?> TryGetCodeDocumentFromGeneratedHintNameAsync(string generatedDocumentHintName, CancellationToken cancellationToken)
{
var runResult = await GetRazorGeneratorResultAsync(cancellationToken).ConfigureAwait(false);
var runResult = await GetRazorGeneratorResultAsync(throwIfNotFound: false, cancellationToken).ConfigureAwait(false);
if (runResult is null)
{
return null;
Expand All @@ -192,7 +185,7 @@ public bool TryGetDocument(string filePath, [NotNullWhen(true)] out IDocumentSna

public async Task<TextDocument?> TryGetRazorDocumentFromGeneratedHintNameAsync(string generatedDocumentHintName, CancellationToken cancellationToken)
{
var runResult = await GetRazorGeneratorResultAsync(cancellationToken).ConfigureAwait(false);
var runResult = await GetRazorGeneratorResultAsync(throwIfNotFound: false, cancellationToken).ConfigureAwait(false);
if (runResult is null)
{
return null;
Expand All @@ -204,31 +197,60 @@ public bool TryGetDocument(string filePath, [NotNullWhen(true)] out IDocumentSna
: null;
}

private async Task<RazorGeneratorResult?> GetRazorGeneratorResultAsync(CancellationToken cancellationToken)
private async Task<RazorGeneratorResult?> GetRazorGeneratorResultAsync(bool throwIfNotFound, CancellationToken cancellationToken)
{
var result = await _project.GetSourceGeneratorRunResultAsync(cancellationToken).ConfigureAwait(false);
if (result is null)
{
if (throwIfNotFound)
{
throw new InvalidOperationException(SR.FormatCouldnt_get_a_source_generator_run_result(_project.Name));
}

return null;
}

var runResult = result.Results.SingleOrDefault(r => r.Generator.GetGeneratorType().Assembly.Location == typeof(RazorSourceGenerator).Assembly.Location);
if (runResult.Generator is null)
{
if (throwIfNotFound)
{
if (result.Results.SingleOrDefault(r => r.Generator.GetGeneratorType().Name == "Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator").Generator is { } wrongGenerator)
{
// Wrong ALC?
throw new InvalidOperationException(SR.FormatRazor_source_generator_reference_incorrect(wrongGenerator.GetGeneratorType().Assembly.Location, typeof(RazorSourceGenerator).Assembly.Location, _project.Name));
}
else
{
throw new InvalidOperationException(SR.FormatRazor_source_generator_is_not_referenced(_project.Name));
}
}

return null;
}

#pragma warning disable RSEXPERIMENTAL004 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
if (!runResult.HostOutputs.TryGetValue(nameof(RazorGeneratorResult), out var objectResult) || objectResult is not RazorGeneratorResult generatorResult)
if (!runResult.HostOutputs.TryGetValue(nameof(RazorGeneratorResult), out var objectResult))
#pragma warning restore RSEXPERIMENTAL004 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
{
Debug.Fail($"""
No RazorGeneratorResult found in host outputs for project '{_project.Name}':
{string.Join(Environment.NewLine, runResult.Diagnostics)}
""");
if (throwIfNotFound)
{
throw new InvalidOperationException(SR.FormatRazor_source_generator_did_not_produce_a_host_output(_project.Name, string.Join(Environment.NewLine, runResult.Diagnostics)));
}

return null;
}

if (objectResult is not RazorGeneratorResult generatorResult)
{
if (throwIfNotFound)
{
// Wrong ALC?
throw new InvalidOperationException(SR.FormatRazor_source_generator_host_output_is_not_RazorGeneratorResult(_project.Name, string.Join(Environment.NewLine, runResult.Diagnostics)));
}

return null;
}
#pragma warning restore RSEXPERIMENTAL004 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.

return generatorResult;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,25 @@
<data name="Project_does_not_belong_to_this_solution" xml:space="preserve">
<value>Project does not belong to this solution.</value>
</data>
<data name="Generator_run_result_did_not_contain_a_code_document" xml:space="preserve">
<value>Generator run result did not contain a code document for '{0}'.</value>
</data>
<data name="Couldnt_get_the_source_generated_document_for_hint_name" xml:space="preserve">
<value>Couldn't get the source generated document for hint name '{0}' that we got from the generator run result.</value>
</data>
<data name="Couldnt_get_a_source_generator_run_result" xml:space="preserve">
<value>Couldn't get a source generator run result for project '{0}'.</value>
</data>
<data name="Razor_source_generator_reference_incorrect" xml:space="preserve">
<value>Razor source generator is referenced from '{0}' but we are expecting '{1}', for project '{2}'.</value>
</data>
<data name="Razor_source_generator_is_not_referenced" xml:space="preserve">
<value>Razor source generator is not referenced or no run result found for project '{0}'.</value>
</data>
<data name="Razor_source_generator_did_not_produce_a_host_output" xml:space="preserve">
<value>Razor source generator did not produce a host output for project '{0}': {1}</value>
</data>
<data name="Razor_source_generator_host_output_is_not_RazorGeneratorResult" xml:space="preserve">
<value>Razor source generator host output is not of type RazorGeneratorResult for project '{0}': {1}</value>
</data>
</root>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading