Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

when attempting to load a lockfile and finding only a manifest, automatically generate a lockfile and load that #1980

Merged
merged 7 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion R/lockfile.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ renv_lockfile_save <- function(lockfile, project) {
}

renv_lockfile_load <- function(project, strict = FALSE) {

path <- renv_lockfile_path(project)
if (file.exists(path))
return(renv_lockfile_read(path))
Expand All @@ -134,6 +133,13 @@ renv_lockfile_load <- function(project, strict = FALSE) {
))
}

manifest <- file.path(project, "manifest.json")
if (file.exists(manifest)) {
caution("No lockfile found; creating from `manifest.json`.")
renv_lockfile_from_manifest(manifest, path)
return(renv_lockfile_read(path))
}

renv_lockfile_init(project = project)

}
Expand Down
4 changes: 1 addition & 3 deletions R/manifest-convert.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,8 @@ renv_lockfile_from_manifest <- function(manifest = "manifest.json",
if (is.na(lockfile))
return(lock)

# otherwise, write to file and report for user
# otherwise, write to file
renv_lockfile_write(lock, file = lockfile)
fmt <- "- Lockfile written to %s."
writef(fmt, renv_path_pretty(lockfile))

invisible(lock)

Expand Down
2 changes: 1 addition & 1 deletion R/paths.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ renv_paths_lockfile <- function(project = NULL) {
return(override)
}

# otherwise, use default location (location location relative to renv folder)
# otherwise, use default location (location relative to renv folder)
project <- renv_project_resolve(project)
renv <- renv_paths_renv(project = project)
file.path(dirname(renv), "renv.lock")
Expand Down
22 changes: 22 additions & 0 deletions tests/testthat/test-lockfile.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,28 @@ test_that("we can create lockfiles from manifests", {

})

test_that("we create lockfile from a manifest automatically when no lockfile found", {

skip_on_cran()

project_dir <- tempfile()
dir.create(project_dir)

manifest <- "resources/manifest.json"
expected_lock <- renv_lockfile_from_manifest("resources/manifest.json")
file.copy(manifest, file.path(project_dir, "manifest.json"))

# when called with `strict = TRUE` does not create manifest
expect_error(renv_lockfile_load(project_dir, strict = TRUE))

# creates and reads lockfile
obtained_lock <- renv_lockfile_load(project_dir)
expect_identical(expected_lock, obtained_lock)
expect_true(file.exists(file.path(project_dir, "renv.lock")))

unlink(project_dir, recursive = TRUE)
})

test_that("the Requirements field is read as character", {

lockfile <- renv_lockfile_read(text = '
Expand Down
Loading