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 @@ -20,7 +20,9 @@ public sealed class RazorLanguageVersion : IEquatable<RazorLanguageVersion>, ICo

public static readonly RazorLanguageVersion Version_3_0 = new RazorLanguageVersion(3, 0);

public static readonly RazorLanguageVersion Latest = Version_3_0;
public static readonly RazorLanguageVersion Version_5_0 = new RazorLanguageVersion(5, 0);

public static readonly RazorLanguageVersion Latest = Version_5_0;

public static readonly RazorLanguageVersion Experimental = new RazorLanguageVersion(1337, 1337);

Expand All @@ -41,6 +43,11 @@ public static bool TryParse(string languageVersion, out RazorLanguageVersion ver
version = Experimental;
return true;
}
else if (languageVersion == "5.0")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{
version = Version_5_0;
return true;
}
else if (languageVersion == "3.0")
{
version = Version_3_0;
Expand All @@ -49,7 +56,7 @@ public static bool TryParse(string languageVersion, out RazorLanguageVersion ver
else if (languageVersion == "2.1")
{
version = Version_2_1;
return true;
return true;
}
else if (languageVersion == "2.0")
{
Expand Down Expand Up @@ -84,7 +91,7 @@ public static RazorLanguageVersion Parse(string languageVersion)
}

throw new ArgumentException(
Resources.FormatRazorLanguageVersion_InvalidVersion(languageVersion),
Resources.FormatRazorLanguageVersion_InvalidVersion(languageVersion),
nameof(languageVersion));
}

Expand Down Expand Up @@ -131,7 +138,7 @@ public override int GetHashCode()
// We don't need to do anything special for our hash code since reference equality is what we're going for.
return base.GetHashCode();
}

public override string ToString() => $"{Major}.{Minor}";

private string DebuggerToString() => $"Razor '{Major}.{Minor}'";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,20 @@ public void TryParse30()
Assert.Same(RazorLanguageVersion.Version_3_0, version);
}

[Fact]
public void TryParse50()
{
// Arrange
var value = "5.0";

// Act
var result = RazorLanguageVersion.TryParse(value, out var version);

// Assert
Assert.True(result);
Assert.Same(RazorLanguageVersion.Version_5_0, version);
}

[Fact]
public void TryParseLatest()
{
Expand All @@ -103,7 +117,7 @@ public void TryParseLatest()

// Assert
Assert.True(result);
Assert.Same(RazorLanguageVersion.Version_3_0, version);
Assert.Same(RazorLanguageVersion.Version_5_0, version);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -625,28 +625,26 @@ public async Task Build_WithoutServer_ErrorDuringBuild_DisplaysErrorInMsBuildOut
{
var result = await DotnetMSBuild(
"Build",
"/p:UseRazorBuildServer=false /p:RazorLangVersion=5.0",
"/p:UseRazorBuildServer=false /p:RazorLangVersion=99.0",
suppressBuildServer: true);

Assert.BuildFailed(result);
Assert.BuildOutputContainsLine(
result,
$"Invalid option 5.0 for Razor language version --version; must be Latest or a valid version in range 1.0 to 3.0.");
$"Invalid option 99.0 for Razor language version --version; must be Latest or a valid version in range 1.0 to 5.0.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or a valid version in range 1.0 to 5.0.

This is a confusing statement indicating 4.0 might be valid. Could we rephrase this somehow?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestions? I would note that none of the point values are supported. (e.g. 3.1 is not valid).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it is easy to do, we could just list all the possible supported values. Just like how dotnet lists all the available SDKs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's what the compiler does:

Error CS1617 Invalid option '9.0' for /langversion. Use '/langversion:?' to list supported values

It's not really easy to find this. I'm kinda tempted to leave these be. AFAIK, nobody configures these. Plus the language versions are precariously tied to the runtime version that setting this is always going to get you in trouble.


// Compilation failed without creating the views assembly
Assert.FileExists(result, IntermediateOutputPath, "SimpleMvc.dll");
Assert.FileDoesNotExist(result, IntermediateOutputPath, "SimpleMvc.Views.dll");
}

[Fact(Skip = "Default C# version is 7.3 for netcoreapp3.1 and later https://github.com/dotnet/aspnetcore/issues/13930")]
[Fact]
[InitializeTestProject("SimpleMvc")]
public async Task Build_ImplicitCSharp8_NullableEnforcement_WarningsDuringBuild_NoBuildServer()
{
var result = await DotnetMSBuild(
"Build",
// Remove /p:LangVersion=Default once we've picked up a compiler that supports .NET 5.0.
// Tracked by https://github.com/dotnet/aspnetcore/issues/13304
"/p:Nullable=enable /p:LangVersion=Default",
"/p:Nullable=enable",
suppressBuildServer: true);
var indexFilePath = Path.Combine(RazorIntermediateOutputPath, "Views", "Home", "Index.cshtml.g.cs");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public async Task GetCopyToOutputDirectoryItems_WhenNoFileIsPresent_ReturnsEmpty
}

[Fact]
[InitializeTestProject("SimpleMvc")]
[InitializeTestProject("SimpleMvc31")]
public async Task RazorSdk_ResolvesRazorLangVersionTo30ForNetCoreApp30Projects()
{
var result = await DotnetMSBuild("ResolveRazorConfiguration", "/t:_IntrospectResolvedConfiguration");
Expand All @@ -98,6 +98,17 @@ public async Task RazorSdk_ResolvesRazorLangVersionTo30ForNetCoreApp30Projects()
Assert.BuildOutputContainsLine(result, "ResolvedRazorConfiguration: MVC-3.0");
}

[Fact]
[InitializeTestProject("SimpleMvc")]
public async Task RazorSdk_ResolvesRazorLangVersionTo50ForNetCoreApp50Projects()
{
var result = await DotnetMSBuild("ResolveRazorConfiguration", "/t:_IntrospectResolvedConfiguration");

Assert.BuildPassed(result);
Assert.BuildOutputContainsLine(result, "RazorLangVersion: 5.0");
Assert.BuildOutputContainsLine(result, "ResolvedRazorConfiguration: MVC-3.0");
}

[Fact]
[InitializeTestProject("ComponentLibrary")]
public async Task RazorSdk_ResolvesRazorLangVersionFromValueSpecified()
Expand Down Expand Up @@ -142,7 +153,7 @@ public async Task RazorSdk_ResolvesRazorConfigurationToMvc30()
var result = await DotnetMSBuild("ResolveRazorConfiguration", "/t:_IntrospectResolvedConfiguration");

Assert.BuildPassed(result);
Assert.BuildOutputContainsLine(result, "RazorLangVersion: 3.0");
Assert.BuildOutputContainsLine(result, "RazorLangVersion: 5.0");
Assert.BuildOutputContainsLine(result, "ResolvedRazorConfiguration: MVC-3.0");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,12 @@ public async Task Build_ErrorInServer_DisplaysErrorInMsBuildOutput()
{
var result = await DotnetMSBuild(
"Build",
"/p:_RazorForceBuildServer=true /p:RazorLangVersion=5.0");
"/p:_RazorForceBuildServer=true /p:RazorLangVersion=99.0");

Assert.BuildFailed(result);
Assert.BuildOutputContainsLine(
result,
$"Invalid option 5.0 for Razor language version --version; must be Latest or a valid version in range 1.0 to 3.0.");
$"Invalid option 99.0 for Razor language version --version; must be Latest or a valid version in range 1.0 to 5.0.");

// Compilation failed without creating the views assembly
Assert.FileExists(result, IntermediateOutputPath, "SimpleMvc.dll");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public async Task Build_WithGenerateRazorHostingAssemblyInfo_AddsConfigurationMe
Assert.FileContainsLine(
result,
razorAssemblyInfo,
"[assembly: Microsoft.AspNetCore.Razor.Hosting.RazorLanguageVersionAttribute(\"3.0\")]");
"[assembly: Microsoft.AspNetCore.Razor.Hosting.RazorLanguageVersionAttribute(\"5.0\")]");
Assert.FileContainsLine(
result,
razorAssemblyInfo,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// 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.IO;
using System.Threading.Tasks;
using Xunit;

namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
{
public class MvcBuildIntegrationTest31 : MvcBuildIntegrationTestLegacy
Expand All @@ -12,5 +16,33 @@ public MvcBuildIntegrationTest31(LegacyBuildServerTestFixture buildServer)

public override string TestProjectName => "SimpleMvc31";
public override string TargetFramework => "netcoreapp3.1";

[Fact]
public async Task Build_WithGenerateRazorHostingAssemblyInfo_AddsConfigurationMetadata()
{
using var project = CreateTestProject();

var razorAssemblyInfo = Path.Combine(IntermediateOutputPath, "SimpleMvc31.RazorAssemblyInfo.cs");
var result = await DotnetMSBuild("Build", "/p:GenerateRazorHostingAssemblyInfo=true");

Assert.BuildPassed(result);

Assert.FileExists(result, IntermediateOutputPath, "SimpleMvc31.Views.dll");
Assert.FileExists(result, IntermediateOutputPath, "SimpleMvc31.Views.pdb");

Assert.FileExists(result, razorAssemblyInfo);
Assert.FileContainsLine(
result,
razorAssemblyInfo,
"[assembly: Microsoft.AspNetCore.Razor.Hosting.RazorLanguageVersionAttribute(\"3.0\")]");
Assert.FileContainsLine(
result,
razorAssemblyInfo,
"[assembly: Microsoft.AspNetCore.Razor.Hosting.RazorConfigurationNameAttribute(\"MVC-3.0\")]");
Assert.FileContainsLine(
result,
razorAssemblyInfo,
"[assembly: Microsoft.AspNetCore.Razor.Hosting.RazorExtensionAssemblyNameAttribute(\"MVC-3.0\", \"Microsoft.AspNetCore.Mvc.Razor.Extensions\")]");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ Copyright (c) .NET Foundation. All rights reserved.
Determine the default Razor configuration
-->
<PropertyGroup Condition="'$(RazorDefaultConfiguration)'==''">
<!-- For 5.0, we're not introducing any new language features for MVC. We can continue using the 3.0 configuration \ extension for MVC support. -->
<RazorDefaultConfiguration Condition="'$(AddRazorSupportForMvc)'=='true'">MVC-3.0</RazorDefaultConfiguration>
<RazorDefaultConfiguration Condition="'$(RazorDefaultConfiguration)'==''">Default</RazorDefaultConfiguration>
</PropertyGroup>
Expand All @@ -80,7 +81,7 @@ Copyright (c) .NET Foundation. All rights reserved.
the project's runtime.
-->
<RazorConfiguration Include="Default" />
<RazorConfiguration Include="MVC-3.0" >
<RazorConfiguration Include="MVC-3.0">
<Extensions>MVC-3.0;$(CustomRazorExtension)</Extensions>
</RazorConfiguration>
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Copyright (c) .NET Foundation. All rights reserved.
<!--
Infer the RazorLangVersion if no value was specified. When adding support for newer target frameworks, list newer language versions first.
-->
<RazorLangVersion Condition="'$(RazorLangVersion)' == '' AND '$(_TargetingNET50OrLater)' == 'true'">5.0</RazorLangVersion>
<RazorLangVersion Condition="'$(RazorLangVersion)' == '' AND '$(_TargetingNETCoreApp30OrLater)' == 'true'">3.0</RazorLangVersion>

<!--
Expand Down