Skip to content

Commit

Permalink
Merge pull request #163 from r-lib/fix/r-cmd-crash
Browse files Browse the repository at this point in the history
Handle R CMD check crashes better
  • Loading branch information
gaborcsardi authored Sep 22, 2021
2 parents ab1e3bc + 5f19b29 commit cabc75f
Show file tree
Hide file tree
Showing 18 changed files with 320 additions and 169 deletions.
2 changes: 1 addition & 1 deletion R/background.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ rcc_init <- function(self, private, super, path, args, build_args,
# The updated PATH is also inherited in the subprocess below.
if (should_use_rs_pandoc()) local_path(Sys.getenv("RSTUDIO_PANDOC"))

pkgbuild::local_build_tools()
pkgbuild::without_cache(pkgbuild::local_build_tools())

targz <- build_package(path, check_dir, build_args = build_args,
libpath = libpath, quiet = TRUE)
Expand Down
36 changes: 30 additions & 6 deletions R/package.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ NULL
#' errors as well. If `"note"`, then any check failure generated an
#' error. Its default can be modified with the `RCMDCHECK_ERROR_ON`
#' environment variable. If that is not set, then `"never"` is used.
#' @param env A named character vector, rxtra environment variables to
#' set in the check process.
#' @return An S3 object (list) with fields `errors`,
#' `warnings` and `notes`. These are all character
#' vectors containing the output for the failed check.
Expand All @@ -119,7 +121,8 @@ rcmdcheck <- function(
error_on = Sys.getenv(
"RCMDCHECK_ERROR_ON",
c("never", "error", "warning", "note")[1]
)) {
),
env = character()) {

error_on <- match.arg(error_on, c("never", "error", "warning", "note"))

Expand All @@ -139,7 +142,7 @@ rcmdcheck <- function(
# Add pandoc to the PATH, for R CMD build and R CMD check
if (should_use_rs_pandoc()) local_path(Sys.getenv("RSTUDIO_PANDOC"))

pkgbuild::local_build_tools()
pkgbuild::without_cache(pkgbuild::local_build_tools())

targz <- build_package(path, check_dir, build_args = build_args,
libpath = libpath, quiet = quiet)
Expand All @@ -156,7 +159,8 @@ rcmdcheck <- function(
libpath = libpath,
repos = repos,
quiet = quiet,
timeout = timeout
timeout = timeout,
env = env
)
)

Expand Down Expand Up @@ -185,7 +189,7 @@ rcmdcheck <- function(
#' @importFrom withr with_envvar

do_check <- function(targz, package, args, libpath, repos,
quiet, timeout) {
quiet, timeout, env) {

session_output <- tempfile()
profile <- make_fake_profile(session_output = session_output)
Expand All @@ -198,7 +202,11 @@ do_check <- function(targz, package, args, libpath, repos,
rlibsuser <- if (callr_version < "3.0.0.9001")
paste(libpath, collapse = .Platform$path.sep)

chkenv <- callr::rcmd_safe_env()
if (length(env)) chkenv[names(env)] <- env

if (!quiet) cat_head("R CMD check")
callback <- if (!quiet) detect_callback(as_cran = "--as-cran" %in% args)
res <- with_envvar(
c(R_PROFILE_USER = profile, R_LIBS_USER = rlibsuser),
rcmd_safe(
Expand All @@ -208,13 +216,29 @@ do_check <- function(targz, package, args, libpath, repos,
user_profile = TRUE,
repos = repos,
stderr = "2>&1",
block_callback = if (!quiet) detect_callback(as_cran = "--as-cran" %in% args),
block_callback = callback,
spinner = !quiet && should_add_spinner(),
timeout = timeout,
fail_on_status = FALSE
fail_on_status = FALSE,
env = chkenv
)
)

# To print an incomplete line on timeout or crash
if (!is.null(callback) && (res$timeout || res$status != 0)) callback("\n")

# Non-zero status is an error, the check process failed
# R CMD check returns 1 for installation errors, we don't want to error
# for those.
if (res$status != 0 && res$status != 1) {
stop(
call. = FALSE,
"R CMD check process failed with exit status ", res$status,
"\n\nStandard output and error:\n",
res$stdout
)
}

list(result = res, session_info = session_output)
}

Expand Down
6 changes: 5 additions & 1 deletion man/rcmdcheck.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

172 changes: 86 additions & 86 deletions tests/testthat/_snaps/callback.md

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions tests/testthat/bad3/tests/testthat/test-bad.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ context("bad")

test_that("good", {
expect_true(TRUE)
expect_true(FALSE)
})
18 changes: 18 additions & 0 deletions tests/testthat/bad4/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Package: badpackage
Title: Advice on R package building
Date: 2016 February
Version: 1.0.0
Author: Gabor Csardi
Maintainer: Gabor Csardi <[email protected]>
Description: Give advice about good practices when building R packages.
License: GPL
Suggests:
testthat,
knitr,
rmarkdown,
svglite
RoxygenNote: 6.1.0
Repository: CRAN
Encoding: UTF-8
VignetteBuilder: knitr
Config/build/clean-inst-doc: FALSE
3 changes: 3 additions & 0 deletions tests/testthat/bad4/NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Generated by roxygen2: do not edit by hand

export(foobar2)
20 changes: 20 additions & 0 deletions tests/testthat/bad4/R/package.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

foobar <- function(argument) {
argument
}

#' foobar2
#' @export
#' @examples
#' foobar2()

foobar2 <- function() {
outfile <- Sys.getenv("RCMDCHECK_OUTPUT", "")
if (nzchar(outfile)) {
cat(Sys.getpid(), sep = "\n", file = outfile)
}
}

foobar2()

while (TRUE) Sys.sleep(1000)
14 changes: 14 additions & 0 deletions tests/testthat/bad4/man/foobar2.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 15 additions & 15 deletions tests/testthat/fixtures/checks/comparing.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
* using log directory /private/tmp/readr.Rcheck
* using log directory '/private/tmp/readr.Rcheck'
* using R version 4.1.0 (2021-05-18)
* using platform: x86_64-apple-darwin17.0 (64-bit)
* using session charset: UTF-8
* checking for file readr/DESCRIPTION ... OK
* this is package readr version 2.0.1.9000
* checking for file 'readr/DESCRIPTION' ... OK
* this is package 'readr' version '2.0.1.9000'
* package encoding: UTF-8
* checking package namespace information ... OK
* checking package dependencies ... OK
Expand All @@ -13,10 +13,10 @@
* checking for hidden files and directories ... OK
* checking for portable file names ... OK
* checking for sufficient/correct file permissions ... OK
* checking whether package readr can be installed ... OK
* checking whether package 'readr' can be installed ... OK
* checking installed package size ... OK
* checking package directory ... OK
* checking build directory ... OK
* checking 'build' directory ... OK
* checking DESCRIPTION meta-information ... OK
* checking top-level files ... OK
* checking for left-over files ... OK
Expand Down Expand Up @@ -46,24 +46,24 @@
* checking R/sysdata.rda ... OK
* checking line endings in C/C++/Fortran sources/headers ... OK
* checking compiled code ... OK
* checking installed files from inst/doc ... OK
* checking files in vignettes ... OK
* checking installed files from 'inst/doc' ... OK
* checking files in 'vignettes' ... OK
* checking examples ... OK
* checking for unstated dependencies in tests ... OK
* checking for unstated dependencies in 'tests' ... OK
* checking tests ...
Running first_edition.R
Running second_edition.R
Running spelling.R
Comparing spelling.Rout to spelling.Rout.save ...6,8d5
Running 'first_edition.R'
Running 'second_edition.R'
Running 'spelling.R'
Comparing 'spelling.Rout' to 'spelling.Rout.save' ...6,8d5
< Potential spelling errors:
< WORD FOUND IN
< programatically NEWS.md:14
OK
* checking for unstated dependencies in vignettes ... OK
* checking package vignettes in inst/doc ... OK
* checking package vignettes in 'inst/doc' ... OK
* checking running R code from vignettes ...
locales.Rmd using UTF-8... OK
readr.Rmd using UTF-8... OK
'locales.Rmd' using 'UTF-8'... OK
'readr.Rmd' using 'UTF-8'... OK
NONE
* checking re-building of vignette outputs ... OK
* checking PDF version of manual ... OK
Expand Down
28 changes: 14 additions & 14 deletions tests/testthat/fixtures/checks/comparing2.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
* using log directory /Users/gaborcsardi/works/ps.Rcheck
* using log directory '/Users/gaborcsardi/works/ps.Rcheck'
* using R version 4.1.0 (2021-05-18)
* using platform: x86_64-apple-darwin17.0 (64-bit)
* using session charset: UTF-8
* checking for file ps/DESCRIPTION ... OK
* this is package ‘ps’ version 1.6.0.9000
* checking for file 'ps/DESCRIPTION' ... OK
* this is package 'ps' version '1.6.0.9000'
* package encoding: UTF-8
* checking package namespace information ... OK
* checking package dependencies ... OK
Expand All @@ -13,7 +13,7 @@
* checking for hidden files and directories ... OK
* checking for portable file names ... OK
* checking for sufficient/correct file permissions ... OK
* checking whether package ‘ps’ can be installed ... OK
* checking whether package 'ps' can be installed ... OK
* checking installed package size ... OK
* checking package directory ... OK
* checking DESCRIPTION meta-information ... OK
Expand Down Expand Up @@ -51,24 +51,24 @@
* checking use of PKG_*FLAGS in Makefiles ... OK
* checking compiled code ... OK
* checking examples ... OK
* checking for unstated dependencies in tests ... OK
* checking for unstated dependencies in 'tests' ... OK
* checking tests ...
Running test-1.R
Comparing test-1.Rout to test-1.Rout.save ... OK
Running test-2.R
Comparing test-2.Rout to test-2.Rout.save ... OK
Running test.R
Comparing test.Rout to test.Rout.save ...1,4d0
Running 'test-1.R'
Comparing 'test-1.Rout' to 'test-1.Rout.save' ... OK
Running 'test-2.R'
Comparing 'test-2.Rout' to 'test-2.Rout.save' ... OK
Running 'test.R'
Comparing 'test.Rout' to 'test.Rout.save' ...1,4d0
<
<
< >
< > cat("This is the output.\n")
6d1
< >
Running testthat.R/Library/Frameworks/R.framework/Versions/4.1/Resources/bin/BATCH: line 60: 84659 Abort trap: 6 ${R_HOME}/bin/R -f ${in} ${opts} ${R_BATCH_OPTIONS} > ${out} 2>&1
Running 'testthat.R'/Library/Frameworks/R.framework/Versions/4.1/Resources/bin/BATCH: line 60: 84659 Abort trap: 6 ${R_HOME}/bin/R -f ${in} ${opts} ${R_BATCH_OPTIONS} > ${out} 2>&1

ERROR
Running the tests in tests/testthat.R failed.
Running the tests in 'tests/testthat.R' failed.
Last 13 lines of output:
+ ## ps does not support this platform
+ reporter <- "progress"
Expand All @@ -90,5 +90,5 @@ Last 13 lines of output:

Status: 1 ERROR
See
/Users/gaborcsardi/works/ps.Rcheck/00check.log
'/Users/gaborcsardi/works/ps.Rcheck/00check.log'
for details.
22 changes: 11 additions & 11 deletions tests/testthat/fixtures/checks/test-error.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,23 @@
* checking use of PKG_*FLAGS in Makefiles ... OK
* checking compiled code ... OK
* checking examples ... OK
* checking for unstated dependencies in tests ... OK
* checking for unstated dependencies in 'tests' ... OK
* checking tests ...
Running testthat.R
Running 'testthat.R'
ERROR
Running the tests in tests/testthat.R failed.
Running the tests in 'tests/testthat.R' failed.
Last 13 lines of output:
| 0 | windows
| 0 | winver
| 0 | winver
| 6 | winver
/ | 0 | windows
/ | 0 | winver
/ | 0 | winver
v | 6 | winver

══ Results ═════════════════════════════════════════════════════════════════════
== Results =====================================================================
Duration: 13.5 s

── Skipped tests ──────────────────────────────────────────────────────────────
Needs working IPv6 connection (2)
On CRAN (13)
-- Skipped tests --------------------------------------------------------------
* Needs working IPv6 connection (2)
* On CRAN (13)

[ FAIL 1 | WARN 0 | SKIP 15 | PASS 367 ]
Error: Test failures
Expand Down
Loading

0 comments on commit cabc75f

Please sign in to comment.