dotnet / sdk

Core functionality needed to create .NET Core projects, that is shared between Visual Studio and CLI
https://dot.net/core
MIT License
2.71k stars 1.06k forks source link

dotnet tool --install Should Insert In a Deterministic Order #14693

Open aolszowka opened 3 years ago

aolszowka commented 3 years ago

As it says on the tin: dotnet tool --install should insert tools into dotnet-tools.json in some type of deterministic order (I suggest via some type of alphabetical/pseudo-alphabetical order).

Our use case involves us with several 10's of dotnet tool packages (more every day as we dive in deeper into the system). Due to the size of our code base and multiple branches we work hard to avoid merge conflicts resulting in the race to push.

When dealing with dotnet tool it is frustrating to have to work around the non-deterministic ordering when inserting new tools due to the various merge conflicts that arise.

Steps to Reproduce

dotnet new tool-manifest
dotnet tool install dotnet-format
dotnet tool install dotnet-dump

Expected Result

{
  "version": 1,
  "isRoot": true,
  "tools": {
    "dotnet-dump": {
      "version": "5.0.152202",
      "commands": [
        "dotnet-dump"
      ]
    },
    "dotnet-format": {
      "version": "4.1.131201",
      "commands": [
        "dotnet-format"
      ]
    }
  }
}

Instead as of:

dotnet --version
5.0.100

This still inserts in whatever order we happen to do.

In addition I cannot recall if JSON requires that the comma for an array be on the same line, but if that is not required it'd be great to not have that, this way when you add to the last element you don't get a garbage merge conflict due to , being added.

Work Arounds

Since its just a JSON file you can use any JSON sorting abilities you have, or baring that we rebuild the entire file by dumping the installed tools, sorting based on name, and then readding them (not super efficient, but good enough).

Thank you

am11 commented 3 years ago

Agreed, sorted tools comes handy when the list grows. My workaround is using jq in bash script:

jq '.tools |= (to_entries | sort_by(.key) | from_entries)' dotnet-tools.json  > _x && mv _x dotnet-tools.json