diff --git a/base/loading.jl b/base/loading.jl index 1bea8ee01ed87..075b3f27d5ece 100644 --- a/base/loading.jl +++ b/base/loading.jl @@ -2694,6 +2694,7 @@ function require_stdlib(package_uuidkey::PkgId, ext::Union{Nothing, String}, fro return newm end env = Sys.STDLIB + loading_started = false try depot_path = append_bundled_depot_path!(empty(DEPOT_PATH)) from_stdlib = true # set to false if `from` is a normal package so we do not want the internal loader for the extension either @@ -2718,10 +2719,11 @@ function require_stdlib(package_uuidkey::PkgId, ext::Union{Nothing, String}, fro set_pkgorigin_version_path(this_uuidkey, sourcepath) newm = start_loading(this_uuidkey, UInt128(0), true) newm === nothing || return newm + loading_started = true newm = _require_search_from_serialized(this_uuidkey, sourcepath, UInt128(0), false; DEPOT_PATH=depot_path) end finally - end_loading(this_uuidkey, newm) + loading_started && end_loading(this_uuidkey, newm) end if newm isa Module # After successfully loading, notify downstream consumers diff --git a/test/loading.jl b/test/loading.jl index 47ef536d8f7aa..c96bf49c00edc 100644 --- a/test/loading.jl +++ b/test/loading.jl @@ -1639,3 +1639,22 @@ end end end end +@testset "require_stdlib extension with non-stdlib from module" begin + # Test that require_stdlib doesn't error when called with an extension + # and the `from` module is not from the bundled depot (from_stdlib=false path) + # This is a regression test for https://github.com/JuliaLang/julia/issues/60392 + cmd = `$(Base.julia_cmd()) --startup-file=no -e ' + Pkg_pkgid = Base.PkgId(Base.UUID("44cfe95a-1eb2-52ea-b672-e2afdf69b78f"), "Pkg") + REPLExt_pkgid = Base.PkgId(Base.uuid5(Pkg_pkgid.uuid, "REPLExt"), "REPLExt") + # Create and register a fake REPL module to simulate a non-stdlib module being loaded + # This triggers the from_stdlib=false path since the fake module is not from the bundled depot + FakeREPL = Module(:REPL) + FakeREPL_pkgid = Base.PkgId(Base.UUID("3fa0cd96-eef1-5676-8a61-b3b8758bbffb"), "REPL") + Base.register_root_module(FakeREPL) + # This should not throw a KeyError from end_loading + Base.require_stdlib(Pkg_pkgid, "REPLExt", FakeREPL) + # Verify the extension was loaded + Base.maybe_root_module(REPLExt_pkgid) isa Module || error("Something is wrong") + '` + @test success(cmd) +end \ No newline at end of file