From 4f02838d9bbbbd2969434b6210575fc8c6dc1215 Mon Sep 17 00:00:00 2001 From: Lionel Henry Date: Mon, 6 Nov 2023 13:31:01 +0100 Subject: [PATCH] Set `ROXYGEN_PKG` envvar in roxygen setup Closes #1517 --- NEWS.md | 3 +++ R/namespace.R | 4 ++-- R/roxygenize-setup.R | 16 +++++++++++++--- tests/testthat/test-namespace.R | 2 +- 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/NEWS.md b/NEWS.md index ff1f2ac6..248a35b7 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,8 @@ # roxygen2 (development version) +* The `ROXYGEN_PKG` environment variable is now set up while roxygen + is running to the name of the package being documented (#1517). + * Import directives are now ignored if they try to import from the package being documented. This is useful to add self-dependencies in standalone files meant to be used in other packages (r-lib/usethis#1853). diff --git a/R/namespace.R b/R/namespace.R index 635a49a0..19be1eff 100644 --- a/R/namespace.R +++ b/R/namespace.R @@ -326,7 +326,7 @@ repeat_first <- function(name, x) { } one_per_line_ignore_current <- function(name, x) { - current <- getOption("roxygen2:::package") + current <- peek_roxygen_pkg() # Ignore any occurrence of `current` inside `x` if (is_string(current)) { @@ -336,7 +336,7 @@ one_per_line_ignore_current <- function(name, x) { one_per_line(name, x) } repeat_first_ignore_current <- function(name, x) { - current <- getOption("roxygen2:::package") + current <- peek_roxygen_pkg() # Ignore the whole command if "first" is `current` if (is_string(current) && length(x) && x[[1]] == current) { diff --git a/R/roxygenize-setup.R b/R/roxygenize-setup.R index 71ae86e2..f3f21b28 100644 --- a/R/roxygenize-setup.R +++ b/R/roxygenize-setup.R @@ -25,14 +25,24 @@ roxygen_setup <- function(path = ".", man_path <- file.path(path, "man") dir.create(man_path, recursive = TRUE, showWarnings = FALSE) - local_options( - "roxygen2:::package" = desc::desc_get("Package", path), - .frame = frame + withr::local_envvar( + ROXYGEN_PKG = desc::desc_get("Package", path), + .local_envir = frame ) is_first } +peek_roxygen_pkg <- function() { + pkg <- Sys.getenv("ROXYGEN_PKG") + + if (nzchar(pkg)) { + pkg + } else { + NULL + } +} + update_roxygen_version <- function(path, cur_version = NULL) { cur <- cur_version %||% as.character(utils::packageVersion("roxygen2")) prev <- roxygen_version(path) diff --git a/tests/testthat/test-namespace.R b/tests/testthat/test-namespace.R index 92fe1e4c..6eebfd40 100644 --- a/tests/testthat/test-namespace.R +++ b/tests/testthat/test-namespace.R @@ -204,7 +204,7 @@ test_that("other namespace tags produce correct output", { }) test_that("import directives for current package are ignored", { - local_options("roxygen2:::package" = "ignored") + withr::local_envvar(c("ROXYGEN_PKG" = "ignored")) out <- roc_proc_text(namespace_roclet(), " #' @import ignored