Skip to content

Commit b51b809

Browse files
authored
Warn if an already loaded package is attempted to be loaded from a different path (#44329)
1 parent 52ff558 commit b51b809

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

base/loading.jl

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,6 +1502,7 @@ function _tryrequire_from_serialized(modkey::PkgId, build_id::UInt128)
15021502
assert_havelock(require_lock)
15031503
loaded = nothing
15041504
if root_module_exists(modkey)
1505+
warn_if_already_loaded_different(modkey)
15051506
loaded = root_module(modkey)
15061507
else
15071508
loaded = start_loading(modkey)
@@ -1532,6 +1533,7 @@ function _tryrequire_from_serialized(modkey::PkgId, path::String, ocachepath::Un
15321533
assert_havelock(require_lock)
15331534
loaded = nothing
15341535
if root_module_exists(modkey)
1536+
warn_if_already_loaded_different(modkey)
15351537
loaded = root_module(modkey)
15361538
else
15371539
loaded = start_loading(modkey)
@@ -1975,11 +1977,50 @@ function __require_prelocked(uuidkey::PkgId, env=nothing)
19751977
# After successfully loading, notify downstream consumers
19761978
run_package_callbacks(uuidkey)
19771979
else
1980+
warn_if_already_loaded_different(uuidkey)
19781981
newm = root_module(uuidkey)
19791982
end
19801983
return newm
19811984
end
19821985

1986+
const already_warned_path_change_pkgs = Set{UUID}()
1987+
# warns if the loaded version of a module is different to the one that locate_package wants to load
1988+
function warn_if_already_loaded_different(uuidkey::PkgId)
1989+
uuidkey.uuid already_warned_path_change_pkgs && return
1990+
pkgorig = get(pkgorigins, uuidkey, nothing)
1991+
if pkgorig !== nothing && pkgorig.path !== nothing
1992+
new_path = locate_package(uuidkey)
1993+
if !samefile(fixup_stdlib_path(pkgorig.path), new_path)
1994+
if isnothing(pkgorig.version)
1995+
v = get_pkgversion_from_path(dirname(dirname(pkgorig.path)))
1996+
cur_vstr = isnothing(v) ? "" : "v$v "
1997+
else
1998+
cur_vstr = "v$v "
1999+
end
2000+
new_v = get_pkgversion_from_path(dirname(dirname(new_path)))
2001+
new_vstr = isnothing(new_v) ? "" : "v$new_v "
2002+
warnstr = """
2003+
$uuidkey is already loaded from a different path:
2004+
loaded: $cur_vstr$(repr(pkgorig.path))
2005+
requested: $new_vstr$(repr(new_path))
2006+
"""
2007+
if isempty(already_warned_path_change_pkgs)
2008+
warnstr *= """
2009+
This might indicate a change of environment has happened between package loads and can mean that
2010+
incompatible packages have been loaded"""
2011+
if JLOptions().startupfile < 2 #(0 = auto, 1 = yes)
2012+
warnstr *= """. If this happened due to a startup.jl consider starting julia
2013+
directly in the project via the `--project` arg."""
2014+
else
2015+
warnstr *= "."
2016+
end
2017+
end
2018+
@warn warnstr
2019+
push!(already_warned_path_change_pkgs, uuidkey.uuid)
2020+
end
2021+
end
2022+
end
2023+
19832024
mutable struct PkgOrigin
19842025
path::Union{String,Nothing}
19852026
cachepath::Union{String,Nothing}

0 commit comments

Comments
 (0)