Repository navigation
Description
It is a well known issue that the languageserver only parses files other than those that it is notified are opened in the case that it detects the workspace root is a package.
#253
This means that the way that workspace symbols and definitions work are not how many people expect and seem to deviate a little from the protocol specification: https://microsoft.github.io/language-server-protocol/specification#workspace_symbol.
There is also a related issue that, even in open files, source() is not understood as something that affects the workspace namespace:
https://github.com/REditorSupport/vscode-r-lsp/issues/65
I believe a pretty cheap solution to this is to allow users to configure directories that the server should parse E.g. options(languageserver.r.source.dirs = c("R", "scripts") would cause workspace$load_all() parse R files in those two top level folders, even if it cannot identify the project as a package.
Another possibly nicer alternative is for the option to work as an exclusion list, so parsing starts at "." and any paths containing folders with names defined in the option are ignored. But there seems to a lot of inertia behind the "no parse by default" position.
The implementation would be pretty cheap:
in:
on_initialized <- function(self, params) {
logger$info("on_initialized")
project_root <- self$rootPath
if (length(project_root) && is_package(project_root)) {
# a bit like devtools::load_all()
self$workspace$load_all(self)
# TODO: result lint result of the package
# lint_result <- lintr::lint_package(rootPath)
}workspace$load_all could take a vector of paths to parse.
E.g. workspace$load_all(self, source_paths)
So then we'd just need to build the vector of paths which is whatever languageserver.r.source.dirs says unioned with "R" in the case that the project is a package.
source_paths <- union(
getOption("languageserver.r.source.dirs", NULL),
if (is_package(project_root)) "R" else NULL)
)
The change to workspace$load_all is minimal too, since file.path and list.files are already vectorised:
load_all = function(langserver, source_paths) {
source_dirs <- file.path(self$root, source_paths)
files <- list.files(source_dirs, 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, NULL, 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()
},I'd also argue for recursive = TRUE in list.files.
Activity
renkun-ken commentedon Mar 11, 2021
I think the idea of using an option to control the default sources makes good sense, given that I was trying to implement #382 to handle
source()calls but soon find it much trickier than expected in the following perspectives:source()could be recursive. Handlingsource()by opening and closing documents in workspace requires that a dependency graph be well maintained, being aware of whether each document is open or not open but sourced by other open documents. Handling this on document change could be expensive, only handle source graph on document open and close might make more sense.With these thinking, handling
source()in an sensible way is not easy. An option to control default sources could solve much of the problem.MilesMcBain commentedon Mar 11, 2021
Great!
What do you think about having an exclusion list in addition to the source dirs list? It would allow something like:
options(languageserver.r.source.dirs = ".", languageserver.excluded.source.dirs = c("renv", ".drake"))etc.A bit of fancy footwork somewhere to avoid parsing R folder twice in some cases, possibly just by making files to be parsed
unique().renkun-ken commentedon Mar 11, 2021
I guess an important use case is to specify a regex or glob pattern to choose a collection of files, both could be handled by
fs::dir_ls().renkun-ken commentedon Mar 11, 2021
Also, I find
Sys.glob()useful to specify a glob pattern to specify a collection of files:MilesMcBain commentedon Mar 13, 2021
Okay I quite like this. So the options could be globs? I'm going to have a go at an implementation and see how this feels.
MilesMcBain commentedon Mar 15, 2021
Not quite ready for a PR, but it seems to be working well: https://github.com/MilesMcBain/languageserver/tree/config-src-globs
renkun-ken commentedon Mar 15, 2021
Please feel free to send a PR anytime.
sebffischer commentedon Dec 27, 2021
@MilesMcBain Do you intend to finish this PR? If not, what were the issues that you ran into?
MilesMcBain commentedon Dec 27, 2021
The main issue I had was writing tests for the code. The change itself wasn't too hard although code mat have drifted now.
MilesMcBain commentedon Dec 27, 2021
@sebffischer to answer the first part of the question for you, I'm not currently working on it and have no plans to pick it up.
gowerc commentedon Feb 3, 2022
@randy3k & @renkun-ken , Just wanted to say it would be amazing if someone could pick this up again :( This limitation in particular causes pain when writing large packages with significant testthat/ code in vscode as it means you can't reliably depend on the reference finding when re-factoring code + tests.
Even if the full feature isn't implemented it would be great to have tests/ added as a default directory that is scanned alongside the R/ directory.
(obligatury thank you guys for your work, this package makes a huge difference to doing R based development)
renkun-ken commentedon Feb 3, 2022
I'd like to summarize what we need to consider for this issue:
options(), a standalone config file (e.g..languageserver.yml), or an existing config file<project>.Rproj?workspace/didChangeWatchedFileshandled byworkspace_did_change_watched_files, which includes creating/renaming/removing a file or directory in the workspace folder.gowerc commentedon Feb 3, 2022
I mean my personal preferance would be to go down the route similar to
.gitignorewhere you just have a flat text file listing paths and use a!prefix to indicate "not" i.e.celbig commentedon Jun 20, 2023
I'm digging up this issue because that's a feature that most (if not all) people of our research team want/need. The way our projects are organized makes it very difficult to use
langagueserverin a useful way. I wrote an ugly fix for myself sometimes ago (pretty much recursively loading all R files in the workspace) but since several colleagues asked me for my hacky version of the package, I decided to clean it and make it publicly available.I made the following choices for the implementation:
languageserver.source_dirswhich should be a list of folder's pathlanguageserver.source_dirs = list()the previous behavior is conserved (the language serrver tries to detect if the workspace is a package)I do have a working version in my fork at https://github.com/celbig/languageserver but no tests for now. If you are interested, I'm willing to do a proper PR with your suggestions and requirements. Considering tests, I'm not sure of what to test but I'm willing to implement whatever you find necessary.
I hope this can help enhancing this package since this feature seems to be requested regularly
n8layman commentedon Feb 19, 2025
Just adding my voice to request this feature. Something like
options(languageserver.r.source.dirs = c("R", "scripts")would be awesome. I honestly thought that was how it was supposed to work by default and spent an hour trying to troubleshoot why I couldn't see my Roxygen comments on hover or go to the definition without first opening the file.Add nested_packages_depth to index packages in sub-directories
renkun-ken commentedon Oct 6, 2026
#751 has merged and adds configurable project indexing, including
languageserver.index_include/languageserver.index_excludeglob patterns, workspace-file change handling, and transitive staticsource()dependencies. For example:The include globs control indexing. Unrelated closed scripts receive shallow symbol summaries for workspace search; full semantic parsing is reserved for package R files, open files, and static source dependencies. Adding a directory therefore does not yet provide unrestricted hover/definition/reference scope across all of its scripts. Keeping this open for that remaining part.