Suppose I just did using Foo. How do I find out the version of Foo.jl given the module Foo ?
The only way I found was to know the UUID of the package and query Pkg. I am hoping I am just overlooking something simple? Ideally I’d be hoping for something like Foo.version or so.
Motivation: I’d like to keep my code compatible with both CxxWrap 0.9.1 and 0.10.1, but there are some differences I need to deal with, no matter what. So far, all ways to query the version I found require using Pkg.jl, the UUID, and different code depending on whether one is using Julia 1.3 or 1.4 (or newer).
First of, thanks for the quick and helpful reply. That is actually a bit nicer than what I have. I wonder why something like that isn’t part of Pkg.jl.
But actually, I was wondering why Pkg.jl doesn’t just inject the relevant info into the module when loading it? It could simply insert Foo.version (well, that probably would be a bad idea as package may already have a symbol version on the toplevel; but it could also store a dictionary with this info and then use that to implement a function that allows querying the version (or even a full PackageSpec) for the currently loaded version of a package. Surely it must have all this information at some point
I realize such functionality this is not in there, but it seems like a sensible feature request to me, isn’t it? I can submit it as an issue to Pkg.jl I guess? I’d even work on a PR for it, but I’d first like to know if the idea has any chance at all
I am not sure about this. Pkg and code loading work in a declarative way: you specify what you need (packages and their compat bounds), the resolver takes care of it. Any alternative gets chaotic very quickly.
As for your specific problem, it is a common workaround to test for the presence of various functions/types/values/etc in other modules, and condition code on this, eg with isdefined. That said, the cleanest solution may be just picking a version of CxxWrap you want to support, and moving on. Earlier versions of your package remain usable with other versions.
Just to support @fingolfin:
we communicate with Julia by package name only (add Blah, using Blah)
we communicate with the users via name and version (cool feature in version 0.10.3 of Blah)
The uuid is neccessary for Project.toml (deps)
The version is neccessary to Project.toml (compat)
Yet, there is no “legal” way within Julia to obtain either the uuid, nor the version. Even worse, versions of indirect dependencies are not even shown with Pkg.status by default…
It is difficult to see why a package is not updated, let alone to see that it is not updated.
I like the package manager and the declarative way, but I still need to be able to see and test the information. It has been part of Julia for a long time to e.g. check the Julia version to enable/ disable features. I can test for signatures being present, but I cannot test for a new algorithm being there unless the API changes.
There are workarounds like the PkgVersion, or using undocumented features (Pkg.installed(), Pkg.dependencies()), but it feels like it should be exponsed in a legal way.
Just for reference, this is the code I ended up with:
# pkgdir was added in Julia 1.4
if VERSION < v"1.4"
pkgdir(m::Core.Module) = abspath(Base.pathof(Base.moduleroot(m)), "..", "..")
pkgproject(m::Core.Module) = Pkg.Operations.read_project(Pkg.Types.projectfile_path(pkgdir(m)))
pkgversion(m::Core.Module) = pkgproject(m).version
const VERSION_NUMBER = pkgversion(@__MODULE__)
Adding another use case: for development purposes. I (probably) found a bug in IndexedTables and I could not figure out which version am I running. PkgVersion.Version() eventually helped so that I know which branch of the code to look at.