HenrikBengtsson / future

:rocket: R package: future: Unified Parallel and Distributed Processing in R for Everyone
https://future.futureverse.org
951 stars 83 forks source link

R CMD build: Ignore R_FUTURE_PLAN during package build to avoid cyclic loading #541

Open HenrikBengtsson opened 3 years ago

HenrikBengtsson commented 3 years ago

Issue

$ R_FUTURE_PLAN=future.callr::callr R CMD build future.apply
* checking for file ‘future.apply/DESCRIPTION’ ... OK
* preparing ‘future.apply’:
* checking DESCRIPTION meta-information ... OK
* installing the package to build vignettes
      -----------------------------------
* installing *source* package ‘future.apply’ ...
** using staged installation
** R
** inst
** byte-compile and prepare package for lazy loading
Error: package or namespace load failed for ‘future’:
 .onLoad failed in loadNamespace() for 'future', details:
  call: loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]])
  error: cyclic namespace dependency detected when loading ‘future’, already loading ‘future.callr’, ‘future’
Execution halted
ERROR: lazy loading failed for package ‘future.apply’
* removing ‘/tmp/hb/RtmpuGbp1R/Rinst4a9660c92d74/future.apply’
      -----------------------------------
ERROR: package installation failed

Action

Have .onLoad() detect when R CMD build is running and unset R_FUTURE_PLAN.

HenrikBengtsson commented 3 years ago

Ah, here's a smaller example:

$ R_FUTURE_PLAN=future.callr::callr Rscript --vanilla -e "loadNamespace('future')"
Error: .onLoad failed in loadNamespace() for 'future', details:
  call: loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]])
  error: cyclic namespace dependency detected when loading ‘future’, already loading ‘future.callr’, ‘future’
Execution halted

and

$ R_FUTURE_PLAN=future.callr::callr R --quiet --vanilla
> loadNamespace("future")
Error: .onLoad failed in loadNamespace() for 'future', details:
  call: loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]])
  error: cyclic namespace dependency detected when loading ‘future’, already loading ‘future.callr’, ‘future’
> traceback()
3: stop(gettextf("%s failed in %s() for '%s', details:\n  call: %s\n  error: %s", 
       hookname, "loadNamespace", pkgname, deparse(conditionCall(res))[1L], 
       conditionMessage(res)), call. = FALSE, domain = NA)
2: runHook(".onLoad", env, package.lib, package)
1: loadNamespace("future")

To work around this, the future plan needs to be deferred to later (after the future package is loaded).

Details

$ R_FUTURE_DEBUG=true R_FUTURE_PLAN=future.callr::callr Rscript --vanilla -e "loadNamespace('future')"
...
[21:53:56.218] parallelly-specific environment variables:
- R_FUTURE_DEBUG=‘true’
- R_FUTURE_PLAN=‘future.callr::callr’
[21:53:56.231] Future-specific environment variables:
- R_FUTURE_DEBUG=‘true’
- R_FUTURE_PLAN=‘future.callr::callr’
...
[21:53:56.235] Environment variable ‘R_FUTURE_GLOBALS_KEEPWHERE’ not set
[21:53:56.237] Option 'future.plan' = ‘future.callr::callr’
[21:53:56.237] R process uuid: cc462a76-6aa8-84ca-8578-752f3cab43ca
[21:53:56.237] Setting plan('default')
Error: .onLoad failed in loadNamespace() for 'future', details:
  call: loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]])
  error: cyclic namespace dependency detected when loading ‘future’, already loading ‘future.callr’, ‘future’
Execution halted