Some uses of `@__DIR__` should be relocatable.
~The proposed use I have for this is for when packages are looking for their Artifacts.toml file, they have to annoying things, and non relocatable ones even more annyoingly. (Maybe we need to add something like @__PKGDIR that does the relocatable thing)~
Currently @__DIR__ bakes in the current path of the whenever it's called, but for this it's usually the path of the src directory of a package. Examples of this are the Artifacts.toml file or a examples/directory, The issue arises if one tries to move that package to another place, either via PkgCompiler or via copying the depot around. Those baked in absolute paths mean that it's not safe for this package to be relocatable.
I don't have a clear proposal for this, but I imagine it would just be a expansion of the relocatable package image work done by @fatteneder, specially since if you get the package those files are already there, we just don't have a nice way of getting to them.
I agree with @gbaraldi that this is a serious limitation. It means that most uses of @__DIR__ cause your package to become immediately non-relocatable.
There are two common exceptions that are relocatable:
- the
@__DIR__path is used to access files entirely at parse-time / compile-time - e.g. JLL's do this: https://github.com/JuliaLang/julia/blob/4793328656cc1bffaa5625a62b7cc36baa32760c/stdlib/Artifacts/src/Artifacts.jl#L691-L693 - the
@__DIR__path is never used to open/write any files at all (just printing/logging) -@errordoes this
https://github.com/JuliaLang/julia/pull/53906 added a Base.isrelocatable predicate, but it is not able to detect this problem.
The issue arises if one tries to move that package to another place, either via PkgCompiler or via copying the depot around.
There are more challenges for PackageCompiler.jl too.
It deletes any source files from its relocated "depot". So even if this is relocatable, it would need to have a way to detect a dependency on package source so that it can copy those files to make this work.
https://github.com/JuliaPackaging/RelocatableFolders.jl is a bit of a "hack" to work around this.
FWIW Pkg.jl best practices already mention that one should not assume a package to be located in a stable location. So this rules out the use of the current @__DIR__ for pkg code.
What should be done for this issue to be solved? It seems to lack a clear goal right now.
I personally would not bother with making uses of @__DIR__ relocatable. I have used things like normpath(joinpath(@__DIR__, <some relative path>)) myself before, and I tinkering with @__DIR__ would only break those uses.
Instead I would add another macro, something like @__PKGDIR__ as suggested by Gabriel initially.