From 3e872da15b489664a57a761e4242551ff4df22c8 Mon Sep 17 00:00:00 2001 From: Phillip Alday Date: Thu, 5 Oct 2023 11:13:53 -0500 Subject: [PATCH 1/7] pkgversion --- Project.toml | 9 +++++---- src/Compat.jl | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ test/runtests.jl | 6 ++++++ 3 files changed, 63 insertions(+), 4 deletions(-) diff --git a/Project.toml b/Project.toml index 8af5b2682..9c35ca0d4 100644 --- a/Project.toml +++ b/Project.toml @@ -1,23 +1,24 @@ name = "Compat" uuid = "34da2185-b29b-5c13-b0c7-acf172513d20" -version = "4.10.0" +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] julia = "1.6" +[extensions] +CompatLinearAlgebraExt = "LinearAlgebra" + [extras] Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" -[extensions] -CompatLinearAlgebraExt = "LinearAlgebra" - [targets] test = ["Dates", "LinearAlgebra", "Test"] diff --git a/src/Compat.jl b/src/Compat.jl index 49356523c..233512063 100644 --- a/src/Compat.jl +++ b/src/Compat.jl @@ -385,6 +385,58 @@ end end end +# this function is available as of Julia 1.9 +# https://github.com/JuliaLang/julia/pull/45607 +@static if !isdefined(Base, :pkgversion) + using TOML: parsefile + export pkgversion + + const project_names = ("JuliaProject.toml", "Project.toml") + const manifest_names = ("JuliaManifest.toml", "Manifest.toml") + const preferences_names = ("JuliaLocalPreferences.toml", "LocalPreferences.toml") + + 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 true + 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 + 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) + isnothing(path) && return nothing + return get_pkgversion_from_path(path) + end +end + # https://github.com/JuliaLang/julia/pull/43334 if VERSION < v"1.9.0-DEV.1163" import Base: IteratorSize, HasLength, HasShape, OneTo diff --git a/test/runtests.jl b/test/runtests.jl index fe39e2cab..06133f31d 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,5 +1,6 @@ using Compat using Dates +using TOML using Test @test isempty(detect_ambiguities(Base, Core, Compat)) @@ -457,6 +458,11 @@ end @test isempty(ea) end +@testset "pkgversion" begin + toml = joinpath(pkgdir(Compat), "Project.toml") + @test pkgversion(Compat) == VersionNumber(TOML.parsefile(toml)["version"]) +end + # https://github.com/JuliaLang/julia/pull/43334 @testset "stack" begin # Basics From 6c3945a93c1cfc12dfd78abfe8410d9d5b2d596f Mon Sep 17 00:00:00 2001 From: Phillip Alday Date: Thu, 5 Oct 2023 11:18:06 -0500 Subject: [PATCH 2/7] back to original order --- Project.toml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Project.toml b/Project.toml index 9c35ca0d4..fdb0ead33 100644 --- a/Project.toml +++ b/Project.toml @@ -11,14 +11,14 @@ UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4" [compat] julia = "1.6" -[extensions] -CompatLinearAlgebraExt = "LinearAlgebra" - [extras] Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" +[extensions] +CompatLinearAlgebraExt = "LinearAlgebra" + [targets] test = ["Dates", "LinearAlgebra", "Test"] From b87d11a92e5dd72bc1be73eaec9550dd3d8cf81d Mon Sep 17 00:00:00 2001 From: Phillip Alday Date: Thu, 5 Oct 2023 11:33:18 -0500 Subject: [PATCH 3/7] add test for pkg without version --- test/runtests.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/test/runtests.jl b/test/runtests.jl index 06133f31d..a04b89e78 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -461,6 +461,7 @@ 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 From 409a2bac0093c468050816f39091aaed56fb1446 Mon Sep 17 00:00:00 2001 From: Phillip Alday Date: Thu, 5 Oct 2023 11:42:03 -0500 Subject: [PATCH 4/7] update README --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index b59db731c..2f837ffb4 100644 --- a/README.md +++ b/README.md @@ -120,6 +120,8 @@ changes in `julia`. * `@something` and `@coalesce` as short-circuiting versions of `something` and `coalesce` ([#40729]) (since Compat 3.29) +* `pkgversion` Return the version of the package that imported module ([#45607]) (since Compat 4.11) + ## Developer tips One of the most important rules for `Compat.jl` is to avoid breaking user code @@ -172,6 +174,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 From c80a1403c41f1ce5d750b2bb19467c3606e7476a Mon Sep 17 00:00:00 2001 From: Phillip Alday Date: Tue, 17 Oct 2023 15:55:37 -0500 Subject: [PATCH 5/7] Apply suggestions from code review Co-authored-by: Martin Holters --- src/Compat.jl | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Compat.jl b/src/Compat.jl index 233512063..d35fe37a3 100644 --- a/src/Compat.jl +++ b/src/Compat.jl @@ -392,8 +392,6 @@ end export pkgversion const project_names = ("JuliaProject.toml", "Project.toml") - const manifest_names = ("JuliaManifest.toml", "Manifest.toml") - const preferences_names = ("JuliaLocalPreferences.toml", "LocalPreferences.toml") function locate_project_file(env::String) for proj in project_names @@ -402,7 +400,7 @@ end return project_file end end - return true + return nothing end function get_pkgversion_from_path(path) From 10fdd0b05c8715b851cedceb11cbaf8e4458f045 Mon Sep 17 00:00:00 2001 From: Phillip Alday Date: Wed, 10 Jan 2024 15:53:19 -0600 Subject: [PATCH 6/7] Apply suggestions from code review Co-authored-by: Curtis Vogt --- src/Compat.jl | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/Compat.jl b/src/Compat.jl index 4a038e438..751355a1f 100644 --- a/src/Compat.jl +++ b/src/Compat.jl @@ -387,10 +387,14 @@ end # this function is available as of Julia 1.9 # https://github.com/JuliaLang/julia/pull/45607 +# 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") function locate_project_file(env::String) @@ -430,8 +434,19 @@ end """ function pkgversion(m::Module) path = pkgdir(m) - isnothing(path) && return nothing - return get_pkgversion_from_path(path) + 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) + pkgorigin = get(Base.pkgorigins, Base.PkgId(Base.moduleroot(m)), nothing) + # Cache the version + if pkgorigin !== nothing && pkgorigin.version === nothing + pkgorigin.version = v + end + end + return v + end end end From f39f862ab5fc96f905a9c6d61590a3b73937b61c Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Wed, 10 Jan 2024 16:04:37 -0600 Subject: [PATCH 7/7] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f5cf747a7..17ce22b9d 100644 --- a/README.md +++ b/README.md @@ -118,7 +118,7 @@ changes in `julia`. * `@something` and `@coalesce` as short-circuiting versions of `something` and `coalesce` ([#40729]) (since Compat 3.29) -* `pkgversion` Return the version of the package that imported module ([#45607]) (since Compat 4.11) +* `pkgversion(m::Module)` returns the version of the package that loaded a given module ([#45607]) (since Compat 4.11) ## Developer tips