Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@
replacing them (#472).

* pkgdepends now handles the case when a repository (PPM typically) serves
a source package instead of the binary
a source package instead of the binary. Such a package is put back into
the installation queue as a source package, and its (recursive) hard
dependencies are installed before it is built. If a `LinkingTo`
dependency needed for the build is missing from the installation plan,
pkgdepends now fails with an informative error, suggesting the
`include_linkingto` configuration option. The raw dependencies of each
package, including `LinkingTo`, are now also recorded in the lockfile
(https://github.com/r-lib/pak/issues/891).

* Resolution for downloads for a different platform no longer fails with a
Expand Down
16 changes: 16 additions & 0 deletions R/dep-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ make_null_deps <- function() {
)
}

lockfile_deps <- function(deps) {
if (length(deps) == 0) {
return(make_null_deps())
}
get <- function(field) {
vcapply(deps, function(d) d[[field]] %||% NA_character_)
}
data_frame(
ref = get("ref"),
type = get("type"),
package = get("package"),
op = get("op"),
version = get("version")
)
}

parse_deps <- function(deps, type) {
assert_that(length(deps) == length(type))
deps <- lapply(strsplit(deps, ","), str_trim)
Expand Down
101 changes: 88 additions & 13 deletions R/install-plan.R
Original file line number Diff line number Diff line change
Expand Up @@ -943,19 +943,7 @@ stop_task_install <- function(state, worker) {

# A source package was served instead of a binary (from PPM typically)
if (inherits(worker$result, "install_needs_build")) {
alert(
"info",
"{.pkg {pkg}} {.version {version}} was served as a source package, \\
building it from source"
)
state$plan$binary[[pkgidx]] <- FALSE
state$plan$build_done[[pkgidx]] <- FALSE
state$plan$worker_id[[pkgidx]] <- NA_character_
nc <- worker$result$needscompilation
if (!is.null(nc) && !is.na(nc)) {
state$plan$needscompilation[[pkgidx]] <- nc
}
return(state)
return(handle_install_needs_build(state, worker))
}

## TODO: make sure the install status is non-zero on exit
Expand Down Expand Up @@ -996,6 +984,93 @@ stop_task_install <- function(state, worker) {
state
}

# binary -> source fallback
# 1. need all dependencies installed first
# 2. need to restore LinkingTo dependencies as well
# 3. a LinkingTo dependency might be missing from the plan, error
# https://github.com/r-lib/pak/issues/891.

handle_install_needs_build <- function(state, worker) {
pkgidx <- worker$task$args$pkgidx
pkg <- state$plan$package[pkgidx]
version <- state$plan$version[pkgidx]

check_source_fallback_linkingto(state$plan, pkgidx)

alert(
"info",
"{.pkg {pkg}} {.version {version}} was served as a source package,
will build it from source"
)

state$plan$binary[[pkgidx]] <- FALSE
state$plan$build_done[[pkgidx]] <- FALSE
state$plan$worker_id[[pkgidx]] <- NA_character_
nc <- worker$result$needscompilation
if (!is.null(nc) && !is.na(nc)) {
state$plan$needscompilation[[pkgidx]] <- nc
}

state$plan$dependencies[[pkgidx]] <- source_deps(state$plan, pkgidx)

# Now that the package is a source package, it must wait for its complete
# (recursive) dependency tree to be installed before it can be built.
# `add_recursive_dependencies()` expands the `dependencies` of every source
# package to its recursive closure; re-running it now picks up this package
# too. It is idempotent for the packages that were already source packages.
state$plan <- add_recursive_dependencies(state$plan)
installed <- state$plan$package[state$plan$install_done]
state$plan$deps_left[[pkgidx]] <-
setdiff(state$plan$dependencies[[pkgidx]], installed)

state
}

check_source_fallback_linkingto <- function(plan, pkgidx) {
pkg <- plan$package[[pkgidx]]
deps <- plan$deps[[pkgidx]]
if (is.null(deps) || !all(c("package", "type") %in% names(deps))) {
return(invisible())
}
linkingto <- deps$package[tolower(deps$type) == "linkingto"]
linkingto <- setdiff(linkingto, c("R", base_packages()))
missing <- setdiff(linkingto, plan$package)
if (length(missing) == 0) {
return(invisible())
}
throw(pkg_error(
"Cannot install {.pkg {pkg}} from source: it was served as a source
package instead of a binary (typically by Posit Package Manager), but
{cli::qty(missing)} its {.code LinkingTo} dependenc{?y/ies}
{.pkg {missing}} {cli::qty(missing)}{?is/are} not part of the
installation plan.",
i = "{.code LinkingTo} dependencies are omitted from the plan for binary
packages, because they are not needed to install a binary.",
i = "To build {.pkg {pkg}} from source, set the
{.code pkg.include_linkingto} option or the
{.envvar PKG_INCLUDE_LINKINGTO} environment variable to {.code TRUE}
and try again, to always include {.code LinkingTo} dependencies in the
installation plan."
))
}

source_deps <- function(plan, pkgidx) {
known <- plan$dependencies[[pkgidx]]
deps <- plan$deps[[pkgidx]]
if (is.null(deps) || !all(c("package", "type") %in% names(deps))) {
return(known)
}
types <- if ("dep_types" %in% names(plan)) {
intersect(plan$dep_types[[pkgidx]], pkg_dep_types_hard())
} else {
pkg_dep_types_hard()
}
pkgs <- deps$package[tolower(deps$type) %in% tolower(types)]
pkgs <- intersect(pkgs, plan$package)
pkgs <- setdiff(pkgs, c(plan$package[[pkgidx]], "R", base_packages()))
unique(c(known, pkgs))
}

create_install_result <- function(state) {
result <- state$plan
class(result) <- c("pkginstall_result", class(result))
Expand Down
1 change: 1 addition & 0 deletions R/pkg-plan.R
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ pkgplan_init_lockfile <- function(
pkgs,
function(x) unlist(x$dependencies) %||% character()
),
deps = lapply(pkgs, function(x) lockfile_deps(x$deps)),
sources = lapply(pkgs, function(x) unlist(x$sources)),
metadata = lapply(pkgs, function(x) unlist(x$metadata)),
dep_types = lapply(pkgs, function(x) unlist(x$dep_types)),
Expand Down
1 change: 1 addition & 0 deletions R/solve.R
Original file line number Diff line number Diff line change
Expand Up @@ -1426,6 +1426,7 @@ pkgplan_export_install_plan <- function(self, private, plan_file, version) {
"direct",
"binary",
"dependencies",
"deps",
"vignettes",
"needscompilation",
"metadata",
Expand Down
3 changes: 3 additions & 0 deletions R/tojson.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ tojson <- local({
# Atomic columns are unboxed. Atomic NA values are omitted.
# List columns remove the extra wrapping list.
j_df <- function(x, opts) {
if (nrow(x) == 0L) {
return("[]")
}
sub <- unlist(comma(
lapply(seq_len(nrow(x)), function(i) {
row <- as.list(x[i, ])
Expand Down
8 changes: 6 additions & 2 deletions R/zzz-pkgdepends-config.R
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,13 @@ pkgdepends_config <- sort_by_name(list(
type = "flag",
default = FALSE,
docs = "Whether to always include `LinkingTo` dependencies in the solution
of and installation, even if they are needed because the packages
of and installation, even if they are not needed because the packages
are installed from binaries. This is sometimes useful, see e.g.
<https://github.com/r-lib/pak/issues/485> for an example use case."
<https://github.com/r-lib/pak/issues/485> for an example use case.
It is also needed if a repository (Posit Package Manager typically)
might serve a source package instead of a binary, so that the source
package can be built, see
<https://github.com/r-lib/pak/issues/891>."
),

# -----------------------------------------------------------------------
Expand Down
Binary file modified inst/docs/pak-config-docs.rds
Binary file not shown.
6 changes: 5 additions & 1 deletion man/pkg_config.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions tests/testthat/_snaps/install-plan.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@
character(0)


# handle_install_needs_build errors on missing LinkingTo dep

Code
handle_install_needs_build(state, worker)
Condition
Error:
! ! Cannot install A from source: it was served as a source package instead of a binary (typically by Posit Package Manager), but its `LinkingTo` dependency X is not part of the installation plan.
i `LinkingTo` dependencies are omitted from the plan for binary packages, because they are not needed to install a binary.
i To build A from source, set the `pkg.include_linkingto` option or the `PKG_INCLUDE_LINKINGTO` environment variable to `TRUE` and try again, to always include `LinkingTo` dependencies in the installation plan.

# ignore-build-errors parameter

Code
Expand Down
28 changes: 16 additions & 12 deletions tests/testthat/_snaps/unix/pkg-installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@
Code
lock$packages
Output
ref package version type direct binary dependencies vignettes
1 pkg1 pkg1 1.0.0 standard FALSE FALSE FALSE
2 pkg2 pkg2 1.0.0 standard FALSE FALSE pkg1 FALSE
3 pkg3 pkg3 1.0.0 standard TRUE FALSE pkg2 FALSE
needscompilation metadata.RemoteType metadata.RemotePkgRef metadata.RemoteRef
1 FALSE standard pkg1 pkg1
2 FALSE standard pkg2 pkg2
3 FALSE standard pkg3 pkg3
metadata.RemoteRepos metadata.RemotePkgPlatform metadata.RemoteSha
1 http://127.0.0.1:<port> source 1.0.0
2 http://127.0.0.1:<port> source 1.0.0
3 http://127.0.0.1:<port> source 1.0.0
ref package version type direct binary dependencies
1 pkg1 pkg1 1.0.0 standard FALSE FALSE
2 pkg2 pkg2 1.0.0 standard FALSE FALSE pkg1
3 pkg3 pkg3 1.0.0 standard TRUE FALSE pkg2
deps vignettes needscompilation metadata.RemoteType
1 NULL FALSE FALSE standard
2 pkg1, depends, pkg1, , FALSE FALSE standard
3 pkg2, depends, pkg2, , FALSE FALSE standard
metadata.RemotePkgRef metadata.RemoteRef metadata.RemoteRepos
1 pkg1 pkg1 http://127.0.0.1:<port>
2 pkg2 pkg2 http://127.0.0.1:<port>
3 pkg3 pkg3 http://127.0.0.1:<port>
metadata.RemotePkgPlatform metadata.RemoteSha
1 source 1.0.0
2 source 1.0.0
3 source 1.0.0
sources
1 http://127.0.0.1:<port>/src/contrib/pkg1_1.0.0.tar.gz, http://127.0.0.1:<port>/src/contrib/Archive/pkg1/pkg1_1.0.0.tar.gz
2 http://127.0.0.1:<port>/src/contrib/pkg2_1.0.0.tar.gz, http://127.0.0.1:<port>/src/contrib/Archive/pkg2/pkg2_1.0.0.tar.gz
Expand Down
28 changes: 16 additions & 12 deletions tests/testthat/_snaps/windows/pkg-installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@
Code
lock$packages
Output
ref package version type direct binary dependencies vignettes
1 pkg1 pkg1 1.0.0 standard FALSE FALSE FALSE
2 pkg2 pkg2 1.0.0 standard FALSE FALSE pkg1 FALSE
3 pkg3 pkg3 1.0.0 standard TRUE FALSE pkg2 FALSE
needscompilation metadata.RemoteType metadata.RemotePkgRef metadata.RemoteRef
1 FALSE standard pkg1 pkg1
2 FALSE standard pkg2 pkg2
3 FALSE standard pkg3 pkg3
metadata.RemoteRepos metadata.RemotePkgPlatform metadata.RemoteSha
1 http://127.0.0.1:<port> source 1.0.0
2 http://127.0.0.1:<port> source 1.0.0
3 http://127.0.0.1:<port> source 1.0.0
ref package version type direct binary dependencies
1 pkg1 pkg1 1.0.0 standard FALSE FALSE
2 pkg2 pkg2 1.0.0 standard FALSE FALSE pkg1
3 pkg3 pkg3 1.0.0 standard TRUE FALSE pkg2
deps vignettes needscompilation metadata.RemoteType
1 NULL FALSE FALSE standard
2 pkg1, depends, pkg1, , FALSE FALSE standard
3 pkg2, depends, pkg2, , FALSE FALSE standard
metadata.RemotePkgRef metadata.RemoteRef metadata.RemoteRepos
1 pkg1 pkg1 http://127.0.0.1:<port>
2 pkg2 pkg2 http://127.0.0.1:<port>
3 pkg3 pkg3 http://127.0.0.1:<port>
metadata.RemotePkgPlatform metadata.RemoteSha
1 source 1.0.0
2 source 1.0.0
3 source 1.0.0
sources
1 http://127.0.0.1:<port>/src/contrib/pkg1_1.0.0.tar.gz, http://127.0.0.1:<port>/src/contrib/Archive/pkg1/pkg1_1.0.0.tar.gz
2 http://127.0.0.1:<port>/src/contrib/pkg2_1.0.0.tar.gz, http://127.0.0.1:<port>/src/contrib/Archive/pkg2/pkg2_1.0.0.tar.gz
Expand Down
Loading
Loading