Skip to content

Return different exit codes when nuget push fails (especially for 409) to improve scriptability #7723

Closed

Description

@Erwinvandervalk

Summary

When pushing a package fails, Nuget.exe should return a different exitcode for different status codes.

Problem description

When pushing a package to a nuget feed fails, Nuget.Exe prints out the error and returns '1' as the exitcode, to indicate that it fails, regardless of the reason of the failure.

This is especially problematic when pushing a package with the same version twice (but with different build metadata). The server returns a 409 status code, but Nuget.Exe only returns the exitcode 1, indicating a failure. Because Nuget.Exe only returns '1', it becomes very difficult to programmatically control what it should do, for example from a script.

Http statuscodes carry meaning about the actual reason for failure. Having to grep this from stdout is not really a solid solution, so it would be much better.

Some more background

We build and push packages to nuget / myget as part of our CI pipeline. Sometimes it's needed to re-run a build (for whatever reason). Now, rerunning a succesful build will fail the second time, because a package with the same build metadata cannot be pushed again.

Suggested solution

If the nuget push fails, Nuget.exe should return the statuscode as exit code. Any non zero exit code indicates failure, but the invoker of nuget can derive meaning from the httpstatuscode as well.

Implementation option 1:

The following line currently rethrows the http exception:
https://github.com/NuGet/NuGet.Client/blob/dev/src/NuGet.Core/NuGet.Protocol/Resources/PackageUpdateResource.cs#L327

If pushing a package fails, we could throw a new ExitCodeException(response.statuscode).

Implementation option 2:

We can also catch the HttpRequestException and throw an ExitCodeException(response.statuscode) in the Nuget.Client PushCommand:

https://github.com/NuGet/NuGet.Client/blob/dev/src/NuGet.Clients/NuGet.CommandLine/Commands/PushCommand.cs#L78

Implementation option 3:

We can catch the HttpRequestException at the top level and return the statuscode as an exit code.

https://github.com/NuGet/NuGet.Client/blob/dev/src/NuGet.Clients/NuGet.CommandLine/Program.cs#L197

Related

The issue #1630 tries to resolve the same problem, but with a different approach.

Activity

  1. added
    Type:DCR
    Area:ErrorHandling
    on Jan 23, 2019
  2. adamralph commented on Feb 27, 2019

    @adamralph

    FWIW, I believe #1630 is a better solution.

  3. added
    Priority:3
    and removed on Apr 3, 2024
  4. added
    Status:Inactive
    on May 7, 2026
  5. dotnet-policy-service commented on May 7, 2026

    @dotnet-policy-service
    Contributor

    Due to lack of recent activity, this issue has been marked as a candidate for icebox cleanup. It will be closed if no further activity occurs within 14 more days. If the issue receives enough upvotes, it will be flagged with Triage:NeedsTriageDiscussion for team review.
    This process is part of the experimental Stale icebox issues cleanup we are currently trialing. Please share any feedback you might have in the linked issue.

  6. dotnet-policy-service commented on May 21, 2026

    @dotnet-policy-service
    Contributor

    This issue will now be closed since it had been marked Icebox cleanup candidate, but received no further activity in the past 14 days.

  7. adamralph commented on Jul 29, 2026

    @adamralph

    Should be closed as not planned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area:ErrorHandlingArea:HttpCommunicationFunctionality:PushIcebox cleanup candidatePriority:3Product:NuGet.exeStatus:InactiveType:DCR

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions