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

Executable Rscript not found in VS Code #543

Open
3 of 5 tasks
joshpersi opened this issue Jan 25, 2024 · 11 comments
Open
3 of 5 tasks

Executable Rscript not found in VS Code #543

joshpersi opened this issue Jan 25, 2024 · 11 comments

Comments

@joshpersi
Copy link

Before filing a bug

  • I have installed the latest dev version of {precommit} with remotes::install_github('lorenzwalthert/precommit')
  • I have installed the latest hook revisions (update with precommit::autoupdate())
  • I have installed the latest release of the upstream Python framework pre-comit as described under the update instructions.

Describe the bug
I am able to use the precommit package successfully in RStudio but if I try to use precommit (on the same computer) in VS code, I receive errors like:

$ git commit
style-files..............................................................Failed
- hook id: style-files
- exit code: 1

Executable `Rscript` not found

lintr....................................................................Failed
- hook id: lintr
- exit code: 1

Executable `Rscript` not found

parsable-R...............................................................Failed
- hook id: parsable-R
- exit code: 1

Executable `Rscript` not found

no-browser-statement.....................................................Failed
- hook id: no-browser-statement
- exit code: 1

Executable `Rscript` not found

To Reproduce
Steps to reproduce the behavior:

  1. Create a new folder and open it in VS Code
  2. Create an empty R script
  3. Init git
  4. Run precommit::use_precommit()
  5. Stage changes and attempt to commit

Expected behavior
I expect precommit's checks to run on VS

Additional context

  • My operating system is:
  • My project uses {renv}.
  • the output of packageVersion('renv')
@lorenzwalthert
Copy link
Owner

Thanks for the report. Is Rscript on your PATH?

@lorenzwalthert
Copy link
Owner

Also see #441 (comment).

@joshpersi
Copy link
Author

joshpersi commented Jan 29, 2024

I honestly don't know how to tell if Rscript is on my PATH.

If I run Sys.getenv("PATH") in RStudio, where precommit works, this is the output (I've broken lines at ; for readability):

C:\\rtools43\\x86_64-w64-mingw32.static.posix\\bin
C:\\rtools43\\usr\\bin
C:\\Users\\joshpersi\\AppData\\Local\\Programs\\R\\R-4.3.1\\bin\\x64
C:\\Program Files (x86)\\Common Files\\Oracle\\Java\\javapath
C:\\Oracle\\product\\12.1\\bin
C:\\WINDOWS\\System32
C:\\WINDOWS
C:\\WINDOWS\\System32\\Wbem
C:\\WINDOWS\\System32\\Windowspowershell\\V1.0\\
C:\\WINDOWS\\System32\\Openssh\\
C:\\WINDOWS\\system32
C:\\WINDOWS
C:\\WINDOWS\\System32\\Wbem
C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\
C:\\WINDOWS\\System32\\OpenSSH\\
C:\\Program Files\\1E\\Client\\Extensibility\\NomadBranch
C:\\Program Files\\dotnet\\
C:\\Program Files\\Docker\\Docker\\resources\\bin
C:\\Users\\joshpersi\\AppData\\Local\\Programs\\Quarto\\bin
C:\\Users\\joshpersi\\AppData\\Local\\Microsoft\\WindowsApps
C:\\Users\\joshpersi\\AppData\\Local\\Programs\\Git\\cmd
C:\\Users\\joshpersi\\AppData\\Roaming\\TinyTeX\\bin\\win32
C:\\Users\\joshpersi\\AppData\\Local\\Pandoc\\
C:\\Users\\joshpersi\\AppData\\Local\\Programs\\Microsoft VS Code\\bin
C:\\Users\\joshpersi\\AppData\\Local\\Programs\\Quarto\\bin
C:\\Program Files\\RStudio\\resources\\app\\bin\\postback

And here's what I get if I run Sys.getenv("PATH") in VS Code:

C:\\rtools43/x86_64-w64-mingw32.static.posix/bin
C:\\rtools43/usr/bin
C:\\Users\\joshpersi\\AppData\\Local\\r-miniconda
C:\\Users\\joshpersi\\AppData\\Local\\r-miniconda\\Library\\mingw-w64\\bin
C:\\Users\\joshpersi\\AppData\\Local\\r-miniconda\\Library\\usr\\bin
C:\\Users\\joshpersi\\AppData\\Local\\r-miniconda\\Library\\bin
C:\\Users\\joshpersi\\AppData\\Local\\r-miniconda\\Scripts
C:\\Users\\joshpersi\\AppData\\Local\\r-miniconda\\bin
C:\\Users\\joshpersi\\AppData\\Local\\r-miniconda\\condabin
C:\\Users\\joshpersi\\AppData\\Local\\Programs\\Git\\mingw64\\bin
C:\\Users\\joshpersi\\AppData\\Local\\Programs\\Git\\usr\\bin
C:\\Users\\joshpersi\\bin
C:\\Program Files (x86)\\Common Files\\Oracle\\Java\\javapath
C:\\Oracle\\product\\12.1\\bin
C:\\WINDOWS\\System32
C:\\WINDOWS
C:\\WINDOWS\\System32\\Wbem
C:\\WINDOWS\\System32\\Windowspowershell\\V1.0
C:\\WINDOWS\\System32\\Openssh
C:\\WINDOWS\\system32
C:\\WINDOWS
C:\\WINDOWS\\System32\\Wbem
C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0
C:\\WINDOWS\\System32\\OpenSSH
C:\\Program Files\\1E\\Client\\Extensibility\\NomadBranch
C:\\Program Files\\dotnet
C:\\Program Files\\Docker\\Docker\\resources\\bin
C:\\Users\\joshpersi\\AppData\\Local\\Programs\\Quarto\\bin
C:\\Users\\joshpersi\\AppData\\Local\\Microsoft\\WindowsApps
C:\\Users\\joshpersi\\AppData\\Local\\Programs\\Git\\cmd
C:\\Users\\joshpersi\\AppData\\Roaming\\TinyTeX\\bin\\win32
C:\\Users\\joshpersi\\AppData\\Local\\Pandoc
C:\\Users\\joshpersi\\AppData\\Local\\Programs\\Microsoft VS Code\\bin
C:\\Program Files (x86)\\Common Files\\Oracle\\Java\\javapath
C:\\Oracle\\product\\12.1\\bin
C:\\WINDOWS\\System32
C:\\WINDOWS
C:\\WINDOWS\\System32\\Wbem
C:\\WINDOWS\\System32\\Windowspowershell\\V1.0\\
C:\\WINDOWS\\System32\\Openssh\\
C:\\WINDOWS\\system32
C:\\WINDOWS
C:\\WINDOWS\\System32\\Wbem
C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\
C:\\WINDOWS\\System32\\OpenSSH\\
C:\\Program Files\\1E\\Client\\Extensibility\\NomadBranch
C:\\Program Files\\dotnet\\
C:\\Program Files\\Docker\\Docker\\resources\\bin
C:\\Users\\joshpersi\\AppData\\Local\\Programs\\Quarto\\bin
C:\\Users\\joshpersi\\AppData\\Local\\Microsoft\\WindowsApps
C:\\Users\\joshpersi\\AppData\\Local\\Programs\\Git\\cmd
C:\\Users\\joshpersi\\AppData\\Roaming\\TinyTeX\\bin\\win32
C:\\Users\\joshpersi\\AppData\\Local\\Pandoc\\
C:\\Users\\joshpersi\\AppData\\Local\\Programs\\Microsoft VS Code\\bin

If it's helpful, here are two VS code settings I've manually configured:

"r.rterm.windows": "C:\\Users\\persij\\AppData\\Local\\Programs\\R\\R-4.3.1\\bin\\x64\\R.exe",
"r.rpath.windows": "C:\\Users\\persij\\AppData\\Local\\Programs\\R\\R-4.3.1",

I tried to set the path environment in VS code to be the same as the one in RStudio but to no avail.

Please let me know if there's anything else I can do to help!

@lorenzwalthert
Copy link
Owner

Ok finding out how Rscript gets ob your PATH is not a pre-commit issue. Does the same problem happen when you open a terminal with Apple‘s terminal app and try to git commit in that repo? I don’t know how you installed R but seems not a common installation avenue…

@joshpersi
Copy link
Author

Thanks for your patience - I now appreciate this isn't a precommit issue. I'll look into it a bit more on my own. Thanks for your help!

@lorenzwalthert
Copy link
Owner

I think if you use VS Code and a reasonable recent version of macOS, then your default shell is zsh (and there should be no difference to opening a new zsh shell with the Apple terminal app) and you can add the location of your R executable to the PATH by adding it to the respective profile file (there are a few different files that have different behaviour mainly based on how you start the shell).

Or you just install R again with an installation method that places the executable on the PATH, or you symlink the current executable in a place that is already on the PATH.

@skwde
Copy link

skwde commented Feb 12, 2024

@lorenzwalthert I have the same issue, Rscript is not found.

I basically use two different conda environments, one for pre-commit and another one for R.
Of course Rscript is not available in the pre-commit environment.

To my understanding pre-commit will fetch what is needed on pre-commit run --all.
Why doesn't it fetch Rscript as well?
Isn't that the power of pre-commit to just specify a hook in pre-commit-config.yaml and forget about the setup?

@lorenzwalthert
Copy link
Owner

Ok. I don't think I accounted for the situation where you don’t have a global R executable but instead only one in a conda environment. In fact, I think this would require a change to the python pre-commit package because when you call git to commit, git calls pre-commit which calls Rscript on the PATH to activate renv to call the hooks. Temporary workaround is to have a global R installation you can fall back on that is on the PATH.

@lorenzwalthert
Copy link
Owner

How can you reliably detect if a directory uses a conda environment? And how to find the R executable?

@skwde
Copy link

skwde commented Feb 13, 2024

Ok, the availability of Rscript (and R in general) is a dependency of pre-commit itself and not your hook (https://pre-commit.com/#r). So it makes actually sense to put this along side the pre-commit install (i.e. into the pre-commit environment).

So in that case it would be enough to look for Rscript in the environment where pre-commit is installed.

Regarding your question, does this https://pre-commit.com/#conda help?
I.e. pre-commit supports conda as long as a environment.yml is present.
Hmm I think it would be better though if one can specify a environment.yml to use. Mine are often called something like environment.<name>.yml.

@lorenzwalthert
Copy link
Owner

lorenzwalthert commented Feb 13, 2024

Ok, the availability of Rscript (and R in general) is a dependency of pre-commit itself and not your hook (https://pre-commit.com/#r)

In pre-commit, there are different levels of languages, r is implemented as a second level language. This means that a global R executable is required, but R package dependencies will be managed in an isolated fashion (with {renv}).

So it makes actually sense to put this along side the pre-commit install (i.e. into the pre-commit environment).

Usually though, pre-commit is installed globally (at least I haven't come across a project where pre-commit was listed as a dev requirement), and there is not necessarily a pre-commit conda or venv virtual environment associated with it.

Also note that for example in R, the {renv} environment that pre-commit uses is not same as the user created for his project (if any), but these are separate virtual environments. While most R projects don't use {renv}, some do, and pre-commit's {renv} environment is separate from it. So if you are using conda to manage your project's dependencies, it does not mean that pre-commit has to use conda as well. In fact if you install {precommit} into your conda environment, it is only useful as far as the R API goes, e.g. to precommit::install() or perform other operations from the R console interactively.

Regarding your question, does this https://pre-commit.com/#conda help?

If this repository's hooks were implemented with language: conda instead of language: r (see .pre-commit-hooks.yaml), then probably a global R installation would not be required, yes, because the Rscript executable would be bootstrapped as part of the conda environment. Unfortunately, I think a hook repo (like lorenzwalthert/precommit) can only contain an implementation of one language, so we had to create a second repo to do this.

Summa summarum, I think the easiest is just to install R globally so you can use it for pre-commit. Using the hooks from this repo won't install any packages in your global R package library, except maybe {renv}.

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

No branches or pull requests

3 participants