Skip to content

lint(text=) finds local settings again #2863

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 9 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 .lintr
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ linters: all_linters(
`<<-` = NULL
)),
unnecessary_concatenation_linter(allow_single_expression = FALSE),
cyclocomp_linter = if (requireNamespace("cyclocomp", quietly = TRUE)) cyclocomp_linter(),
Copy link
Collaborator

Choose a reason for hiding this comment

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

This we should avoid I think. It could inadvertently disable the linter for its core purpose (linting lintr) if the dev dependencies aren't correct elsewhere.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

True... any ideas? The problem is that both R-CMD-check-hard and R-CMD-check GHA use this same config, but the former can't work without cyclocomp_linter().

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it was a good idea to not parse settings by default for lint(text=).
If we want to go back on that, maybe add a step in R-CMD-check-hard that removes our .lintr file entirely before running?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I thought so too, however, I don't know how else to restore the Emacs behavior described in the issue.

Copy link
Collaborator

Choose a reason for hiding this comment

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

If I understand the (7 year old) ess-lint code correctly, it gives all arguments to lint() by position.
We could soft-deprecate parsing settings if the code is pushed in via file= instead of text=.
WDYT?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

What's the deprecation path, exactly? Would it suffice for tools like ess-lint to explicitly supply parse_settings=TRUE? Or such tools are required to do full-file parsing instead?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think explicitly requesting parse_settings = TRUE is a good way forward.

absolute_path_linter = NULL,
library_call_linter = NULL,
nonportable_path_linter = NULL,
Expand Down
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
## Bug fixes

* `Lint()`, and thus all linters, ensures that the returned object's `message` attribute is consistently a simple character string (and not, for example, an object of class `"glue"`; #2740, @MichaelChirico).
* Files with encoding inferred from settings read more robustly under `lint(parse_settings = TRUE)` (#2803, @MichaelChirico).
* Files with encoding inferred from settings read more robustly under `lint(parse_settings = TRUE)` (#2803, @MichaelChirico). Thanks also to @bastistician for detecting a regression caused by the initial change for users of Emacs (#2847).
* `assignment_linter()` no longer errors if `"%<>%"` is an allowed operator (#2850, @AshesITR).

## Changes to default linters
Expand Down
3 changes: 1 addition & 2 deletions R/lint.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ lint <- function(filename, linters = NULL, ..., cache = FALSE, parse_settings =

needs_tempfile <- missing(filename) || re_matches(filename, rex(newline))
inline_data <- !is.null(text) || needs_tempfile
parse_settings <- !inline_data && isTRUE(parse_settings)

if (parse_settings) {
if (isTRUE(parse_settings)) {
read_settings(filename)
on.exit(reset_settings(), add = TRUE)
}
Expand Down
2 changes: 2 additions & 0 deletions R/settings.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@
read_settings <- function(filename, call = parent.frame()) {
reset_settings()

# doing lint(text=) should read settings from the current directory, required e.g. for Emacs, #2847
if (missing(filename)) filename <- "./any_local_file"
config_file <- find_config(filename)
default_encoding <- find_default_encoding(filename)
if (!is.null(default_encoding)) {
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-backport_linter.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
test_that("backport_linter produces error when R version misspecified", {
expect_error(
lint(text = "numToBits(2)", linters = backport_linter(420L)),
lint(text = "numToBits(2)", linters = backport_linter(420L), parse_settings = FALSE),
"`r_version` must be an R version number"
)
})
Expand Down
6 changes: 5 additions & 1 deletion tests/testthat/test-cyclocomp_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,9 @@ test_that("a null linter is returned, with warning, if cyclocomp is unavailable"
linter <- cyclocomp_linter(1L)
})

expect_error(lint(text = "if (TRUE) 1 else 2", linters = linter), "disabled", fixed = TRUE)
expect_error(
lint(text = "if (TRUE) 1 else 2", linters = linter, parse_settings = FALSE),
"disabled",
fixed = TRUE
)
})
19 changes: 13 additions & 6 deletions tests/testthat/test-lint.R
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,13 @@ test_that("lint() results from file or text should be consistent", {
file <- normalize_path(file)

lint_from_file <- lint(file, linters = linters)
lint_from_lines <- lint(linters = linters, text = lines)
lint_from_text <- lint(linters = linters, text = text)
lint_from_lines <- lint(linters = linters, text = lines, parse_settings = FALSE)
lint_from_text <- lint(linters = linters, text = text, parse_settings = FALSE)

# Remove file before linting to ensure that lint works and do not
# assume that file exists when both filename and text are supplied.
expect_identical(unlink(file), 0L)
lint_from_text2 <- lint(file, linters = linters, text = text)
lint_from_text2 <- lint(file, linters = linters, text = text, parse_settings = FALSE)

expect_length(lint_from_file, 2L)
expect_length(lint_from_lines, 2L)
Expand Down Expand Up @@ -205,12 +205,12 @@ test_that("old compatibility usage errors", {
)

expect_error(
lint("a <- 1\n", linters = function(two, arguments) NULL),
lint("a <- 1\n", linters = function(two, arguments) NULL, parse_settings = FALSE),
error_msg
)

expect_error(
lint("a <- 1\n", linters = "equals_na_linter"),
lint("a <- 1\n", linters = "equals_na_linter", parse_settings = FALSE),
error_msg
)
})
Expand All @@ -234,7 +234,6 @@ test_that("typo in argument name gives helpful error", {
expect_error(lint("xxx", litners = identity), "Found unknown arguments in `...`: `litners`")
})


test_that("gitlab_output() writes expected report", {
skip_if_not_installed("jsonlite")

Expand Down Expand Up @@ -279,3 +278,11 @@ test_that("gitlab_output() writes expected report", {
))
)
})

test_that("settings are picked up under lint(text=)", {
.lintr <- withr::local_tempfile(lines = "linters: list(assignment_linter())")
withr::local_options(lintr.linter_file = .lintr)

# lint '=', but not the operator spacing
expect_length(lint(text = "a=1"), 1L)
})
4 changes: 2 additions & 2 deletions tests/testthat/test-methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ test_that("as.data.frame.lints", {
})

test_that("summary.lints() works (no lints)", {
no_lints <- lint("x <- 1\n", linters = assignment_linter())
no_lints <- lint("x <- 1\n", linters = assignment_linter(), parse_settings = FALSE)
no_lint_summary <- summary(no_lints)
expect_s3_class(no_lint_summary, "data.frame")
expect_identical(nrow(no_lint_summary), 0L)
})

test_that("summary.lints() works (lints found)", {
has_lints <- lint("x = 1\n", linters = assignment_linter())
has_lints <- lint("x = 1\n", linters = assignment_linter(), parse_settings = FALSE)
has_lint_summary <- summary(has_lints)

expect_s3_class(has_lint_summary, "data.frame")
Expand Down
6 changes: 5 additions & 1 deletion tests/testthat/test-spaces_left_parentheses_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,11 @@ test_that("doesn't produce a warning", {
}
")

expect_no_warning(lint(text = complex_lines, linters = spaces_left_parentheses_linter()))
expect_no_warning(lint(
text = complex_lines,
linters = spaces_left_parentheses_linter(),
parse_settings = FALSE
))
})

test_that("lints vectorize", {
Expand Down
Loading