Skip to content

Commit

Permalink
let hydrate search 'default' library paths
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinushey committed Mar 9, 2023
1 parent ed30434 commit c068880
Show file tree
Hide file tree
Showing 17 changed files with 54 additions and 43 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: renv
Type: Package
Title: Project Environments
Version: 0.17.0-31
Version: 0.17.0-32
Authors@R: c(
person("Kevin", "Ushey", role = c("aut", "cre"), email = "kevin@rstudio.com"),
person("Posit Software, PBC", role = c("cph", "fnd"))
Expand Down
4 changes: 2 additions & 2 deletions R/bioconductor.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ renv_bioconductor_init_biocmanager <- function(library = NULL) {
if (renv_package_checking())
return(TRUE)

library <- library %||% renv_libpaths_default()
library <- library %||% renv_libpaths_active()
if (renv_package_installed("BiocManager", lib.loc = library))
return(TRUE)

Expand All @@ -26,7 +26,7 @@ renv_bioconductor_init_biocinstaller <- function(library = NULL) {
if (renv_package_checking())
return(TRUE)

library <- library %||% renv_libpaths_default()
library <- library %||% renv_libpaths_active()
if (renv_package_installed("BiocInstaller", lib.loc = library))
return(TRUE)

Expand Down
2 changes: 1 addition & 1 deletion R/cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ renv_cache_path_components <- function(path) {
renv_cache_synchronize <- function(record, linkable = FALSE) {

# construct path to package in library
library <- renv_libpaths_default()
library <- renv_libpaths_active()
path <- file.path(library, record$Package)
if (!file.exists(path))
return(FALSE)
Expand Down
2 changes: 1 addition & 1 deletion R/ci.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ renv_ci_dependencies <- function() {
renv_ci_repair <- function() {

# get installed packages
library <- renv_libpaths_default()
library <- renv_libpaths_active()
db <- renv_installed_packages(lib.loc = library)

# attempt to load these packages
Expand Down
20 changes: 12 additions & 8 deletions R/hydrate.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ hydrate <- function(packages = NULL,

renv_activate_prompt("hydrate", library, prompt, project)

library <- renv_path_normalize(library %||% renv_libpaths_default())
library <- renv_path_normalize(library %||% renv_libpaths_active())
packages <- packages %||% renv_hydrate_packages(project)

# find packages used in this project, and the dependencies of those packages
Expand Down Expand Up @@ -254,15 +254,19 @@ renv_hydrate_libpaths <- function() {
if (is.character(conf) && length(conf))
conf <- unlist(strsplit(conf, ":", fixed = TRUE))

libpaths <- if (renv_tests_running())
renv_libpaths_all()
else if (length(conf))
conf
else
c(renv_libpaths_user(), renv_libpaths_site(), renv_libpaths_system())
libpaths <- case(
renv_tests_running() ~ renv_libpaths_all(),
length(conf) ~ conf,
~ c(
renv_libpaths_default(),
renv_libpaths_user(),
renv_libpaths_site(),
renv_libpaths_system()
)
)

libpaths <- .expand_R_libs_env_var(libpaths)
normalizePath(libpaths, winslash = "/", mustWork = FALSE)
unique(renv_path_normalize(libpaths))

}

Expand Down
2 changes: 1 addition & 1 deletion R/imbue.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ renv_imbue_impl <- function(project, version = NULL, force = FALSE) {

renv_scope_restore(
project = project,
library = renv_libpaths_default(),
library = renv_libpaths_active(),
records = records,
packages = "renv",
recursive = FALSE
Expand Down
16 changes: 8 additions & 8 deletions R/install.R
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ install <- function(packages = NULL,
before <- Sys.time()
renv_scope_restore(
project = project,
library = renv_libpaths_default(),
library = renv_libpaths_active(),
records = records,
packages = packages,
rebuild = rebuild
Expand Down Expand Up @@ -233,7 +233,7 @@ install <- function(packages = NULL,
vwritef(
fmt,
nplural("package", length(records)),
renv_path_pretty(renv_libpaths_default())
renv_path_pretty(renv_libpaths_active())
)
}

Expand Down Expand Up @@ -292,7 +292,7 @@ renv_install_staged_library_path_impl <- function() {
# retrieve current project, library path
staging <- Sys.getenv("RENV_PATHS_LIBRARY_STAGING", unset = NA)
project <- Sys.getenv("RENV_PROJECT", unset = NA)
libpath <- renv_libpaths_default()
libpath <- renv_libpaths_active()

# determine root directory for staging
root <- if (!is.na(staging))
Expand Down Expand Up @@ -344,7 +344,7 @@ renv_install_staged_library_path <- function() {

# try to make sure it has the same permissions as the library itself
if (!renv_platform_windows()) {
libpath <- renv_libpaths_default()
libpath <- renv_libpaths_active()
umask <- Sys.umask("0")
on.exit(Sys.umask(umask), add = TRUE)
info <- renv_file_info(libpath)
Expand Down Expand Up @@ -433,7 +433,7 @@ renv_install_package_cache <- function(record, cache, linker) {
if (renv_install_package_cache_skip(record, cache))
return(TRUE)

library <- renv_libpaths_default()
library <- renv_libpaths_active()
target <- file.path(library, record$Package)

# back up the previous installation if needed
Expand Down Expand Up @@ -467,7 +467,7 @@ renv_install_package_cache_skip <- function(record, cache) {
return(FALSE)

# check for matching cache + target paths
library <- renv_restore_state("library") %||% renv_libpaths_default()
library <- renv_restore_state("library") %||% renv_libpaths_active()
target <- file.path(library, record$Package)

renv_file_same(cache, target)
Expand Down Expand Up @@ -576,7 +576,7 @@ renv_install_package_impl <- function(record, quiet = TRUE) {
on.exit(after(package), add = TRUE)

# backup an existing installation of the package if it exists
library <- renv_libpaths_default()
library <- renv_libpaths_active()
destination <- file.path(library, package)
callback <- renv_file_backup(destination)
on.exit(callback(), add = TRUE)
Expand All @@ -585,7 +585,7 @@ renv_install_package_impl <- function(record, quiet = TRUE) {
path <- renv_path_normalize(path, winslash = "/", mustWork = TRUE)

# get library path
library <- renv_libpaths_default()
library <- renv_libpaths_active()

# if a package already exists at that path, back it up first
# this avoids problems with older versions of R attempting to
Expand Down
21 changes: 13 additions & 8 deletions R/libpaths.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ renv_libpaths_init <- function() {
assign(".Library.site", .Library.site, envir = `_renv_libpaths`)
}

renv_libpaths_default <- function() {
.libPaths()[1]
renv_libpaths_active <- function() {
.libPaths()[[1L]]
}

renv_libpaths_all <- function() {
Expand Down Expand Up @@ -48,8 +48,8 @@ renv_libpaths_safe <- function(libpaths) {

renv_libpaths_safe_check <- function(libpaths) {

# if any of the paths have single quotes, then
# we need to use a safe path
# if any of the paths have single quotes,
# then we need to use a safe path
# https://bugs.r-project.org/bugzilla/show_bug.cgi?id=17973
if (any(grepl("'", libpaths, fixed = TRUE)))
return(FALSE)
Expand Down Expand Up @@ -139,6 +139,10 @@ renv_libpaths_set <- function(libpaths) {
oldlibpaths
}

renv_libpaths_default <- function() {
`_renv_libpaths`$`.libPaths()`
}

# NOTE: may return more than one library path!
renv_libpaths_user <- function() {

Expand All @@ -147,16 +151,17 @@ renv_libpaths_user <- function() {
for (envvar in envvars) {

value <- Sys.getenv(envvar, unset = NA)
if (is.na(value) || value == "<NA>" || value == "NULL" || !nzchar(value))
if (is.na(value) || value %in% c("", "<NA>", "NULL"))
next

parts <- strsplit(value, .Platform$path.sep, fixed = TRUE)[[1]]
parts <- strsplit(value, .Platform$path.sep, fixed = TRUE)[[1L]]
return(parts)

}

# otherwise, default to active library (shouldn't happen but best be safe)
renv_libpaths_default()
# otherwise, default to active library
# (shouldn't happen but best be safe)
renv_libpaths_active()

}

Expand Down
2 changes: 1 addition & 1 deletion R/r.R
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ r_cmd_install <- function(package, path, ...) {
}

# resolve default library path
library <- renv_libpaths_default()
library <- renv_libpaths_active()

# validate that we have command line tools installed and
# available for e.g. macOS
Expand Down
2 changes: 1 addition & 1 deletion R/remove.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ remove <- function(packages,
project <- renv_project_resolve(project)
renv_project_lock(project = project)

library <- renv_path_normalize(library %||% renv_libpaths_default())
library <- renv_path_normalize(library %||% renv_libpaths_active())

descpaths <- file.path(library, packages, "DESCRIPTION")
records <- lapply(descpaths, renv_snapshot_description)
Expand Down
2 changes: 1 addition & 1 deletion R/restore.R
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ renv_restore_run_actions <- function(project, actions, current, lockfile, rebuil

renv_scope_restore(
project = project,
library = renv_libpaths_default(),
library = renv_libpaths_active(),
records = renv_records(lockfile),
packages = packages,
rebuild = rebuild
Expand Down
2 changes: 1 addition & 1 deletion R/snapshot-auto.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ renv_snapshot_auto_enabled <- function(project) {
return(FALSE)

# don't auto-snapshot unless the active library is the project library
if (!renv_file_same(renv_libpaths_default(), library))
if (!renv_file_same(renv_libpaths_active(), library))
return(FALSE)

TRUE
Expand Down
2 changes: 1 addition & 1 deletion R/snapshot.R
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ renv_snapshot_r_packages_impl <- function(library = NULL,
snapshot = TRUE)
{
# list packages in the library
library <- renv_path_normalize(library %||% renv_libpaths_default())
library <- renv_path_normalize(library %||% renv_libpaths_active())
paths <- list.files(library, full.names = TRUE)

# remove 'base' packages
Expand Down
2 changes: 1 addition & 1 deletion R/upgrade.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ renv_upgrade_impl <- function(project, version, reload, prompt) {

renv_scope_restore(
project = project,
library = renv_libpaths_default(),
library = renv_libpaths_active(),
records = list(renv = new),
packages = "renv",
recursive = FALSE
Expand Down
4 changes: 3 additions & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -442,4 +442,6 @@ stringify <- function(object, collapse = " ") {
paste(deparse(object, width.cutoff = 500L), collapse = collapse)
}


env2list <- function(env) {
as.list.environment(env, all.names = TRUE)
}
8 changes: 4 additions & 4 deletions tests/testthat/test-bootstrap.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ test_that("we can bootstrap the current version of renv", {

renv_tests_scope()

library <- renv_libpaths_default()
library <- renv_libpaths_active()
bootstrap(version = "1.0.0", library = library)
expect_true(renv_package_installed("renv", library))
expect_true(renv_package_version("renv") == "1.0.0")
Expand All @@ -22,7 +22,7 @@ test_that("we can bootstrap an archived version of renv", {

renv_tests_scope()

library <- renv_libpaths_default()
library <- renv_libpaths_active()
bootstrap(version = "0.1.0", library = library)
expect_true(renv_package_installed("renv", library))
expect_true(renv_package_version("renv") == "0.1.0")
Expand All @@ -36,7 +36,7 @@ test_that("we can install a version of renv from GitHub", {

renv_tests_scope()

library <- renv_libpaths_default()
library <- renv_libpaths_active()
bootstrap(version = "0.12.3-1", library = library)
expect_true(renv_package_installed("renv", library))
expect_true(renv_package_version("renv") == "0.12.3-1")
Expand All @@ -51,7 +51,7 @@ test_that("bootstrap succeeds with empty repos", {
renv_tests_scope()
renv_scope_options(repos = character())

library <- renv_libpaths_default()
library <- renv_libpaths_active()
bootstrap(version = "1.0.0", library = library)
expect_true(renv_package_installed("renv", library))
expect_true(renv_package_version("renv") == "1.0.0")
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-init.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ test_that("the remotes field in a DESCRIPTION is honored", {
renv_tests_scope("halloween")
renv::install("halloween")

ip <- renv_installed_packages(lib.loc = renv_libpaths_default())
ip <- renv_installed_packages(lib.loc = renv_libpaths_active())
expect_true("halloween" %in% rownames(ip))
expect_true("skeleton" %in% rownames(ip))

Expand Down Expand Up @@ -154,7 +154,7 @@ test_that("we use an external library path for package projects", {
init()

# check for external library path
library <- renv_libpaths_default()
library <- renv_libpaths_active()


expect_true(
Expand Down

0 comments on commit c068880

Please sign in to comment.