Skip to content
This repository has been archived by the owner. It is now read-only.

Razor Compilation doesn't respect LangVersion property #1591

Closed
tanaka-takayoshi opened this issue Jul 31, 2017 · 17 comments

Comments

@tanaka-takayoshi
Copy link

commented Jul 31, 2017

Version: ASP.NET Core 2.0 Preview 2 with 2017 Preview (preview) (Version 15.3.0 Preview 6.0)
NuGet Version: Microsoft.AspNetCore.All 2.0.0-preview2-final

Though Razor has updated to support C# 7.1 in #1385, I failed to compile when I use C# 7.1 feature in Razor View Page.

@page
@model IndexModel
@{
    ViewData["Title"] = "Home Page";
    var x = 1;
    var y = 2;
    var t = (x, y);
    ViewData["X"] = t.x;
    ViewData["Y"] = t.y;
}

I suspect this is because explicit LangVersion in csproj is required to use C# 7.1 feature.

  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    <LangVersion>7.1</LangVersion>
  </PropertyGroup>

When I set LangVersion and write C# 7.1 feature in a normal csharp class, meaning not a Razor page, it can be compiled.

public static void Main(string[] args)
{
    var x = 1;
    var y = 2;
    var t = (x, y);
    Console.WriteLine(t.x);
    Console.WriteLine(t.y);
    BuildWebHost(args).Run();
}

Is there any workaround to respect LangVersion during razor compilation?

@pranavkm

This comment has been minimized.

Copy link
Member

commented Aug 1, 2017

@tanaka-takayoshi looks like we have a bug in how we parse the language version string. We'll get this fixed for the next release. For the time being, you should be able to override the service that calculates the CSharpParseOptions to fix this:

using Microsoft.AspNetCore.Mvc.Razor.Internal.;

public class FixedCSharpCompiler : CSharpCompiler
{
    public FixedCSharpCompiler(RazorReferenceManager manager, IHostingEnvironment hostingEnvironment) : base(manager, hostingEnvironment)
    {
    }

    public override CSharpParseOptions ParseOptions => base.ParseOptions.WithLanguageVersion(LanguageVersion.CSharp7_1);
}

// In your Startup
...
void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddMvc();
    services.AddSingleton<CSharpCompiler, FixedCSharpCompiler>();
    ...
}
pranavkm added a commit to aspnet/Mvc that referenced this issue Aug 1, 2017
@DamianEdwards

This comment has been minimized.

Copy link
Member

commented Aug 1, 2017

This is surprising, as I did a demo just last week using @using static ... in a Razor layout file and it worked OK. I was likely selecting "Latest" as the C# version though, not 7.1 directly. Does that make a difference?

@rynowak

This comment has been minimized.

Copy link
Member

commented Aug 1, 2017

using static is 7.0

@DamianEdwards

This comment has been minimized.

Copy link
Member

commented Aug 1, 2017

Ah, nuts

@DamianEdwards

This comment has been minimized.

Copy link
Member

commented Aug 1, 2017

So we never had a test for this? Urgh

@pranavkm

This comment has been minimized.

Copy link
Member

commented Aug 1, 2017

Specifying Latest works pretty well. That would be better workaround than replacing an internal service.

@tanaka-takayoshi

This comment has been minimized.

Copy link
Author

commented Aug 1, 2017

Hi @pranavkm
Specifying Latest doesn't work in my application. Also, your suggested workaround failed to compile. In my application, CSharpCompiler is defined like below.

public class CSharpCompiler
{
    public CSharpCompiler(RazorReferenceManager manager, IOptions<RazorViewEngineOptions> optionsAccessor);

    public EmitOptions EmitOptions { get; }

    public CSharpCompilation CreateCompilation(string assemblyName);
    public SyntaxTree CreateSyntaxTree(SourceText sourceText);
}
@pranavkm

This comment has been minimized.

Copy link
Member

commented Aug 1, 2017

@tanaka-takayoshi - have you done a clean rebuild of the project since changing LangVersion to Latest? There's an issue where some of the build outputs don't get regenerated when the csproj is touched.

@tanaka-takayoshi

This comment has been minimized.

Copy link
Author

commented Aug 1, 2017

@pakrym Yes, I cleaned and rebuilt. But still doesn't work. I also deleted bin and obj folder, but it also still doesn't work.

@pranavkm

This comment has been minimized.

Copy link
Member

commented Aug 1, 2017

Would you mind updating to the RTM build of our packages? Here's the feed for it - https://dotnet.myget.org/F/dotnet-2-0-0-rtm/api/v3/index.json. Specifying <LangVersion>Latest</LangVersion> works for me on a vanilla 2.0.0 application.

@DamianEdwards

This comment has been minimized.

Copy link
Member

commented Aug 1, 2017

Also ensure you close and reopen Visual Studio after changing the language version settings.

@tanaka-takayoshi

This comment has been minimized.

Copy link
Author

commented Aug 1, 2017

After I updated to use 2.0.0 RTM as followed

 <packageSources>
   <add key="dotnet-2-0-0-rtm" value="https://dotnet.myget.org/F/dotnet-2-0-0-rtm/api/v3/index.json" />
 </packageSources>
  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
  </ItemGroup>

  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="2.0.0" />
  </ItemGroup>

I got below errors when startin up the project. Do you have any idea of this error?

System.IO.FileLoadException occurred
  HResult=0x80131040
  Message=Could not load file or assembly 'System.Diagnostics.DiagnosticSource, Version=4.0.2.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
  Source=<Cannot evaluate the exception source>
  StackTrace:
   at Microsoft.AspNetCore.Hosting.WebHostBuilder.BuildCommonServices(AggregateException& hostingStartupErrors)
   at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
   at RazorPageExample.Program.BuildWebHost(String[] args) in D:\Documents\GitHub\LondonOpenShiftMeetupDemos\RazorPageExample\Program.cs:line 21
   at RazorPageExample.Program.Main(String[] args) in D:\Documents\GitHub\LondonOpenShiftMeetupDemos\RazorPageExample\Program.cs:line 17
@DamianEdwards

This comment has been minimized.

Copy link
Member

commented Aug 1, 2017

You'll likely need to also install the latest 2.0.0 rtw SDK build

@tanaka-takayoshi

This comment has been minimized.

Copy link
Author

commented Aug 1, 2017

@DamianEdwards where should I download 2.0.0 RTW SDK?

@pranavkm

This comment has been minimized.

Copy link
Member

commented Aug 1, 2017

@tanaka-takayoshi

This comment has been minimized.

Copy link
Author

commented Aug 1, 2017

Thanks, all. After I install 2.0.0 RTM SDK and use 2.0.0 RTM NuGet packages, I confirmed it's working well.

  • Specifying <LangVersion>Latest</LangVersion>
    C# 7.1 in Razor works without any configuration.

  • Specifying <LangVersion>7.1</LangVersion>
    C# 7.1 in Razor doesn't work. But @pranavkm 's workaround #1591 (comment) works well.

@rynowak rynowak added this to the 2.1.0 milestone Aug 8, 2017
@rynowak

This comment has been minimized.

Copy link
Member

commented Aug 8, 2017

@pranavkm is already working on this

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
4 participants
You can’t perform that action at this time.