ThinkR-open / attachment

Tools to deal with dependencies in scripts, Rmd and packages
https://thinkr-open.github.io/attachment/
Other
108 stars 13 forks source link

`attachment::att_amend_desc()` loads functions but dependencies cannot be resolved. #52

Closed ALanguillaume closed 2 years ago

ALanguillaume commented 2 years ago

Expected Result

If functions are loaded and made available to the users they should be runable without any trouble.

Actual Result

If I run attachment::att_amend_desc() my package function are available in the global environment but do not run properly because R cannot resolve their dependencies.

Reprex

Below is the full reprex of the issue.

dummypackage <- tempfile('dummypackage')
suppressMessages(
  usethis::create_package(
    path = dummypackage, 
    open = FALSE,
  )
)
#> Package: dummypackage17699584f5f07
#> Title: What the Package Does (One Line, Title Case)
#> Version: 0.0.0.9000
#> Authors@R (parsed):
#>     * First Last <first.last@example.com> [aut, cre] (YOUR-ORCID-ID)
#> Description: What the package does (one paragraph).
#> License: `use_mit_license()`, `use_gpl3_license()` or friends to pick a
#>     license
#> Encoding: UTF-8
#> Roxygen: list(markdown = TRUE)
#> RoxygenNote: 7.2.0

# Add a dummy function using an external dependency
# namely the {magrittr} pipe %>% 
r_file <- file.path(dummypackage, "R", "fun.R")
file.create(r_file)
#> [1] TRUE
writeLines(
  text = "#' @importFrom magrittr %>%
#' @export
my_length <- function(x) {
  x %>% length()
}",
con = r_file
)

# Add {magrittr} in the DESCRIPTION Imports Field
usethis::use_package("magrittr")
#> Error: Path '/tmp/RtmpT8Kiax/reprex-1761e3d8a0014-milky-cub/' does not appear to be inside a project or package.

# Convert Roxygen tags to .Rd
roxygen2::roxygenise(
  package.dir = dummypackage
)
#> ℹ Loading dummypackage17699584f5f07
#> Writing 'NAMESPACE'
#> Writing 'NAMESPACE'

# => This fails because the package is loaded before the NAMESPACE is created.
# roxygen2::roxygenise() uses roxygen2::load_pkgload() which by default equals to
# pkgload::load_all(path, helpers = FALSE, attach_testthat = FALSE)$env
# This is because: "roxygen2 is a dynamic documentation system, which means it 
# works with the objects inside your package, not just the source code used to create them."
# -> First all functions are loaded then the NAMESPACE is generated. 
# my_length() is available in the global environmeent but as there was no NAMESPACE when 
# it was loaded it cannot resolve its dependencies.
my_length(1)
#> Error in x %>% length(): could not find function "%>%"

# Re running roxygen2::load_pkgload(), reloads my_length() 
# but this time the NAMESPACE does exists, R can use it to 
# resolve dependencies and then... 
roxygen2::load_pkgload(path = dummypackage)
#> ℹ Loading dummypackage17699584f5f07
#> <environment: namespace:dummypackage17699584f5f07>

# ... my_length() works
my_length(1)
#> [1] 1

# Removing the NAMESPACE
namespace <- file.path(dummypackage, "NAMESPACE")
file.remove(namespace)
#> [1] TRUE

roxygen2::roxygenise(
  package.dir = dummypackage
)
#> ℹ Loading dummypackage17699584f5f07
#> Writing 'NAMESPACE'
#> Writing 'NAMESPACE'
# Does not work
my_length(1)
#> Error in x %>% length(): could not find function "%>%"

roxygen2::roxygenise(
  package.dir = dummypackage
)
#> ℹ Loading dummypackage17699584f5f07
# Works since roxygen2::roxygenise() was run 2 times
# The NAMESPACE was around when the package was loaded the second time.
my_length(1)
#> [1] 1

# attachment::att_amend_desc() systematically remove the NAMESPACE, 
# thus running running this function 2 times wont fix the problem
attachment::att_amend_desc(
  path = dummypackage
)
#> Updating dummypackage17699584f5f07 documentation
#> ℹ Loading dummypackage17699584f5f07Writing 'NAMESPACE'Writing 'NAMESPACE'[+] 1 package(s) added: magrittr.
# Does not work
my_length(1)
#> Error in x %>% length(): could not find function "%>%"
attachment::att_amend_desc(
  path = dummypackage
)
#> Updating dummypackage17699584f5f07 documentation
#> ℹ Loading dummypackage17699584f5f07Writing 'NAMESPACE'Writing 'NAMESPACE'
# Still does not work
my_length(1)
#> Error in x %>% length(): could not find function "%>%"

attachment::att_amend_desc(
  path = dummypackage
)
#> Updating dummypackage17699584f5f07 documentation
#> ℹ Loading dummypackage17699584f5f07Writing 'NAMESPACE'Writing 'NAMESPACE'
roxygen2::load_pkgload(path = dummypackage)
#> ℹ Loading dummypackage17699584f5f07
#> <environment: namespace:dummypackage17699584f5f07>
# works !
my_length(1)
#> [1] 1

# The main culprit is attachment::att_from_namespace()
# This where the NAMESPACE is removed and recreated with roxygen2::roxygenise()
# each time
attachment::att_from_namespace(
  path = file.path(dummypackage, "NAMESPACE")
)
#> Updating dummypackage17699584f5f07 documentation
#> ℹ Loading dummypackage17699584f5f07Writing 'NAMESPACE'Writing 'NAMESPACE'
#> [1] "magrittr"
# Does not work
my_length(1)
#> Error in x %>% length(): could not find function "%>%"

roxygen2::load_pkgload(path = dummypackage)
#> ℹ Loading dummypackage17699584f5f07
#> <environment: namespace:dummypackage17699584f5f07>
# works !
my_length(1)
#> [1] 1

Created on 2022-07-08 by the reprex package (v2.0.1)

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.2.1 (2022-06-23) #> os Pop!_OS 22.04 LTS #> system x86_64, linux-gnu #> ui X11 #> language (EN) #> collate en_US.UTF-8 #> ctype en_US.UTF-8 #> tz Europe/Amsterdam #> date 2022-07-08 #> pandoc 2.18 @ /usr/lib/rstudio/bin/quarto/bin/tools/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> ! package * version date (UTC) lib source #> attachment 0.2.5.9000 2022-07-08 [1] local #> brio 1.1.3 2021-11-30 [1] CRAN (R 4.2.0) #> cli 3.3.0 2022-04-25 [1] CRAN (R 4.2.0) #> crayon 1.5.1 2022-03-26 [1] CRAN (R 4.2.0) #> desc 1.4.1 2022-03-06 [1] CRAN (R 4.2.0) #> digest 0.6.29 2021-12-01 [1] CRAN (R 4.2.0) #> R dummypackage17699584f5f07 * 0.0.0.9000 [?] #> ellipsis 0.3.2 2021-04-29 [1] CRAN (R 4.2.0) #> evaluate 0.15 2022-02-18 [1] CRAN (R 4.2.0) #> fansi 1.0.3 2022-03-24 [1] CRAN (R 4.2.0) #> fastmap 1.1.0 2021-01-25 [1] CRAN (R 4.2.0) #> fs 1.5.2 2021-12-08 [1] CRAN (R 4.2.0) #> glue 1.6.2 2022-02-24 [1] CRAN (R 4.2.0) #> highr 0.9 2021-04-16 [1] CRAN (R 4.2.0) #> htmltools 0.5.2 2021-08-25 [1] CRAN (R 4.2.0) #> knitr 1.39 2022-04-26 [1] CRAN (R 4.2.0) #> lifecycle 1.0.1 2021-09-24 [1] CRAN (R 4.2.0) #> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.2.0) #> pillar 1.7.0 2022-02-01 [1] CRAN (R 4.2.0) #> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.2.0) #> pkgload 1.3.0 2022-06-27 [1] CRAN (R 4.2.0) #> purrr 0.3.4 2020-04-17 [1] CRAN (R 4.2.0) #> R6 2.5.1 2021-08-19 [1] CRAN (R 4.2.0) #> reprex 2.0.1 2021-08-05 [1] CRAN (R 4.2.0) #> rlang 1.0.3 2022-06-27 [1] CRAN (R 4.2.0) #> rmarkdown 2.14 2022-04-25 [1] RSPM (R 4.2.0) #> roxygen2 7.2.0 2022-05-13 [1] CRAN (R 4.2.0) #> rprojroot 2.0.3 2022-04-02 [1] CRAN (R 4.2.0) #> rstudioapi 0.13 2020-11-12 [1] CRAN (R 4.2.0) #> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.2.0) #> stringi 1.7.6 2021-11-29 [1] CRAN (R 4.2.0) #> stringr 1.4.0 2019-02-10 [1] CRAN (R 4.2.0) #> testthat 3.1.4 2022-04-26 [1] CRAN (R 4.2.0) #> tibble 3.1.7 2022-05-03 [1] CRAN (R 4.2.0) #> usethis 2.1.6 2022-05-25 [1] CRAN (R 4.2.0) #> utf8 1.2.2 2021-07-24 [1] CRAN (R 4.2.0) #> vctrs 0.4.1 2022-04-13 [1] CRAN (R 4.2.0) #> withr 2.5.0 2022-03-03 [1] CRAN (R 4.2.0) #> xfun 0.31 2022-05-10 [1] CRAN (R 4.2.0) #> xml2 1.3.3 2021-11-30 [1] CRAN (R 4.2.0) #> yaml 2.3.5 2022-02-21 [1] CRAN (R 4.2.0) #> #> [1] /home/bob/R/x86_64-pc-linux-gnu-library/4.2 #> [2] /opt/R/4.2.1/lib/R/library #> #> R ── Package was removed from disk. #> #> ────────────────────────────────────────────────────────────────────────────── ```