Skip to content

Commit

Permalink
Artifacts: disallow downloading via Pkg
Browse files Browse the repository at this point in the history
Packages should never access Base.loaded_modules() to call functions from it.
  • Loading branch information
vtjnash committed Oct 1, 2020
1 parent a4bfb9c commit 9999d5d
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 40 deletions.
5 changes: 1 addition & 4 deletions stdlib/Artifacts/src/Artifacts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -492,10 +492,7 @@ function _artifact_str(__module__, artifacts_toml, name, path_tail, artifact_dic
end
end

# If not, we need to download it. We look up the Pkg module through `Base.loaded_modules()`
# then invoke `ensure_artifact_installed()`:
Pkg = first(filter(p-> p[1].name == "Pkg", Base.loaded_modules))[2]
return jointail(Pkg.Artifacts.ensure_artifact_installed(string(name), artifacts_toml; platform), path_tail)
error("Artifact $(repr(name)) is missing. Try `using Pkg; Pkg.build(\"$__module__\"); Pkg.Artifacts.ensure_all_artifacts_installed($(repr(artifacts_toml)))`?")
end

"""
Expand Down
1 change: 1 addition & 0 deletions stdlib/Artifacts/test/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/artifacts/
22 changes: 22 additions & 0 deletions stdlib/Artifacts/test/refresh_artifacts.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Artifacts: with_artifacts_directory
# using Pkg.Artifacts: ensure_all_artifacts_installed
using Pkg.Artifacts: load_artifacts_toml, ensure_artifact_installed
let
tempdir = joinpath(@__DIR__, "artifacts")
rm(tempdir; recursive=true, force=true)
toml = joinpath(@__DIR__, "Artifacts.toml")
unused = Base.BinaryPlatforms.Platform(string(Sys.ARCH), "linux")
with_artifacts_directory(tempdir) do
# ensure_all_artifacts_installed(toml; include_lazy=true)
dict = load_artifacts_toml(toml)
for (name, meta) in dict
if meta isa Array
for meta in meta
ensure_artifact_installed(name, meta, toml; platform=unused)
end
else; meta::Dict
ensure_artifact_installed(name, meta, toml; platform=unused)
end
end
end
end
72 changes: 36 additions & 36 deletions stdlib/Artifacts/test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Artifacts, Test, Base.BinaryPlatforms
using Artifacts: with_artifacts_directory, pack_platform!, unpack_platform

run(`$(Base.julia_cmd()) $(joinpath(@__DIR__, "refresh_artifacts.jl"))`)

@testset "Artifact Paths" begin
mktempdir() do tempdir
with_artifacts_directory(tempdir) do
Expand Down Expand Up @@ -76,44 +78,42 @@ end
end

@testset "Artifact Slash-indexing" begin
mktempdir() do tempdir
with_artifacts_directory(tempdir) do
exeext = Sys.iswindows() ? ".exe" : ""

# simple lookup, gives us the directory for `c_simple` for the current architecture
c_simple_dir = artifact"c_simple"
@test isdir(c_simple_dir)
c_simple_exe_path = joinpath(c_simple_dir, "bin", "c_simple$(exeext)")
@test isfile(c_simple_exe_path)

# Simple slash-indexed lookup
c_simple_bin_path = artifact"c_simple/bin"
@test isdir(c_simple_bin_path)
# Test that forward and backward slash are equivalent
@test artifact"c_simple\\bin" == artifact"c_simple/bin"

# Dynamically-computed lookup; not done at compile-time
generate_artifact_name() = "c_simple"
c_simple_dir = @artifact_str(generate_artifact_name())
@test isdir(c_simple_dir)
c_simple_exe_path = joinpath(c_simple_dir, "bin", "c_simple$(exeext)")
@test isfile(c_simple_exe_path)

# Dynamically-computed slash-indexing:
generate_bin_path(pathsep) = "c_simple$(pathsep)bin$(pathsep)c_simple$(exeext)"
@test isfile(@artifact_str(generate_bin_path("/")))
@test isfile(@artifact_str(generate_bin_path("\\")))
end
tempdir = joinpath(@__DIR__, "artifacts")
with_artifacts_directory(tempdir) do
exeext = Sys.iswindows() ? ".exe" : ""

# simple lookup, gives us the directory for `c_simple` for the current architecture
c_simple_dir = artifact"c_simple"
@test isdir(c_simple_dir)
c_simple_exe_path = joinpath(c_simple_dir, "bin", "c_simple$(exeext)")
@test isfile(c_simple_exe_path)

# Simple slash-indexed lookup
c_simple_bin_path = artifact"c_simple/bin"
@test isdir(c_simple_bin_path)
# Test that forward and backward slash are equivalent
@test artifact"c_simple\\bin" == artifact"c_simple/bin"

# Dynamically-computed lookup; not done at compile-time
generate_artifact_name() = "c_simple"
c_simple_dir = @artifact_str(generate_artifact_name())
@test isdir(c_simple_dir)
c_simple_exe_path = joinpath(c_simple_dir, "bin", "c_simple$(exeext)")
@test isfile(c_simple_exe_path)

# Dynamically-computed slash-indexing:
generate_bin_path(pathsep) = "c_simple$(pathsep)bin$(pathsep)c_simple$(exeext)"
@test isfile(@artifact_str(generate_bin_path("/")))
@test isfile(@artifact_str(generate_bin_path("\\")))
end
end

@testset "@artifact_str Platform passing" begin
mktempdir() do tempdir
with_artifacts_directory(tempdir) do
win64 = Platform("x86_64", "windows")
mac64 = Platform("x86_64", "macos")
@test basename(@artifact_str("c_simple", win64)) == "444cecb70ff39e8961dd33e230e151775d959f37"
@test basename(@artifact_str("c_simple", mac64)) == "7ba74e239348ea6c060f994c083260be3abe3095"
end
tempdir = joinpath(@__DIR__, "artifacts")
with_artifacts_directory(tempdir) do
win64 = Platform("x86_64", "windows")
mac64 = Platform("x86_64", "macos")
@test basename(@artifact_str("c_simple", win64)) == "444cecb70ff39e8961dd33e230e151775d959f37"
@test basename(@artifact_str("c_simple", mac64)) == "7ba74e239348ea6c060f994c083260be3abe3095"
end
end
end

0 comments on commit 9999d5d

Please sign in to comment.