Skip to content
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

Add missing_package_linter #547

Merged
merged 11 commits into from Nov 1, 2020
Merged

Conversation

renkun-ken
Copy link
Collaborator

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

Closes #536

image

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

@renkun-ken
Copy link
Collaborator Author

renkun-ken commented Oct 30, 2020

@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
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
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 mentioned this pull request Oct 31, 2020
@AshesITR
Copy link
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
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
Collaborator Author

Any idea why test jobs against old releases failed?

https://travis-ci.org/github/jimhester/lintr/jobs/740571821

@AshesITR
Copy link
Collaborator

AshesITR commented Nov 1, 2020

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

@AshesITR
Copy link
Collaborator

AshesITR commented Nov 1, 2020

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
Copy link
Collaborator

AshesITR commented Nov 1, 2020

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
Copy link
Collaborator

AshesITR commented Nov 1, 2020

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
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
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