Skip to content

Commit c082292

Browse files
authored
Merge pull request #1841 from lorenzwalthert/renv-activate2
Avoid warnings with R hooks when renv version don't match
2 parents 0107df0 + b8fff8c commit c082292

File tree

7 files changed

+919
-18
lines changed

7 files changed

+919
-18
lines changed

pre_commit/languages/r.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from pre_commit.util import cmd_output_b
1616

1717
ENVIRONMENT_DIR = 'renv'
18+
RSCRIPT_OPTS = ('--no-save', '--no-restore', '--no-site-file', '--no-environ')
1819
get_default_version = helpers.basic_get_default_version
1920
healthy = helpers.basic_healthy
2021

@@ -69,12 +70,11 @@ def _entry_validate(entry: Sequence[str]) -> None:
6970

7071

7172
def _cmd_from_hook(hook: Hook) -> Tuple[str, ...]:
72-
opts = ('--no-save', '--no-restore', '--no-site-file', '--no-environ')
7373
entry = shlex.split(hook.entry)
7474
_entry_validate(entry)
7575

7676
return (
77-
*entry[:1], *opts,
77+
*entry[:1], *RSCRIPT_OPTS,
7878
*_prefix_if_file_entry(entry, hook.prefix),
7979
*hook.args,
8080
)
@@ -89,22 +89,23 @@ def install_environment(
8989
with clean_path_on_failure(env_dir):
9090
os.makedirs(env_dir, exist_ok=True)
9191
shutil.copy(prefix.path('renv.lock'), env_dir)
92+
shutil.copytree(prefix.path('renv'), os.path.join(env_dir, 'renv'))
9293
cmd_output_b(
9394
'Rscript', '--vanilla', '-e',
9495
f"""\
9596
prefix_dir <- {prefix.prefix_dir!r}
96-
missing_pkgs <- setdiff(
97-
"renv", unname(installed.packages()[, "Package"])
98-
)
9997
options(
10098
repos = c(CRAN = "https://cran.rstudio.com"),
10199
renv.consent = TRUE
102100
)
103-
install.packages(missing_pkgs)
104-
renv::activate()
101+
source("renv/activate.R")
105102
renv::restore()
106103
activate_statement <- paste0(
107-
'renv::activate("', file.path(getwd()), '"); '
104+
'suppressWarnings({{',
105+
'old <- setwd("', getwd(), '"); ',
106+
'source("renv/activate.R"); ',
107+
'setwd(old); ',
108+
'renv::load("', getwd(), '");}})'
108109
)
109110
writeLines(activate_statement, 'activate.R')
110111
is_package <- tryCatch(
@@ -122,12 +123,13 @@ def install_environment(
122123
cwd=env_dir,
123124
)
124125
if additional_dependencies:
125-
cmd_output_b(
126-
'Rscript', '-e',
127-
'renv::install(commandArgs(trailingOnly = TRUE))',
128-
*additional_dependencies,
129-
cwd=env_dir,
130-
)
126+
with in_env(prefix, version):
127+
cmd_output_b(
128+
'Rscript', *RSCRIPT_OPTS, '-e',
129+
'renv::install(commandArgs(trailingOnly = TRUE))',
130+
*additional_dependencies,
131+
cwd=env_dir,
132+
)
131133

132134

133135
def run_hook(
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright 2021 RStudio, PBC
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

0 commit comments

Comments
 (0)