VS: DOTNET_SDK is broken if VS_PROJECT_REFERENCES is not set or if building 'install' target.
There are two cases I've found where DOTNET_SDK does not restore package references before build. As mentioned in the docs for CMAKE_VS_NUGET_PACKAGE_RESTORE, "Targets that use the DOTNET_SDK are required to run a restore before building". Since the restore isn't run, the build is broken.
All examples that follow will use this example "HelloWorld.cs" file:
using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
}
}
}
Case 1: Build fails with DOTNET_SDK if no VS_PACKAGE_REFERENCES are set
CMakeLists.txt:
cmake_minimum_required(VERSION 3.23)
project(HelloWorld LANGUAGES CSharp)
add_executable(HelloWorld HelloWorld.cs)
set_target_properties(HelloWorld PROPERTIES
DOTNET_SDK "Microsoft.NET.Sdk"
DOTNET_TARGET_FRAMEWORK "net472"
)
command line: cmake.exe -S. -Bbuild -G"Visual Studio 17 2022" ; cmake.exe --build build
output:
-- The CSharp compiler identification is Microsoft Visual Studio 2022
-- The CSharp compiler version is 4.1.0
-- Check for working C# compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/MSBuild/Current/Bin/Roslyn/csc.exe
-- Check for working C# compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/MSBuild/Current/Bin/Roslyn/csc.exe - works
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/Remy/Documents/src/test/build
Microsoft (R) Build Engine version 17.1.0+ae57d105c for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.
Checking Build System
C:\Program Files\dotnet\sdk\6.0.201\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolution.targets(267,5)
: error NETSDK1004: Assets file 'C:\Users\Remy\Documents\src\test\build\obj\project.assets.json' not found. Run a NuGet
package restore to generate this file. [C:\Users\Remy\Documents\src\test\build\HelloWorld.csproj]
Case 2: Build fails with DOTNET_SDK when building the 'install' target
CMakeLists.txt:
cmake_minimum_required(VERSION 3.23)
project(HelloWorld LANGUAGES CSharp)
add_executable(HelloWorld HelloWorld.cs)
set_target_properties(HelloWorld PROPERTIES
VS_PACKAGE_REFERENCES Newtonsoft.Json_13.0.1
DOTNET_SDK "Microsoft.NET.Sdk"
DOTNET_TARGET_FRAMEWORK "net472"
)
install(TARGETS HelloWorld DESTINATION "${CMAKE_SOURCE_DIR}/install")
command line: cmake.exe -S. -Bbuild -G"Visual Studio 17 2022" ; cmake.exe --build build --target install
output:
-- The CSharp compiler identification is Microsoft Visual Studio 2022
-- The CSharp compiler version is 4.1.0
-- Check for working C# compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/MSBuild/Current/Bin/Roslyn/csc.exe
-- Check for working C# compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/MSBuild/Current/Bin/Roslyn/csc.exe - works
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/Remy/Documents/src/test/build
Microsoft (R) Build Engine version 17.1.0+ae57d105c for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.
Checking Build System
C:\Program Files\dotnet\sdk\6.0.201\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolution.targets(267,5)
: error NETSDK1004: Assets file 'C:\Users\Remy\Documents\src\test\build\obj\project.assets.json' not found. Run a NuGet
package restore to generate this file. [C:\Users\Remy\Documents\src\test\build\HelloWorld.csproj]