From 9718c02b68a5437699be2b4da0d6244a3e662434 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Cs=C3=A1rdi?= Date: Fri, 22 Sep 2023 16:35:54 +0200 Subject: [PATCH] Fix solver when top direct version is ruled out I.e. there are multiple versions of a direct ref, so the latest (possible) should be installed. Closes https://github.com/r-lib/pak/issues/538. --- R/solve.R | 3 ++- tests/testthat/test-solve-conflicts.R | 35 +++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/R/solve.R b/R/solve.R index 39127830..94920d8c 100644 --- a/R/solve.R +++ b/R/solve.R @@ -437,7 +437,8 @@ pkgplan_i_lp_satisfy_direct <- function(lp) { } } } - lapply(seq_len(lp$num_candidates)[lp$pkgs$direct], satisfy) + direct <- setdiff(which(lp$pkgs$direct), lp$ruled_out) + lapply(direct, satisfy) lp } diff --git a/tests/testthat/test-solve-conflicts.R b/tests/testthat/test-solve-conflicts.R index e4373f23..321c7b67 100644 --- a/tests/testthat/test-solve-conflicts.R +++ b/tests/testthat/test-solve-conflicts.R @@ -107,3 +107,38 @@ test_that("failed resolution", { suppressMessages(p2$solve()) expect_snapshot(error = TRUE, p2$stop_for_solution_error()) }) + +test_that("ruled out direct dep", { + # I.e. there are multiple versions, the newer version is ruled out + # for the current R version, but the older vresion is fine. + + # pkgcache::cran_app cannot seem to do this with a single repo + repo1 <- dcf(" + Package: pkg1 + Version: 1.0.0 + ") + + repo2 <- dcf(" + Package: pkg1 + Version: 1.0.1 + Depends: R (>= 20000.0.0) + ") + + fake1 <- webfakes::new_app_process(cran_app(repo1)) + fake2 <- webfakes::new_app_process(cran_app(repo2)) + withr::local_options(repos = c(CRAN = fake1$url(), X = fake2$url())) + + lib <- tempfile() + lock <- tempfile() + on.exit(unlink(c(lib, lock), recursive = TRUE), add = TRUE) + + p <- suppressMessages(new_pkg_installation_proposal( + c("pkg1"), + config = list( + dependencies = TRUE, + library = lib + ) + )) + suppressMessages(p$solve()) + expect_equal(p$get_solution()$data$version, "1.0.0") +})