Skip to content

add lint for missing packages in object_usage_linter #2877

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
* New `gitlab_output()` function to output lints to GitLab format (#2858, @lschneiderbauer)
* `brace_linter()` requires `test_that()`'s `code=` argument to have curly braces (#2292, @MichaelChirico).
* `fixed_regex_linter()` recognizes usage of the new (R 4.5.0) `grepv()` wrapper of `grep()`; `regex_subset_linter()` also recommends `grepv()` alternatives (#2855, @MichaelChirico).
* `object_usage_linter()` lints missing packages that may cause false positives (#2872, @AshesITR)

### New linters

Expand Down
36 changes: 28 additions & 8 deletions R/object_usage_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,16 @@ object_usage_linter <- function(interpret_glue = NULL, interpret_extensions = c(

xml <- source_expression$full_xml_parsed_content

# Catch missing packages and report them as lints
library_lints <- list()
lint_hook <- function(lint_node, lint_msg) {
library_lints[[length(library_lints) + 1L]] <<- xml_nodes_to_lints(
lint_node, source_expression = source_expression, lint_message = lint_msg, type = "warning"
)
}

# run the following at run-time, not "compile" time to allow package structure to change
env <- make_check_env(pkg_name, xml)
env <- make_check_env(pkg_name, xml, lint_hook)

fun_assignments <- xml_find_all(xml, xpath_function_assignment)

Expand Down Expand Up @@ -144,12 +152,15 @@ object_usage_linter <- function(interpret_glue = NULL, interpret_extensions = c(
if (is.na(line_based_match)) fun_assignment else line_based_match
})

xml_nodes_to_lints(nodes, source_expression = source_expression, lint_message = res$message, type = "warning")
c(
library_lints,
xml_nodes_to_lints(nodes, source_expression = source_expression, lint_message = res$message, type = "warning")
)
})
})
}

make_check_env <- function(pkg_name, xml) {
make_check_env <- function(pkg_name, xml, lint_hook) {
if (!is.null(pkg_name)) {
parent_env <- try_silently(getNamespace(pkg_name))
}
Expand All @@ -160,7 +171,7 @@ make_check_env <- function(pkg_name, xml) {

symbols <- c(
get_assignment_symbols(xml),
get_imported_symbols(xml)
get_imported_symbols(xml, lint_hook)
)

# Just assign them an empty function
Expand Down Expand Up @@ -264,7 +275,7 @@ parse_check_usage <- function(expression,
res
}

get_imported_symbols <- function(xml) {
get_imported_symbols <- function(xml, lint_hook) {
import_exprs_xpath <- "
//SYMBOL_FUNCTION_CALL[text() = 'library' or text() = 'require']
/parent::expr
Expand All @@ -280,10 +291,19 @@ get_imported_symbols <- function(xml) {
import_exprs <- xml_find_all(xml, import_exprs_xpath)
imported_pkgs <- get_r_string(import_exprs)

unlist(lapply(imported_pkgs, function(pkg) {
unlist(lapply(seq_along(import_exprs), function(i) {
tryCatch(
getNamespaceExports(pkg),
error = function(e) character()
getNamespaceExports(imported_pkgs[[i]]),
error = function(e) {
pkg <- imported_pkgs[[i]]
lint_node <- xml2::xml_parent(import_exprs[[i]])
# nolint start: undesirable_function_name. .libPaths() is neccessary here.
lint_msg <- paste0("Could not find exported symbols for package \"", pkg, "\" in library ",
toString(shQuote(.libPaths())), ". This may lead to false positives.")
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The usual message will be "there is no package called ‘blah’".
Showing .libPaths() to help show where the package should be.

# nolint end
lint_hook(lint_node, lint_msg)
character()
}
)
}))
}
Expand Down
6 changes: 4 additions & 2 deletions tests/testthat/test-object_usage_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,6 @@ test_that("used symbols are detected correctly", {
)



# regression #1322
expect_silent(expect_lint("assign('x', 42)", NULL, object_usage_linter()))
})
Expand Down Expand Up @@ -648,7 +647,10 @@ test_that("missing libraries don't cause issue", {
a
}
"),
NULL,
list(
"Could not find exported symbols for package \"a.a.a.z.z.z\"",
line_number = 1L
),
object_usage_linter()
)
})
Expand Down
Loading