diff --git a/R/lockfile.R b/R/lockfile.R index f98513f71..e19ff9988 100644 --- a/R/lockfile.R +++ b/R/lockfile.R @@ -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)) @@ -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) } diff --git a/R/manifest-convert.R b/R/manifest-convert.R index da711c02e..4201585bb 100644 --- a/R/manifest-convert.R +++ b/R/manifest-convert.R @@ -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) diff --git a/R/paths.R b/R/paths.R index ebb353188..ee836e348 100644 --- a/R/paths.R +++ b/R/paths.R @@ -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") diff --git a/tests/testthat/test-lockfile.R b/tests/testthat/test-lockfile.R index 4e04f3d7c..d9527903f 100644 --- a/tests/testthat/test-lockfile.R +++ b/tests/testthat/test-lockfile.R @@ -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 = '