Skip to content

Commit

Permalink
Helpful message when build fails for long paths (#178)
Browse files Browse the repository at this point in the history
* Helpful message when build fails for long paths

On windows. Closes #84.

* Fix typo in message

* Refactor long path msg into function
  • Loading branch information
gaborcsardi authored Oct 2, 2018
1 parent 868442f commit c06f7f4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
27 changes: 25 additions & 2 deletions R/install.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,36 @@ 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")
msg_for_long_paths(output)
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)]))
}
}

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")
}
}

Expand Down
18 changes: 12 additions & 6 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-install.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"
))
})

Expand Down

0 comments on commit c06f7f4

Please sign in to comment.