Skip to content

Inherit context from source sites - #1383

Open
lionel- wants to merge 6 commits into
oak-source/1-r-collationfrom
oak-source/2-source-inheritance
Open

Inherit context from source sites#1383
lionel- wants to merge 6 commits into
oak-source/1-r-collationfrom
oak-source/2-source-inheritance

Conversation

@lionel-

@lionel- lionel- commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Progress towards posit-dev/positron#14790
Progress towards #1338

Previously, only the source() site would see sourced context (top-level bindings and transitive Source and Attach effects). With this PR, the sourced file now sees the context from the source site too.

In a way, this is a more principled mechanism for the R/ auto-collation mechanism of the previous PR. In another way, this is a bit unprincipled, as standalone files are much easier to analyse, both by static analysis tools and by humans (https://github.com/include-what-you-use/include-what-you-use/blob/master/docs/WhyIWYU.md).

When both the R/ auto-collation and the inherited source site apply, the latter wins: a resolved source() is a better signal than the directory-name convention. They should possibly union, since e.g. a Shiny-style autoload really does happen alongside. We can refine later.

# file1.R
x <- 1
source("foo.R")

# file2.R
x <- 1
y <- 2
source("foo.R")

# foo.R
list(x, y)

Now when you goto-def of x in foo.R, you'll see both file1.R and file2.R targets. Note how y is conditionally undefined: this will need to be linted once we implement Oak-based symbol diagnostics.

For now we have two new experimental diagnostics for effects ambiguity:

  1. source-cycle, when files source each other:
# a.R
source("b.R")

# b.R
source("a.R")

warning[source-cycle]: This file takes part in a cycle of mutual source() calls. Language analysis will be incomplete until the cycle is resolved.

  1. inherited-shadow, when an inherited binding changes what a callee resolves to:
# a.R
source <- identity
base::source("b.R")

# b.R
source("c.R")

info[inherited-shadow]: This source call has an ambiguous effect. It resolves through package base when the file is sourced on its own, and to a binding in a.R when sourced by a.R.

Positron Release Notes

New Features

  • Sourced files now see the context inherited from the source() location, allowing you to navigate to the inherited symbols or rename them.

Bug Fixes

  • N/A

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant