From 338dce678bdfd6198d32db2f8e9d5a53156cc003 Mon Sep 17 00:00:00 2001 From: novica Date: Sun, 1 Nov 2026 19:59:28 +0100 Subject: [PATCH 1/3] fix: restore renv lockfiles from their own repositories A global repos override made renv ignore the lockfile's repositories, so packages from r-universe or internal repos failed to restore. Assisted-by: Claude Opus 5.5 --- DESCRIPTION | 1 + R/lambda-util.R | 30 +++++++++++---- inst/lambdr_dockerfile.txt | 6 +-- tests/testthat/test-lambda-util.R | 61 +++++++++++++++++++++++++++---- 4 files changed, 80 insertions(+), 18 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 3805bcf..09cc625 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -34,6 +34,7 @@ Imports: Suggests: covr, paws.common, + renv, testthat (>= 3.0.0), withr Config/testthat/edition: 3 diff --git a/R/lambda-util.R b/R/lambda-util.R index 245c4ca..e1d0814 100644 --- a/R/lambda-util.R +++ b/R/lambda-util.R @@ -46,11 +46,10 @@ aws_connect <- function(service) { install_deps_line <- function(deps) { checkmate::assert_character(deps) - repo <- single_quote( - "https://packagemanager.posit.co/cran/__linux__/rhel9/latest" - ) glued <- glue_collapse(single_quote(deps), sep = ", ") - glue('RUN Rscript -e "install.packages(c({glued}), repos = {repo})"') + glue( + 'RUN Rscript -e "install.packages(c({glued}), repos = Sys.getenv(\'R_REPO\'))"' + ) } #' system_deps_line @@ -76,12 +75,29 @@ support_line <- function(support) { } #' renv_line +#' +#' Restores from the lockfile's repositories, except that CRAN and Posit +#' Package Manager are replaced with `R_REPO`, which has binaries for the +#' image's OS. git is installed for packages restored from a git remote. #' @noRd renv_line <- function(renvlock_path) { checkmate::assert_character(renvlock_path) - cmd1 <- glue("COPY {renvlock_path} renv.lock") - cmd2 <- "RUN Rscript -e 'renv::restore()'" - glue_collapse(c(cmd1, cmd2), sep = "\n") + restore <- paste( + 'repos <- unlist(renv::lockfile_read("renv.lock")$R$Repositories);', + 'cran <- names(repos) == "CRAN" |', + 'grepl("packagemanager[.]posit[.]co|p3m[.]dev", repos);', + 'repos[cran] <- Sys.getenv("R_REPO");', + 'if (!any(cran)) repos <- c(CRAN = Sys.getenv("R_REPO"), repos);', + "renv::restore(repos = repos)" + ) + glue_collapse( + c( + "RUN dnf -y install git && dnf clean all", + glue("COPY {renvlock_path} renv.lock"), + glue("RUN Rscript -e '{restore}'") + ), + sep = "\n" + ) } #' parse password from ecr token diff --git a/inst/lambdr_dockerfile.txt b/inst/lambdr_dockerfile.txt index cdad85c..20b492b 100644 --- a/inst/lambdr_dockerfile.txt +++ b/inst/lambdr_dockerfile.txt @@ -15,10 +15,10 @@ RUN rpm_file=R-${R_VERSION}-1-1.$(uname -m).rpm \ && dnf clean all ENV PATH="${PATH}:/opt/R/${R_VERSION}/bin/" -# Install binary packages, also when restoring an renv.lock. -ENV RENV_CONFIG_REPOS_OVERRIDE="https://packagemanager.posit.co/cran/__linux__/rhel9/latest" +# Posit Package Manager's binary packages for RHEL 9. +ENV R_REPO="https://packagemanager.posit.co/cran/__linux__/rhel9/latest" -RUN Rscript -e 'install.packages(c("lambdr", "renv"), repos = "https://packagemanager.posit.co/cran/__linux__/rhel9/latest")' +RUN Rscript -e 'install.packages(c("lambdr", "renv"), repos = Sys.getenv("R_REPO"))' RUN printf '#!/bin/sh\ncd /lambda\nRscript runtime.R' > /var/runtime/bootstrap \ && chmod +x /var/runtime/bootstrap diff --git a/tests/testthat/test-lambda-util.R b/tests/testthat/test-lambda-util.R index 0f22eb6..360f402 100644 --- a/tests/testthat/test-lambda-util.R +++ b/tests/testthat/test-lambda-util.R @@ -53,15 +53,60 @@ test_that("runtime_line works with correct input", { test_that("renv_line works", { expect_error(renv_line(renvlock = 1)) - renvlock <- renv_line("renv.lock") - test <- glue::glue_collapse( - c( - "COPY renv.lock renv.lock", - "RUN Rscript -e 'renv::restore()'" - ), - sep = "\n" + lines <- strsplit(renv_line("renv.lock"), "\n")[[1]] + expect_equal(lines[[1]], "RUN dnf -y install git && dnf clean all") + expect_equal(lines[[2]], "COPY renv.lock renv.lock") + expect_match(lines[[3]], "^RUN Rscript -e '[^']+'$") +}) + +test_that("renv_line restores from the lockfile repos with CRAN replaced", { + skip_if_not_installed("renv") + restore_code <- function() { + line <- strsplit(renv_line("renv.lock"), "\n")[[1]][[3]] + parse(text = sub("^RUN Rscript -e '(.*)'$", "\\1", line)) + } + restore_repos <- function(repositories) { + dir <- withr::local_tempdir() + lockfile <- list(R = list(Version = "4.6.1"), Packages = list()) + lockfile$R$Repositories <- repositories + jsonlite::write_json( + lockfile, + file.path(dir, "renv.lock"), + auto_unbox = TRUE + ) + withr::local_dir(dir) + withr::local_envvar(R_REPO = "https://ppm/rhel9") + used <- NULL + local_mocked_bindings( + restore = function(repos) { + used <<- repos + }, + .package = "renv" + ) + eval(restore_code(), envir = new.env()) + used + } + + expect_equal( + restore_repos(list( + list(Name = "CRAN", URL = "https://cloud.r-project.org"), + list(Name = "ru", URL = "https://x.r-universe.dev") + )), + c(CRAN = "https://ppm/rhel9", ru = "https://x.r-universe.dev") + ) + expect_equal( + restore_repos(list( + list( + Name = "RSPM", + URL = "https://packagemanager.posit.co/cran/__linux__/jammy/latest" + ) + )), + c(RSPM = "https://ppm/rhel9") + ) + expect_equal( + restore_repos(list(list(Name = "ru", URL = "https://x.r-universe.dev"))), + c(CRAN = "https://ppm/rhel9", ru = "https://x.r-universe.dev") ) - expect_equal(test, renvlock) }) ##### From 1fed4e33334ea320d80dec67aa88810e99cb1fa5 Mon Sep 17 00:00:00 2001 From: novica Date: Sun, 1 Nov 2026 20:01:15 +0100 Subject: [PATCH 2/3] fix: install git only for lockfiles with git remotes Also keep non-CRAN Posit Package Manager repositories, such as Bioconductor. Assisted-by: Claude Opus 5.5 --- R/lambda-util.R | 29 +++++++++++++++++++------ tests/testthat/test-lambda-util.R | 36 ++++++++++++++++++++++++++++--- 2 files changed, 55 insertions(+), 10 deletions(-) diff --git a/R/lambda-util.R b/R/lambda-util.R index e1d0814..14cc166 100644 --- a/R/lambda-util.R +++ b/R/lambda-util.R @@ -76,23 +76,27 @@ support_line <- function(support) { #' renv_line #' -#' Restores from the lockfile's repositories, except that CRAN and Posit -#' Package Manager are replaced with `R_REPO`, which has binaries for the -#' image's OS. git is installed for packages restored from a git remote. +#' Restores from the lockfile's repositories, except that CRAN and the CRAN +#' repositories of Posit Package Manager are replaced with `R_REPO`, which has +#' binaries for the image's OS. +#' @param renvlock_path path of the lockfile in the image's build folder +#' @param git logical, whether to install git, which renv needs for packages +#' from a git remote #' @noRd -renv_line <- function(renvlock_path) { +renv_line <- function(renvlock_path, git = FALSE) { checkmate::assert_character(renvlock_path) + checkmate::assert_flag(git) restore <- paste( 'repos <- unlist(renv::lockfile_read("renv.lock")$R$Repositories);', 'cran <- names(repos) == "CRAN" |', - 'grepl("packagemanager[.]posit[.]co|p3m[.]dev", repos);', + 'grepl("(packagemanager[.]posit[.]co|p3m[.]dev)/cran/", repos);', 'repos[cran] <- Sys.getenv("R_REPO");', 'if (!any(cran)) repos <- c(CRAN = Sys.getenv("R_REPO"), repos);', "renv::restore(repos = repos)" ) glue_collapse( c( - "RUN dnf -y install git && dnf clean all", + if (git) "RUN dnf -y install git && dnf clean all", glue("COPY {renvlock_path} renv.lock"), glue("RUN Rscript -e '{restore}'") ), @@ -100,6 +104,14 @@ renv_line <- function(renvlock_path) { ) } +#' Whether an renv lockfile has packages from a git remote +#' @param renvlock_path path to the lockfile +#' @noRd +lockfile_has_git <- function(renvlock_path) { + packages <- jsonlite::fromJSON(renvlock_path, simplifyVector = FALSE)$Packages + any(vapply(packages, function(x) identical(x$RemoteType, "git"), TRUE)) +} + #' parse password from ecr token #' @noRd parse_password <- function(ecr_token) { @@ -330,7 +342,10 @@ create_lambda_dockerfile <- if (!is.null(renvlock_path)) { file.copy(renvlock_path, file.path(folder, "renv.lock")) - renv_string <- renv_line("renv.lock") + renv_string <- renv_line( + "renv.lock", + git = lockfile_has_git(renvlock_path) + ) write(renv_string, file = file.path(folder, "Dockerfile"), append = TRUE) } diff --git a/tests/testthat/test-lambda-util.R b/tests/testthat/test-lambda-util.R index 360f402..1c2b73b 100644 --- a/tests/testthat/test-lambda-util.R +++ b/tests/testthat/test-lambda-util.R @@ -54,15 +54,37 @@ test_that("renv_line works", { expect_error(renv_line(renvlock = 1)) lines <- strsplit(renv_line("renv.lock"), "\n")[[1]] + expect_equal(lines[[1]], "COPY renv.lock renv.lock") + expect_match(lines[[2]], "^RUN Rscript -e '[^']+'$") + + lines <- strsplit(renv_line("renv.lock", git = TRUE), "\n")[[1]] expect_equal(lines[[1]], "RUN dnf -y install git && dnf clean all") - expect_equal(lines[[2]], "COPY renv.lock renv.lock") - expect_match(lines[[3]], "^RUN Rscript -e '[^']+'$") +}) + +test_that("lockfile_has_git finds packages from a git remote", { + lockfile <- withr::local_tempfile(fileext = ".lock") + write_lock <- function(packages) { + jsonlite::write_json( + list(R = list(Version = "4.6.1"), Packages = packages), + lockfile, + auto_unbox = TRUE + ) + } + write_lock(list(glue = list(Package = "glue", Source = "Repository"))) + expect_false(lockfile_has_git(lockfile)) + write_lock(list( + glue = list(Package = "glue", Source = "Repository"), + mypkg = list(Package = "mypkg", RemoteType = "git") + )) + expect_true(lockfile_has_git(lockfile)) + write_lock(list()) + expect_false(lockfile_has_git(lockfile)) }) test_that("renv_line restores from the lockfile repos with CRAN replaced", { skip_if_not_installed("renv") restore_code <- function() { - line <- strsplit(renv_line("renv.lock"), "\n")[[1]][[3]] + line <- strsplit(renv_line("renv.lock"), "\n")[[1]][[2]] parse(text = sub("^RUN Rscript -e '(.*)'$", "\\1", line)) } restore_repos <- function(repositories) { @@ -103,6 +125,14 @@ test_that("renv_line restores from the lockfile repos with CRAN replaced", { )), c(RSPM = "https://ppm/rhel9") ) + bioc <- "https://packagemanager.posit.co/bioconductor/packages/3.19/bioc" + expect_equal( + restore_repos(list( + list(Name = "CRAN", URL = "https://cloud.r-project.org"), + list(Name = "BioCsoft", URL = bioc) + )), + c(CRAN = "https://ppm/rhel9", BioCsoft = bioc) + ) expect_equal( restore_repos(list(list(Name = "ru", URL = "https://x.r-universe.dev"))), c(CRAN = "https://ppm/rhel9", ru = "https://x.r-universe.dev") From 9997caa0f73aded018ee199785ef197b8eef285f Mon Sep 17 00:00:00 2001 From: novica Date: Sun, 1 Nov 2026 20:02:15 +0100 Subject: [PATCH 3/3] fix: install git for the xgit and git2r remote types renv restores those with git too. Assisted-by: Claude Opus 5.5 --- R/lambda-util.R | 7 ++++++- tests/testthat/test-lambda-util.R | 4 ++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/R/lambda-util.R b/R/lambda-util.R index 14cc166..cc72e6f 100644 --- a/R/lambda-util.R +++ b/R/lambda-util.R @@ -109,7 +109,12 @@ renv_line <- function(renvlock_path, git = FALSE) { #' @noRd lockfile_has_git <- function(renvlock_path) { packages <- jsonlite::fromJSON(renvlock_path, simplifyVector = FALSE)$Packages - any(vapply(packages, function(x) identical(x$RemoteType, "git"), TRUE)) + # renv treats the xgit and git2r remote types as git too. + is_git <- function(x) { + identical(x$Source, "git") || + isTRUE(x$RemoteType %in% c("git", "xgit", "git2r")) + } + any(vapply(packages, is_git, TRUE)) } #' parse password from ecr token diff --git a/tests/testthat/test-lambda-util.R b/tests/testthat/test-lambda-util.R index 1c2b73b..4410456 100644 --- a/tests/testthat/test-lambda-util.R +++ b/tests/testthat/test-lambda-util.R @@ -77,6 +77,10 @@ test_that("lockfile_has_git finds packages from a git remote", { mypkg = list(Package = "mypkg", RemoteType = "git") )) expect_true(lockfile_has_git(lockfile)) + write_lock(list(mypkg = list(Package = "mypkg", RemoteType = "xgit"))) + expect_true(lockfile_has_git(lockfile)) + write_lock(list(mypkg = list(Package = "mypkg", Source = "git"))) + expect_true(lockfile_has_git(lockfile)) write_lock(list()) expect_false(lockfile_has_git(lockfile)) })