Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Diagnostics: No symbol in scope when assigning variables in function calls #3048

Closed
juliasilge opened this issue May 7, 2024 · 5 comments
Closed
Assignees
Labels
area: diagnostics Issues related to Diagnostics area: kernels Issues related to Jupyter kernels and LSP servers bug Something isn't working lang: r support

Comments

@juliasilge
Copy link
Contributor

Discussed in https://github.com/posit-dev/positron-beta/discussions/175

Originally posted by jeffkeller-einc May 7, 2024

Screenshot 2024-05-07 at 10 04 29 AM

I don't know what this pattern is called, but I do it a lot to modularize my non-package R projects.

source("helper_functions.R", local = helpers <- new.env())
helpers$myfun(...)
```</div>
@juliasilge juliasilge transferred this issue from another repository May 7, 2024
@juliasilge
Copy link
Contributor Author

This is valid code that we currently do not correctly lint:

sum(x <- 1:10)
#> [1] 55
x * 2
#>  [1]  2  4  6  8 10 12 14 16 18 20

Created on 2024-05-07 with reprex v2.1.0

It says "no symbol named 'x' in scope" for the second x there.

@wesm wesm added the bug Something isn't working label May 8, 2024
@lionel-
Copy link
Contributor

lionel- commented May 9, 2024

In theory, we should analyse the semantics of argument evaluation and only consider <- when passed to "strict" arguments (non-lazy/NSE ones). In practice, we can just consider all arguments to be strict, at least in the short and medium terms.

Such a pragmatic approach will have lots of false positives though, in particular local(x <- foo) and test_that("", { x <- 1 }) which we should special-case.

For the longer term, the annotations discussed last year at the R sprint would be useful here: https://bugs.r-project.org/show_bug.cgi?id=14354

@petetronic petetronic added this to the Future milestone May 13, 2024
@lionel- lionel- changed the title No symbol in scope when assigning variables in function calls Diagnostics: No symbol in scope when assigning variables in function calls May 16, 2024
@lionel- lionel- added area: kernels Issues related to Jupyter kernels and LSP servers area: diagnostics Issues related to Diagnostics labels May 16, 2024
@jennybc
Copy link
Member

jennybc commented Nov 1, 2024

I just came across a variant of this in cli, except it's a variable assignment in an if() condition.

The general form that's problematic:

if (nzchar(ASDF <- Sys.getenv("ASDF"))) {
  if (ASDF == "whatever") {
    message("hi")
  }
}

In Positron, the ASDF inside the if body gets the diagnostic "No symbol named 'ASDF' in scope."

The real world example: https://github.com/r-lib/cli/blob/7ca06ff9c1c881359b562ecb98a1414e44d78fb8/R/ansi-hyperlink.R#L347-L355

@lionel-
Copy link
Contributor

lionel- commented Nov 4, 2024

I'm now thinking we should be overly optimistic about potential assignments instead of overly cautious.

@lionel- lionel- modified the milestones: Future, 2024.12.0 Pre-Release Nov 4, 2024
@lionel- lionel- self-assigned this Nov 18, 2024
lionel- added a commit that referenced this issue Nov 27, 2024
## Positron Notes

### Release Notes

#### New Features

- The variable pane now supports labels from the {haven} package
(#5327.

- The variable pane has improved support for formulas
(#4119).

#### Bug Fixes

- Assignments in function calls (e.g. `list(x <- 1)`) are now detected
by the missing symbol linter to avoid annoying false positive
diagnostics (#3048). The downside is that this causes
false negatives when the assignment happens in a call with local scope,
e.g. in `local()` or `test_that()`. In these cases the nested
assignments will incorrectly overlast the end of the call. We prefer to
be overly permissive than overly cautious in these matters.

- The following environment variables are now set in the same way that R
does:

  - `R_SHARE_DIR`
  - `R_INCLUDE_DIR`
  - `R_DOC_DIR`

  This solves a number of problems in situations that depend on these
variables being defined
(#3637).
lionel- added a commit that referenced this issue Nov 27, 2024
### Release Notes

#### New Features

- The variable pane now supports labels from the {haven} package
(#5327.

- The variable pane has improved support for formulas
(#4119).

#### Bug Fixes

- Assignments in function calls (e.g. `list(x <- 1)`) are now detected
by the missing symbol linter to avoid annoying false positive
diagnostics (#3048). The downside is that this causes
false negatives when the assignment happens in a call with local scope,
e.g. in `local()` or `test_that()`. In these cases the nested
assignments will incorrectly overlast the end of the call. We prefer to
be overly permissive than overly cautious in these matters.

- The following environment variables are now set in the same way that R
does:

  - `R_SHARE_DIR`
  - `R_INCLUDE_DIR`
  - `R_DOC_DIR`

This solves a number of problems in situations that depend on these
variables being defined
(#3637).

<!-- Thank you for submitting a pull request.
If this is your first pull request you can find information about
contributing here:
  * https://github.com/posit-dev/positron/blob/main/CONTRIBUTING.md

We recommend synchronizing your branch with the latest changes in the
main branch by either pulling or rebasing.
-->

<!--
  Describe briefly what problem this pull request resolves, or what
  new feature it introduces. Include screenshots of any new or altered
  UI. Link to any GitHub issues but avoid "magic" keywords that will 
  automatically close the issue. If there are any details about your 
approach that are unintuitive or you want to draw attention to, please
  describe them here.
-->
@testlabauto
Copy link
Contributor

Verified Fixed

Positron Version(s) : 2025.01.0-2
OS Version          : OSX

Test scenario(s)

Verified with Julia's example

Link(s) to TestRail test cases run or created:

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: diagnostics Issues related to Diagnostics area: kernels Issues related to Jupyter kernels and LSP servers bug Something isn't working lang: r support
Projects
None yet
Development

No branches or pull requests

6 participants