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

If Pkg.dir() is deprecated, what is its replacement? #417

Closed
tkluck opened this issue Jun 21, 2018 · 19 comments
Closed

If Pkg.dir() is deprecated, what is its replacement? #417

tkluck opened this issue Jun 21, 2018 · 19 comments

Comments

@tkluck
Copy link

tkluck commented Jun 21, 2018

The function Pkg.dir() gives the following warning:

Warning: Pkg.dir is only kept for legacy CI script reasons

which I think means that it is deprecated. I currently use it to pass the right library environment to a non-julia subprocess (source). What's the best way to get the same result?

I'll be happy to submit a pull request to document the alternative, hopefully in the warning itself.

@tkluck
Copy link
Author

tkluck commented Jun 21, 2018

FWIW, here's what I used previously:

tkluck/StatProfilerHTML.jl@df697a1

I thought Pkg.dir() was a marked improvement!

@KristofferC
Copy link
Member

KristofferC commented Jun 21, 2018

Use relative paths (with e.g. @__DIR__). Pkg.dir is definitely the wrong thing to use because it assumed that the package is installed in .julia/v0.x which might not be true at all.

tkluck added a commit to tkluck/StatProfilerHTML.jl that referenced this issue Jun 21, 2018
This function has been deprecated in Pkg3. See here for
the discussion:

JuliaLang/Pkg.jl#417

This reverts commit df697a1.
@tkluck
Copy link
Author

tkluck commented Jun 21, 2018

Thanks @KristofferC! I just pushed tkluck/StatProfilerHTML.jl@6eb9335 by your recommendation.

Pkg.dir is definitely the wrong thing to use because it assumed that the package is installed in .julia/v0.x which might not be true at all.

Is that a necessary limitation for this function? Couldn't Pkg.dir theoretically use the same resolution logic as what using and import use? (Looking at the source, I can't really decide whether that isn't what it does.)

@KristofferC
Copy link
Member

It was a limitation with the old function, but it could indeed support showing the actual location (and that is actually what the deprecation sneakily uses).

JuliaLang/julia#27592 should be relevant.

@tpapp
Copy link
Contributor

tpapp commented Jun 22, 2018

Another use case I had for Pkg.dir is

cd(Pkg.dir("somepkg", "test"))

so that I could step through a runtest.jl with includes interactively.

Is there a suggested replacement for this?

@KristofferC
Copy link
Member

Until JuliaLang/julia#27592 is fixed you can use something like

julia> pkgdir(pkg::String) = abspath(joinpath(dirname(Base.find_package(pkg)), ".."))
pkgdir (generic function with 1 method)

julia> pkgdir("Example")
"/Users/kristoffer/.julia/packages/Example/kH44/"

But what package julia will load is completely independent of the package manager which is why I feel this should maybe be a Base API.

@fredrikekre
Copy link
Member

For most uses I recommend trying to rewrite the code to use @__DIR__, but we now also have Base.pathof(::Module) if need be.

@tpapp
Copy link
Contributor

tpapp commented Sep 4, 2018

Not opening another issue, but I am not sure how to determine where the coverage files ended up after a

Pkg.test("SomePkg"; coverage = true)

using the official API. Base.pathof would assume that I loaded the module, right?

@fredrikekre
Copy link
Member

fredrikekre commented Sep 4, 2018

For Coverage submission on CI? You don't need the cd call there anyway, we are already in the root of the package.

@tpapp
Copy link
Contributor

tpapp commented Sep 4, 2018

No, for LocalCoverage.jl. I want to know where the files are so I can run genhtml etc on them.

I think this is a reasonable use case. Coverage may be run outside CI, and one may want to know where the output is for processing.

@tpapp
Copy link
Contributor

tpapp commented Sep 4, 2018

Also, if there is no immediate fix, please reopen the issue or let me know if I should create another one.

@fredrikekre
Copy link
Member

Why can't you load it? You can also use Base.find_package

@fredrikekre
Copy link
Member

fredrikekre commented Sep 4, 2018

Another option is to change your API, and make it generate_coverage(::Module) instead. That is what we have done with DocumenterTools.

@tpapp
Copy link
Contributor

tpapp commented Sep 4, 2018

Why can't you load it?

My understanding is that it would be a global side-effect, which I would want to avoid in a function.

You can also use Base.find_package

A comment above suggests that it may not be the right solution, did that change?

In any case, I think my issue boils down to the following: Pkg.test(...; coverage = true) generates files as an intended side effect, but it is not possible to find out where they are via the official API.

@KristofferC
Copy link
Member

That is pretty much #624. We could have something like activate --target=... that generates the new files and keeps you in that environment (so you can do e.g. include("test/runtests.jl").

@tpapp
Copy link
Contributor

tpapp commented Sep 4, 2018

I thought of reworking the API of LocalCoverage.jl to use the active project instead (if any), but I am not sure how to hook into that either. I could proceed from Pkg.Types.Context().env.project_file, but that sounds like an even more horrid hack.

@KristofferC
Copy link
Member

JuliaLang/julia#29030

@tpapp
Copy link
Contributor

tpapp commented Sep 4, 2018

Thanks. Is there a way to query if the result of Base.active_project() is something I can run pkg"test" on?

@KristofferC
Copy link
Member

KristofferC commented Sep 4, 2018

If it has a name = ... in the TOML file and if test/runtests.jl exists.

Could probably do it like

function can_run_test()
    p = Base.active_project()
    try Pkg.Types.read_package(p)
    catch
        return false
    end
    return isfile(joinpath(dirname(p), "test", "runtests.jl"))
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants