From 60983d395f841b06c42b8db4d29974e355c0df2c Mon Sep 17 00:00:00 2001 From: Marco Ferrari Date: Thu, 21 Dec 2023 20:52:57 +0100 Subject: [PATCH] fix: fail if r package installation fails (#4994) - Fail if the installation of a R package fails. - Install the remotes package once during the image build, and not when we scan files at runtime. - Reuse the default R library directory instead of moving it to /home/r-library --- Dockerfile | 4 ++-- lib/functions/detectFiles.sh | 1 - scripts/install-lintr.sh | 6 +----- scripts/install-r-package-or-fail.R | 12 ++++++++++++ test/inspec/super-linter/controls/super_linter.rb | 2 -- 5 files changed, 15 insertions(+), 10 deletions(-) create mode 100755 scripts/install-r-package-or-fail.R diff --git a/Dockerfile b/Dockerfile index 8dd95968..5a7747c5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -219,8 +219,8 @@ RUN --mount=type=secret,id=GITHUB_TOKEN /install-glibc.sh && rm -rf /install-gli ################# # Install Lintr # ################# -COPY scripts/install-lintr.sh / -RUN /install-lintr.sh && rm -rf /install-lintr.sh +COPY scripts/install-lintr.sh scripts/install-r-package-or-fail.R / +RUN /install-lintr.sh && rm -rf /install-lintr.sh /install-r-package-or-fail.R ################################# # Install luacheck and luarocks # diff --git a/lib/functions/detectFiles.sh b/lib/functions/detectFiles.sh index 67ccffd4..d55d416f 100755 --- a/lib/functions/detectFiles.sh +++ b/lib/functions/detectFiles.sh @@ -479,7 +479,6 @@ function RunAdditionalInstalls() { ############################## INSTALL_CMD=$( cd "${WORKSPACE_PATH}" || exit 0 - R -e "install.packages('remotes', repos = 'https://cloud.r-project.org/')" 2>&1 R -e "remotes::install_local('.', dependencies=T)" 2>&1 ) diff --git a/scripts/install-lintr.sh b/scripts/install-lintr.sh index ea2cd402..7a454ef5 100755 --- a/scripts/install-lintr.sh +++ b/scripts/install-lintr.sh @@ -2,8 +2,4 @@ set -euo pipefail -mkdir -p /home/r-library -cp -r /usr/lib/R/library/ /home/r-library/ -Rscript -e "install.packages(c('lintr','purrr'), repos = 'https://cloud.r-project.org/')" -R -e "install.packages(list.dirs('/home/r-library',recursive = FALSE), repos = NULL, type = 'source')" -mv /etc/R/* /usr/lib/R/etc/ +Rscript --no-save /install-r-package-or-fail.R lintr purrr remotes diff --git a/scripts/install-r-package-or-fail.R b/scripts/install-r-package-or-fail.R new file mode 100755 index 00000000..c53067a3 --- /dev/null +++ b/scripts/install-r-package-or-fail.R @@ -0,0 +1,12 @@ +#!/usr/bin/env Rscript + +# Based on: https://stackoverflow.com/questions/26244530/how-do-i-make-install-packages-return-an-error-if-an-r-package-cannot-be-install + +packages = commandArgs(trailingOnly=TRUE) + +for (l in packages) { + install.packages(l, repos='https://cloud.r-project.org/'); + if ( ! library(l, character.only=TRUE, logical.return=TRUE) ) { + quit(status=1, save='no') + } +} diff --git a/test/inspec/super-linter/controls/super_linter.rb b/test/inspec/super-linter/controls/super_linter.rb index eda1cf94..31e82a89 100644 --- a/test/inspec/super-linter/controls/super_linter.rb +++ b/test/inspec/super-linter/controls/super_linter.rb @@ -402,7 +402,6 @@ control "super-linter-validate-directories" do desc "Check that directories that Super-Linter needs are installed." dirs = [ - "/home/r-library", "/node_modules", "/action/lib", "/action/lib/functions", @@ -413,7 +412,6 @@ control "super-linter-validate-directories" do # Removed linters from slim image SLIM_IMAGE_REMOVED_DIRS=%w( - /home/r-library ) dirs.each do |item|