sdk: Error NETSDK1004 occurs when building any C++/CLI project.
Describe the bug
When building any C++ project using the CLR Class Library (.NET) template, the project cannot be successfully built with the MSBuild command line. Instead, it can only be successfully built using Visual Studio 2022. An error, NETSDK1004, occurs when attempting to build the project with MSBuild.
error NETSDK1004: Assets file 'obj\project.ass
ets.json' not found. Run a NuGet package restore to generate this file.
To Reproduce
- Open Visual Studio 2022 (17.5.3, or 17.6.0 Preview 2.0)
- Create a new C++ project using the CLR Class Library (.NET) template
- Create Git Repository for this solution with default .gitignore template
- Close the Visual Studio instance
- ‼️important‼️ Use
git clean -xdfto clean the entire repo, excluding the project files - Open the Terminal and use
msbuild -restoreto build the solution
Here is a repository containing only the newly created project.
Exceptions
Here’s the output:
C:\Program Files\dotnet\sdk\7.0.300-preview.23122.5\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolut
ion.targets(266,5): error NETSDK1004: Assets file 'D:\WIP\Desktop\Walterlv.IssueDemo\Walterlv.IssueDemo\obj\project.ass
ets.json' not found. Run a NuGet package restore to generate this file. [D:\WIP\Desktop\Walterlv.IssueDemo\Walterlv.Iss
ueDemo\Walterlv.IssueDemo.vcxproj]
0 Warning(s)
1 Error(s)
If you want more detail output, I’ll post it here.
Known Workarounds
- If we open the solution with Visual Studio 2022, simply opening it will ensure that the MSBuild command line works correctly. Opening the project using Visual Studio will create some files within the project, and even after Visual Studio is closed, the MSBuild command line will still function properly.
- When building a C++ project using the CLR Class Library (.NET Framework) template, the MSBuild command line works as expected.
By referring to the official documentation, we can find a workaround by using this command line: devenv SolutionName.sln /Build.
Further technical details
I’ve tested this issue in two Windows machines and both failed:
- .NET SDK 7.0.202 + Visual Studio 2022 (17.5.3)
- .NET SDK 7.0.300-preview.23122.5 + Visual Studio 2022 (17.6.0 Preview 2.0)
Here is an info of the 7.0.202 one:
$ dotnet --info
.NET SDK:
Version: 7.0.202
Commit: 6c74320bc3
运行时环境:
OS Name: Windows
OS Version: 10.0.22621
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\7.0.202\
Host:
Version: 7.0.4
Architecture: x64
Commit: 0a396acafe
.NET SDKs installed:
5.0.400 [C:\Program Files\dotnet\sdk]
6.0.102 [C:\Program Files\dotnet\sdk]
8.0.202 [C:\Program Files\dotnet\sdk]
About this issue
- Original URL
- State: open
- Created a year ago
- Reactions: 3
- Comments: 16 (2 by maintainers)
@gureedo @walterlv @GitDaBytes @bharath-brat thanks for reporting the issue! FYI in Visual Studio 2022 17.7 Preview 2 (released today) there are fixes for CLR .NET projects (aka C++/CLI projects), in particular the projects participating in NuGet’s
PackageReference(that is<EnableManagedPackageReferenceSupport>true</>), are now nuget-restored correctly (not skipped anymore), and also they follow correctly the compatibility rules for the TargetPlatformMoniker (identifier and version).Let us know if you can try it out and report any feedback, thank you.
FYI - this is also affecting any .NET 6 C++ / CLR projects. All of the above hold true. setting <EnableManagedPackageReferenceSupport>false</EnableManagedPackageReferenceSupport> stops the project compiling in VS2022, but does allow the build to work in our DevOps Build Pipeline.
the error from VS2022 is error : Your project does not reference “.NETCoreApp,Version=v6.0” framework. Add a reference to “.NETCoreApp,Version=v6.0” in the “TargetFrameworks” property of your project file and then re-run NuGet restore.
Attempting the other workaround msbuild /t:restore project.vcxproj appears to only restore pure csharp projects referenced by my C++ dll, and does not generate an assets file for the C++ project.
Got more details from @lukka offline. Specifying the individual project (
msbuild /t:restore project.vcxproj) or disabling package reference support should work around this for now. Full support should be coming in 17.7.@ryanbinnsrv thank you, the problem will be fixed in VS 17.8. In order to allow the community to raise the importance of this issue, I created a Feedback Ticket at https://developercommunity.visualstudio.com/t/VS-attempts-to-nuget-restore-a-newly-c/10444084 which is used to ponder whether backport to VS 17.7.
I’ve performed tests on this repository with the 17.7.0 Preview 4.0 version and I am pleased to report that the issue has been addressed successfully. Thank you for your diligent efforts in resolving this.
Temporary workaround: remove
<EnableManagedPackageReferenceSupport>true</EnableManagedPackageReferenceSupport>from your project.