Skip to content

Add missing_package_linter - #547

Merged
AshesITR merged 11 commits into
r-lib:masterfrom
renkun-ken:missing_package_linter
Nov 1, 2020
Merged

AshesITR merged 11 commits into
r-lib:masterfrom
renkun-ken:missing_package_linter

Conversation

@renkun-ken

@renkun-ken renkun-ken commented Oct 30, 2020 •

Copy link
Copy Markdown
Collaborator

Closes #536

image

  • Check library calls (library, require, loadNamespace, requireNamespace)
  • Test cases

@renkun-ken
renkun-ken requested a review from jimhester as a code owner October 30, 2020 06:05
@renkun-ken

renkun-ken commented Oct 30, 2020 •

Copy link
Copy Markdown
Collaborator Author

@jimhester I'm wondering which makes more sense: check both library() calls and namepsace:: calls in one linter or in separate linters so that users could choose either or both accordingly?

@AshesITR

Copy link
Copy Markdown
Collaborator

I'd suggest to separate library / require / loadNamespace (which will error if a package is not installed)
from requireNamespace, :: and :::.

The latter three are often used in packages to conditionally use functionality.
The following shouldn't lint with the library linter imho.

if (requireNamespace("data.table", quietly = TRUE)) {
  # do data.table stuff, e.g.
  .data <- data.table::as.data.table(.data)
} else {
  # do data.frame stuff
  .data <- as.data.frame(.data)
}

@renkun-ken

Copy link
Copy Markdown
Collaborator Author

@AshesITR I agree. So I won't check namespace:: calls then. I'll add some test cases for this.

BTW, do you think the linting message of missing packages should be an error or warning? Do we have a clear definition about what should be error and warning?

@renkun-ken
renkun-ken requested a review from AshesITR October 31, 2020 05:57
@renkun-ken renkun-ken mentioned this pull request Oct 31, 2020
@AshesITR

Copy link
Copy Markdown
Collaborator

Currently the only error linter is the parse errors.
I'd say uninstalled packages should be treated like the object_usage_linter which looks for missing bindings. That would be warning.

@renkun-ken

Copy link
Copy Markdown
Collaborator Author

warning makes good sense. Updated.

AshesITR
AshesITR previously approved these changes Nov 1, 2020
AshesITR
AshesITR previously approved these changes Nov 1, 2020
@renkun-ken

Copy link
Copy Markdown
Collaborator Author

@AshesITR

AshesITR commented Nov 1, 2020

Copy link
Copy Markdown
Collaborator

sprintf("%s", as.name("asd")) seems to fail in oldrels.
I think adding as.character() to all formatted symbols might fix it.

@AshesITR

AshesITR commented Nov 1, 2020 •

Copy link
Copy Markdown
Collaborator

Just looked into R 4.0.3s base::format.default().

The implementation for mode(x) == "name" is

deparse(x, backtick = FALSE)

This is in R >= 4.0.0

@AshesITR

AshesITR commented Nov 1, 2020 •

Copy link
Copy Markdown
Collaborator

A minimal compatibility fix would be adding this code:

if (getRversion() < '4.0.0') {
  # format.default handles names since R 4.0.0
  format.name <- function(x) deparse(x, backtick = FALSE)
}

But I'd prefer to use as.character() at the call sites if that works, since this way we could avoid R version specific patch code.

AshesITR
AshesITR previously approved these changes Nov 1, 2020
@AshesITR

AshesITR commented Nov 1, 2020

Copy link
Copy Markdown
Collaborator

the merge unfortunately necessitates another document() :(

@AshesITR
AshesITR merged commit f58894b into r-lib:master Nov 1, 2020
pkg_names <- parse(text = pkg_names, keep.source = FALSE)
pkg_names <- vapply(pkg_names, as.character, character(1L))

installed_packges <- .packages(all.available = TRUE)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

could this lib.paths be an argument to this linter? or perhaps known_packages as an argument. would make it more flexible to check code statically in scenarios where the packages may not be installed or the linter is run with a funny PATH

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Not-installed package linter

3 participants