diff --git a/base/loading.jl b/base/loading.jl index c6405e8c5b37e..a8e3549da5260 100644 --- a/base/loading.jl +++ b/base/loading.jl @@ -3005,7 +3005,13 @@ end return stale_cachefile(PkgId(""), UInt128(0), modpath, cachefile; ignore_loaded) end @constprop :none function stale_cachefile(modkey::PkgId, build_id::UInt128, modpath::String, cachefile::String; ignore_loaded::Bool = false) - io = open(cachefile, "r") + io = try + open(cachefile, "r") + catch ex + ex isa IOError || ex isa SystemError || rethrow() + @debug "Rejecting cache file $cachefile for $modkey because it could not be opened" isfile(cachefile) + return true + end try checksum = isvalid_cache_header(io) if iszero(checksum) diff --git a/test/precompile.jl b/test/precompile.jl index d317731b51370..0b9cc9eab3417 100644 --- a/test/precompile.jl +++ b/test/precompile.jl @@ -522,6 +522,10 @@ precompile_test_harness(false) do dir @eval using Baz @test Base.invokelatest(Baz.baz) == 1 + # should not throw if the cachefile does not exist + @test !isfile("DoesNotExist.ji") + @test Base.stale_cachefile("", "DoesNotExist.ji") === true + # Issue #12720 FooBar1_file = joinpath(dir, "FooBar1.jl") write(FooBar1_file,