Skip to content

Commit

Permalink
Automatically add compat entries when adding deps to a package (#3732)
Browse files Browse the repository at this point in the history
* add compat entries when adding to a package

* update tests that assume there's no compat entries

* Update CHANGELOG.md

* add docs

* fix qualification

* wording tweaks

* add tests

* fix tests
  • Loading branch information
IanButterworth authored and KristofferC committed May 9, 2024
1 parent 2ae6de2 commit 22938d1
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 17 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ Pkg v1.11 Release Notes

- Pkg now obeys `[compat]` bounds for `julia` and raises an error if the version of the running Julia binary is incompatible with the bounds in `Project.toml`.
Pkg has always obeyed this compat when working with Registry packages. This change affects mostly local packages. ([#3526])
- `pkg> add` and `Pkg.add` will now add compat entries for new direct dependencies if the active environment is a
package (has a `name` and `uuid` entry) ([#3732])

Pkg v1.10 Release Notes
=======================
Expand Down
19 changes: 16 additions & 3 deletions src/Operations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1390,15 +1390,28 @@ function add(ctx::Context, pkgs::Vector{PackageSpec}, new_git=Set{UUID}();
end
foreach(pkg -> ctx.env.project.deps[pkg.name] = pkg.uuid, pkgs) # update set of deps
# resolve
pkgs, deps_map = _resolve(ctx.io, ctx.env, ctx.registries, pkgs, preserve, ctx.julia_version)
update_manifest!(ctx.env, pkgs, deps_map, ctx.julia_version)
man_pkgs, deps_map = _resolve(ctx.io, ctx.env, ctx.registries, pkgs, preserve, ctx.julia_version)
update_manifest!(ctx.env, man_pkgs, deps_map, ctx.julia_version)
new_apply = download_source(ctx)
fixup_ext!(ctx.env, pkgs)
fixup_ext!(ctx.env, man_pkgs)

# After downloading resolutionary packages, search for (Julia)Artifacts.toml files
# and ensure they are all downloaded and unpacked as well:
download_artifacts(ctx.env, platform=platform, julia_version=ctx.julia_version, io=ctx.io)

# if env is a package add compat entries
if ctx.env.project.name !== nothing && ctx.env.project.uuid !== nothing
compat_names = String[]
for pkg in pkgs
haskey(ctx.env.project.compat, pkg.name) && continue
pkgversion = Base.thispatch(ctx.env.manifest[pkg.uuid].version)
set_compat(ctx.env.project, pkg.name, string(pkgversion))
push!(compat_names, pkg.name)
end
printpkgstyle(ctx.io, :Compat, """entries added for $(join(compat_names, ", "))""")
end
record_project_hash(ctx.env) # compat entries changed the hash after it was last recorded in update_manifest!

write_env(ctx.env) # write env before building
show_update(ctx.env, ctx.registries; io=ctx.io)
build_versions(ctx, union(new_apply, new_git))
Expand Down
3 changes: 3 additions & 0 deletions src/Pkg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ Add a package to the current project. This package will be available by using th
`import` and `using` keywords in the Julia REPL, and if the current project is
a package, also inside that package.
If the active environment is a package (the Project has both `name` and `uuid` fields) compat entries will be
added automatically with a lower bound of the added version.
## Resolution Tiers
`Pkg` resolves the set of packages in your environment using a tiered algorithm.
The `preserve` keyword argument allows you to key into a specific tier in the resolve algorithm.
Expand Down
3 changes: 3 additions & 0 deletions src/REPLMode/command_declarations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ are of the form `@1`, `@1.2` or `@1.2.3`, allowing any version with a prefix
that matches, or ranges thereof, such as `@1.2-3.4.5`. A git revision can be
specified by `#branch` or `#commit`.
If the active environment is a package (the Project has both `name` and `uuid` fields) compat entries will be
added automatically with a lower bound of the added version.
If a local path is used as an argument to `add`, the path needs to be a git repository.
The project will then track that git repository just like it would track a remote repository online.
If the package is not located at the top of the git repository, a subdirectory can be specified with
Expand Down
27 changes: 21 additions & 6 deletions test/new.jl
Original file line number Diff line number Diff line change
Expand Up @@ -577,10 +577,13 @@ end
end
# Double add should not change state, this would be an unnecessary change.
isolate(loaded_depot=true) do
@test !haskey(Pkg.Types.Context().env.project.compat, "Example")
Pkg.add(name="Example", version="0.3.0")
@test Pkg.dependencies()[exuuid].version == v"0.3.0"
@test !haskey(Pkg.Types.Context().env.project.compat, "Example")
Pkg.add("Example")
@test Pkg.dependencies()[exuuid].version == v"0.3.0"
@test !haskey(Pkg.Types.Context().env.project.compat, "Example")
end
# Adding a new package should not alter the version of existing packages.
isolate(loaded_depot=true) do
Expand Down Expand Up @@ -808,6 +811,23 @@ end
Pkg.add(name="libpng_jll", version=v"1.6.37+5")
@test Pkg.dependencies()[pngjll_uuid].version == v"1.6.37+5"
end
# Adding a new package to a package should add compat entries
isolate(loaded_depot=true) do
mktempdir() do tempdir
Pkg.activate(tempdir)
mkpath(joinpath(tempdir, "src"))
touch(joinpath(tempdir, "src", "Foo.jl"))
ctx = Pkg.Types.Context()
ctx.env.project.name = "Foo"
ctx.env.project.uuid = UUIDs.UUID(0)
Pkg.Types.write_project(ctx.env)
Pkg.add(name="Example", version="0.3.0")
@test Pkg.dependencies()[exuuid].version == v"0.3.0"
@test Pkg.Types.Context().env.project.compat["Example"] == Pkg.Types.Compat(Pkg.Types.VersionSpec("0.3"), "0.3.0")
Pkg.add(name="Example", version="0.3.1")
@test Pkg.Types.Context().env.project.compat["Example"] == Pkg.Types.Compat(Pkg.Types.VersionSpec("0.3"), "0.3.0")
end
end
end # withenv
end

Expand Down Expand Up @@ -2428,12 +2448,7 @@ end
Pkg.status(; outdated=true, io=io)
str = String(take!(io))
@test occursin(Regex("\\s*\\[7876af07\\] Example\\s*v0.4.0\\s*\\(<v$v\\)"), str)
open(Base.active_project(), "a") do io
write(io, """
[compat]
Example = "0.4.1"
""")
end
Pkg.compat("Example", "0.4.1")
Pkg.status(; outdated=true, io=io)
str = String(take!(io))
@test occursin(Regex("\\s*\\[7876af07\\] Example\\s*v0.4.0\\s*\\[<v0.4.1\\], \\(<v$v\\)"), str)
Expand Down
9 changes: 1 addition & 8 deletions test/repl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -483,14 +483,7 @@ temp_pkg_dir() do project_path; cd(project_path) do
json_uuid = Pkg.project().dependencies["JSON"]
current_json = Pkg.dependencies()[json_uuid].version
old_project = read("Project.toml", String)
open("Project.toml"; append=true) do io
print(io, """
[compat]
JSON = "0.18.0"
"""
)
end
Pkg.compat("JSON", "0.18.0")
pkg"up"
@test Pkg.dependencies()[json_uuid].version.minor == 18
write("Project.toml", old_project)
Expand Down

0 comments on commit 22938d1

Please sign in to comment.