Skip to content

Populate NAMESPACE and Depends imports in diagnostics globals for object_usage_linter() - #775

Merged
renkun-ken merged 8 commits into
REditorSupport:masterfrom
MichaelChirico:object-usage
Oct 4, 2026
Merged

renkun-ken merged 8 commits into
REditorSupport:masterfrom
MichaelChirico:object-usage

Conversation

@MichaelChirico

@MichaelChirico MichaelChirico commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Closes #773; closes #652. Written by Gemini. It's a lot larger than I would prefer for a codebase I'm not too familiar with, so extensive feedback is welcome. LLM description follows:


Problem

  1. Workspace$get_diagnostics_globals() previously populated "languageserver:globals" with top-level definitions from R/*.R files, but omitted bindings imported via NAMESPACE (import(), import(..., except = ...), importFrom(), importMethodsFrom()) and DESCRIPTION (Depends). When linting an uninstalled package or newly added NAMESPACE imports, lintr::object_usage_linter() reported false-positive no visible global function definition and no visible binding for global variable warnings (e.g., for @import ggplot2 or @importFrom dplyr .data).
  2. When WorkspaceIndex was enabled, Workspace$get_diagnostics_globals(uri) assigned NULL for all indexed definitions (and skipped unindexed files still in self$index$pending). Because codetools::checkUsageEnterGlobal() checks function calls with exists(n, envir = w$globalenv, mode = "function"), NULL placeholders caused false-positive no visible global function definition warnings for cross-file package functions.

Solution

  • Parse NAMESPACE directives via base::parseNamespaceFile() (extract_namespace_imports()) and DESCRIPTION Depends (extract_package_imports()), sharing the parsed imports between Workspace$import_from_namespace_file() and Workspace$get_diagnostics_globals().
  • Populate "languageserver:globals" with exported symbols from imported packages (respecting import(..., except = ...)), explicitly imported objects/methods, and function closures (funct with formals or any_args_function) for package definitions.
  • Ensure pending package files in WorkspaceIndex are indexed on demand in get_diagnostics_globals(uri) and invalidate diagnostic caches when NAMESPACE is updated on disk.

@renkun-ken renkun-ken left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reproduced three correctness issues that should be addressed before merging: changing NAMESPACE can stop the server, imported-function placeholders suppress valid argument warnings, and the package-root fallback drops source dependencies for scripts outside R/. Details and suggested fixes are in the inline comments.

Validation at 32514ec: the focused workspace, server, index, and lint tests passed, including integration tests. The existing coverage shard 3 failure in lintr config file works did not reproduce locally. These cases need additional regression coverage.

Comment thread R/languageserver.R Outdated
Comment thread R/workspace.R Outdated
Comment thread R/workspace.R Outdated

@renkun-ken renkun-ken left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The three findings from my previous review are addressed in a5c1608, and the added regression tests pass locally. The focused workspace, server, lint, and index tests also pass, including integration tests; all current CI checks are green.

I found one new correctness issue in the signature-preserving import resolution: it can select a signature from the wrong package when imports overlap. The inline comment includes a reproducer. I also checked renamed imports, primitives, re-exports, and non-function bindings without finding another issue in those paths.

Comment thread R/workspace.R Outdated

@renkun-ken renkun-ken left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previously reported import-precedence reproducer now passes: Depends bindings are populated before NAMESPACE directives, and later directives replace earlier imports. I found four follow-up issues in this rewrite, detailed inline.

Local validation: the workspace-core test reproduces the two failing mad assertions from CI. The focused language-server, lint (including integration), index, document, performance-index, and intelligence-performance tests pass. I also reproduced the skipped nested imports, stale imports after installing a previously missing dependency, and a namespace-loading error escaping process_events(). CI is currently failing on all three R-CMD-check platforms and coverage shards 3 and 4; shard 3 reports the previously seen lintr config file works failure, which did not reproduce locally.

Comment thread R/workspace.R Outdated
Comment thread R/workspace.R Outdated
Comment thread R/workspace.R
Comment thread tests/testthat/test-workspace-core.R Outdated

@renkun-ken renkun-ken left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All findings from my previous reviews are addressed in 9353f07. I found no new actionable correctness issues in the latest changes.

The focused workspace, language-server, lint (including integration), index, and document tests pass locally. I also verified the fixes with a real dependency installation in an existing Workspace, both indexed and unindexed diagnostics, a real dependency .onLoad failure during NAMESPACE refresh, and imports inside braces and both branches of conditional directives.

All three R-CMD-check platforms and lint pass. Coverage shard 3 still fails in lintr works and lintr config file works with missing diagnostics/key-not-found errors. Those tests pass locally, and similar failures appeared on previous revisions; please rerun or investigate that coverage job before merging.

@renkun-ken
renkun-ken merged commit 83dc120 into REditorSupport:master Oct 4, 2026
7 of 8 checks passed
@MichaelChirico
MichaelChirico deleted the object-usage branch October 4, 2026 05:36
@MichaelChirico

Copy link
Copy Markdown
Contributor Author

Thanks for the patient review!

@renkun-ken

Copy link
Copy Markdown
Member

Thanks for the work!

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

Labels

None yet

Projects

None yet

2 participants