Skip to content

settings$encoding used before set #2803

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

Closed
MichaelChirico opened this issue Feb 28, 2025 · 3 comments · Fixed by #2805
Closed

settings$encoding used before set #2803

MichaelChirico opened this issue Feb 28, 2025 · 3 comments · Fixed by #2805

Comments

@MichaelChirico
Copy link
Collaborator

MichaelChirico commented Feb 28, 2025

lintr/R/lint.R

Lines 48 to 62 in a1a2cda

lines <- get_lines(filename, text)
if (needs_tempfile) {
filename <- tempfile()
con <- file(filename, open = "w", encoding = settings$encoding)
on.exit(unlink(filename), add = TRUE)
writeLines(text = lines, con = con, sep = "\n")
close(con)
}
filename <- normalize_path(filename, mustWork = !inline_data) # to ensure a unique file in cache
source_expressions <- get_source_expressions(filename, lines)
if (isTRUE(parse_settings)) {
read_settings(filename)

get_lines() is run before read_settings() but it relies on settings$encoding:

lintr/R/lint.R

Lines 685 to 691 in a1a2cda

get_lines <- function(filename, text) {
if (!is.null(text)) {
strsplit(paste(text, collapse = "\n"), "\n", fixed = TRUE)[[1L]]
} else if (re_matches(filename, rex(newline))) {
strsplit(gsub("\n$", "", filename), "\n", fixed = TRUE)[[1L]]
} else {
read_lines(filename)

lintr/R/utils.R

Line 161 in a1a2cda

read_lines <- function(file, encoding = settings$encoding, ...) {

I seem to recall running into issues moving read_settings() earlier in lint(), but this situation seems incorrect.

This came up in #2801 -- when trying to move away from manually running lintr:::read_settings() in the test, we fail because get_source_expressions() doesn't know the encoding, and neither does moving to accessing the file by lint() instead help:

test_that("Can read non UTF-8 file", {
file <- test_path("dummy_projects", "project", "cp1252.R")
lintr:::read_settings(file)
expect_null(get_source_expressions(file)$error)
})

@MichaelChirico MichaelChirico changed the title settings used before set settings$encoding used before set Feb 28, 2025
@MichaelChirico
Copy link
Collaborator Author

Here's the test_dir() results if we make the simple change:

══ Failed tests ════════════════════════════════════════════════════════════════════════════════════════════════════
── Error (test-backport_linter.R:2:3): backport_linter produces error when R version misspecified ──────────────────────────────────────────────────────────────────────────────────
Error in `lint(text = "numToBits(2)", linters = backport_linter(420L))`: argument "filename" is missing, with no default
Backtrace:
    ▆
 1. ├─testthat::expect_error(...) at test-backport_linter.R:2:3
 2. │ └─testthat:::expect_condition_matching(...)
 3. │   └─testthat:::quasi_capture(...)
 4. │     ├─testthat (local) .capture(...)
 5. │     │ └─base::withCallingHandlers(...)
 6. │     └─rlang::eval_bare(quo_get_expr(.quo), quo_get_env(.quo))
 7. └─lintr::lint(text = "numToBits(2)", linters = backport_linter(420L))
 8.   └─lintr:::read_settings(filename) at lintr/R/lint.R:47:5
 9.     └─lintr:::find_config(filename) at lintr/R/settings.R:68:3
── Error (test-lint.R:124:3): lint() results from file or text should be consistent ────────────────────────────────────────────────────────────────────────────────────────────────
Error in `lint(linters = linters, text = lines)`: argument "filename" is missing, with no default
Backtrace:
    ▆
 1. └─lintr::lint(linters = linters, text = lines) at test-lint.R:124:3
 2.   └─lintr:::read_settings(filename) at lintr/R/lint.R:47:5
 3.     └─lintr:::find_config(filename) at lintr/R/settings.R:68:3
── Error (test-methods.R:101:3): print.lint works with empty lints ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Error in `lint(text = "1L")`: argument "filename" is missing, with no default
Backtrace:
    ▆
 1. └─lintr::lint(text = "1L") at test-methods.R:101:3
 2.   └─lintr:::read_settings(filename) at lintr/R/lint.R:47:5
 3.     └─lintr:::find_config(filename) at lintr/R/settings.R:68:3
── Error (test-methods.R:147:3): within.list is dispatched ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Error in `lint(text = "a=1\nb=2", linters = infix_spaces_linter())`: argument "filename" is missing, with no default
Backtrace:
    ▆
 1. └─lintr::lint(text = "a=1\nb=2", linters = infix_spaces_linter()) at test-methods.R:147:3
 2.   └─lintr:::read_settings(filename) at lintr/R/lint.R:47:5
 3.     └─lintr:::find_config(filename) at lintr/R/settings.R:68:3
── Error (test-methods.R:158:3): as_tibble.list is _not_ dispatched directly ───────────────────────────────────────────────────────────────────────────────────────────────────────
Error in `lint(text = "a = 1", linters = assignment_linter())`: argument "filename" is missing, with no default
Backtrace:
    ▆
 1. └─lintr::lint(text = "a = 1", linters = assignment_linter()) at test-methods.R:158:3
 2.   └─lintr:::read_settings(filename) at lintr/R/lint.R:47:5
 3.     └─lintr:::find_config(filename) at lintr/R/settings.R:68:3
── Error (test-methods.R:165:3): as.data.table.list is _not_ dispatched directly ───────────────────────────────────────────────────────────────────────────────────────────────────
Error in `lint(text = "a = 1", linters = assignment_linter())`: argument "filename" is missing, with no default
Backtrace:
    ▆
 1. └─lintr::lint(text = "a = 1", linters = assignment_linter()) at test-methods.R:165:3
 2.   └─lintr:::read_settings(filename) at lintr/R/lint.R:47:5
 3.     └─lintr:::find_config(filename) at lintr/R/settings.R:68:3
── Error (test-methods.R:178:3): (code run outside of `test_that()`) ───────────────────────────────────────────────────────────────────────────────────────────────────────────────
Error in `lint(text = "a", linters = test_linter())`: argument "filename" is missing, with no default
Backtrace:
    ▆
 1. └─base::eval(...)
 2.   └─base::eval(...)
 3.     └─lintr::lint(text = "a", linters = test_linter()) at test-methods.R:178:3
 4.       └─lintr:::read_settings(filename) at lintr/R/lint.R:47:5
 5.         └─lintr:::find_config(filename) at lintr/R/settings.R:68:3
── Error (test-sarif_output.R:4:3): `sarif_output` produces expected error ─────────────────────────────────────────────────────────────────────────────────────────────────────────
Error in `lint(text = "x = 1", linters = assignment_linter())`: argument "filename" is missing, with no default
Backtrace:
    ▆
 1. └─lintr::lint(text = "x = 1", linters = assignment_linter()) at test-sarif_output.R:4:3
 2.   └─lintr:::read_settings(filename) at lintr/R/lint.R:47:5
 3.     └─lintr:::find_config(filename) at lintr/R/settings.R:68:3
── Error (test-semicolon_linter.R:100:3): Compound semicolons only ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Error in `lint(text = "a <- 1;", linters = semicolon_linter(allow_trailing = TRUE, 
    allow_compound = TRUE))`: argument "filename" is missing, with no default
Backtrace:
    ▆
 1. ├─testthat::expect_error(...) at test-semicolon_linter.R:100:3
 2. │ └─testthat:::expect_condition_matching(...)
 3. │   └─testthat:::quasi_capture(...)
 4. │     ├─testthat (local) .capture(...)
 5. │     │ └─base::withCallingHandlers(...)
 6. │     └─rlang::eval_bare(quo_get_expr(.quo), quo_get_env(.quo))
 7. └─lintr::lint(...)
 8.   └─lintr:::read_settings(filename) at lintr/R/lint.R:47:5
 9.     └─lintr:::find_config(filename) at lintr/R/settings.R:68:3
── Error (test-spaces_left_parentheses_linter.R:96:3): doesn't produce a warning ───────────────────────────────────────────────────────────────────────────────────────────────────
Error in `lint(text = complex_lines, linters = spaces_left_parentheses_linter())`: argument "filename" is missing, with no default
Backtrace:
     ▆
  1. ├─testthat::expect_no_warning(lint(text = complex_lines, linters = spaces_left_parentheses_linter())) at test-spaces_left_parentheses_linter.R:96:3
  2. │ └─testthat:::expect_no_(...)
  3. │   └─testthat:::quasi_capture(enquo(object), NULL, capture)
  4. │     ├─testthat (local) .capture(...)
  5. │     │ └─rlang::try_fetch(...)
  6. │     │   └─base::withCallingHandlers(...)
  7. │     └─rlang::eval_bare(quo_get_expr(.quo), quo_get_env(.quo))
  8. └─lintr::lint(text = complex_lines, linters = spaces_left_parentheses_linter())
  9.   └─lintr:::read_settings(filename) at lintr/R/lint.R:47:5
 10.     └─lintr:::find_config(filename) at lintr/R/settings.R:68:3

[ FAIL 10 | WARN 0 | SKIP 2 | PASS 6649 ]
✖ | 1        0 | testthat [93.7s]                                                                                   
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

@MichaelChirico
Copy link
Collaborator Author

That part's easy enough -- we're doing read_settings(filename) but lint(text = ., linters = .) style has missing(filename).

@MichaelChirico
Copy link
Collaborator Author

Have something working in settings-timing branch. Still something amiss with the print methods in the text= case.

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 a pull request may close this issue.

1 participant