35

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.

Nenad
  • 24,809
  • 11
  • 75
  • 93
  • 1
    Can't reproduce the problem on my side (using VS 2019 16.11.0 version). You can try to create a new Application and check whether it works or not? If still not working, can you share the detailed steps to reproduce the problem? And, whether you are installing the .Net 6 package or not? On my machine, I don't install it. Besides, I also find [this issue](https://github.com/dotnet/aspnetcore/issues/34623), as a workaround, you can downgrade the VS 2019 version or try to use the VS 2022 version. – Zhi Lv Aug 15 '21 at 08:46
  • @ZhiLv You are right about new ASP.NET Core MVC project (NET 5.0). It compiles without issues in VS 2019 16.11.0. But probably only because it's quite small and simple razor code in templates. I found workaround in the meantime by placing `global.json` one level higher in the hierarchy of folders, instead of putting it in the root Web project folder. – Nenad Aug 15 '21 at 10:12
  • FYI. If I load the same project in VS2022 I don't have the issue. – mortenma71 Apr 19 '22 at 14:07

7 Answers7

36

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

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
  • 3
    As Glorfindel experienced, the other solutions did not work for me, but removing latest from the project file did finally solve the problem. – NLandis Nov 10 '21 at 19:04
  • 3
    I also encountered this problem recently with the update to Visual Studio 16.11.6. The entry in my .csproj file was `latest`, and removing this line solved the problem for me, so thank you! – Danny Meyer-Kristensen Nov 11 '21 at 09:44
  • Thank you @Goldfindel. I run into this problem when I installed VS 2022. This solved my problem and now I'll be upgrading to .NET 6.0 :) – Manuel.B Mar 14 '22 at 06:08
22

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.

Failwyn
  • 767
  • 8
  • 22
  • THIS. I had enabled Previews because I was testing with the NET6.0 beta. Switching this off fixes my other project. – MothraTL Sep 30 '21 at 02:20
  • This worked but isn't that annoying!! We have a couple new projects on .Net 6, 20 lagging behind.... not keen on switching this flag every time I switch between working on projects targeting these different frameworks. – br3nt Oct 25 '21 at 07:00
  • Based on this Microsoft DevBlog and some things I have read elsewhere, it doesn't sound like VS 2019 is going to support .Net 6 going forward. ".NET 6 RC2 has been tested and is supported with Visual Studio 2022 Preview 5, also releasing today. .NET 6 will be supported with Visual Studio 2022 and not Visual Studio 2019. Similarly, it will be supported with MSBuild 17.x and not 16.x. If you want to use .NET 6, you will need to upgrade to Visual Studio 2022." https://devblogs.microsoft.com/dotnet/announcing-net-6-release-candidate-2/ – Failwyn Oct 26 '21 at 11:16
  • 3
    Got this on the latest Version 16.11.6. "Preview" was turned off by default... Still looking for solution. – Alexander Nov 10 '21 at 09:30
  • @Alexander - Change LangVersion to 9 in the csproj file to fix this now that .Net 6 is no longer considered a preview version. – Failwyn Nov 12 '21 at 12:19
6

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.

Nenad
  • 24,809
  • 11
  • 75
  • 93
4

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://learn.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version

Jiří Herník
  • 2,412
  • 1
  • 25
  • 26
3

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

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 24 '22 at 19:07
2

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.

Salahuddin Ahmed
  • 4,854
  • 4
  • 14
  • 35
  • 1
    I've found quick workaround. Put it in the [answer bellow](https://stackoverflow.com/a/68790558/186822). – Nenad Aug 15 '21 at 10:18
0

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...

Ted
  • 3,985
  • 1
  • 20
  • 33