Skip to content

Commit

Permalink
Set ROXYGEN_PKG envvar in roxygen setup
Browse files Browse the repository at this point in the history
Closes r-lib#1517
  • Loading branch information
lionel- committed Nov 6, 2023
1 parent 9386fd2 commit aac8a0c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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).
Expand Down
4 changes: 2 additions & 2 deletions R/namespace.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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) {
Expand Down
16 changes: 13 additions & 3 deletions R/roxygenize-setup.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-namespace.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit aac8a0c

Please sign in to comment.