Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add pathof(::Module) #28310

Merged
merged 10 commits into from
Jul 31, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -545,8 +545,7 @@ Library improvements
* `Char` is now a subtype of `AbstractChar`, and most of the functions that
take character arguments now accept any `AbstractChar` ([#26286]).

* `abspath(module, paths...)` returns the path a module was imported
from ([#28310]).
* `abspath(module)` returns the path a module was imported from ([#28310]).

* `bytes2hex` now accepts an optional `io` argument to output to a hexadecimal stream
without allocating a `String` first ([#27121]).
Expand Down
16 changes: 4 additions & 12 deletions base/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -250,23 +250,15 @@ end
locate_package(::Nothing) = nothing

"""
abspath(m::Module, paths::AbstractString...)
abspath(m::Module)

Return the path of `m.jl` file that was used to `import` module `m`,
or `nothing` if `m` was not imported from a package. The optional
`paths` arguments are appended to `abspath(m)`, equivalent to
[`joinpath`](@ref)`(abspath(m), paths...)`.

Note that the source directory containing the module file can be
obtained by `abspath(m, "..")`, and the parent package directory
is `abspath(m, "..", "..")`.
or `nothing` if `m` was not imported from a package.
"""
function Base.Filesystem.abspath(m::Module, paths::AbstractString...)
function Base.Filesystem.abspath(m::Module)
pkgid = get(Base.module_keys, m, nothing)
pkgid === nothing && return nothing
path = Base.locate_package(pkgid)
path === nothing && return nothing
return normpath(abspath(path, paths...))
return normpath(Base.locate_package(pkgid))
end

## generic project & manifest API ##
Expand Down
2 changes: 1 addition & 1 deletion stdlib/Pkg/src/API.jl
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ function clone(url::String, name::String = "")
end

function dir(pkg::String, paths::AbstractString...)
@warn "`Pkg.dir(pkgname, paths...)` is deprecated; instead, do `import $pkg; abspath($pkg, \"..\", \"..\", paths...)`." maxlog=1
@warn "`Pkg.dir(pkgname, paths...)` is deprecated; instead, do `import $pkg; joinpath(dirname(abspath($pkg)), \"..\", paths...)`." maxlog=1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the dirname call you don't want the .. anymore.

Copy link
Member Author

@stevengj stevengj Jul 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You still need .., because dirname will return the src directory, while Pkg.dir returns the parent directory.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, gotcha.

pkgid = Base.identify_package(pkg)
pkgid === nothing && return nothing
path = Base.locate_package(pkgid)
Expand Down
4 changes: 1 addition & 3 deletions test/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,7 @@ module NotPkgModule; end
@test Foo.which == "path"

@testset "abspath" begin
p = normpath(abspath(@__DIR__, "project/deps/Foo1/src/Foo.jl"))
@test abspath(Foo) == p
@test abspath(Foo, "..", "blah") == joinpath(dirname(p), "blah")
@test abspath(Foo) == normpath(abspath(@__DIR__, "project/deps/Foo1/src/Foo.jl"))
@test abspath(NotPkgModule) === nothing
end

Expand Down