Skip to content

Commit

Permalink
Add a hint when failing to import a module in cases where a user migh…
Browse files Browse the repository at this point in the history
…t want to use a relative import (JuliaLang#40984)

* Add a hint when failing to import a module in cases where a user might want to use a relative import
  • Loading branch information
KristofferC authored Sep 30, 2021
1 parent 00a602b commit 203d234
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 3 deletions.
16 changes: 13 additions & 3 deletions base/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -975,10 +975,20 @@ function require(into::Module, mod::Symbol)
if uuidkey === nothing
where = PkgId(into)
if where.uuid === nothing
hint, dots = begin
if isdefined(into, mod) && getfield(into, mod) isa Module
true, "."
elseif isdefined(parentmodule(into), mod) && getfield(parentmodule(into), mod) isa Module
true, ".."
else
false, ""
end
end
hint_message = hint ? ", maybe you meant `import/using $(dots)$(mod)`" : ""
start_sentence = hint ? "Otherwise, run" : "Run"
throw(ArgumentError("""
Package $mod not found in current path:
- Run `import Pkg; Pkg.add($(repr(String(mod))))` to install the $mod package.
"""))
Package $mod not found in current path$hint_message.
- $start_sentence `import Pkg; Pkg.add($(repr(String(mod))))` to install the $mod package."""))
else
s = """
Package $(where.name) does not have $mod in its dependencies:
Expand Down
83 changes: 83 additions & 0 deletions test/errorshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -803,3 +803,86 @@ if Sys.isapple() || (Sys.islinux() && Sys.ARCH === :x86_64)
end
end
end # Sys.isapple()

@testset "error message hints relative modules #40959" begin
m = Module()
expr = :(module Foo
module Bar
end

using Bar
end)
try
Base.eval(m, expr)
catch err
err_str = sprint(showerror, err)
@test contains(err_str, "maybe you meant `import/using .Bar`")
end

m = Module()
expr = :(module Foo
Bar = 3

using Bar
end)
try
Base.eval(m, expr)
catch err
err_str = sprint(showerror, err)
@test !contains(err_str, "maybe you meant `import/using .Bar`")
end

m = Module()
expr = :(module Foo
using Bar
end)
try
Base.eval(m, expr)
catch err
err_str = sprint(showerror, err)
@test !contains(err_str, "maybe you meant `import/using .Bar`")
end

m = Module()
expr = :(module Foo
module Bar end
module Buzz
using Bar
end
end)
try
Base.eval(m, expr)
catch err
err_str = sprint(showerror, err)
@test contains(err_str, "maybe you meant `import/using ..Bar`")
end

m = Module()
expr = :(module Foo
Bar = 3
module Buzz
using Bar
end
end)
try
Base.eval(m, expr)
catch err
err_str = sprint(showerror, err)
@test !contains(err_str, "maybe you meant `import/using ..Bar`")
end

m = Module()
expr = :(module Foo
module Bar end
module Buzz
module Bar end
using Bar
end
end)
try
Base.eval(m, expr)
catch err
err_str = sprint(showerror, err)
@test contains(err_str, "maybe you meant `import/using .Bar`")
end
end

0 comments on commit 203d234

Please sign in to comment.