Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio 2019 version 16.11.0 - error CS1576: The line number specified for #line directive is missing or invalid

Since updating to Visual Studio 2019 version 16.11.0 (today), compilation of Razor MVC views is failing on multiple cshtml files in multiple projects:

error CS1576: The line number specified for #line directive is missing or invalid

I've tried to set fixed version of .NET Core SDK in global.json file, which was placed in a root folder of MVC Web project, as described here, but that did not help as well.

like image 490
Nenad Avatar asked Aug 13 '21 07:08

Nenad


2 Answers

I had the same error message with a .NET Core 5.0 web project in Visual Studio for Mac 8.10.11, after installing the Visual Studio for Mac 2022 preview with (as you guess) .NET Core 6.0 Preview. It might work on Windows too as mentioned in a now deleted answer.

The preview features as mentioned in @Failwyn's answer, under Preferences → Preview Features did not include the option to use previews of the .NET SDK. Adding global.json as described in @Nenad's answer did not work either.

Fortunately, I did have another .NET Core 5.0 project which did compile, so I was able to figure out the cause. Or at least the solution: this was to remove the <LangVersion> indications from the project file:

  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <LangVersion>latestmajor</LangVersion>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <LangVersion>latestmajor</LangVersion>
  </PropertyGroup>

This surprises me, since it indicates the C# version, but apparently this influences the .NET SDK version as well.

enter image description here

like image 159
Glorfindel Avatar answered Sep 24 '22 13:09

Glorfindel


UPDATE: Now that .Net 6 is no longer considered a preview, We have to change LangVersion from "Latest" to "9" in the csproj file to fix the issue.

This can be fixed by going to tools -> options -> Environment -> Preview Features -> Uncheck "Use previews of the .Net SDK (requires restart)".

Restart Visual Studio, and the projects will build again. I turned this on to test .Net 6 in Visual Studio 2019 and it broke all of my .Net 5 projects.

like image 23
Failwyn Avatar answered Sep 24 '22 13:09

Failwyn