Skip to content

lazily load Pkg #4955

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

Merged
merged 4 commits into from
Jul 5, 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
6 changes: 1 addition & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,7 @@ jobs:
- os: ubuntu-latest
experimental: true
prefix: xvfb-run
version: '~1.11.0-0' # upcoming julia version, next `rc`
- os: ubuntu-latest
experimental: true
prefix: xvfb-run
version: 'nightly'
version: 'pre' # upcoming julia version, next `rc`

steps:
- uses: actions/checkout@v4
Expand Down
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Showoff = "992d4aef-0814-514b-bc4d-f2e9a6c4116f"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
UnicodeFun = "1cfade01-22cf-5700-b092-accc4b62d6e1"
UnitfulLatexify = "45397f5d-5981-4c77-b2b3-fc36d6e9b728"
Expand Down
2 changes: 1 addition & 1 deletion src/Plots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ if isdefined(Base, :Experimental) && isdefined(Base.Experimental, Symbol("@max_m
@eval Base.Experimental.@max_methods 1
end

using Pkg, Dates, Printf, Statistics, Base64, LinearAlgebra, SparseArrays, Random
using Dates, Printf, Statistics, Base64, LinearAlgebra, SparseArrays, Random, TOML
using PrecompileTools, Reexport, RelocatableFolders
using Base.Meta
@reexport using RecipesBase
Expand Down
35 changes: 19 additions & 16 deletions src/backends.jl
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
struct NoBackend <: AbstractBackend end

const _plots_project = Pkg.Types.read_package(normpath(@__DIR__, "..", "Project.toml"))
const _current_plots_version = _plots_project.version
const _plots_compats = _plots_project.compat
lazyloadPkg() = Base.require(@__MODULE__, :Pkg)

const _current_plots_version = VersionNumber(TOML.parsefile(normpath(@__DIR__, "..", "Project.toml"))["version"])
const _plots_compats = TOML.parsefile(normpath(@__DIR__, "..", "Project.toml"))["compat"]

const _backendSymbol = Dict{DataType,Symbol}(NoBackend => :none)
const _backendType = Dict{Symbol,DataType}(:none => NoBackend)
const _backend_packages = Dict{Symbol,Symbol}()
const _initialized_backends = Set{Symbol}()
const _backends = Symbol[]

const _plots_deps = let toml = Pkg.TOML.parsefile(normpath(@__DIR__, "..", "Project.toml"))
const _plots_deps = let toml = TOML.parsefile(normpath(@__DIR__, "..", "Project.toml"))
merge(toml["deps"], toml["extras"])
end

Expand Down Expand Up @@ -38,22 +39,20 @@ function _check_installed(backend::Union{Module,AbstractString,Symbol}; warn = t
version = if pkg_id === nothing
nothing
else
get(Pkg.dependencies(), pkg_id.uuid, (; version = nothing)).version
pkg = lazyloadPkg()
get(Base.invokelatest(pkg.dependencies), pkg_id.uuid, (; version = nothing)).version
end
version === nothing && @warn "backend `$str` is not installed."
version
end

function _check_compat(m::Module; warn = true)
(be_v = _check_installed(m; warn)) === nothing && return
if (be_c = _plots_compats[string(m)]) isa String # julia 1.6
if be_v ∉ Pkg.Types.semver_spec(be_c)
@warn "`$m` $be_v is not compatible with this version of `Plots`. The declared compatibility is $(be_c)."
end
else
if intersect(be_v, be_c.val) |> isempty
@warn "`$m` $be_v is not compatible with this version of `Plots`. The declared compatibility is $(be_c.str)."
end
be_c = _plots_compats[string(m)]
pkg = lazyloadPkg()
semver = Base.invokelatest(pkg.Types.semver_spec, be_c)
if Base.invokelatest(∉, be_v, semver)
@warn "`$m` $be_v is not compatible with this version of `Plots`. The declared compatibility is $(be_c)."
end
nothing
end
Expand Down Expand Up @@ -214,9 +213,11 @@ function diagnostics(io::IO = stdout)
else
be_name = string(backend_package_name(be))
@info "selected `Plots` backend: $be_name, from $origin"
Pkg.status(
pkg = lazyloadPkg()
Base.invokelatest(
pkg.status,
["Plots", "RecipesBase", "RecipesPipeline", be_name];
mode = Pkg.PKGMODE_MANIFEST,
mode = pkg.PKGMODE_MANIFEST,
io,
)
end
Expand Down Expand Up @@ -379,7 +380,9 @@ function _initialize_backend(pkg::AbstractBackend)
@eval name === :GR ? Plots : Main begin
import $name
export $name
$(_check_compat)($name)
if $(QuoteNode(name)) !== :GR
$(_check_compat)($name)
end
end
_post_imports(pkg)
_runtime_init(pkg)
Expand Down
Loading