-
Notifications
You must be signed in to change notification settings - Fork 199
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cohost wireup #11412
Draft
chsienki
wants to merge
5
commits into
dotnet:main
Choose a base branch
from
chsienki:cohost_wireup
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Cohost wireup #11412
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fa72e8a
Disable existing language server and project system if cohosting is on
davidwengier 7b44757
Configure source generator when in cohost mode
chsienki d4f023f
Save razor documents to host outputs via a dedicated output object
chsienki 51b2b81
Extract the documents from the razor compiler instead of generating t…
chsienki 23acc87
Update tests to use generator and skip failing tests
chsienki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
...mpiler/Microsoft.CodeAnalysis.Razor.Compiler/src/SourceGenerators/RazorGeneratorResult.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using Microsoft.AspNetCore.Razor.Language; | ||
|
||
namespace Microsoft.NET.Sdk.Razor.SourceGenerators | ||
{ | ||
internal class RazorGeneratorResult(IReadOnlyList<TagHelperDescriptor> tagHelpers, ImmutableDictionary<string, (string hintName, RazorCodeDocument document)> documents) | ||
{ | ||
public IReadOnlyList<TagHelperDescriptor> TagHelpers => tagHelpers; | ||
|
||
public RazorCodeDocument? GetCodeDocument(string physicalPath) => documents.TryGetValue(physicalPath, out var pair) ? pair.document : null; | ||
|
||
public string? GetHintName(string physicalPath) => documents.TryGetValue(physicalPath, out var pair) ? pair.hintName : null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -3,6 +3,7 @@ | |||||
|
||||||
using System; | ||||||
using System.Collections.Generic; | ||||||
using System.Collections.Immutable; | ||||||
using System.IO; | ||||||
using System.Linq; | ||||||
using Microsoft.AspNetCore.Razor.Language; | ||||||
|
@@ -18,6 +19,8 @@ public partial class RazorSourceGenerator : IIncrementalGenerator | |||||
{ | ||||||
private static RazorSourceGeneratorEventSource Log => RazorSourceGeneratorEventSource.Log; | ||||||
|
||||||
internal static bool UseRazorCohostServer { get; set; } = false; | ||||||
|
||||||
// Testing usage only. | ||||||
private readonly string? _testSuppressUniqueIds; | ||||||
|
||||||
|
@@ -37,7 +40,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context) | |||||
var compilation = context.CompilationProvider; | ||||||
|
||||||
// determine if we should suppress this run and filter out all the additional files and references if so | ||||||
var isGeneratorSuppressed = analyzerConfigOptions.CheckGlobalFlagSet("SuppressRazorSourceGenerator"); | ||||||
var isGeneratorSuppressed = analyzerConfigOptions.CheckGlobalFlagSet("SuppressRazorSourceGenerator").Select((suppress, _) => !UseRazorCohostServer && suppress); | ||||||
var additionalTexts = context.AdditionalTextsProvider.EmptyOrCachedWhen(isGeneratorSuppressed, true); | ||||||
var metadataRefs = context.MetadataReferencesProvider.EmptyOrCachedWhen(isGeneratorSuppressed, true); | ||||||
|
||||||
|
@@ -147,7 +150,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context) | |||||
|
||||||
// When using the generator cache in the compiler it's possible to encounter metadata references that are different instances | ||||||
// but ultimately represent the same underlying assembly. We compare the module version ids to determine if the references are the same | ||||||
if (!compilationA.References.SequenceEqual(compilationB.References, new LambdaComparer<MetadataReference>((old, @new) => | ||||||
if (!compilationA.References.SequenceEqual(compilationB.References, new LambdaComparer<MetadataReference>((old, @new) => | ||||||
{ | ||||||
if (ReferenceEquals(old, @new)) | ||||||
{ | ||||||
|
@@ -290,11 +293,19 @@ public void Initialize(IncrementalGeneratorInitializationContext context) | |||||
}); | ||||||
} | ||||||
|
||||||
var csharpDocuments = processed(designTime: false) | ||||||
var razorDocuments = processed(designTime: false) | ||||||
.Select(static (pair, _) => | ||||||
{ | ||||||
var (filePath, document) = pair; | ||||||
return (filePath, csharpDocument: document.CodeDocument.GetCSharpDocument()); | ||||||
return (hintName: GetIdentifierFromPath(filePath), codeDocument: document.CodeDocument); | ||||||
}) | ||||||
.WithTrackingName("RazorDocuments"); | ||||||
|
||||||
var csharpDocuments = razorDocuments | ||||||
.Select(static (pair, _) => | ||||||
{ | ||||||
var (hintName, document) = pair; | ||||||
return (hintName, csharpDocument: document.GetCSharpDocument()); | ||||||
}) | ||||||
.WithLambdaComparer(static (a, b) => | ||||||
{ | ||||||
|
@@ -315,14 +326,11 @@ public void Initialize(IncrementalGeneratorInitializationContext context) | |||||
|
||||||
context.RegisterImplementationSourceOutput(csharpDocumentsWithSuppressionFlag, static (context, pair) => | ||||||
{ | ||||||
var ((filePath, csharpDocument), isGeneratorSuppressed) = pair; | ||||||
var ((hintName, csharpDocument), isGeneratorSuppressed) = pair; | ||||||
|
||||||
// When the generator is suppressed, we may still have a lot of cached data for perf, but we don't want to actually add any of the files to the output | ||||||
if (!isGeneratorSuppressed) | ||||||
{ | ||||||
// Add a generated suffix so tools, such as coverlet, consider the file to be generated | ||||||
var hintName = GetIdentifierFromPath(filePath) + ".g.cs"; | ||||||
|
||||||
RazorSourceGeneratorEventSource.Log.AddSyntaxTrees(hintName); | ||||||
foreach (var razorDiagnostic in csharpDocument.Diagnostics) | ||||||
{ | ||||||
|
@@ -333,6 +341,21 @@ public void Initialize(IncrementalGeneratorInitializationContext context) | |||||
context.AddSource(hintName, csharpDocument.Text); | ||||||
} | ||||||
}); | ||||||
|
||||||
var hostOutputs = razorDocuments | ||||||
.Collect() | ||||||
.Combine(allTagHelpers) | ||||||
.WithTrackingName("HostOutputs"); | ||||||
|
||||||
#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. | ||||||
context.RegisterHostOutput(hostOutputs, (context, pair) => | ||||||
#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. | ||||||
{ | ||||||
var (documents, tagHelpers) = pair; | ||||||
|
||||||
var documentDictionary = documents.Select(p => new KeyValuePair<string, (string, RazorCodeDocument)>(p.codeDocument.Source.FilePath!, (p.hintName, p.codeDocument))).ToImmutableDictionary(); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
context.AddOutput(nameof(RazorGeneratorResult), new RazorGeneratorResult(tagHelpers, documentDictionary)); | ||||||
}); | ||||||
} | ||||||
} | ||||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.