godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
90.55k stars 21.08k forks source link

Default .csproj template: Error CS0012:...You must add a reference to assembly ‘netstandard. #41439

Open Keppl opened 4 years ago

Keppl commented 4 years ago

Godot version: 3.2.3-rc4 Mono

OS/device including version: Windows 10

Issue description:

A fresh mono projects fails to build with Error CS0012: The type ‘Object’ is defined in an assembly that is not referenced. You must add a reference to assembly ‘netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=...

By default .csproj now contains:

<Project Sdk="Godot.NET.Sdk/3.2.3">
  <PropertyGroup>
    <TargetFramework>net472</TargetFramework>
  </PropertyGroup>
</Project>

Chaining it to seems to fix the issue for me

<Project Sdk="Godot.NET.Sdk/3.2.3">
  <PropertyGroup>
    <TargetFramework>net472</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="netstandard" />
  </ItemGroup>
</Project>

Steps to reproduce:

As mentioned before editing .csproj and adding <Reference Include="netstandard" /> seems to fix it for me.

Minimal reproduction project:

neikeq commented 4 years ago

What if instead of netstandard you reference System? Does that work? The project template was backported from 4.0 where we target netstandard so the reference isn't needed there. But we need itnin 3.x as we target .netframework.

Keppl commented 4 years ago

Using System or System.Core instead of netstandard still seems to produce that error for me.

Not sure if I'm doing it right, but I tried <Reference Include="System" /> and/or <Reference Include="System.Core" />. End result looking like this:

<Project Sdk="Godot.NET.Sdk/3.2.3">
  <PropertyGroup>
    <TargetFramework>net472</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="System" />
    <Reference Include="System.Core" />
  </ItemGroup>
</Project>
neikeq commented 4 years ago

Hmm, I just tested on Windows 10 with all build tools (except Mono's MSBuild which is giving me other issues). I couldn't reproduce this errors. Microsoft.NET.Sdks should be adding those references implicitly unless you set DisableImplicitFrameworkReferences=true in the csproj. What build tool are you using and what's the MSBuild version for it?

Keppl commented 4 years ago

Not quite sure about specific build tool. I just clicked build inside godot itself. Though based on error log, it looks like it is MSBuild 15.0, at least that the directory name. Also looks like it is building against .NET framework 4.72. (see command below).

I'm using packages from Visual Studio Community 2017 (had installed for other reasons, so just added C# sdk/tools): .Net desktop development package and bunch of .NET framework SDKs and targeting packs. It is possible that I'm missing something, but as I said just adding a reference seems to fixes it for me. I'm probably out to update to 2019 at some point...

If it helps, here what I think is the build command from the logs:

Csc: D:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\Roslyn\csc.exe /noconfig /unsafe- /checked- /nowarn:1701,1702,1705,1701,1702 /nostdlib+ /errorreport:prompt /warn:4 /define:GODOT;GODOT_WINDOWS;GODOT_PC;TRACE;TOOLS;DEBUG;NET472 /highentropyva+ /reference:D:\development\games\Projects\Godot_test_6.mono\assemblies\Debug\GodotSharp.dll /reference:D:\development\games\Projects\Godot_test_6.mono\assemblies\Debug\GodotSharpEditor.dll /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.7.2\mscorlib.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.7.2\System.Core.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.7.2\System.Data.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.7.2\System.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.7.2\System.Drawing.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.7.2\System.IO.Compression.FileSystem.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.7.2\System.Numerics.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.7.2\System.Runtime.Serialization.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.7.2\System.Xml.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.7.2\System.Xml.Linq.dll" /debug+ /debug:portable /filealign:512 /nologo /optimize- /out:D:\development\games\Projects\Godot_test_6.mono\temp\obj\Debug\Godot_test_6.dll /ruleset:"D:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Team Tools\Static Analysis Tools\Rule Sets\MinimumRecommendedRules.ruleset" /subsystemversion:6.00 /target:library /warnaserror- /utf8output /deterministic+ new_script.cs "C:\Users\-------\AppData\Local\Temp.NETFramework,Version=v4.7.2.AssemblyAttributes.cs" D:\---------\Godot_test_6.mono\temp\obj\Debug\Godot_test_6.AssemblyInfo.cs

Keppl commented 4 years ago

Hmm here something intresting, if I just change <TargetFramework>net472</TargetFramework> to <TargetFramework>net471</TargetFramework> it builds fine, without any error. Maybe it something with .NET 4.72 specifically, though don't know if it is in general or just my installation.

neikeq commented 4 years ago

Is that Csc command the one you're receiving errors with? It seems to be implicitly referencing the assemblies as expected. Just in case can you upload an example project? Maybe I did something differently in my test.

neikeq commented 4 years ago

This may be an issue with an older toolchain. In any case, the best may be to just make projects include netstandard by default.

Keppl commented 4 years ago

Here is a example project: Godot_test_8.zip

Though as I mentioned I didn't do much aside setting up mono and creating default script file. It might also be something weird with my installation of .NET 4.72 framework tool chains since targeting it at at net471 also seems to fix it.

Kersoph commented 4 years ago

I can reproduce the behaviour on my system using the Visual Studio Community 2017 packages. I receive the exact same msbuild log error (except the path of course). I just created a new project and added one default C# script. I'm unsure what exactly is the source of the issue (if its really depending on the VSC 2017 / 2019) I could solve it by Include="netstandard" or using "net471" as written by @Keppl

Keppl commented 3 years ago

Something I just found out, apparently Editor->Editor Settings...->Mono->Builds->Build Tools was (auto?) sets to "MSBuild(VS Build Tools)" for me. After I changed it to "dotnet CLI", projects compiles for without any problems or any need for editing csproj file. I'm not sure why it was set to "MSBuild(VS Build Tools)" for me since it doesn't seems to be the default. Maybe something that got carried over from settings of an old version of Godot saved somewhere, unless I changed at some point and forgot...?

Main reason I even thought of checking this option was because on another machine, recently reimaged, I couldn't get Godot to even find msbuild with MSBuild(VS Build Tools) option, but it worked immediately after I set it set to "dotnet CLI".

In any case, there might still be something weird with MSBuild(VS Build Tools) (at least on my setup), but setting Build Tool to "dotnet CLI" seems to fix this issue for me.

Edit: I tested this on 3.3

Edit2: Finally decided to switch over to Visual Studio 2019 (and removed 2017) C# scripts now compile with both dotnet CLI and MSBuild(VS Build Tool) settings without any issues.

Just FYI the 2019 minimal components set from installer that worked for me were (without selecting any workloads):

akien-mga commented 2 years ago

So I guess the issue was that VS 2017's MSBuild and install .NET components were not sufficient for what Godot needs? Is this something we should document somewhere?

GlitchedCode commented 2 years ago

I encounter this issue on pretty much the same capacity, except adding netstandard as a reference does not work on my machine, neither does changing TargetFramework to net471. Instead, for some reason it works consistently ONLY if i set it to net47, and I don't even need to delete the obj and bin directories after making this change.