Resolve package name to UUID

Is there an API for resolving the name of a package (a string) to the UUID of a package (in a specific registry, defaulting to General)? The package is not necessarily installed in the local environment.

[this has been asked earlier, see here and here. I am aware of the possibility of installing the package and then looking in the manifest, or digging the UUID out from Github, looking for a solution within Julia.]

1 Like

You can certainly retrieve it with non-public Pkg functions, but if you want a stable solution, this is the best I can propose:

using RegistryInstances: reachable_registries
function find_uuid(registry_name, package_name)
    registry = only(filter(r -> r.name == registry_name, reachable_registries()))
    return only(key for (key, value) in registry.pkgs if value.name == package_name)
end
2 Likes

You could use RegistryInstances:

julia> using RegistryInstances

julia> general = first(reachable_registries()) # if General is the first registry in the list
Registry: "General" at "/Users/eph/.julia/registries/General.toml":
  uuid: 23338594-aafe-5451-b93e-139f81909106
  repo: https://github.com/JuliaRegistries/General.git
  git-tree-sha1: e50069c157ca55b02def51fe6395dcf5444f2c3b
  packages: 12940


julia> lookup = Dict(entry.name => uuid for (uuid, entry) in general.pkgs)
Dict{String, Base.UUID} with 12940 entries:
  "FastaLoader"           => UUID("139838d8-4077-4d8a-94e1-e6dd554a184c")
  "GAP_pkg_digraphs_jll"  => UUID("ce5c30f6-a98c-53bc-b643-089f5717f8db")
  "SyntaxTree"            => UUID("a4af3ec5-f8ac-5fed-a759-c2e80b4d74cb")
  "TypeParsers"           => UUID("6b5504e1-0c77-489f-ba22-2b282cee145f")
  "GeneratorArrays"       => UUID("8fff9178-7e8c-4f28-98e3-90d0f4178808")
  "Luxor"                 => UUID("ae8d54c2-7ccd-5906-9d76-62fc9837b5bc")
  "Foresight"             => UUID("c6e1034d-1844-4b96-abf2-c5824a5aa8c3")
  "AtomsBase"             => UUID("a963bdd2-2df7-4f54-a1ee-49d51e6be12a")
  "BloqadeGates"          => UUID("9b473568-af80-491c-8f51-e251334462c1")
  "NVTX_jll"              => UUID("e98f9f5b-d649-5603-91fd-7774390e6439")
  "ClimaAnalysis"         => UUID("29b5916a-a76c-4e73-9657-3c8fd22e65e6")
  "UnsafeArrays"          => UUID("c4a57d5a-5b31-53a6-b365-19f8c011fbd6")
  "HackerNews"            => UUID("d281a1a7-e441-575c-a368-03cb18f92a7f")
  "BifurcationKit"        => UUID("0f109fa4-8a5d-4b75-95aa-f515264e7665")
  "GAP_pkg_ace_jll"       => UUID("6e11effc-327b-571d-98e3-e12b1d1e7421")
  "Ai4EComponentLib"      => UUID("9901752a-13b3-47c7-8fb4-ce17182539d9")
  "Lectionary"            => UUID("6244d477-9cf0-4e07-8f6b-f8ee26bc0292")
  "PowerModelsONM"        => UUID("25264005-a304-4053-a338-565045d392ac")
  "Avalon"                => UUID("d462d321-1585-4840-9f3e-06f5c1b6ea25")
  "CUDA_Runtime_jll"      => UUID("76a88914-d11a-5bdc-97e0-2f5a05c973a2")
  "CommonSolve"           => UUID("38540f10-b2f7-11e9-35d8-d573e4eb0ff2")
  "libboxer_jll"          => UUID("5847ba37-b048-5c88-b501-c62f664b1471")
  "SentinelMissings"      => UUID("b1dc47e8-ebfd-568c-9de9-431a7ef94af6")
  "FlexiGroups"           => UUID("1e56b746-2900-429a-8028-5ec1f00612ec")
  "MaximumIndependentSet" => UUID("9e812ceb-42a3-473a-b295-b8bb2099a8ce")
  "RadiiPolynomial"       => UUID("f2081a94-c849-46b6-8dc9-07bb90ed72a9")
  "Trixi2Vtk"             => UUID("bc1476a1-1ca6-4cc3-950b-c312b255ff95")
  ⋮                       => ⋮

julia> lookup["JuMP"]
Base.UUID("4076af6c-e467-56ae-b986-b466b2749572")
1 Like

If someone wants to add a function doing this to RegistryInstances.jl, with a little more polish than my suggestion, I’m receptive to PRs.

2 Likes

I am now confused, isn’t that done already by RegistryInstances.uuids_from_name?

(Great package, and answers my question!)

It covers the second line of my function. Goes to show how well I know my own package. :slight_smile:

(To be clear I haven’t written much more than the README. The code is a snapshot of the Pkg code for handling registries, specifically intended to make it publically available without locking Pkg itself to a specific API.)

1 Like

I may be misremembering something, but isn’t it in general permitted to have the same name multiple times in a registry? Or is the mapping to a UUID guaranteed unique within one registry?

The Pkg and registry design allows multiple packages with the same name in one registry. The current registry tooling does not support it but it can be added when needed. The General registry will most likely never allow it.

3 Likes

Then I remembered correctly, thanks! I guess such a mapping from package name to one single UUID is too simplistic then :sweat_smile:

1 Like

Just to clarify, the use case is filling in parts of the Project.toml that have no CLI API, eg [weakdeps].

In 1.12 at least, you can do pkg> add --weak Example to add a weak dep.

Also, something a lot of folks don’t know, is you can use the ? help mode from the pkg mode, like pkg>?add which lists the options.

4 Likes

I have always added them as regular dependencies and then moved them with my editor. Picking up the UUID from a side channel sounds much more annoying.

1 Like

But then things precompile, which is time-consuming.

Good point, thanks!