2
2
local({
3
3
4
4
# the requested version of renv
5
- version <- " 1.0.2 "
5
+ version <- " 1.0.7 "
6
6
attr(version , " sha" ) <- NULL
7
7
8
8
# the project directory
9
- project <- getwd()
9
+ project <- Sys.getenv(" RENV_PROJECT" )
10
+ if (! nzchar(project ))
11
+ project <- getwd()
10
12
11
13
# use start-up diagnostics if enabled
12
14
diagnostics <- Sys.getenv(" RENV_STARTUP_DIAGNOSTICS" , unset = " FALSE" )
@@ -31,6 +33,14 @@ local({
31
33
if (! is.null(override ))
32
34
return (override )
33
35
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
+
34
44
# next, check environment variables
35
45
# TODO: prefer using the configuration one in the future
36
46
envvars <- c(
@@ -50,9 +60,22 @@ local({
50
60
51
61
})
52
62
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
+
54
75
return (FALSE )
55
76
77
+ }
78
+
56
79
# avoid recursion
57
80
if (identical(getOption(" renv.autoloader.running" ), TRUE )) {
58
81
warning(" ignoring recursive attempt to run renv autoloader" )
@@ -108,6 +131,21 @@ local({
108
131
109
132
}
110
133
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
+
111
149
startswith <- function (string , prefix ) {
112
150
substring(string , 1 , nchar(prefix )) == prefix
113
151
}
@@ -610,6 +648,9 @@ local({
610
648
611
649
# if the user has requested an automatic prefix, generate it
612
650
auto <- Sys.getenv(" RENV_PATHS_PREFIX_AUTO" , unset = NA )
651
+ if (is.na(auto ) && getRversion() > = " 4.4.0" )
652
+ auto <- " TRUE"
653
+
613
654
if (auto %in% c(" TRUE" , " True" , " true" , " 1" ))
614
655
return (renv_bootstrap_platform_prefix_auto())
615
656
@@ -801,24 +842,23 @@ local({
801
842
802
843
# the loaded version of renv doesn't match the requested version;
803
844
# 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 )
805
847
paste(" rstudio/renv" , description [[" RemoteSha" ]], sep = " @" )
806
- } else {
848
+ else
807
849
paste(" renv" , description [[" Version" ]], sep = " @" )
808
- }
809
850
810
851
# display both loaded version + sha if available
811
852
friendly <- renv_bootstrap_version_friendly(
812
853
version = description [[" Version" ]],
813
- sha = description [[" RemoteSha" ]]
854
+ sha = if ( dev ) description [[" RemoteSha" ]]
814
855
)
815
856
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
+ " )
822
862
catf(fmt , friendly , renv_bootstrap_version_friendly(version ), remote )
823
863
824
864
FALSE
@@ -1034,27 +1074,14 @@ local({
1034
1074
1035
1075
}
1036
1076
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
-
1050
1077
renv_json_read <- function (file = NULL , text = NULL ) {
1051
1078
1052
1079
jlerr <- NULL
1053
1080
1054
1081
# if jsonlite is loaded, use that instead
1055
1082
if (" jsonlite" %in% loadedNamespaces()) {
1056
1083
1057
- json <- catch (renv_json_read_jsonlite(file , text ))
1084
+ json <- tryCatch (renv_json_read_jsonlite(file , text ), error = identity )
1058
1085
if (! inherits(json , " error" ))
1059
1086
return (json )
1060
1087
@@ -1063,7 +1090,7 @@ local({
1063
1090
}
1064
1091
1065
1092
# 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 )
1067
1094
if (! inherits(json , " error" ))
1068
1095
return (json )
1069
1096
@@ -1076,14 +1103,14 @@ local({
1076
1103
}
1077
1104
1078
1105
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 " )
1080
1107
jsonlite :: fromJSON(txt = text , simplifyVector = FALSE )
1081
1108
}
1082
1109
1083
1110
renv_json_read_default <- function (file = NULL , text = NULL ) {
1084
1111
1085
1112
# find strings in the JSON
1086
- text <- paste(text %|| % read (file ), collapse = " \n " )
1113
+ text <- paste(text %|| % readLines (file , warn = FALSE ), collapse = " \n " )
1087
1114
pattern <- ' ["](?:(?:\\\\ .)|(?:[^"\\\\ ]))*?["]'
1088
1115
locs <- gregexpr(pattern , text , perl = TRUE )[[1 ]]
1089
1116
@@ -1131,14 +1158,14 @@ local({
1131
1158
map <- as.list(map )
1132
1159
1133
1160
# remap strings in object
1134
- remapped <- renv_json_remap (json , map )
1161
+ remapped <- renv_json_read_remap (json , map )
1135
1162
1136
1163
# evaluate
1137
1164
eval(remapped , envir = baseenv())
1138
1165
1139
1166
}
1140
1167
1141
- renv_json_remap <- function (json , map ) {
1168
+ renv_json_read_remap <- function (json , map ) {
1142
1169
1143
1170
# fix names
1144
1171
if (! is.null(names(json ))) {
@@ -1165,7 +1192,7 @@ local({
1165
1192
# recurse
1166
1193
if (is.recursive(json )) {
1167
1194
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 ))
1169
1196
}
1170
1197
}
1171
1198
@@ -1185,16 +1212,8 @@ local({
1185
1212
# construct full libpath
1186
1213
libpath <- file.path(root , prefix )
1187
1214
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 )
1198
1217
1199
1218
invisible ()
1200
1219
0 commit comments