Skip to content

Commit

Permalink
Don't duplicate paths
Browse files Browse the repository at this point in the history
Fixes #1095
  • Loading branch information
hadley committed May 11, 2023
1 parent 40aa593 commit 3abcec5
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 12 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ URL: https://rstudio.github.io/renv/
BugReports: https://github.com/rstudio/renv/issues
Imports: utils
Suggests: BiocManager, cli, covr, devtools, jsonlite, knitr, miniUI, packrat, pak,
R6, remotes, reticulate, rmarkdown, rstudioapi, shiny, testthat, uuid, yaml
R6, remotes, reticulate, rmarkdown, rstudioapi, shiny, testthat, uuid, withr, yaml
Encoding: UTF-8
RoxygenNote: 7.2.3
Roxygen: list(markdown = TRUE)
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

# renv 0.18.0 (UNRELEASED)

* `renv::load()` no longer duplicates entries on the `PATH` environment variable
(#1095).

* `renv` gains a new function `renv::checkout()`, for installing the
latest-available packages from a repository. For example, one can
use `renv::checkout(date = "2023-02-08")` to install the packages available
Expand Down
17 changes: 6 additions & 11 deletions R/envvar.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,15 @@ renv_envvar_clear <- function(key) {

renv_envvar_modify <- function(envvar, value, prepend) {

old <- Sys.getenv(envvar, unset = NA)
old <- Sys.getenv(envvar, unset = "")
old <- strsplit(old, .Platform$path.sep)[[1]]

parts <- if (prepend)
c(value, if (!is.na(old)) old)
else
c(if (!is.na(old)) old, value)
parts <- if (prepend) union(value, old) else union(old, value)
new <- paste(parts, collapse = .Platform$path.sep)

new <- paste(unique(parts), collapse = .Platform$path.sep)

names(new) <- envvar
do.call(Sys.setenv, as.list(new))

new
do.call(Sys.setenv, as.list(setNames(new, envvar)))

invisible(new)
}

renv_envvar_prepend <- function(envvar, value) {
Expand Down
17 changes: 17 additions & 0 deletions tests/testthat/test-envvar.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
context("envvar")

test_that("renv_envvar_prepend doesn't duplicate paths", {
path_string <- function(...) {
paste(c(...), collapse = .Platform$path.sep)
}

withr::local_envvar(TESTPATH = path_string("a", "b", "c"))
expect_equal(
renv_envvar_prepend("TESTPATH", "a"),
path_string("a", "b", "c")
)
expect_equal(
renv_envvar_prepend("TESTPATH", "b"),
path_string("b", "a", "c")
)
})

0 comments on commit 3abcec5

Please sign in to comment.