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 https://github.com/aspnet/AspNetCore/issues/8996
This commit is contained in:
parent
04f1f1f389
commit
fb037bda01
|
|
@ -209,19 +209,23 @@ namespace Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation
|
|||
|
||||
var parseOptions = new CSharpParseOptions(preprocessorSymbols: defines);
|
||||
|
||||
if (!string.IsNullOrEmpty(dependencyContextOptions.LanguageVersion))
|
||||
if (string.IsNullOrEmpty(dependencyContextOptions.LanguageVersion))
|
||||
{
|
||||
if (LanguageVersionFacts.TryParse(dependencyContextOptions.LanguageVersion, out var languageVersion))
|
||||
{
|
||||
parseOptions = parseOptions.WithLanguageVersion(languageVersion);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Fail($"LanguageVersion {languageVersion} specified in the deps file could not be parsed.");
|
||||
}
|
||||
// During preview releases, Roslyn assumes Preview language version for netcoreapp3.0 targeting projects.
|
||||
// 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).
|
||||
// Prior to 3.0 RTM, this value needs to be changed to "Latest". This is tracked via https://github.com/aspnet/AspNetCore/issues/9129
|
||||
parseOptions = parseOptions.WithLanguageVersion(LanguageVersion.Preview);
|
||||
}
|
||||
else if (LanguageVersionFacts.TryParse(dependencyContextOptions.LanguageVersion, out var languageVersion))
|
||||
{
|
||||
parseOptions = parseOptions.WithLanguageVersion(languageVersion);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Fail($"LanguageVersion {languageVersion} specified in the deps file could not be parsed.");
|
||||
}
|
||||
|
||||
return parseOptions;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -83,6 +83,7 @@ namespace Microsoft.Extensions.DependencyInjection
|
|||
services.TryAddSingleton(s =>
|
||||
{
|
||||
var fileSystem = s.GetRequiredService<RazorProjectFileSystem>();
|
||||
var csharpCompiler = s.GetRequiredService<CSharpCompiler>();
|
||||
var projectEngine = RazorProjectEngine.Create(RazorConfiguration.Default, fileSystem, builder =>
|
||||
{
|
||||
RazorExtensions.Register(builder);
|
||||
|
|
@ -95,6 +96,7 @@ namespace Microsoft.Extensions.DependencyInjection
|
|||
// TagHelperDescriptorProviders (actually do tag helper discovery)
|
||||
builder.Features.Add(new DefaultTagHelperDescriptorProvider());
|
||||
builder.Features.Add(new ViewComponentTagHelperDescriptorProvider());
|
||||
builder.SetCSharpLanguageVersion(csharpCompiler.ParseOptions.LanguageVersion);
|
||||
});
|
||||
|
||||
return projectEngine;
|
||||
|
|
|
|||
|
|
@ -2,5 +2,8 @@
|
|||
<PropertyGroup>
|
||||
<PreserveCompilationContext>true</PreserveCompilationContext>
|
||||
<PreserveCompilationReferences>true</PreserveCompilationReferences>
|
||||
|
||||
<!-- Generate hosting attributes during build time compilation to support runtime compilation -->
|
||||
<GenerateRazorHostingAssemblyInfo Condition="'$(GenerateRazorHostingAssemblyInfo)'==''">true</GenerateRazorHostingAssemblyInfo>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
|
|
|
|||
|
|
@ -124,7 +124,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation
|
|||
|
||||
// Act & Assert
|
||||
var parseOptions = compiler.ParseOptions;
|
||||
Assert.Equal(LanguageVersion.CSharp7_3, parseOptions.LanguageVersion);
|
||||
Assert.Equal(LanguageVersion.CSharp8, parseOptions.LanguageVersion);
|
||||
Assert.Equal(new[] { "DEBUG" }, parseOptions.PreprocessorSymbolNames);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ namespace Templates.Test
|
|||
|
||||
[Theory]
|
||||
[InlineData(null)]
|
||||
[InlineData("F#", Skip = "https://github.com/aspnet/AspNetCore/issues/8996")]
|
||||
[InlineData("F#")]
|
||||
public async Task MvcTemplate_NoAuthImplAsync(string languageOverride)
|
||||
{
|
||||
Project = await ProjectFactory.GetOrCreateProject("mvcnoauth" + (languageOverride == "F#" ? "fsharp" : "csharp"), Output);
|
||||
|
|
|
|||
Loading…
Reference in New Issue