rstudio / rstudio

RStudio is an integrated development environment (IDE) for R
https://posit.co/products/open-source/rstudio/
Other
4.69k stars 1.09k forks source link

Python integration within RStudio terminal causes problems with $PATH #9942

Open ksm113 opened 3 years ago

ksm113 commented 3 years ago

System details

RStudio Edition : Server
RStudio Version : RStudio 2021.09.0-preview+340 "Ghost Orchid" Preview (8aea4f32e427a4f97a9fedf507a3ef88a228553e, 2021-08-27) 
OS Version      :  Ubuntu 20.04.3 LTS
R Version       :  4.0.3

Steps to reproduce the problem

Start a new RStudio bash terminal or restart R.

Describe the problem in detail

Terminal starts with a warning: ln: failed to create symbolic link '/usr/lib/rstudio-server/resources/terminal/bash/.selectR': Permission denied

The $PATH within the RStudio terminal does not match $PATH in a standalone terminal. Notably, /opt/R/4.0.3/bin is missing from $PATH within RStudio.

Describe the behavior you expected

This can be fixed by disabling Python integration under the terminal settings within RStudio; therefore, I assume that automatic loading of Python environments is not functioning correctly in this build. I didn't find anyone else with the same issue when searching for solutions to this problem myself, so I wanted to register it here in case it helps someone.

My organization is planning an update to the latest build within the next few days.

ronblum commented 3 years ago

@ksm113 Thank you for raising the issue!

@kevinushey I can't reproduce the error issue; however, the file .selectR doesn't exist—should it?

rstudio@ip-10-0-0-165:~$ ls -la /usr/lib/rstudio-server/resources/terminal/bash/
total 12
drwxr-xr-x 2 root root 4096 Sep 30 13:17 .
drwxr-xr-x 4 root root 4096 Sep 30 13:17 ..
-rw-r--r-- 1 root root  555 Sep 23 17:54 .bash_profile
kevinushey commented 3 years ago

It shouldn't; the problem is most likely that the user's .bash_profile (or similar startup script) is being executed with the wrong value of the HOME environment variable set.

@ksm113, what shell have you configured RStudio to use, and what startup scripts do you have for your shell profile? Can you confirm what the value of the HOME environment variable is, wherever that attempt to create .selectR is being done? (assuming it's in your own startup profile scripts somewhere)

kevinushey commented 3 years ago

Here's where the terminal magic happens (note that RStudio sets the HOME environment variable so that this profile is found first):

https://github.com/rstudio/rstudio/blob/release-ghost-orchid/src/cpp/session/resources/terminal/bash/.bash_profile

so my hypothesis is that our attempt to reset HOME to REALHOME is failing for your shell, for some reason. (Maybe we need to re-export the HOME variable after setting it? Maybe the shell being used doesn't support re-defining HOME in this way?)

ksm113 commented 3 years ago

My shell is configured for bash, and yes, the value of $HOME matched the attempt to set the softlink. Took me some time to track down other answers: It turns out that .selectR is my organization's way of implementing the ability to select different R versions for RStudio. This is what I got from our admin regarding startup scripts:

#!/bin/sh

#export MODULEPATH=$MODULEPATH:/opt/R/modulefiles

module use /opt/R/modulefiles

if [ ! -f ~/.selectR ]; then
    ln -s /etc/selectR.d/4.0 ~/.selectR
fi
if link_target=$(readlink -f ~/.selectR) && [ "$(dirname $link_target)" = /etc/selectR.d ]; then
    r_version=$(basename $link_target)
    module load r/$r_version
fi
unset link_target r_version
function selectR-config {
    local cmd_stdout=$(command selectR-config $@)
    if [ -n "$cmd_stdout" ]; then
        echo "$cmd_stdout"
        if [[ $1 == --select && $cmd_stdout =~ TRUE ]]; then
            module load r/$2
        fi
    fi
}
Enchufa2 commented 3 years ago

Is there any good reason for placing this at the beginning of the PATH?

https://github.com/rstudio/rstudio/blob/88dcc629b60d2cc895338b0b85a1281fa41062d4/src/cpp/session/resources/terminal/hooks#L26

This is quite problematic. For example, when _RS_PYTHON_BIN=/usr/bin, this takes precedence over everthing: ~/.local/bin, ~/bin, /usr/local/bin... but also /usr/lib64/ccache. So I was wondering why package building was taking so long lately... basically, ccache stopped working in RStudio since the last update.

Enchufa2 commented 3 years ago

Checking whether _RS_PYTHON_BIN is already in the PATH would solve the issue.

kevinushey commented 3 years ago

The intent here is to ensure the version of Python selected by the user is put first on the PATH, so that terminals "see" the same version of Python (and related utilities) as they would within R itself.

Obviously, this does not have the desired behavior if we auto-select a version of Python in one of the common bin directories, though. :-/

We'll need to think of a more suitable way to handle this. Perhaps by creating a temporary directory, symlinking the appropriate version of Python, then placing that on the PATH?

We can't just check if it's on the PATH as we want to make sure the python executable in that directory is preferred to any other copies of Python that might be on the PATH somewhere.

Enchufa2 commented 3 years ago

I see. Then yes, you'd need to create a symlink in some temporary directory to avoid side effects. I also noticed that the hooks file is not the only place where the PATH is modified in this way, so maybe this belongs to a separate issue.

ksm113 commented 3 years ago

I can now confirm that updating to the most recent version of RStudio Server does not solve this issue.

diana-cn commented 2 years ago

I have run into an issue with accessing the .gitconfig file using Git bash in the terminal, and I landed here. Long story short, I found that when the "Python integration" is enabled in the "Terminal" settings, running echo $HOME in:

Deleting the terminal/bash folder or its contents (.bash_profile) does not seem to have any effect on this. Untick the "Python integration" in the "Terminal" settings, then open a new terminal and a new shell window, running echo $HOME in both results in /c/Users/MyUserName which is my desired HOME path, as it is the same as in the Windows command prompt, and it fixed my ".gitconfig file not found" issue. Sorry about my bad English and/or lack of professional language, I have very little experience in this. I suspect my issue was caused by a similar reason here, but please let me know if that's not the case.

My system details:

RStudio Version 1.4.1717 © 2009-2021 RStudio, PBC "Juliet Rose" (df86b69e, 2021-05-24) for Windows Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) QtWebEngine/5.12.8 Chrome/69.0.3497.128 Safari/537.36

R version 4.1.3 (2022-03-10) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 8.1 x64 (build 9600)

Matrix products: default

kevinushey commented 2 years ago

@diana-cn, thanks for the bug report -- do you see the same issue in the latest daily builds of RStudio?

diana-cn commented 2 years ago

@kevinushey, ticking "Python integration" in "Terminal Options" now returns C:/Users/MyUserName/Documents when calling echo $HOME in a Git Bash terminal. The behaviour in the Shell remains the same.

The version I tested was: RStudio 2022.06.0-daily+341 "Spotted Wakerobin" Daily (5072281d60d27f46071eee0ff04ca2081528c897, 2022-04-28) for Windows Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) QtWebEngine/5.12.8 Chrome/69.0.3497.128 Safari/537.36

Product RStudio Desktop
Platform Windows 10+ (installer-less)
Branch Spotted Wakerobin
Channel Daily
Version 2022.06.0-daily+341
Download URL https://s3.amazonaws.com/rstudio-ide-build/desktop/windows/RStudio-2022.06.0-daily-341.zip
Built Friday, April 29 2022 at 6:10 am (21 hours ago)
Size 249464809 bytes (237 MiB)
SHA256 a939d709de032a054d544716d81e31e5dfa0731d289461a2176bec7a1c9f6c4c
Commit Hash 5072281d60d27f46071eee0ff04ca2081528c897 in rstudio/rstudio