Skip to content

Support dir-nested R packages (monorepo support) #619

Closed

Description

@Tal500

Currently this language server have two modes (AFAIK):

  1. If the workspace is an R package, i.e. have a "NAMESPACE" file), then the whole code of the package will be loaded and parsed.
  2. If not, only the opened files in the editor will be parsed.

While this is a good default behavior in my opinion, I suggest to generalize this and give more control for the user, so in the same workspace there could be many (or even only one) R packages in nested directory.

My suggestion is to introduce a new option to the user configuration, say nested.packages.depth.
This option is a number specifying what is the maximal nested directory depth that the language server will scan for R package. Some special cases:

  • The default value is 0, which mathematically says exactly the current behavior specified above.
  • For full recursive scanning, the user can specify the value of Inf (don't know how to handle cycled soft/hard links though)
  • For forcing the language server to ignore any package appears in the workspace, the user can define this value to be a negative number.

What do you think?

Activity

  1. argxltuijnder commented on Oct 4, 2024

    @argxltuijnder

    While not necessarily looking for monorepo support. I am too looking for support of R packages in a subdirectory.

    When I develop an R package it is usually within a subdirectory of compared to the root of the project. Since I want to keep my package root clean an separate from all the other stuff that might come looking in project development (dockerfiles, ci-files, client-documentation, etc...)

    Having the ability to point the LSP towards the directory in which my R package lives would be REALLY great!

  2. mmyrte commented on Feb 4, 2025

    @mmyrte

    I've only looked through the code for a bit, so take this with a good helping of salt, but it might be feasible to add such a feature. HOWEVER it might be smarter to just wait for ark to mature to the point where it can be used as a standalone LSP. It already handles my situation with a nested package, but I don't want to use the Positron, where it apparently runs as a Jupyter kernel.


    The logic of reading ${workspace}/R/*.R could be changed to simply (?) look for more files by dropping this conditionality:

    on_initialized <- function(self, params) {
    logger$info("on_initialized")
    if (is_package(self$rootPath)) {
    # a bit like devtools::load_all()
    self$workspace$load_all(self)
    # TODO: result lint result of the package
    # lint_result <- lintr::lint_package(rootPath)
    }
    }

    And globbing for more R/*.r or NAMESPACE files indicating a package root:

    load_all = function(langserver) {
    source_dir <- file.path(self$root, "R")
    files <- list.files(source_dir, pattern = "\\.r$", ignore.case = TRUE)
    for (f in files) {
    logger$info("load ", f)
    path <- file.path(source_dir, f)
    uri <- path_to_uri(path)
    doc <- Document$new(uri, language = "r", version = NULL, content = stringi::stri_read_lines(path))
    self$documents$set(uri, doc)
    # TODO: move text_sync to Workspace!?
    langserver$text_sync(uri, document = doc, parse = TRUE)
    }
    self$import_from_namespace_file()
    },

    Whereas the NAMESPACE logic should make fewer assumptions about the location of the file versus the workspace root:

    import_from_namespace_file = function() {
    namespace_file <- file.path(self$root, "NAMESPACE")
    if (!file.exists(namespace_file)) {
    return(NULL)
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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