Skip to content

Commit

Permalink
closes #1788
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinushey committed Jan 16, 2024
1 parent 7cdc28b commit 5cdbd72
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 31 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Suggests: BiocManager, cli, covr, cpp11, devtools, gitcreds, jsonlite, knitr, mi
packrat, pak, R6, remotes, reticulate, rmarkdown, rstudioapi, shiny, testthat,
uuid, waldo, yaml, webfakes
Encoding: UTF-8
RoxygenNote: 7.2.3
RoxygenNote: 7.3.0
Roxygen: list(markdown = TRUE)
VignetteBuilder: knitr
Config/Needs/website: tidyverse/tidytemplate
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# renv (development version)

* Fixed an issue where `renv` did not report out-of-sync packages when
one or more packages used in the project were not installed. (#1788)

* Fixed an issue where `renv` could over-aggressively activate P3M
repositories when initializing a project. (#1782)

Expand Down
65 changes: 37 additions & 28 deletions R/status.R
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,11 @@ status <- function(project = NULL,
renv_lockfile_records(lockfile) <- omit(renv_lockfile_records(lockfile), ignored)
renv_lockfile_records(library) <- omit(renv_lockfile_records(library), ignored)

synchronized <-
renv_status_check_consistent(lockfile, library, packages) &&
renv_status_check_synchronized(lockfile, library) &&
synchronized <- all(
renv_status_check_consistent(lockfile, library, packages),
renv_status_check_synchronized(lockfile, library),
renv_status_check_version(lockfile)
)

if (sources) {
synchronized <- synchronized &&
Expand All @@ -195,7 +196,7 @@ status <- function(project = NULL,
if (synchronized)
writef("No issues found -- the project is in a consistent state.")
else
writef(c("", "See ?renv::status() for advice on resolving these issues."))
writef("See ?renv::status() for advice on resolving these issues.")

result <- list(
library = library,
Expand All @@ -218,37 +219,44 @@ renv_status_check_consistent <- function(lockfile, library, used) {

packages <- sort(unique(c(names(library), names(lockfile), used)))

status <- data.frame(
package = packages,
status <- data_frame(
package = packages,
installed = packages %in% names(library),
recorded = packages %in% names(lockfile),
used = packages %in% used
recorded = packages %in% names(lockfile),
used = packages %in% used
)

ok <- status$installed & (status$used == status$recorded)
if (all(ok))
return(TRUE)

if (renv_verbose()) {
# If any packages are not installed, we don't know for sure what's used
# because our dependency graph is incomplete
issues <- status[!ok, , drop = FALSE]
missing <- !issues$installed
issues$installed <- ifelse(issues$installed, "y", "n")
issues$recorded <- ifelse(issues$recorded, "y", "n")
issues$used <- ifelse(issues$used, "y", if (any(missing)) "?" else "n")

if (any(missing)) {
msg <- "The following package(s) are missing:"
issues <- issues[missing, ]
} else {
msg <- "The following package(s) are in an inconsistent state:"
}
writef(msg)
writef()
print(issues, row.names = FALSE, right = FALSE)
if (!renv_verbose())
return(FALSE)

issues <- status[!ok, , drop = FALSE]
missing <- issues$used & !issues$installed
if (all(missing)) {

caution_bullets(
preamble = "The following package(s) are used in this project, but are not installed:",
values = issues$package[missing]
)

return(FALSE)

}

issues$installed <- ifelse(issues$installed, "y", "n")
issues$recorded <- ifelse(issues$recorded, "y", "n")
issues$used <- ifelse(issues$used, "y", if (any(missing)) "?" else "n")

preamble <- "The following package(s) are in an inconsistent state:"

writef(preamble)
writef()
print(issues, row.names = FALSE, right = FALSE)
writef()

FALSE

}
Expand Down Expand Up @@ -302,9 +310,8 @@ renv_status_check_synchronized <- function(lockfile, library) {
actions <- renv_lockfile_diff_packages(lockfile, library)
rest <- c("upgrade", "downgrade", "crossgrade")

if (all(!rest %in% actions)) {
if (all(!rest %in% actions))
return(TRUE)
}

pkgs <- names(actions[actions %in% rest])
formatter <- function(lhs, rhs)
Expand All @@ -329,6 +336,8 @@ renv_status_check_version <- function(lockfile) {

fmt <- "The lockfile was generated with R %s, but you're using R %s."
writef(fmt, version, getRversion())
writef()

FALSE

}
Expand Down
18 changes: 16 additions & 2 deletions tests/testthat/_snaps/status.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
Code
. <- status()
Output
The following package(s) are missing:
The following package(s) are in an inconsistent state:
package installed recorded used
bread n n y
Expand Down Expand Up @@ -67,7 +67,6 @@
- egg [repo: * != CRAN; ver: 2.0.0 != 1.0.0]
- oatmeal [repo: * != CRAN; ver: 0.9.0 != 1.0.0]
See ?renv::status() for advice on resolving these issues.

# status() notifies user if R version does not match
Expand All @@ -79,3 +78,18 @@
See ?renv::status() for advice on resolving these issues.

# status() notifies user if packages are missing and inconsistent

Code
. <- status()
Output
The following package(s) are used in this project, but are not installed:
- breakfast
The following package(s) are out of sync [lockfile != library]:
# CRAN ---
- bread [1.0.0 != 0.1.0]
See ?renv::status() for advice on resolving these issues.

12 changes: 12 additions & 0 deletions tests/testthat/test-status.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,15 @@ test_that("status() notifies user if R version does not match", {
expect_snapshot(. <- status())

})

test_that("status() notifies user if packages are missing and inconsistent", {

project <- renv_tests_scope("bread")
init()

writeLines("library(breakfast)", con = "_deps.R")
install("[email protected]")

expect_snapshot(. <- status())

})

0 comments on commit 5cdbd72

Please sign in to comment.