Pkg.jl icon indicating copy to clipboard operation
Pkg.jl copied to clipboard

`Pkg.add` operates in-place on the arguments without notice

Open giordano opened this issue 2 years ago • 4 comments

julia> using Pkg

julia> Pkg.activate(; temp=true, io=devnull)

julia> spec = Pkg.PackageSpec(; name = "Example", version = Pkg.Types.VersionSpec("0.5.3"));

julia> Pkg.add(Pkg.Types.Context(), [spec]; io=devnull)

julia> spec
PackageSpec(
  name = Example
  uuid = 7876af07-990d-54b4-ab0e-23690620f79a
  tree_hash = 46e44e869b4d90b96bd8ed1fdcf32244fddfb6cc
  version = v"0.5.3"
)

spec changed after the call to Pkg.add, it acquired the tree hash and the UUID. One could claim this method is internal, but this still violates the convention of appending ! to a function name when arguments are modified in-place.

giordano avatar Jun 16 '22 21:06 giordano

https://github.com/JuliaLang/Pkg.jl/blob/423343402943074825392faa8b04a6d353204689/src/API.jl#L154

KristofferC avatar Jun 16 '22 21:06 KristofferC

That's not the same method I used above.

giordano avatar Jun 16 '22 21:06 giordano

The rub again is that the first arg Context() methods aren't part of the public API. And adding them was rejected https://github.com/JuliaLang/Pkg.jl/pull/2952

So seems like you need to deepcopy before passing the arg in to use this internal method

IanButterworth avatar Jun 17 '22 04:06 IanButterworth

The rub again is that the first arg Context() methods aren't part of the public API.

In fact I said

One could claim this method is internal, but this still violates the convention of appending ! to a function name when arguments are modified in-place.

So seems like you need to deepcopy before passing the arg in to use this internal method

Which is what I had done before opening the issue: https://github.com/JuliaPackaging/BinaryBuilderBase.jl/pull/250. But discovering that a method not following the naming convention was modifying my specs, just to make me run into another Pkg bug #3113, wasn't exactly fun

giordano avatar Jun 17 '22 07:06 giordano