Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pkgversion(m::Module) to get the version of the package that loaded a given module #45607

Merged
merged 5 commits into from
Jun 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ New library functions
* `Iterators.flatmap` was added ([#44792]).
* New helper `Splat(f)` which acts like `x -> f(x...)`, with pretty printing for
inspecting which function `f` was originally wrapped. ([#42717])
* New `pkgversion(m::Module)` function to get the version of the package that loaded
a given module, similar to `pkgdir(m::Module)`. ([#45607])

Library changes
---------------
Expand Down
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,7 @@ export
parentmodule,
pathof,
pkgdir,
pkgversion,
names,
which,
@isdefined,
Expand Down
20 changes: 20 additions & 0 deletions base/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,26 @@ function pkgdir(m::Module, paths::String...)
return joinpath(dirname(dirname(path)), paths...)
end

"""
pkgversion(m::Module)

Return the version of the package that imported module `m`,
or `nothing` if `m` was not imported from a package, or imported
from a package without a version field set.

The version is read from the package's Project.toml during package
load.

!!! compat "Julia 1.9"
This function was introduced in Julia 1.9.
"""
function pkgversion(m::Module)
rootmodule = moduleroot(m)
pkg = PkgId(rootmodule)
pkgorigin = get(pkgorigins, pkg, nothing)
return pkgorigin === nothing ? nothing : pkgorigin.version
end

## generic project & manifest API ##

const project_names = ("JuliaProject.toml", "Project.toml")
Expand Down
1 change: 1 addition & 0 deletions doc/src/base/base.md
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ Base.nameof(::Module)
Base.parentmodule
Base.pathof(::Module)
Base.pkgdir(::Module)
Base.pkgversion(::Module)
Base.moduleroot
__module__
__source__
Expand Down
7 changes: 7 additions & 0 deletions test/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,13 @@ module NotPkgModule; end
@test pkgdir(NotPkgModule, "src") === nothing
end

@testset "pkgversion" begin
@test pkgversion(Foo) == v"1.2.3"
@test pkgversion(Foo.SubFoo1) == v"1.2.3"
@test pkgversion(Foo.SubFoo2) == v"1.2.3"
@test pkgversion(NotPkgModule) === nothing
end

end

## systematic generation of test environments ##
Expand Down
3 changes: 3 additions & 0 deletions test/project/deps/Foo1/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name = "Foo"
uuid = "1a6589dc-c33c-4d54-9a54-f7fc4b3ff616"
version = "1.2.3"