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

Viewed 7578

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.

7 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

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.

I found a workaround to fix the issue (at least for me).

Placing global.json file in the Visual Studio solution root folder:

{
  "sdk": {
    "allowPrerelease": false
  }
}

Initially I added global.json only in the root folder of my Web (MVC) project (which is one level deeper), and it does not fix the issue. So it has to be solution root folder.

I had the same issue, after installing VS 2022, however none of the answers helped me, but it showed me the way. I was not able to edit the language in the property page. cannot change version

I had to add following line to the csproj which was not able to build. in the propertyGroup, right where AsseblyName is for my .netcore 3 project.

<LangVersion>8</LangVersion>

the trick with "Latest" didn't work, so I had to select version from this table according to .net framework version used:

The compiler determines a default based on these rules:

DEFAULTS
Target          framework   C# language version default
.NET            6.x         C# 10
.NET            5.x         C# 9.0
.NET Core       3.x         C# 8.0
.NET Core       2.x         C# 7.3
.NET Standard   2.1         C# 8.0
.NET Standard   2.0         C# 7.3
.NET Standard   1.x         C# 7.3
.NET Framework  all         C# 7.3

source: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version

I found that updating to vs 2022 solved the issue for me

I'm having the same problem. I thought it was because I had recently updated .Net 6 to Prerelease 7 but looks like there is a serious bug somewhere in the 16.11 release.

None of the above solutions worked for me. And anyways, going around and changing the project files may cause other problems to other team members working with different versions of Visual Studio.

I had recently installed Visual Studio Community 2022, I just uninstalled it and the problem went away. I guess we'll avoid having both 2019 and 2022 on the same machines for a while...

Related