Path of this package?

Unless you are in Main the result given by Base.find_package("Foo") is very likely to be wrong. Well, it’s currently probably right because nobody is currently relying on the new ability to load different packages with the same name, but in the future it will suddenly break when people do that. You can’t have a function like Pkg.dir("Foo") that works correctly, you need to give it the context of where you currently are. To do this correctly, you can call Base.find_package with the current module as the first argument:

julia> Base.find_package(@__MODULE__, "BenchmarkTools")
"/Users/stefan/.julia/packages/BenchmarkTools/dtwnm/src/BenchmarkTools.jl"

We could expose that as an official API but I’m just not sure this entire approach where people want to know where random packages live is a great idea in the first place. So feel free to use this, but it’s not officially sanctioned.

2 Likes

Yeah I hear what you are saying, and I think you are probably right that it shouldn’t be exposed as an official API, it’s likely to lead to confusion. Again, most of us here are operating from the context “I only ever want to use the most recent version of a package and I always make sure they have distinct names”. Hopefully even for non-bleeding-edge users that will be true about 95% of the time, but chaos would ensue the remaining 5% of the time.

I still haven’t seen a compelling use case here so it is a real possibility that this discussion suffers from the xy problem.

A concrete use-case is when you are writing tooling. Then you need to be able to tell what exact files are being loaded etc.

That’s true, but for tooling it’s acceptable to use an internal API that might change in the future.

My point was just that it is a valid use case to want to know about absolute paths to packages.

1 Like

I have to do a lot of annoying CI/CD stuff in the next few weeks or months, I’ll let you know if I start seeing many really solid use cases for this. At present I still feel rather ignorant about what CI/CD scripts will look like, I just sort of feel like “Oh shit, I’m going to need that”.

Imagine the number of things that are running on CI that does not have a Pkg that tells you the pwd :wink:

This is what I ended up placing into my startup.jl to help navigate

cdpkg(pkg) = cd(dirname(Base.find_package(string(pkg))))

This is only meant to make my REPL workflow faster, not for use in packages themselves, only to help navigate and edit them faster, since I use ;vim from inside the julia REPL, additionally you can define

macro cdpkg(pkg)
    cdpkg(pkg)
    return nothing
end

Then you can navigate like this @cdpkg ModuleName (less parenthesis hahahahaha)

Not sure when pkgdir() was first introduced, but as of Julia 1.7, I think a better method to address the OP’s original question is to call pkgdir(@__MODULE__) inside the module of the package being developed. This returns the root directory of the package.

You can further specify the relative path to the root directory as the second argument of pkgdir(). For example, pkgdir(@__MODULE__, "src") returns the src/ directory of the package.

3 Likes