From 448a419592c748abd6bf6ed77ffbf5944012e74e Mon Sep 17 00:00:00 2001 From: Gabor Csardi Date: Tue, 2 Oct 2018 05:25:10 -0700 Subject: [PATCH 1/3] Helpful message when build fails for long paths On windows. Closes #84. --- R/install.R | 23 +++++++++++++++++++++-- R/utils.R | 18 ++++++++++++------ tests/testthat/test-install.R | 2 +- 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/R/install.R b/R/install.R index 92f1f40f..af66b503 100644 --- a/R/install.R +++ b/R/install.R @@ -91,13 +91,32 @@ safe_build_package <- function(pkgdir, build_opts, dest_path, quiet, use_pkgbuil pkgdir <- normalizePath(pkgdir) + message("Running `R CMD build`...") in_dir(dest_path, { with_envvar(env, { - output <- rcmd("build", c(build_opts, shQuote(pkgdir)), quiet = quiet) + output <- rcmd("build", c(build_opts, shQuote(pkgdir)), quiet = quiet, + fail_on_status = FALSE) }) }) - file.path(dest_path, sub("^[*] building[^[:alnum:]]+([[:alnum:]_.]+)[^[:alnum:]]+$", "\\1", output[length(output)])) + if (output$status != 0) { + cat("STDOUT:\n") + cat(output$stdout, sep = "\n") + cat("STDERR:\n") + cat(output$stderr, sep = "\n") + if (sys_type() == "windows" && + any(grepl("over-long path length", output$stderr))) { + message( + "\nIt seems that this package contains files with very long paths.\n", + "This is not supported on most Windows versions. Please contant the\n", + "package authors and tell them about this. See this GitHub issue\n", + "for more details: https://github.com/r-lib/remotes/issues/84\n") + } + stop(sprintf("Failed to `R CMD build` package, try `build = FALSE`."), + call. = FALSE) + } + + file.path(dest_path, sub("^[*] building[^[:alnum:]]+([[:alnum:]_.]+)[^[:alnum:]]+$", "\\1", output$stdout[length(output$stdout)])) } } diff --git a/R/utils.R b/R/utils.R index 7bda78d8..e8ad40cd 100644 --- a/R/utils.R +++ b/R/utils.R @@ -9,7 +9,7 @@ viapply <- function(X, FUN, ..., USE.NAMES = TRUE) { vapply(X, FUN, integer(1L), ..., USE.NAMES = USE.NAMES) } -rcmd <- function(cmd, args, path = R.home("bin"), quiet) { +rcmd <- function(cmd, args, path = R.home("bin"), quiet, fail_on_status = TRUE) { if (os_type() == "windows") { real_cmd <- file.path(path, "Rcmd.exe") args <- c(cmd, args) @@ -18,19 +18,25 @@ rcmd <- function(cmd, args, path = R.home("bin"), quiet) { args <- c("CMD", cmd, args) } - outfile <- tempfile() - status <- system2(real_cmd, args, stderr = NULL, stdout = outfile) - out <- readLines(outfile, warn = FALSE) + stdoutfile <- tempfile() + stderrfile <- tempfile() + on.exit(unlink(c(stdoutfile, stderrfile), recursive = TRUE), add = TRUE) + status <- system2(real_cmd, args, stderr = stderrfile, stdout = stdoutfile) + out <- tryCatch(readLines(stdoutfile, warn = FALSE), error = function(x) "") + err <- tryCatch(readLines(stderrfile, warn = FALSE), error = function(x) "") - if (status != 0) { + if (fail_on_status && status != 0) { + cat("STDOUT:\n") cat(out, sep = "\n") + cat("STDERR:\n") + cat(err, sep = "\n") stop(sprintf("Error running '%s' (status '%i')", cmd, status), call. = FALSE) } if (!quiet) { cat(out, sep = "\n") } - out + list(stdout = out, stderr = err, status = status) } is_bioconductor <- function(x) { diff --git a/tests/testthat/test-install.R b/tests/testthat/test-install.R index d0ca78af..cb518dc4 100644 --- a/tests/testthat/test-install.R +++ b/tests/testthat/test-install.R @@ -39,7 +39,7 @@ test_that("safe_build_package fails appropriately without pkgbuild", { capture.output( expect_error(fixed = TRUE, safe_build_package(test_path("invalidpkg"), build_opts = opts , out, quiet = TRUE, use_pkgbuild = FALSE), - "Error running 'build' (status '1')" + "Failed to `R CMD build` package" )) }) From c3cc25c37b6ea284147dbcf7d08d957427e49eb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Cs=C3=A1rdi?= Date: Tue, 2 Oct 2018 13:46:14 +0100 Subject: [PATCH 2/3] Fix typo in message --- R/install.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/install.R b/R/install.R index af66b503..9e1d0cb0 100644 --- a/R/install.R +++ b/R/install.R @@ -108,7 +108,7 @@ safe_build_package <- function(pkgdir, build_opts, dest_path, quiet, use_pkgbuil any(grepl("over-long path length", output$stderr))) { message( "\nIt seems that this package contains files with very long paths.\n", - "This is not supported on most Windows versions. Please contant the\n", + "This is not supported on most Windows versions. Please contact the\n", "package authors and tell them about this. See this GitHub issue\n", "for more details: https://github.com/r-lib/remotes/issues/84\n") } From b5a2ccedf59a8aad3568c804551439ad856e16c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Cs=C3=A1rdi?= Date: Tue, 2 Oct 2018 13:48:28 +0100 Subject: [PATCH 3/3] Refactor long path msg into function --- R/install.R | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/R/install.R b/R/install.R index 9e1d0cb0..332363a4 100644 --- a/R/install.R +++ b/R/install.R @@ -104,14 +104,7 @@ safe_build_package <- function(pkgdir, build_opts, dest_path, quiet, use_pkgbuil cat(output$stdout, sep = "\n") cat("STDERR:\n") cat(output$stderr, sep = "\n") - if (sys_type() == "windows" && - any(grepl("over-long path length", output$stderr))) { - message( - "\nIt seems that this package contains files with very long paths.\n", - "This is not supported on most Windows versions. Please contact the\n", - "package authors and tell them about this. See this GitHub issue\n", - "for more details: https://github.com/r-lib/remotes/issues/84\n") - } + msg_for_long_paths(output) stop(sprintf("Failed to `R CMD build` package, try `build = FALSE`."), call. = FALSE) } @@ -120,6 +113,17 @@ safe_build_package <- function(pkgdir, build_opts, dest_path, quiet, use_pkgbuil } } +msg_for_long_paths <- function(output) { + if (sys_type() == "windows" && + any(grepl("over-long path length", output$stderr))) { + message( + "\nIt seems that this package contains files with very long paths.\n", + "This is not supported on most Windows versions. Please contact the\n", + "package authors and tell them about this. See this GitHub issue\n", + "for more details: https://github.com/r-lib/remotes/issues/84\n") + } +} + #' Install package dependencies if needed. #' #' @inheritParams package_deps