Skip to content

lintr crashes with any lints #2886

@nwiltsie

Description

@nwiltsie

Describe the bug
The R shim code used to extract the number of errors from the lintr`output seems to rely on outdated behavior in either the lintr or purrr libraries. Any code lints cause a crash.

To Reproduce
Steps to reproduce the behavior:

  1. Create an R file with a trailing semicolon:mkdir tester && cd tester && echo "a <- 1;" > source.R
  2. Run Megalinter, either in GitHub actions or locally
  3. See error
❌ Linted [R] files with [lintr]: Found 1 error(s) - (2.47s)
- Using [lintr v0.0.0] https://megalinter.io/7.3.0/descriptors/r_lintr
- MegaLinter key: [R_LINTR]
- Rules config: [.lintr]
- Number of files analyzed: [1]
[lintr] source.R - ERROR - 1 error(s)
--Error detail:
/tmp/lint/source.R:1:7: style: [semicolon_linter] Trailing semicolons are not needed.
a <- 1;
      ^
Error in `purrr::keep()`:
ℹ In index: 1.
ℹ With name: /tmp/lint/source.R.
Caused by error in `.fn()`:
! object '.type' not found
Backtrace:

  1. ├─purrr::keep(lints, ~.type == "error")
  2. │ └─purrr:::where_if(.x, .p, ...)
  3. │   └─purrr:::map_(.x, .p, ..., .type = "logical", .purrr_error_call = .purrr_error_call)
  4. │     ├─purrr:::with_indexed_errors(...)
  5. │     │ └─base::withCallingHandlers(...)
  6. │     ├─purrr:::call_with_cleanup(...)
  7. │     └─purrr (local) .f(.x[[i]], ...)
  8. │       └─global .fn(...)
  9. └─base::.handleSimpleError(...)
 10.   └─purrr (local) h(simpleError(msg, call))
 11.     └─cli::cli_abort(...)
 12.       └─rlang::abort(...)
Execution halted

Expected behavior
Lints should not crash the linter.

Screenshots
Example of the same bug in GitHub actions:
Screenshot 2023-08-14 at 12 10 39 PM

Additional context
I think this change is sufficient to resolve the issue:

diff --git a/megalinter/linters/RLinter.py b/megalinter/linters/RLinter.py
index e9714da1bf..9cde7e4ca9 100644
--- a/megalinter/linters/RLinter.py
+++ b/megalinter/linters/RLinter.py
@@ -20,7 +20,7 @@ class RLinter(Linter):
         r_commands = [
             f"lints <- lintr::lint('{file}');",
             "print(lints);",
-            "errors <- purrr::keep(lints, ~ .type == 'error');",
+            "errors <- purrr::keep(lints, \(x) x$type == 'error');",
             "quit(save = 'no', status = if (length(errors) > 0) 1 else 0)",
         ]
         # Build shell command

Metadata

Metadata

Assignees

No one assigned

    Labels

    O: stale 🤖This issue or pull request is stale, it will be closed if there is no activitybugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions