Search code examples
nugetwarningstfsbuildnuget-package-restorewarning-level

NuGet restore - how to make it treat warnings as errors?


Due to missing .NET Core SDK, package restore succeeds only partially and omits SDK projects.

WARNING: Error reading msbuild project information, ensure that your input solution or project file is valid. NETCore and UAP projects will be skipped, only packages.config files will be restored.

This is just a warning and nuget.exe exits with status code 0, so the build pipeline continues and fails later, making it harder to find the cause.

Because this is a common problem in our environment, I want this warning to be treated as an error. It is perfectly OK (and even desirable) if other warnings are treated as errors, too.

I know that NuGet reads MSBuild properties TreatWarningsAsErrors and WarningsAsErrors, but I have not managed to use them from command line. I tried setting NUGET_RESTORE_MSBUILD_ARGS environment variable to /p:TreatWarningsAsErrors=true, but even though the option was passed to the internal MSBuild call, it did not affect the warning printed by nuget.exe. I did not find any other suitable CLI options nor environment variables.

Context

In an on-premises Azure Pipelines build, I run a NuGet task to restore the packages for my solution. The solution contains both projects targeting .NET Framework and using packages.config and projects targeting .NET Core using PackageReference.

The build agents are not under my control. Some have the correct SDK version installed, some don't. I use .NET Core SDK Installer task to install the appropriate SDK.

Due to changes to the build definition or SDK update in the solution, the build may break. In that case, the error message is obscure, though, because is comes from VS Build and not from the package restore build step, where the root cause lies but which shines green.

Internally, the NuGet tasks (version 2.*) executes nuget.exe (currently version 5.0.2), which in turn executes MSBuild to perform a part of its job related to PackageReference. MSBuild fails, causing NuGet.CommandLine.ExitCodeException to be thrown, but caught, logged and discarded. Just above the Warning_ReadingProjectsFailed warning mentioned above, the stack trace is printed:

NuGet.CommandLine.ExitCodeException: Exception of type 'NuGet.CommandLine.ExitCodeException' was thrown.
    at NuGet.CommandLine.MsBuildUtility.<GetProjectReferencesAsync>d__6.MoveNext()
 --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    at NuGet.CommandLine.RestoreCommand.<GetDependencyGraphSpecAsync>d__52.MoveNext()
 --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    at NuGet.CommandLine.RestoreCommand.<DetermineInputsFromMSBuildAsync>d__47.MoveNext()

The comment at the top of the catch clause says:

                // At this point reading the project has failed, to keep backwards
                // compatibility this should warn instead of error if
                // packages.config files exist, but no project.json files.
                // This will skip NETCore projects which is a problem, but there is
                // not a good way to know if they exist, or if this is an old type of
                // project that the targets file cannot handle.

Solution

  • NuGet restore - how to make it treat warnings as errors?

    I am afraid we can't make it treat warnings as errors at this moment, because this behavior is as designed.

    I have already reported a similar issue to the NuGet team and I have received the following reply:

    This message is expected, but it should not block your restore.

    In the future once msbuild provides a way to skip projects that are missing a target this message will go away: microsoft/msbuild#2471

    Besides, the property TreatWarningsAsErrors is set for one/all NuGet warnings at project wide level, but above warning will be thrown when MSBuild/VS starts reading the project file .csproj. That is the reason why the property TreatWarningsAsErrors was not working, even though it was set.

    Since the build agents are not under your control, we can't install the required .NET Core SDK version directly on the agent, add a corresponding capability to the agent and add a demand for the capability to the build pipeline. It seems we have to add a comment under microsoft/msbuild#2471 to ask if we can have a wrench to set info is error or warning.