Skip to content

Commit fb037bd

Browse files
authored
Set CSharpLangVersion during runtime compilation (#9135)
* Set CSharpLangVersion during runtime compilation * Pass CSharpLangVersion inferred by the CSharpCompiler to RazorEngine * Set GenerateRazorHostingAssemblyInfo in Razor.RuntimeCompilation targets * Unskip failing test Fixes #8996
1 parent 04f1f1f commit fb037bd

File tree

5 files changed

+21
-12
lines changed

5 files changed

+21
-12
lines changed

src/Mvc/Mvc.Razor.RuntimeCompilation/src/CSharpCompiler.cs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -209,19 +209,23 @@ private static CSharpParseOptions GetParseOptions(
209209

210210
var parseOptions = new CSharpParseOptions(preprocessorSymbols: defines);
211211

212-
if (!string.IsNullOrEmpty(dependencyContextOptions.LanguageVersion))
212+
if (string.IsNullOrEmpty(dependencyContextOptions.LanguageVersion))
213213
{
214-
if (LanguageVersionFacts.TryParse(dependencyContextOptions.LanguageVersion, out var languageVersion))
215-
{
216-
parseOptions = parseOptions.WithLanguageVersion(languageVersion);
217-
}
218-
else
219-
{
220-
Debug.Fail($"LanguageVersion {languageVersion} specified in the deps file could not be parsed.");
221-
}
214+
// During preview releases, Roslyn assumes Preview language version for netcoreapp3.0 targeting projects.
215+
// We will match the behavior if the project does not specify a value for C# language (e.g. if you're using Razor compilation in a F# project).
216+
// Prior to 3.0 RTM, this value needs to be changed to "Latest". This is tracked via https://github.com/aspnet/AspNetCore/issues/9129
217+
parseOptions = parseOptions.WithLanguageVersion(LanguageVersion.Preview);
218+
}
219+
else if (LanguageVersionFacts.TryParse(dependencyContextOptions.LanguageVersion, out var languageVersion))
220+
{
221+
parseOptions = parseOptions.WithLanguageVersion(languageVersion);
222+
}
223+
else
224+
{
225+
Debug.Fail($"LanguageVersion {languageVersion} specified in the deps file could not be parsed.");
222226
}
223227

224228
return parseOptions;
225229
}
226230
}
227-
}
231+
}

src/Mvc/Mvc.Razor.RuntimeCompilation/src/DependencyInjection/RazorRuntimeCompilationMvcCoreBuilderExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ internal static void AddServices(IServiceCollection services)
8383
services.TryAddSingleton(s =>
8484
{
8585
var fileSystem = s.GetRequiredService<RazorProjectFileSystem>();
86+
var csharpCompiler = s.GetRequiredService<CSharpCompiler>();
8687
var projectEngine = RazorProjectEngine.Create(RazorConfiguration.Default, fileSystem, builder =>
8788
{
8889
RazorExtensions.Register(builder);
@@ -95,6 +96,7 @@ internal static void AddServices(IServiceCollection services)
9596
// TagHelperDescriptorProviders (actually do tag helper discovery)
9697
builder.Features.Add(new DefaultTagHelperDescriptorProvider());
9798
builder.Features.Add(new ViewComponentTagHelperDescriptorProvider());
99+
builder.SetCSharpLanguageVersion(csharpCompiler.ParseOptions.LanguageVersion);
98100
});
99101

100102
return projectEngine;

src/Mvc/Mvc.Razor.RuntimeCompilation/src/build/netcoreapp3.0/Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.targets

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@
22
<PropertyGroup>
33
<PreserveCompilationContext>true</PreserveCompilationContext>
44
<PreserveCompilationReferences>true</PreserveCompilationReferences>
5+
6+
<!-- Generate hosting attributes during build time compilation to support runtime compilation -->
7+
<GenerateRazorHostingAssemblyInfo Condition="'$(GenerateRazorHostingAssemblyInfo)'==''">true</GenerateRazorHostingAssemblyInfo>
58
</PropertyGroup>
69
</Project>

src/Mvc/Mvc.Razor.RuntimeCompilation/test/CSharpCompilerTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public void EnsureOptions_ConfiguresDefaultParseOptions()
124124

125125
// Act & Assert
126126
var parseOptions = compiler.ParseOptions;
127-
Assert.Equal(LanguageVersion.CSharp7_3, parseOptions.LanguageVersion);
127+
Assert.Equal(LanguageVersion.CSharp8, parseOptions.LanguageVersion);
128128
Assert.Equal(new[] { "DEBUG" }, parseOptions.PreprocessorSymbolNames);
129129
}
130130

src/ProjectTemplates/test/MvcTemplateTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public MvcTemplateTest(ProjectFactoryFixture projectFactory, ITestOutputHelper o
2626

2727
[Theory]
2828
[InlineData(null)]
29-
[InlineData("F#", Skip = "https://github.com/aspnet/AspNetCore/issues/8996")]
29+
[InlineData("F#")]
3030
public async Task MvcTemplate_NoAuthImplAsync(string languageOverride)
3131
{
3232
Project = await ProjectFactory.GetOrCreateProject("mvcnoauth" + (languageOverride == "F#" ? "fsharp" : "csharp"), Output);

0 commit comments

Comments
 (0)