.net-coremsbuildc++-clibuild-error

MSB4018 Error on Every Second Build: System.Text.Json Not Found and Empty .deps.json File


I'm working on a C++/CLI project in Visual Studio 2019 (32-bit target platform) using .NET SDK 6.0.135. This project runs on a build server and has a dependency on System.Text.Json, although it's not explicitly referenced as a NuGet package. The build fails every other time with the following error:

Error MSB4018: The "GenerateDepsFile" task failed unexpectedly. System.IO.FileNotFoundException: Could not load file or assembly 'System.Text.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified.

Additional details from the advanced log show that System.Text.Json is missing in the .deps.json directory or that the .deps.json file is empty, causing this error. However, the error only appears on every second build.

Additional Observation: A colleague noticed that, whenever the error occurs, the build process attempts to load one of the following:

C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/MSBuild/Current/Bin/System.Text.Json.dll C:/Program Files/dotnet/sdk/6.0.135/Sdks/Microsoft.NET.Sdk/tools/net472/System.Text.Json.dll

Both of these assemblies are version 4.x, but the build requires version 6.0.0.0. However, if the .deps.json file was not deleted, it correctly uses the version of System.Text.Json from the 6.0.135 SDK, and the build succeeds.

What I've treid so far:


Solution

  • The question seems to contain a hint for a workaround that worked for me, so many thanks!

    The issue seems to be specific to sdk 6.0.135. I've had exactly same setup as the topic starter on my local machine and run into exact same issue. Furthermore it started to show up on any(!) project, even blank VS2019 console hello world template (targeting net5.0).

    Also my colleague having 6.0.131 on his machine saw no trouble.

    Update with more info: