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

pkgversion #807

Merged
merged 8 commits into from
Jan 10, 2024
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
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
name = "Compat"
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
version = "4.10.1"
version = "4.11.0"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"

[compat]
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ changes in `julia`.

* `@something` and `@coalesce` as short-circuiting versions of `something` and `coalesce` ([#40729]) (since Compat 3.29)

* `pkgversion(m::Module)` returns the version of the package that loaded a given module ([#45607]) (since Compat 4.11)

## Developer tips

One of the most important rules for `Compat.jl` is to avoid breaking user code
Expand Down Expand Up @@ -170,5 +172,7 @@ Note that you should specify the correct minimum version for `Compat` in the
[#43334]: https://github.com/JuliaLang/julia/issues/43334
[#43354]: https://github.com/JuliaLang/julia/issues/43354
[#43852]: https://github.com/JuliaLang/julia/issues/43852
[#45607]: https://github.com/JuliaLang/julia/issues/45607
[#46104]: https://github.com/JuliaLang/julia/issues/46104
[#48038]: https://github.com/JuliaLang/julia/issues/48038
[#50105]: https://github.com/JuliaLang/julia/issues/50105
65 changes: 65 additions & 0 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,71 @@
end
end

# this function is available as of Julia 1.9
# https://github.com/JuliaLang/julia/pull/45607
palday marked this conversation as resolved.
Show resolved Hide resolved
# https://github.com/JuliaLang/julia/pull/45695
# https://github.com/JuliaLang/julia/pull/45861
# https://github.com/JuliaLang/julia/pull/46738
@static if !isdefined(Base, :pkgversion)
using TOML: parsefile
export pkgversion

const require_lock = isdefined(Base, :require_lock) ? Base.require_lock : Base.ReentrantLock()
const project_names = ("JuliaProject.toml", "Project.toml")
palday marked this conversation as resolved.
Show resolved Hide resolved

function locate_project_file(env::String)
for proj in project_names
project_file = joinpath(env, proj)
if Base.isfile_casesensitive(project_file)
return project_file
end
end
return nothing

Check warning on line 407 in src/Compat.jl

View check run for this annotation

Codecov / codecov/patch

src/Compat.jl#L407

Added line #L407 was not covered by tests
end

function get_pkgversion_from_path(path)
project_file = locate_project_file(path)
if project_file isa String
d = parsefile(project_file)
v = get(d, "version", nothing)
if v !== nothing
return VersionNumber(v::String)
end
end
return nothing

Check warning on line 419 in src/Compat.jl

View check run for this annotation

Codecov / codecov/patch

src/Compat.jl#L419

Added line #L419 was not covered by tests
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.

To get the version of the package that imported the current module
the form `pkgversion(@__MODULE__)` can be used.
"""
function pkgversion(m::Module)
path = pkgdir(m)
path === nothing && return nothing
Base.@lock require_lock begin
v = get_pkgversion_from_path(path)
# https://github.com/JuliaLang/julia/pull/44318
@static if hasfield(Base.PkgOrigin, :version)

Check warning on line 441 in src/Compat.jl

View check run for this annotation

Codecov / codecov/patch

src/Compat.jl#L441

Added line #L441 was not covered by tests
pkgorigin = get(Base.pkgorigins, Base.PkgId(Base.moduleroot(m)), nothing)
# Cache the version
if pkgorigin !== nothing && pkgorigin.version === nothing
pkgorigin.version = v

Check warning on line 445 in src/Compat.jl

View check run for this annotation

Codecov / codecov/patch

src/Compat.jl#L445

Added line #L445 was not covered by tests
end
end
return v
end
end
omus marked this conversation as resolved.
Show resolved Hide resolved
end

# https://github.com/JuliaLang/julia/pull/43334
if VERSION < v"1.9.0-DEV.1163"
import Base: IteratorSize, HasLength, HasShape, OneTo
Expand Down
7 changes: 7 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Compat
using Dates
using TOML
using Test

@test isempty(detect_ambiguities(Base, Core, Compat))
Expand Down Expand Up @@ -457,6 +458,12 @@ end
@test isempty(ea)
end

@testset "pkgversion" begin
toml = joinpath(pkgdir(Compat), "Project.toml")
@test pkgversion(Compat) == VersionNumber(TOML.parsefile(toml)["version"])
@test pkgversion(Base) === nothing
end

# https://github.com/JuliaLang/julia/pull/43334
@testset "stack" begin
# Basics
Expand Down
Loading