Skip to content

settings$encoding used before set #2803

@MichaelChirico

Description

@MichaelChirico

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions