godotengine / godot

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

C# Godot project with MSTest - Error: Generator 'ScriptPathAttributeGenerator' failed to generate source. #87753

Open PerMalmberg opened 9 months ago

PerMalmberg commented 9 months ago

Tested versions

VStudio version:

Microsoft Visual Studio Community 2022
Version 17.8.5
VisualStudio.17.Release/17.8.5+34511.84
Microsoft .NET Framework
Version 4.8.09032

Installed Version: Community

ASP.NET and Web Tools   17.8.358.6298
ASP.NET and Web Tools

Azure App Service Tools v3.0.0   17.8.358.6298
Azure App Service Tools v3.0.0

C# Tools   4.8.0-7.23572.1+7b75981cf3bd520b86ec4ed00ec156c8bc48e4eb
C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Microsoft JVM Debugger   1.0
Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines

NuGet Package Manager   6.8.0
NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/

TypeScript Tools   17.0.20920.2001
TypeScript Tools for Microsoft Visual Studio

Visual Basic Tools   4.8.0-7.23572.1+7b75981cf3bd520b86ec4ed00ec156c8bc48e4eb
Visual Basic components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Visual F# Tools   17.8.0-beta.23475.2+10f956e631a1efc0f7f5e49c626c494cd32b1f50
Microsoft Visual F# Tools

Visual Studio IntelliCode   2.2
AI-assisted development for Visual Studio.

System information

Godot v4.2.1.stable.mono - Windows 10.0.22631 - Vulkan (Mobile) - dedicated NVIDIA GeForce RTX 2070 SUPER (NVIDIA; 31.0.15.4617) - AMD Ryzen 7 3800XT 8-Core Processor (16 Threads)

Issue description

When adding an MSTest project to a C# solution/project created by Godot, the following error message is written to the output window when compiling the test project:

Generator 'ScriptPathAttributeGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'InvalidOperationException' with message 'Property 'GodotProjectDir' is null or empty.'

I've seen this in projects with actual content but not seen any actual issues coming from it.

Full output:

Build started at 21:10...
1>------ Build started: Project: PubSub, Configuration: Debug Any CPU ------
1>PubSub -> D:\code\godot-pub-sub\PubSub\.godot\mono\temp\bin\Debug\PubSub.dll
2>------ Build started: Project: Test, Configuration: Debug Any CPU ------
2>CSC : warning CS8785: Generator 'ScriptPathAttributeGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'InvalidOperationException' with message 'Property 'GodotProjectDir' is null or empty.'.
2>Test -> D:\code\godot-pub-sub\Test\bin\Debug\net8.0\Test.dll
2>Done building project "Test.csproj".
========== Build: 2 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
========== Build completed at 21:10 and took 02,718 seconds ==========

Steps to reproduce

Simply compile the attached solution.

Minimal reproduction project (MRP)

repro.zip

Delsin-Yu commented 9 months ago

I don't think Godot API works with the C# test frameworks. But this warning can be resolved by adding

<PropertyGroup>
  <IsGodotToolsProject>true</IsGodotToolsProject>
</PropertyGroup>

To the Testing Csproj file, this disables the ScriptPathAttributeGenerator and GodotPluginsInitializerGenerator which can be useful when you are creating a separate library. I'm using GDUnit4 for unit testing.

PerMalmberg commented 9 months ago

No, it might not and probably isn't intended to for good reasons. As such the generator should not run in the Test-project, or at least not emit errors.

Thanks for a possible workaround to use until a proper fix is in place, @Delsin-Yu

Framebuffers commented 1 day ago

EDIT: did them on Godot 4.3 and Linux.

I checked the .csproj files and noticed something, might not be as important but maybe it's a clue. There's a bunch of mismatching .NET versions:

PubSub.csproj

<Project Sdk="Godot.NET.Sdk/4.2.1">
  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <TargetFramework Condition=" '$(GodotTargetPlatform)' == 'android' ">net7.0</TargetFramework>
    <TargetFramework Condition=" '$(GodotTargetPlatform)' == 'ios' ">net8.0</TargetFramework>
    <EnableDynamicLoading>true</EnableDynamicLoading>
  </PropertyGroup>
</Project>

Test.csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>

    <IsPackable>false</IsPackable>
    <IsTestProject>true</IsTestProject>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
    <PackageReference Include="MSTest.TestAdapter" Version="3.2.0" />
    <PackageReference Include="MSTest.TestFramework" Version="3.2.0" />
    <PackageReference Include="coverlet.collector" Version="6.0.0" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\PubSub\PubSub.csproj" />
  </ItemGroup>

</Project>

Also, I've had this experience with testing suites (and with experience gathered from @chickensoft-games testing rigs), so I added this line to the files: <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies> and... Screenshot from 2024-10-29 08-10-56

at the second build it compiled.


image So... try doing it twice. Here's the folder with the compiled builds. Took the precaution of doing a dotnet clean in between. Same behaviour. Here's the project files with the compiled .dll files so you can check for yourself. I did compile on Linux, so your mileage may vary a lot.

.NET 8.0 repro_8.0.zip

.NET 9.0 repro_9.0.zip