From 73c899ec49a2dd8712730e856da2f5699eceed03 Mon Sep 17 00:00:00 2001 From: Kevin Ushey Date: Wed, 4 Sep 2024 14:49:10 -0700 Subject: [PATCH] fixups --- NEWS.md | 2 ++ R/checkout.R | 31 +++++++++++-------------------- R/install.R | 1 + R/pkgtype.R | 29 +++++++++++++++++++++++++++++ R/vendor.R | 4 ++-- R/zzz.R | 9 --------- inst/resources/vendor/renv.R | 4 ++-- tests/testthat/test-vendor.R | 2 +- 8 files changed, 48 insertions(+), 34 deletions(-) create mode 100644 R/pkgtype.R diff --git a/NEWS.md b/NEWS.md index dac9abc1f..00801f899 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,8 @@ # renv (development version) +* `renv::install()` now errs if an incompatible `type` argument is provided. + * `renv::checkout()` now also checks out the version of `renv` available and associated with the requested snapshot date. (#1966) diff --git a/R/checkout.R b/R/checkout.R index 17393c770..e11606313 100644 --- a/R/checkout.R +++ b/R/checkout.R @@ -105,37 +105,28 @@ checkout <- function(repos = NULL, lockfile <- renv_lockfile_init(project) lockfile$Packages <- records - if ("restore" %in% actions) { + if ("restore" %in% actions) local({ # install the requested packages restore(lockfile = lockfile, clean = clean) - # re-generate the activate script - local({ + # make sure we can find 'renv' on the library paths + path <- renv_namespace_path("renv") + renv_scope_libpaths(c(dirname(path), renv_libpaths_all())) - # make sure we can find 'renv' on the library paths - renv_scope_libpaths(the$library_path) - - # invoke activate - args <- c("--vanilla", "-s", "-e", shQuote("renv::activate()")) - r(args) - - }) + # invoke activate + args <- c("--vanilla", "-s", "-e", shQuote("renv::activate()")) + r(args) # update the renv lockfile record # (note: it might not be available when running tests) - local({ - - renv <- renv_lockfile_records(lockfile)[["renv"]] - if (is.null(renv)) - return() - + renv <- renv_lockfile_records(lockfile)[["renv"]] + if (!is.null(renv)) { renv_scope_options(renv.verbose = FALSE) record(records = list(renv = renv), project = project) + } - }) - - } + }) # re-generate the lockfile if requested if ("snapshot" %in% actions) { diff --git a/R/install.R b/R/install.R index e365e9252..1c9d9254b 100644 --- a/R/install.R +++ b/R/install.R @@ -128,6 +128,7 @@ install <- function(packages = NULL, # check for explicitly-provided type -- we handle this specially for PPM if (!is.null(type)) { + type <- renv_pkgtype_check(type) renv_scope_binding(the, "install_pkg_type", type) renv_scope_options(pkgType = type) } diff --git a/R/pkgtype.R b/R/pkgtype.R new file mode 100644 index 000000000..1efe8d88f --- /dev/null +++ b/R/pkgtype.R @@ -0,0 +1,29 @@ + +renv_pkgtype_check <- function(type) { + + case( + type == "source" ~ renv_pkgtype_check_source(type), + type == "binary" ~ renv_pkgtype_check_binary(type), + ~ abort(sprintf("unrecognized type '%s'", type)) + ) + +} + +renv_pkgtype_check_source <- function(type) { + type +} + +renv_pkgtype_check_binary <- function(type) { + + # if the user has requested installation of a binary package, + # and this edition of R was compiled with support for binary + # packages, then proceed + pkgtype <- .Platform$pkgType + if (grepl("\\bbinary\\b", pkgtype, perl = TRUE)) + return(type) + + # user has requested installation of binary packages, but their + # installation of R only supports source packages + abort("type 'binary' is not supported on this platform") + +} diff --git a/R/vendor.R b/R/vendor.R index 99dbef209..8e6b91c6b 100644 --- a/R/vendor.R +++ b/R/vendor.R @@ -64,8 +64,8 @@ vendor <- function(version = "main", project = getwd()) { # A vendored copy of renv was created at: %s # The renv auto-loader was generated at: %s # - # Please add `renv$initialize()` to your package's `.onLoad()` - # to ensure that renv is initialized on package load. + # Please add `renv$initialize(libname, pkgname)` to your package's + # `.onLoad()` to ensure that renv is initialized on package load. # ") diff --git a/R/zzz.R b/R/zzz.R index b1b13664f..e0dbd0993 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -4,15 +4,6 @@ # NOTE: needs to be visible to embedded instances of renv as well the$envir_self <<- renv_envir_self() - # figure out where 'renv' was loaded from -- if tests are running - # and we're using devtools::load_all(), we might need to fall back - # to whatever version of renv is available on the library paths - load <- Sys.getenv("DEVTOOLS_LOAD", unset = NA) - the$library_path <<- if (identical(load, .packageName)) - dirname(renv_package_find(.packageName)) - else - libname - # make sure renv (and packages using renv!!!) use tempdir for storage # when running tests, or R CMD check if (checking() || testing()) { diff --git a/inst/resources/vendor/renv.R b/inst/resources/vendor/renv.R index 6efb688de..b1f988835 100644 --- a/inst/resources/vendor/renv.R +++ b/inst/resources/vendor/renv.R @@ -1,7 +1,7 @@ renv <- new.env(parent = new.env()) -renv$initialize <- function() { +renv$initialize <- function(libname, pkgname) { # set up renv + imports environments attr(renv, "name") <- "embedded:renv" @@ -25,7 +25,7 @@ renv$initialize <- function() { renv$the$metadata <- ..metadata.. # run our load / attach hooks so internal state is initialized - renv$renv_zzz_load() + renv$.onLoad(libname, pkgname) # remove our initialize method when we're done rm(list = "initialize", envir = renv) diff --git a/tests/testthat/test-vendor.R b/tests/testthat/test-vendor.R index 4756079dd..86d3b0caa 100644 --- a/tests/testthat/test-vendor.R +++ b/tests/testthat/test-vendor.R @@ -26,7 +26,7 @@ test_that("renv can be vendored into an R package", { # make sure renv is initializes in .onLoad() code <- heredoc(' .onLoad <- function(libname, pkgname) { - renv$initialize() + renv$initialize(libname, pkgname) } ')