Skip to content

Commit 540a57c

Browse files
committed
manually update package cache
1 parent b3cf168 commit 540a57c

File tree

2 files changed

+188
-728
lines changed

2 files changed

+188
-728
lines changed

renv/activate.R

Lines changed: 62 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
local({
33

44
# the requested version of renv
5-
version <- "1.0.2"
5+
version <- "1.0.7"
66
attr(version, "sha") <- NULL
77

88
# the project directory
9-
project <- getwd()
9+
project <- Sys.getenv("RENV_PROJECT")
10+
if (!nzchar(project))
11+
project <- getwd()
1012

1113
# use start-up diagnostics if enabled
1214
diagnostics <- Sys.getenv("RENV_STARTUP_DIAGNOSTICS", unset = "FALSE")
@@ -31,6 +33,14 @@ local({
3133
if (!is.null(override))
3234
return(override)
3335

36+
# if we're being run in a context where R_LIBS is already set,
37+
# don't load -- presumably we're being run as a sub-process and
38+
# the parent process has already set up library paths for us
39+
rcmd <- Sys.getenv("R_CMD", unset = NA)
40+
rlibs <- Sys.getenv("R_LIBS", unset = NA)
41+
if (!is.na(rlibs) && !is.na(rcmd))
42+
return(FALSE)
43+
3444
# next, check environment variables
3545
# TODO: prefer using the configuration one in the future
3646
envvars <- c(
@@ -50,9 +60,22 @@ local({
5060

5161
})
5262

53-
if (!enabled)
63+
# bail if we're not enabled
64+
if (!enabled) {
65+
66+
# if we're not enabled, we might still need to manually load
67+
# the user profile here
68+
profile <- Sys.getenv("R_PROFILE_USER", unset = "~/.Rprofile")
69+
if (file.exists(profile)) {
70+
cfg <- Sys.getenv("RENV_CONFIG_USER_PROFILE", unset = "TRUE")
71+
if (tolower(cfg) %in% c("true", "t", "1"))
72+
sys.source(profile, envir = globalenv())
73+
}
74+
5475
return(FALSE)
5576

77+
}
78+
5679
# avoid recursion
5780
if (identical(getOption("renv.autoloader.running"), TRUE)) {
5881
warning("ignoring recursive attempt to run renv autoloader")
@@ -108,6 +131,21 @@ local({
108131

109132
}
110133

134+
heredoc <- function(text, leave = 0) {
135+
136+
# remove leading, trailing whitespace
137+
trimmed <- gsub("^\\s*\\n|\\n\\s*$", "", text)
138+
139+
# split into lines
140+
lines <- strsplit(trimmed, "\n", fixed = TRUE)[[1L]]
141+
142+
# compute common indent
143+
indent <- regexpr("[^[:space:]]", lines)
144+
common <- min(setdiff(indent, -1L)) - leave
145+
paste(substring(lines, common), collapse = "\n")
146+
147+
}
148+
111149
startswith <- function(string, prefix) {
112150
substring(string, 1, nchar(prefix)) == prefix
113151
}
@@ -610,6 +648,9 @@ local({
610648

611649
# if the user has requested an automatic prefix, generate it
612650
auto <- Sys.getenv("RENV_PATHS_PREFIX_AUTO", unset = NA)
651+
if (is.na(auto) && getRversion() >= "4.4.0")
652+
auto <- "TRUE"
653+
613654
if (auto %in% c("TRUE", "True", "true", "1"))
614655
return(renv_bootstrap_platform_prefix_auto())
615656

@@ -801,24 +842,23 @@ local({
801842

802843
# the loaded version of renv doesn't match the requested version;
803844
# give the user instructions on how to proceed
804-
remote <- if (!is.null(description[["RemoteSha"]])) {
845+
dev <- identical(description[["RemoteType"]], "github")
846+
remote <- if (dev)
805847
paste("rstudio/renv", description[["RemoteSha"]], sep = "@")
806-
} else {
848+
else
807849
paste("renv", description[["Version"]], sep = "@")
808-
}
809850

810851
# display both loaded version + sha if available
811852
friendly <- renv_bootstrap_version_friendly(
812853
version = description[["Version"]],
813-
sha = description[["RemoteSha"]]
854+
sha = if (dev) description[["RemoteSha"]]
814855
)
815856

816-
fmt <- paste(
817-
"renv %1$s was loaded from project library, but this project is configured to use renv %2$s.",
818-
"- Use `renv::record(\"%3$s\")` to record renv %1$s in the lockfile.",
819-
"- Use `renv::restore(packages = \"renv\")` to install renv %2$s into the project library.",
820-
sep = "\n"
821-
)
857+
fmt <- heredoc("
858+
renv %1$s was loaded from project library, but this project is configured to use renv %2$s.
859+
- Use `renv::record(\"%3$s\")` to record renv %1$s in the lockfile.
860+
- Use `renv::restore(packages = \"renv\")` to install renv %2$s into the project library.
861+
")
822862
catf(fmt, friendly, renv_bootstrap_version_friendly(version), remote)
823863

824864
FALSE
@@ -1034,27 +1074,14 @@ local({
10341074

10351075
}
10361076

1037-
1038-
renv_bootstrap_in_rstudio <- function() {
1039-
commandArgs()[[1]] == "RStudio"
1040-
}
1041-
1042-
# Used to work around buglet in RStudio if hook uses readline
1043-
renv_bootstrap_flush_console <- function() {
1044-
tryCatch({
1045-
tools <- as.environment("tools:rstudio")
1046-
tools$.rs.api.sendToConsole("", echo = FALSE, focus = FALSE)
1047-
}, error = function(cnd) {})
1048-
}
1049-
10501077
renv_json_read <- function(file = NULL, text = NULL) {
10511078

10521079
jlerr <- NULL
10531080

10541081
# if jsonlite is loaded, use that instead
10551082
if ("jsonlite" %in% loadedNamespaces()) {
10561083

1057-
json <- catch(renv_json_read_jsonlite(file, text))
1084+
json <- tryCatch(renv_json_read_jsonlite(file, text), error = identity)
10581085
if (!inherits(json, "error"))
10591086
return(json)
10601087

@@ -1063,7 +1090,7 @@ local({
10631090
}
10641091

10651092
# otherwise, fall back to the default JSON reader
1066-
json <- catch(renv_json_read_default(file, text))
1093+
json <- tryCatch(renv_json_read_default(file, text), error = identity)
10671094
if (!inherits(json, "error"))
10681095
return(json)
10691096

@@ -1076,14 +1103,14 @@ local({
10761103
}
10771104

10781105
renv_json_read_jsonlite <- function(file = NULL, text = NULL) {
1079-
text <- paste(text %||% read(file), collapse = "\n")
1106+
text <- paste(text %||% readLines(file, warn = FALSE), collapse = "\n")
10801107
jsonlite::fromJSON(txt = text, simplifyVector = FALSE)
10811108
}
10821109

10831110
renv_json_read_default <- function(file = NULL, text = NULL) {
10841111

10851112
# find strings in the JSON
1086-
text <- paste(text %||% read(file), collapse = "\n")
1113+
text <- paste(text %||% readLines(file, warn = FALSE), collapse = "\n")
10871114
pattern <- '["](?:(?:\\\\.)|(?:[^"\\\\]))*?["]'
10881115
locs <- gregexpr(pattern, text, perl = TRUE)[[1]]
10891116

@@ -1131,14 +1158,14 @@ local({
11311158
map <- as.list(map)
11321159

11331160
# remap strings in object
1134-
remapped <- renv_json_remap(json, map)
1161+
remapped <- renv_json_read_remap(json, map)
11351162

11361163
# evaluate
11371164
eval(remapped, envir = baseenv())
11381165

11391166
}
11401167

1141-
renv_json_remap <- function(json, map) {
1168+
renv_json_read_remap <- function(json, map) {
11421169

11431170
# fix names
11441171
if (!is.null(names(json))) {
@@ -1165,7 +1192,7 @@ local({
11651192
# recurse
11661193
if (is.recursive(json)) {
11671194
for (i in seq_along(json)) {
1168-
json[i] <- list(renv_json_remap(json[[i]], map))
1195+
json[i] <- list(renv_json_read_remap(json[[i]], map))
11691196
}
11701197
}
11711198

@@ -1185,16 +1212,8 @@ local({
11851212
# construct full libpath
11861213
libpath <- file.path(root, prefix)
11871214

1188-
if (renv_bootstrap_in_rstudio()) {
1189-
# RStudio only updates console once .Rprofile is finished, so
1190-
# instead run code on sessionInit
1191-
setHook("rstudio.sessionInit", function(...) {
1192-
renv_bootstrap_exec(project, libpath, version)
1193-
renv_bootstrap_flush_console()
1194-
})
1195-
} else {
1196-
renv_bootstrap_exec(project, libpath, version)
1197-
}
1215+
# run bootstrap code
1216+
renv_bootstrap_exec(project, libpath, version)
11981217

11991218
invisible()
12001219

0 commit comments

Comments
 (0)