Skip to content

Commit

Permalink
relocation: a fix for @depot tag inserting and extra tests (JuliaLa…
Browse files Browse the repository at this point in the history
…ng#51920)

JuliaLang#51892 caused depots to be skipped when inserting the `@depot` tags,
because it assumed that `isdirpath(path) == false` means that `path` is
not a directory.
This is fixed here while preserving the path normalization introduced
there.

Furthermore, this adds tests as requested here
JuliaLang#51892 (comment)
  • Loading branch information
fatteneder committed Oct 31, 2023
1 parent aeb4e7d commit 4ce1c81
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 12 deletions.
23 changes: 13 additions & 10 deletions base/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2600,13 +2600,13 @@ end

function replace_depot_path(path::AbstractString)
for depot in DEPOT_PATH
# Skip depots that don't exist
if !isdirpath(depot)
continue
end
!isdir(depot) && continue

# Strip extraneous pathseps through normalization.
depot = dirname(depot)
if isdirpath(depot)
depot = dirname(depot)
end

if startswith(path, depot)
path = replace(path, depot => "@depot"; count=1)
break
Expand All @@ -2615,18 +2615,21 @@ function replace_depot_path(path::AbstractString)
return path
end

function restore_depot_path(path::AbstractString, depot::AbstractString)
replace(path, r"^@depot" => depot; count=1)
end

# Find depot in DEPOT_PATH for which all @depot tags from the `includes`
# can be replaced so that they point to a file on disk each.
# Return nothing when no depot matched.
function resolve_depot(includes)
function resolve_depot(includes::Union{AbstractVector,AbstractSet})
if any(includes) do inc
!startswith(inc, "@depot")
end
return :missing_depot_tag
end
for depot in DEPOT_PATH
if all(includes) do inc
isfile(replace(inc, r"^@depot" => depot; count=1))
isfile(restore_depot_path(inc, depot))
end
return depot
end
Expand Down Expand Up @@ -2732,7 +2735,7 @@ function parse_cache_header(f::IO, cachefile::AbstractString)
@debug "Missing @depot tag for include dependencies in cache file $cachefile."
else
for inc in includes
inc.filename = replace(inc.filename, r"^@depot" => depot; count=1)
inc.filename = restore_depot_path(inc.filename, depot)
end
end
includes_srcfiles_only = includes[keepidx]
Expand Down Expand Up @@ -2795,7 +2798,7 @@ function _read_dependency_src(io::IO, filename::AbstractString, includes::Vector
fn = if !startswith(depotfn, "@depot")
depotfn
else
basefn = replace(depotfn, r"^@depot" => "")
basefn = restore_depot_path(depotfn, "")
idx = findfirst(includes) do inc
endswith(inc.filename, basefn)
end
Expand Down
47 changes: 45 additions & 2 deletions test/relocatedepot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,51 @@ end

if !test_relocated_depot

@testset "insert @depot tag in path" begin

test_harness() do
mktempdir() do dir
pushfirst!(DEPOT_PATH, dir)
path = dir*dir
@test Base.replace_depot_path(path) == "@depot"*dir
end
end

test_harness() do
mktempdir() do dir
pushfirst!(DEPOT_PATH, dir)
path = joinpath(dir, "foo")
if isdirpath(DEPOT_PATH[1])
DEPOT_PATH[1] = dirname(DEPOT_PATH[1]) # strip trailing pathsep
end
tag = joinpath("@depot", "") # append a pathsep
@test startswith(Base.replace_depot_path(path), tag)
DEPOT_PATH[1] = joinpath(DEPOT_PATH[1], "") # append a pathsep
@test startswith(Base.replace_depot_path(path), tag)
popfirst!(DEPOT_PATH)
@test !startswith(Base.replace_depot_path(path), tag)
end
end

end

@testset "restore path from @depot tag" begin

tmp = tempdir()

path = joinpath("@depot", "foo", "bar")
tmppath = joinpath(tmp, "foo", "bar")
@test Base.restore_depot_path(path, tmp) == tmppath

path = joinpath("no@depot", "foo", "bar")
@test Base.restore_depot_path(path, tmp) == path

path = joinpath("@depot", "foo", "bar\n", "@depot", "foo")
tmppath = joinpath(tmp, "foo", "bar\n", "@depot", "foo")
@test Base.restore_depot_path(path, tmp) == tmppath

end

@testset "precompile RelocationTestPkg1" begin
pkgname = "RelocationTestPkg1"
test_harness() do
Expand All @@ -44,7 +89,6 @@ if !test_relocated_depot
@test Base.isprecompiled(pkg) == false
touch(joinpath(@__DIR__, pkgname, "src", "foo.txt"))
Base.require(pkg) # precompile
@info "SERS OIDA"
@test Base.isprecompiled(pkg, ignore_loaded=true) == true
end
end
Expand All @@ -59,7 +103,6 @@ else
for pkgname in ("RelocationTestPkg1", "RelocationTestPkg2")
pkg = Base.identify_package(pkgname)
cachefile = only(Base.find_all_in_cache_path(pkg))
@info cachefile
@test_throws ArgumentError("""
Failed to determine depot from srctext files in cache file $cachefile.
- Make sure you have adjusted DEPOT_PATH in case you relocated depots.""") Base.isprecompiled(pkg)
Expand Down

0 comments on commit 4ce1c81

Please sign in to comment.