Skip to content

The object_usage_linter won't acknowledge bindings from @import or @importFrom #1595

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
DarianGill opened this issue Apr 5, 2025 · 0 comments
Labels

Comments

@DarianGill
Copy link

DarianGill commented Apr 5, 2025

Describe the bug
The object_usage_linter that comes installed with vscode-R's lintr won't recognize functions added to package scope via @import and @importFrom calls. The linter that runs via devtools::check() or calling lintr::lint() directly, however, will. As such, there seems to be a bug in the lintr that comes packaged with vscode-R but not in other instances of lintr.

To Reproduce
Steps to reproduce the behavior:

  1. Create an R package via the R console usethis::create_package("Test")
  2. Run usethis::use_mit_license()
  3. Open the package in VSCode where you have the R extension installed
  4. Import deplyr and ggplot2 in the DESCRIPTION file

Package: Test
Title: What the Package Does (One Line, Title Case)
Version: 0.0.0.9000
Authors@R:
person("First", "Last", , "[email protected]", role = c("aut", "cre"))
Description: What the package does (one paragraph).
License: MIT + file LICENSE
Imports:
dplyr,
ggplot2,
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.2

  1. Create an R file called test in the R directory R/test.R
  2. Paste these functions into test.R
#' Group rows by name
#'
#' This function groups the rows of a data frame by the column "name"
#' @param df A data frame.
#'
#' @import ggplot2
#' @return A grouped data frame.
#' @export
group_by_name <- function(df) {
  data_grouped <- df |>
    dplyr::group_by(.data$name)
  data_grouped
}

#' Group rows by size
#'
#' This function groups the rows of a data frame by the column "size"
#' @param df A data frame.
#'
#' @importFrom ggplot2 .data
#' @return A grouped data frame.
#' @export
group_by_size <- function(df) {
  data_grouped <- df |>
    dplyr::group_by(.data$size)
  data_grouped
}

  1. Run devtools::document() and devtools::load_all()
  2. Go to problems panel or hover over .data in each of the functions and note the no visible binding for global variable '.data' warnings.
Image
  1. Run devtools::check() and see that no errors, warnings, or notes arise
  2. Run lintr::lint("R/test.R") and see ℹ No lints found.

Can you fix this issue by yourself? (We appreciate the help)

No, attempting to reinstall the R, the vscode-R extension, and rlanguageserver have not helped. Here is my settings.json file:

{
    "git.autofetch": true,
    "git.confirmSync": false,
    "[r]": {
        "editor.defaultFormatter": "REditorSupport.r"
    },
    "r.rterm.mac": "/usr/local/bin/R",
}

Expected behavior
An @import or @importFrom call should update the global namespace such that the imported function is defined for the object_usage_linter in the lintr package included by the vscode-R extension.

Screenshots

Image

Environment (please complete the following information):

  • OS: macOS
  • VSCode Version: 1.99.0 (Universal)
  • R Version: R version 4.4.2 (2024-10-31)
  • vscode-R version: 2.8.4

Additional context
Folks have a lot of issues with the object_usage_linter in lintr but I didn't find any that pertain directly to this, and as mentioned above, this doesn't seem to be an issue when invoking lintr without the vscode-R extension.

@DarianGill DarianGill added the bug label Apr 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant