R/backend-cli.R

Defines functions jarl_diagnostics air_formatting backend_text_edits backend_byte_range backend_byte_index backend_cli_failure backend_cli_run

#' Run a CLI with a UTF-8 buffer and independent output streams
#' @noRd
backend_cli_run <- function(request, arguments) {
    # Spool stdin to a private file so writing a large buffer cannot deadlock
    # against a child writing to stdout/stderr. Its original path is never written.
    input <- tempfile("backend-stdin-")
    on.exit(unlink(input), add = TRUE)
    writeBin(charToRaw(enc2utf8(paste(request$content, collapse = "\n"))), input)
    environment <- request$env
    if (length(environment)) environment <- c("current", environment) else environment <- NULL
    process <- tryCatch(processx::process$new(request$executable, arguments, stdin = input,
            wd = request$working_directory, env = environment, stdout = "|", stderr = "|",
            encoding = "UTF-8", cleanup_tree = TRUE, supervise = TRUE, windows_hide_window = TRUE),
        error = function(error) {
            backend_error(sprintf("Could not execute backend '%s': %s", request$backend$id, conditionMessage(error)))
        })
    descendants <- list()
    on.exit({
        task_kill_descendants(process, descendants)
        tryCatch(process$kill(grace = 0), error = function(e) NULL)
    }, add = TRUE)
    deadline <- proc.time()[[3L]] + request$timeout
    next_descendant_check <- -Inf
    stdout <- stderr <- character()
    repeat {
        output <- process$read_output()
        error_output <- process$read_error()
        if (nzchar(output)) stdout <- c(stdout, output)
        if (nzchar(error_output)) stderr <- c(stderr, error_output)
        alive <- process$is_alive()
        # Process exit does not imply EOF; decoded bytes may remain buffered.
        if (!alive && !process$is_incomplete_output() && !process$is_incomplete_error()) break
        # Process-table enumeration is costly on Windows. Keep it independent
        # of the number of chunks read, as in the parent task's monitoring.
        if (alive && proc.time()[[3L]] >= next_descendant_check) {
            descendants <- task_process_descendants(process, descendants)
            next_descendant_check <- proc.time()[[3L]] + 0.05
        }
        remaining <- deadline - proc.time()[[3L]]
        if (remaining <= 0) {
            # Preserve PID ancestry while discovering and killing descendants;
            # processx::run() may terminate the root before its cleanup handler.
            task_kill_descendants(process, descendants)
            process$kill(grace = 0, close_connections = FALSE)
            backend_error(sprintf("Backend '%s' timed out after %s seconds.",
                    request$backend$id, request$timeout))
        }
        if (!nzchar(output) && !nzchar(error_output)) process$poll_io(min(100, ceiling(remaining * 1000)))
    }
    list(status = process$get_exit_status(), stdout = paste0(stdout, collapse = ""),
        stderr = paste0(stderr, collapse = ""), timeout = FALSE)

}

backend_cli_failure <- function(request, result) {
    detail <- trimws(result$stderr)
    backend_error(sprintf("Backend '%s' failed (exit status %s)%s", request$backend$id,
            result$status, if (nzchar(detail)) paste0(": ", detail) else "."))
}

#' Index the submitted buffer once for all CLI diagnostics
#' @noRd
backend_byte_index <- function(content) {
    bytes <- charToRaw(enc2utf8(paste(content, collapse = "\n")))
    list(bytes = bytes, starts = c(0L, which(bytes == as.raw(10L))))
}

#' Convert half-open UTF-8 byte offsets against the submitted buffer
#' @noRd
backend_byte_range <- function(content, offsets, index = backend_byte_index(content)) {
    bytes <- index$bytes
    offsets <- unlist(offsets, use.names = FALSE)
    if (!is.numeric(offsets) || length(offsets) != 2L || anyNA(offsets) ||
            any(!is.finite(offsets)) || any(offsets != floor(offsets)) ||
            offsets[[1L]] < 0 || offsets[[2L]] < offsets[[1L]] || offsets[[2L]] > length(bytes)) {
        backend_error("CLI diagnostic contains an invalid byte range.")
    }
    starts <- index$starts
    positions <- lapply(offsets, function(offset) {
        line <- findInterval(offset, starts) - 1L
        start <- starts[[line + 1L]]
        prefix <- if (offset == start) "" else rawToChar(bytes[seq.int(start + 1L, offset)])
        if (!validUTF8(prefix)) backend_error("CLI diagnostic splits a UTF-8 character.")
        position(line, sum(ifelse(utf8ToInt(prefix) > 65535L, 2L, 1L)))
    })
    range(positions[[1L]], positions[[2L]])
}

backend_text_edits <- function(request, text) {
    old <- paste(request$content, collapse = "\n")
    # Documents store logical lines; retain the server's LF edit convention.
    text <- gsub("\r\n", "\n", text, fixed = TRUE)
    if (identical(text, old)) return(list())
    last <- length(request$content)
    ending <- if (last) position(last - 1L, code_point_to_unit(request$content[[last]],
            nchar(request$content[[last]]))) else position(0L, 0L)
    list(text_edit(range(position(0L, 0L), ending), text))
}

air_formatting <- function(request) {
    result <- backend_cli_run(request, c("format", "--stdin-file-path", request$path))
    if (!identical(result$status, 0L)) backend_cli_failure(request, result)
    if (nzchar(trimws(paste(request$content, collapse = "\n"))) && !nzchar(result$stdout)) {
        backend_error("Air returned an empty buffer for nonempty input.")
    }
    backend_text_edits(request, result$stdout)
}

jarl_diagnostics <- function(request) {
    result <- backend_cli_run(request, c("check", "--stdin-file-path", request$path,
            "--output-format", "json"))
    # Jarl exits before emitting JSON when a virtual path is excluded.
    if (identical(result$status, 0L) && !nzchar(trimws(result$stdout))) return(list())
    if (!result$status %in% c(0L, 1L, 255L)) backend_cli_failure(request, result)
    output <- tryCatch(jsonlite::fromJSON(result$stdout, simplifyVector = FALSE),
        error = function(e) backend_error("Jarl returned malformed JSON diagnostics."))
    if (!is.list(output) || !all(c("diagnostics", "errors") %in% names(output)) ||
            !is.list(output$diagnostics) || !is.list(output$errors)) {
        backend_error("Jarl JSON must contain diagnostics and errors arrays.")
    }
    index <- if (length(output$diagnostics) || length(output$errors)) backend_byte_index(request$content)
    diagnostics <- lapply(output$diagnostics, function(item) {
        if (!is.list(item) || !backend_string(item$message$body) || !backend_string(item$message$name)) {
            backend_error("Jarl returned an invalid diagnostic.")
        }
        list(range = backend_byte_range(request$content, item$range, index),
            severity = DiagnosticSeverity$Warning, source = "jarl",
            code = item$message$name, message = item$message$body,
            codeDescription = list(href = paste0("https://jarl.etiennebacher.com/rules/", item$message$name)))
    })
    for (error in output$errors) {
        if (!is.list(error) || !length(error$syntax_errors) || !is.list(error$syntax_errors)) {
            if (is.list(error) && backend_string(error$error) && !nzchar(trimws(result$stderr))) {
                result$stderr <- error$error
            }
            backend_cli_failure(request, result)
        }
        for (syntax in error$syntax_errors) {
            if (!backend_string(syntax$message)) backend_error("Jarl returned an invalid syntax error.")
            diagnostics[[length(diagnostics) + 1L]] <- list(
                range = backend_byte_range(request$content, syntax$range, index),
                severity = DiagnosticSeverity$Error, source = "jarl", message = syntax$message)
        }
    }
    if (result$status == 255L && !length(output$errors)) backend_cli_failure(request, result)
    if (result$status == 1L && !length(diagnostics)) backend_error("Jarl reported findings without diagnostics.")
    diagnostics
}

Try the languageserver package in your browser

Any scripts or data that you put into this service are public.

languageserver documentation built on Nov. 16, 2026, 9:07 a.m.