How to add a package but only if it is not already there?

So I include a snippet over in this other thread I linked above that does exactly what you’re asking for. I agree with many of the poster’s above me that it might not be a good idea, though. Use at your own risk.

2 Likes

Sorry to come back to this, but here is what just happened on my system:

(v1.2) pkg> add Coverage
  Updating registry at `~/.julia/registries/General`
  Updating git-repo `https://github.com/JuliaRegistries/General.git`
 Resolving package versions...
  Updating `~/.julia/environments/v1.2/Project.toml`
  [a2441757] + Coverage v0.9.3
  [d254efa0] ↑ PkgSkeleton v0.2.0 [`~/projets/git/Julia/PkgSkeleton.jl`] ⇒ v0.3.1 [`~/projets/git/Julia/PkgSkeleton.jl`]
  Updating `~/.julia/environments/v1.2/Manifest.toml`
  [a2441757] + Coverage v0.9.3
  [d254efa0] ↑ PkgSkeleton v0.2.0 [`~/projets/git/Julia/PkgSkeleton.jl`] ⇒ v0.3.1 [`~/projets/git/Julia/PkgSkeleton.jl`]

I should add that Coverage was already installed on my system, although not in the “home” (v1.2) environment. AFAIK these two packages don’t depend on one another, and it does not look like a common dependency of both was upgraded, which in turn would have needed PkgSkeleton to be upgraded as well for compatibility reasons. What It looks like (to me !) is Pkg is updating the registry and, seeing that a new version of PkgSkeleton is available, upgrades it for me (which is nice, although I didn’t ask for it).

I’m not sure what happened in this case, but it is such experiences that made me think that Pkg automatically performed upgrades in some cases where it was not strictly needed. This is with v1.2 though; maybe things have changed recently…

Yea, that’s what happened. Anything in the manifest (that is not in the project) is free game for Pkg, but if you implement what we discussed earlier, then this wouldn’t happen.

Also note that in Julia 1.4 this would not happen (in most cases) since Pkg uses a “tiered resolver”, see e.g. https://julialang.github.io/Pkg.jl/dev/api/#Pkg.add

1 Like