Skip to content

Commit 8fcf21a

Browse files
authored
[Artifacts]: Support using Pkg.Artifacts with LazyArtifacts (#39210)
Just as we wanted to support users that used `using Pkg`, we should support users that use `using Pkg.Artifacts` with their lazy artifacts. This PR merely extends support to those users and adds regression tests.
1 parent 4e6a3a4 commit 8fcf21a

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

stdlib/Artifacts/src/Artifacts.jl

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,9 @@ function _artifact_str(__module__, artifacts_toml, name, path_tail, artifact_dic
541541
meta = artifact_meta(name, artifact_dict, artifacts_toml; platform)
542542
if meta !== nothing && get(meta, "lazy", false)
543543
if lazyartifacts isa Module && isdefined(lazyartifacts, :ensure_artifact_installed)
544-
nameof(lazyartifacts) === :Pkg && Base.depwarn("using Pkg instead of using LazyArtifacts is deprecated", :var"@artifact_str", force=true)
544+
if nameof(lazyartifacts) in (:Pkg, :Artifacts)
545+
Base.depwarn("using Pkg instead of using LazyArtifacts is deprecated", :var"@artifact_str", force=true)
546+
end
545547
return jointail(lazyartifacts.ensure_artifact_installed(string(name), artifacts_toml; platform), path_tail)
546548
end
547549
error("Artifact $(repr(name)) is a lazy artifact; package developers must call `using LazyArtifacts` in $(__module__) before using lazy artifacts.")
@@ -659,9 +661,13 @@ macro artifact_str(name, platform=nothing)
659661
Base.include_dependency(artifacts_toml)
660662

661663
# Check if the user has provided `LazyArtifacts`, and thus supports lazy artifacts
662-
lazyartifacts = isdefined(__module__, :LazyArtifacts) ? GlobalRef(__module__, :LazyArtifacts) : nothing
663-
if lazyartifacts === nothing && isdefined(__module__, :Pkg)
664-
lazyartifacts = GlobalRef(__module__, :Pkg) # deprecated
664+
# If not, check to see if `Pkg` or `Pkg.Artifacts` has been imported.
665+
lazyartifacts = nothing
666+
for module_name in (:LazyArtifacts, :Pkg, :Artifacts)
667+
if isdefined(__module__, module_name)
668+
lazyartifacts = GlobalRef(__module__, module_name)
669+
break
670+
end
665671
end
666672

667673
# If `name` is a constant, (and we're using the default `Platform`) we can actually load

stdlib/Artifacts/test/runtests.jl

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,27 @@ end
133133
end
134134

135135
@testset "@artifact_str install errors" begin
136-
mktempdir() do tempdir
137-
with_artifacts_directory(tempdir) do
138-
ex = @test_throws ErrorException artifact"c_simple"
139-
@test startswith(ex.value.msg, "Artifact \"c_simple\" was not installed correctly. ")
140-
ex = @test_throws ErrorException artifact"socrates"
141-
@test startswith(ex.value.msg, "Artifact \"socrates\" is a lazy artifact; ")
136+
for imports in ("Artifacts, Pkg", "Pkg, Pkg.Artifacts", "Pkg.Artifacts")
137+
mktempdir() do tempdir
138+
with_artifacts_directory(tempdir) do
139+
ex = @test_throws ErrorException artifact"c_simple"
140+
@test startswith(ex.value.msg, "Artifact \"c_simple\" was not installed correctly. ")
141+
ex = @test_throws ErrorException artifact"socrates"
142+
@test startswith(ex.value.msg, "Artifact \"socrates\" is a lazy artifact; ")
143+
144+
# Can install if we load `Pkg` or `Pkg.Artifacts`
145+
anon = Module(:__anon__)
146+
Core.eval(anon, Meta.parse("using $(imports), Test"))
147+
# Ensure that we get the expected exception, since this test runs with --depwarn=error
148+
Core.eval(anon, quote
149+
try
150+
artifact"socrates"
151+
@assert false "this @artifact_str macro invocation should have failed!"
152+
catch e
153+
@test startswith("using Pkg instead of using LazyArtifacts is deprecated", e.msg)
154+
end
155+
end)
156+
end
142157
end
143158
end
144159
end

0 commit comments

Comments
 (0)