Skip to content

Commit

Permalink
ensure remote fields from r-universe are included in lockfile
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinushey committed Aug 5, 2024
1 parent 6d80457 commit 8573b0d
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 17 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

# renv (development version)

* `renv` now preserves `Remote` fields present on packages installed from
public package repositories (e.g. <https://r-universe.dev/>). (#1961)

* Fixed an issue where `renv::load()` could fail to load an alternate project
when `options(renv.config.autoloader.enabled = FALSE)` was set. (#1959)

Expand Down
21 changes: 14 additions & 7 deletions R/package.R
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,7 @@ renv_package_pkgtypes <- function() {

}

renv_package_augment_standard <- function(record) {

# only done for repository remotes
if (!identical(record$Source, "Repository"))
return(record)
renv_package_augment_standard <- function(path, record) {

# check whether we tagged a url + type for this package
url <- attr(record, "url", exact = TRUE)
Expand All @@ -165,6 +161,17 @@ renv_package_augment_standard <- function(record) {
if (is.null(url) || is.null(type))
return(record)

# skip if this isn't a repository remote
if (!identical(record$Source, "Repository"))
return(record)

# skip if the DESCRIPTION file already has Remote fields
# (mainly relevant for r-universe)
descpath <- file.path(path, "DESCRIPTION")
desc <- renv_description_read(descpath)
if (any(grepl("^Remote", names(desc))))
return(record)

# figure out base of repository URL
pattern <- "/(?:bin|src)/"
index <- regexpr(pattern, url, perl = TRUE)
Expand All @@ -190,8 +197,8 @@ renv_package_augment_standard <- function(record) {

renv_package_augment <- function(installpath, record) {

# try to include repository fields
record <- renv_package_augment_standard(record)
# figure out if we should write this as a 'standard' remote
record <- renv_package_augment_standard(installpath, record)

# check for remotes fields
remotes <- record[grep("^Remote", names(record))]
Expand Down
16 changes: 10 additions & 6 deletions R/snapshot.R
Original file line number Diff line number Diff line change
Expand Up @@ -758,15 +758,19 @@ renv_snapshot_description_impl <- function(dcf, path = NULL) {
# get remotes fields
git <- grep("^git", names(dcf), value = TRUE)
remotes <- grep("^Remote", names(dcf), value = TRUE)
cranlike <- renv_record_cranlike(dcf)

# don't include 'RemoteRef' if it's a non-informative remote
if (identical(dcf[["RemoteRef"]], "HEAD"))
remotes <- setdiff(remotes, "RemoteRef")

# drop remote metadata for 'standard' remotes, to avoid spurious
# diffs that could arise from installing a package using 'pak'
# versus 'install.packages()' or an alternate tool
std <- identical(dcf[["RemoteType"]], "standard")

# only keep relevant fields
extra <- c("Repository", "OS_type")
all <- c(
required, extra,
if (!cranlike) c(remotes, git),
"Requirements", "Hash"
)
all <- c(required, extra, if (!std) c(remotes, git), "Requirements", "Hash")
keep <- renv_vector_intersect(all, names(dcf))

# return as list
Expand Down
2 changes: 1 addition & 1 deletion man/lockfiles.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/snapshot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions tests/testthat/test-install.R
Original file line number Diff line number Diff line change
Expand Up @@ -748,3 +748,13 @@ test_that("installation of package from local sources works", {
expect_true(renv_package_installed("bread"))

})

test_that("packages installed from r-universe preserve their remote metadata", {
skip_on_cran()
skip_on_ci()

project <- renv_tests_scope(packages = "rlang")
install("rlang", repos = "https://r-lib.r-universe.dev")
record <- renv_snapshot_description(package = "rlang")
expect_true(is.character(record[["RemoteSha"]]))
})
19 changes: 17 additions & 2 deletions tests/testthat/test-snapshot.R
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,22 @@ test_that("snapshot() reports missing packages even if renv.verbose is FALSE", {
expect_snapshot(. <- snapshot(force = TRUE))
})

test_that("snapshot() doesn't duplicate packages", {

test_that("packages installed from r-universe preserve remote metadata", {

text <- heredoc("
Package: skeleton
Type: Package
Version: 1.1.0
Remotes: kevinushey/skeleton
Repository: https://kevinushey.r-universe.dev
RemoteUrl: https://github.com/kevinushey/skeleton
RemoteSha: e4aafb92b86ba7eba3b7036d9d96fdfb6c32761a
")

path <- renv_scope_tempfile()
writeLines(text, con = path)

record <- renv_snapshot_description(path = path)
expect_identical(record[["RemoteSha"]], "e4aafb92b86ba7eba3b7036d9d96fdfb6c32761a")

})

0 comments on commit 8573b0d

Please sign in to comment.