Skip to content

Commit

Permalink
Warn and exclude unknown @importFrom exports (#1458)
Browse files Browse the repository at this point in the history
Fixes #1409
  • Loading branch information
MichaelChirico authored Nov 10, 2023
1 parent 7fffa3c commit 38166f0
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# roxygen2 (development version)

* A friendlier error is thrown when attempting to import non-existing
functions with `@importFrom` (#1409, @MichaelChirico).
* authors in `DESCRIPTION` can now have multiple email addresses (@jmbarbone, #1487).

* The `ROXYGEN_PKG` environment variable is now set up while roxygen
Expand Down
9 changes: 9 additions & 0 deletions R/namespace.R
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,15 @@ roxy_tag_parse.roxy_tag_importFrom <- function(x) {
}
#' @export
roxy_tag_ns.roxy_tag_importFrom <- function(x, block, env, import_only = FALSE) {
pkg <- x$val[1L]
if (requireNamespace(pkg, quietly = TRUE)) {
importing <- x$val[-1L]
unknown_idx <- !importing %in% getNamespaceExports(pkg)
if (any(unknown_idx)) {
warn_roxy_tag(x, "Excluding unknown {cli::qty(sum(unknown_idx))} export{?s} in from {.package {pkg}}: {.code {importing[unknown_idx]}}")
x$val <- c(pkg, importing[!unknown_idx])
}
}
repeat_first_ignore_current("importFrom", x$val)
}

Expand Down
35 changes: 35 additions & 0 deletions tests/testthat/test-namespace.R
Original file line number Diff line number Diff line change
Expand Up @@ -378,3 +378,38 @@ test_that("can extract non-imports from namespace preserving source", {
path <- withr::local_tempfile(lines = lines)
expect_equal(namespace_exports(path), lines[c(1:3, 5)])
})

test_that("Invalid imports throw a helpful error", {
expect_warning(
expect_equal(
roc_proc_text(namespace_roclet(), "
#' @importFrom utils head InvalidUtilsFunction
NULL
"),
"importFrom(utils,head)"
),
"Excluding unknown export",
fixed = TRUE
)

# pluralization
expect_warning(
expect_equal(
roc_proc_text(namespace_roclet(), "
#' @importFrom utils head InvalidUtilsFunction1 InvalidUtilsFunction2
NULL
"),
"importFrom(utils,head)"
),
"Excluding unknown exports"
)

# If the package is not available at roxygenize() run time, nothing we can do
expect_equal(
roc_proc_text(namespace_roclet(), "
#' @importFrom AnUnknownUnavailablePackage Unchecked
NULL
"),
"importFrom(AnUnknownUnavailablePackage,Unchecked)"
)
})

0 comments on commit 38166f0

Please sign in to comment.