Skip to content

avoid loading pkg until needed #1

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

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
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
41 changes: 17 additions & 24 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,25 @@ jobs:
strategy:
fail-fast: false
matrix:
julia-version:
- "nightly"
os:
- ubuntu-latest
julia-arch:
- x64
include:
- os: ubuntu-latest
julia-arch: x64
julia-version: 'nightly'
- os: windows-latest
julia-arch: x64
julia-version: 'nightly'
- os: macos-latest
julia-arch: aarch64
julia-version: 'nightly'
steps:
- uses: actions/checkout@v2
- uses: julia-actions/setup-julia@v1
- uses: actions/checkout@v4
- uses: julia-actions/setup-julia@v2
with:
version: ${{ matrix.julia-version }}
arch: ${{ matrix.julia-arch }}
- name: Cache artifacts
uses: actions/cache@v2
env:
cache-name: cache-artifacts
- uses: julia-actions/julia-buildpkg@v1
- uses: julia-actions/julia-runtest@v1
- uses: julia-actions/julia-processcoverage@v1
- uses: codecov/codecov-action@v2
with:
path: ~/.julia/artifacts
key: ${{ runner.os }}-test-${{ env.cache-name }}-${{ hashFiles('**/Project.toml') }}
restore-keys: |
${{ runner.os }}-test-${{ env.cache-name }}-
${{ runner.os }}-test-
${{ runner.os }}-
- uses: julia-actions/julia-buildpkg@latest
- uses: julia-actions/julia-runtest@latest
- uses: julia-actions/[email protected]
continue-on-error: true
- uses: julia-actions/[email protected]
continue-on-error: true
files: lcov.info
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

Manifest.toml
74 changes: 72 additions & 2 deletions src/LazyArtifacts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,77 @@ using Artifacts: Artifacts,
export artifact_exists, artifact_path, artifact_meta, artifact_hash,
select_downloadable_artifacts, find_artifacts_toml, @artifact_str

# define a function for satisfying lazy Artifact downloads
using Pkg.Artifacts: ensure_artifact_installed
using Base.BinaryPlatforms: AbstractPlatform, HostPlatform
using Base: SHA1

# We are mimicking Pkg.Artifacts.ensure_artifact_installed here so that we can
# check if the artifact is already installed before loading Pkg, then load if needed.

"""
ensure_artifact_installed(name::String, artifacts_toml::String;
platform::AbstractPlatform = HostPlatform(),
pkg_uuid::Union{Base.UUID,Nothing}=nothing,
verbose::Bool = false,
quiet_download::Bool = false,
io::IO=stderr)

Ensures an artifact is installed, downloading it via the download information stored in
`artifacts_toml` if necessary. Throws an error if unable to install.
"""
function ensure_artifact_installed(name::String, artifacts_toml::String;
platform::AbstractPlatform=HostPlatform(),
pkg_uuid::Union{Base.UUID,Nothing}=nothing,
verbose::Bool=false,
quiet_download::Bool=false,
io::IO=stderr)
meta = artifact_meta(name, artifacts_toml; pkg_uuid=pkg_uuid, platform=platform)
if meta === nothing
error("Cannot locate artifact '$(name)' in '$(artifacts_toml)'")
end

return ensure_artifact_installed(name, meta, artifacts_toml;
platform, verbose, quiet_download, io)
end

function ensure_artifact_installed(name::String, meta::Dict, artifacts_toml::String;
platform::AbstractPlatform=HostPlatform(),
verbose::Bool=false,
quiet_download::Bool=false,
io::IO=stderr)

hash = SHA1(meta["git-tree-sha1"])
pkg_pkgid = Base.PkgId(Base.UUID("44cfe95a-1eb2-52ea-b672-e2afdf69b78f"), "Pkg")
if !artifact_exists(hash)
# loading Pkg is a bit slow, so we only do it if we need to
if Base.generating_output()
# if precompiling, load and use Pkg in a subprocess to avoid precompilation complexity
code = """
Pkg = Base.require_stdlib($pkg_pkgid);
Pkg.Artifacts.try_artifact_download_sources(
$(repr(name)),
Base.$(repr(hash)),
$(repr(meta)),
$(repr(artifacts_toml));
platform = Base.BinaryPlatforms.$(repr(platform)),
verbose = $(repr(verbose)),
quiet_download = $(repr(quiet_download)),
io = stderr
)
"""
out = readchomp(pipeline(`$(Base.julia_cmd()) -E $code`, stderr=io))
return Meta.parse(out)
else
Pkg = Base.require_stdlib(pkg_pkgid);
return @invokelatest Pkg.Artifacts.try_artifact_download_sources(name, hash, meta, artifacts_toml;
platform, verbose, quiet_download, io)
end
else
return artifact_path(hash)
end
end

if Base.generating_output()
precompile(Tuple{typeof(Artifacts._artifact_str), Module, String, Base.SubString{String}, String, Base.Dict{String, Any}, Base.SHA1, Base.BinaryPlatforms.Platform, Base.Val{LazyArtifacts}})
end

end
Loading