perhaps should add some info on dirname(@__FILE__)
in:
http://docs.julialang.org/en/release-0.4/stdlib/pkg/#Base.Pkg.dir
so devs are more likely to make packages that work under a custom LOAD_PATH
https://github.com/JuliaLang/julia/issues/2295
https://github.com/randyzwitch/NoveltyColors.jl/issues/5
https://github.com/timothyrenner/ColorBrewer.jl/issues/10
fwiw, there's also the convenience function Base.source_dir()
that gives the same result
didn't seem official in 2013
https://groups.google.com/forum/#!msg/julia-users/cgX6Gv2lP7U/mx6l7A2bLl4J
Since Base.source_dir()
is basically the same thing as the dirname(@__FILE__)
idiom but looks at lot nicer and less "magical" I would suggest to make it official and exported.
See also the discussion in #8679 and #12120.
Base.source_dir()
doesn't just look less magical than dirname(@__FILE__)
. It is actually less magical, and thus easier to misuse. If you call it from anywhere but top-level code, it is unlikely to do what you expect. See https://github.com/JuliaLang/julia/issues/4530#issuecomment-26360609 where I convinced @StefanKarpinski to implement @__FILE__
instead of exporting Base.source_path()
. (If people want @__DIR__
, that's fine with me, though.)
@simonster point taken.
Is there a case where one cannot use dirname(@__FILE__)
and must use Pkg.dir(name)
instead? If not the latter should be deprecated and if yes the difference should be documented.
If a package wants to access a different package's directory, then dirname(@__FILE__)
will not work, but Base.find_in_path(name)
might be a better solution than Pkg.dir(name)
.
Or have the package that needs its location used elsewhere define a module-level constant with the value of dirname(@__FILE__)
.
Accessing another package's files without the paths of those being part of the contract (the API) seems like asking for trouble.
Most helpful comment
Since
Base.source_dir()
is basically the same thing as thedirname(@__FILE__)
idiom but looks at lot nicer and less "magical" I would suggest to make it official and exported.