7

I've just set up a new PC with VS2017 Pro (15.2) and Resharper (2017.1.2), on Windows 10 (1703).

I've opened an existing solution, and Resharper has highlighted a load of issues in CSHTML files. The ones I've looked at are all string interpolation and null conditional operators, and the message is "C# 6.0 language feature", as though this is an error. The projects all still compile and run OK, and string interpolation and null conditional operators aren't highlighted as errors in normal CS files.

Errors/Warnings in Solution Razor Editor

The projects in the solution are all set to Framework 4.6.1, and the C# Language Level was set to Default. I've set the LanguageLevel to CSharp60 in the sln.DotSettings file and the projects now all show the language level as C# 6.0, but Resharper is still moaning about the string interpolation errors. Clearing the Resharper cache hasn't helped.

The solution is running on another recently installed PC with no errors at all. The only difference is that on the other PC it's a completely new build, whereas this build is a new OS on a new C:\ drive, but the code is on a second drive that hasn't been changed.

The web.config in my views folder looks like this:

<?xml version="1.0"?>

<configuration>
  <configSections>
    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
      <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
    </sectionGroup>
  </configSections>

  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Optimization"/>
        <add namespace="System.Web.Routing" />
        <add namespace="Az.Ems.Web" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

  <appSettings>
    <add key="webpages:Enabled" value="false" />
  </appSettings>

  <system.webServer>
    <handlers>
      <remove name="BlockViewHandler"/>
      <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
    </handlers>
  </system.webServer>
</configuration>

And the codedom section of the main web.config looks like this

 <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" />
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
    </compilers>
  </system.codedom>
Jonathan Sayce
  • 9,359
  • 5
  • 37
  • 51
  • Can you add your web.config file, please? Language level for Razor is determined by the web.config, and ReSharper's setting can't override it. – citizenmatt Jun 13 '17 at 16:31
  • Hi Matt - I've added the web.config from the views folder - I presume that's the one you meant...? – Jonathan Sayce Jun 13 '17 at 20:49
  • It might actually be the main web.config. You'll need to look for something like in this answer: https://stackoverflow.com/a/30832849/88374. By default, Razor uses C# 5, and you need to enable support for C# 6, either with the checkbox mentioned in that answer, or by installing the Microsoft.CodeDom.Providers.DotNetCompilerPlatform package. ReSharper should get the appropriate language level from this config, although there are a few known issues if the config is different based on view location. – citizenmatt Jun 14 '17 at 12:02
  • I've added the codedom section. The langversion option is set to default - do you know where it would be picking up a default from? The Microsoft.CodeDom.Providers.DotNetCompilerPlatform package is already installed. – Jonathan Sayce Jun 14 '17 at 12:15

2 Answers2

18

Try setting the compiler flags to /langversion:6 instead of default. ReSharper appears to be trying to parse the version as a number, which will fail and keep ReSharper at C# 5.

<system.codedom>
  <compilers>
    <compiler language="c#;cs;csharp" 
              extension=".cs"
              type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
              warningLevel="4"
              compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
    <compiler language="vb;vbs;visualbasic;vbscript"
              extension=".vb"
              type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
              warningLevel="4"
              compilerOptions="/langversion:6 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
  </compilers>
</system.codedom>
citizenmatt
  • 18,085
  • 5
  • 55
  • 60
  • Does the Razor code compile with C# 6 when set to `default`? If you can confirm this, I'll raise it as an issue with ReSharper. – citizenmatt Jun 15 '17 at 13:45
  • Yes, the C# 6 features have all worked fine with that config, on a variety of dev machines. The config file was originally generated years ago (probably in VS2012), but I don't know what it was about this particular dev machine that caused the problem. Anyway, your fix works - thanks for all the help. – Jonathan Sayce Jun 15 '17 at 13:58
  • 1
    I've created an issue to get this fixed: https://youtrack.jetbrains.com/issue/RSRP-465110 – citizenmatt Jun 16 '17 at 09:34
  • It might be worth mentioning in the issue that there's some other unidentified variable involved - I've used the same code in at least three other dev environments without getting the error, so there's more to it than just setting the language version to default gives the error. – Jonathan Sayce Jun 16 '17 at 09:43
  • Also, you will want to install the `Microsoft.CodeDom.Providers.DotNetCompilerPlatform` NuGet if that section is not already in your web.config and/or if you do not already have it installed in your project. – hvaughan3 Sep 22 '17 at 14:10
  • This is not working for me. /langversion:6 is set and the DotNetCompilerPlatform NuGet package is installed. Resharper is still flagging ?. as an error. – stricq Apr 11 '18 at 19:43
2

In addition to citizenmatt's answer, Resharper does not support the <location> tag. In such cases, you need to move the <system.codedom> block outside of the <location> block.

https://resharper-support.jetbrains.com/hc/en-us/community/posts/115000754490-Incorrect-warning-about-C-6-0-Language-Feature-in-editor

stricq
  • 798
  • 6
  • 18