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 all commits
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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,8 @@ Library improvements
* `Char` is now a subtype of `AbstractChar`, and most of the functions that
take character arguments now accept any `AbstractChar` ([#26286]).

* `pathof(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
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,7 @@ export
methods,
nameof,
parentmodule,
pathof,
names,
which,
@isdefined,
Expand Down
12 changes: 12 additions & 0 deletions base/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,18 @@ function locate_package(pkg::PkgId)::Union{Nothing,String}
end
locate_package(::Nothing) = nothing

"""
pathof(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.
"""
function pathof(m::Module)
pkgid = get(Base.module_keys, m, nothing)
pkgid === nothing && return nothing
return Base.locate_package(pkgid)
end

## generic project & manifest API ##

const project_names = ("JuliaProject.toml", "Project.toml")
Expand Down
1 change: 1 addition & 0 deletions doc/src/base/base.md
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ Base.AsyncCondition(::Function)
```@docs
Base.nameof(::Module)
Base.parentmodule
Base.pathof(::Module)
Base.moduleroot
Base.@__MODULE__
Base.fullname
Expand Down
10 changes: 5 additions & 5 deletions stdlib/Pkg/src/API.jl
Original file line number Diff line number Diff line change
Expand Up @@ -429,13 +429,13 @@ function clone(url::String, name::String = "")
develop(ctx, [parse_package(url)])
end

function dir(pkg::String, paths::String...)
@warn "Pkg.dir is only kept for legacy CI script reasons" maxlog=1
function dir(pkg::String, paths::AbstractString...)
@warn "`Pkg.dir(pkgname, paths...)` is deprecated; instead, do `import $pkg; joinpath(dirname(pathof($pkg)), \"..\", paths...)`." maxlog=1
pkgid = Base.identify_package(pkg)
pkgid == nothing && return nothing
pkgid === nothing && return nothing
path = Base.locate_package(pkgid)
pkgid == nothing && return nothing
return joinpath(abspath(path, "..", "..", paths...))
path === nothing && return nothing
return abspath(path, "..", "..", paths...)
end

precompile() = precompile(Context())
Expand Down
8 changes: 8 additions & 0 deletions test/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ Base.ACTIVE_PROJECT[] = nothing
end
end

module NotPkgModule; end

@testset "project & manifest import" begin
@test !@isdefined Foo
@test !@isdefined Bar
Expand Down Expand Up @@ -254,6 +256,12 @@ end
end
end
@test Foo.which == "path"

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

end

## systematic generation of test environments ##
Expand Down