25

When I launch VSCode from the dock, it always complains that

Resolving your shell environment is taking very long. Please
review your shell configuration.

and then a bit later

Unable to resolve your shell environment in a reasonable time.
Please review your shell configuration.

According to this page, Resolving Shell Environment is Slow, the first message is displayed if .bashrc takes more than three seconds and the second is displayed if it takes longer than ten seconds.

I opened a terminal in VSCode and sourced my .bashrc file

dpatterson@dpconsulting$ time source ~/.bashrc
real    0m1.448s
user    0m0.524s
sys     0m0.671s

dpatterson@dpconsulting$ 

As you can see, it takes less than 1.5 seconds.

Environment:

  • MacOS Mojave 10.14.6
  • VSCode 1.53.0

Hopefully someone knows what is causing this.
Barring that, maybe someone can point me to the code that actually generates these errors.

TIA

David Patterson
  • 1,780
  • 3
  • 17
  • 40
  • That's still a really long time to source your .bashrc. Mine runs in sub-second time. A reasonable guess is that the 1.5 seconds plus whatever time it's taking to launch your shell environment (WSL maybe?) + sourcing your bashrc is exceeding the threshhold. I don't see enough info in there to do more than guess, but... maybe move your .bashrc to .bashrc-old and see if that fixes it? If it does, then you know to target your .bashrc complexity. If it doesn't, then it's whatever is happening before (like launching WSL). Or maybe you're memory constrained, etc. etc. – Nicholas Rees Feb 11 '21 at 20:40
  • WSL is definitely not involved. I'm on a Mac. – David Patterson Feb 11 '21 at 23:13
  • Yep. Something in my bash initialization (it _does_ do a lot). Is there any way for my .bashrc to know that it's VSCode starting it up? – David Patterson Feb 11 '21 at 23:19
  • oops. Didn't read far enough down to read that you said mac already. I guess you could do something like adding an if statement in your bashrc to check if the parent pid belongs to VS Code. Or you could set environment variables to pass in depending on if its VS Code or not. But if it were me I'd tend to performance tune what's going on in .bashrc first. Cause waiting even a second and a half would start driving me nuts. My raspberry pi .bashrc excutes in .072 seconds, for comparison. – Nicholas Rees Feb 12 '21 at 00:03
  • 1
    Issue is officially being tracked here https://github.com/microsoft/vscode/issues/108804. Hopefully, there should be a fix/patch/workaround soon. – Deepak Rajendran Apr 25 '21 at 05:22
  • 2
    The fix for me ended up being to comment out the nix and ghcup lines from my zshrc (haskell stuff) – Jethro Larson Nov 02 '21 at 22:43
  • The issue seems to be that in order to resolve environment variables, VSCode starts an interactive login shell non-interactively. You might be reasonably doing something _interactive_ in your .bashrc or .zhrc file, in my case it was starting tmux. The solution is to check if stdin, stdout, and stderr are actually terminals: `[ -t 0 ] && [ -t 1 ] && [ -t 2 ]` – stribika Feb 01 '23 at 15:20

10 Answers10

23

encountered the same situation and find the issue: https://github.com/microsoft/vscode/issues/113869#issuecomment-780072904

I extract nvm load code to the condition function ref in the issue, solved this problem:

function load-nvm {
  export NVM_DIR="$HOME/.nvm"
  [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
  [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion
  [[ -s `brew --prefix`/etc/autojump.sh ]] && . `brew --prefix`/etc/autojump.sh
}

# nvm
if [[ "x${TERM_PROGRAM}" = "xvscode" ]]; then 
  echo 'in vscode, nvm not work; use `load-nvm`';
else 
  load-nvm
fi

Hemisu
  • 231
  • 1
  • 4
4

Open VS Code from a terminal:

code .
iconoclast
  • 21,213
  • 15
  • 102
  • 138
  • This doesn't answer the question. @David Peterson wants to know what the issue with his .rc file is, not just how to avoid getting the issue with vscode. – benjixinator Jul 13 '22 at 23:02
3

OSX Work-around

Background

If VS Code is started via an OSX (or Linux) GUI interface and there's a shell rc process that takes more than 10 seconds to complete during start-up (e.g. nvm) VS Code simply stops trying to start-up that shell.

Work-around

The two known solutions are to either get rid of the process that takes more than 10 seconds or start VS Code from the command line (i.e. code .).

This work-around is for those of us on OSX who —for whatever reason— explicitly don't/can't get rid of the 10+ second process and who —again, for whatever reason— start VS Code via the GUI.

While the following is less desirable than VS Code just having a configuration that we can change, it's still better than having to remember to run code . from the terminal, and it's much, much better than not using nvm (or switching to an alternative).

Note: The file extension does need to be changed between .command and .app several times — this is not a mistake! ;)

  1. Create a new file with the following:

    #!/bin/sh
    
    code .
    
  2. Save the file with a .command extension in your "Applications" directory, e.g. ~/Applications/vs-code-cli-starter.command.

  3. In a terminal, change that new file's permissions:

    chmod +x fileName.command
    
  4. From Finder, right click-on your new app/command and select "Get Info", then repeat with your actual VS Code application.

  5. Drag the big icon from the "Preview" section of the actual VS Code's "Get Info" to the small icon in the upper-left of your newly created app/command's "Get Info". where to drag the source icon from and where to drag it to

  6. Close the two "Get Info" panels.

  7. In the "Applications" directory, update the file extension from .command to .app, e.g. vs-code-cli-starter.app.

  8. Drag the newly created app's icon to your dock.

  9. In the "Applications" directory, swap the file extension back from .app to .command, e.g. vs-code-cli-starter.command.

  10. Enjoy

MetaSean
  • 881
  • 17
  • 25
2

Restarting VSCode on BigSur works for me.

You can also check your user settings to make sure it matches the path of the shell that your terminal uses.

Check https://code.visualstudio.com/docs/supporting/troubleshoot-terminal-launch for other troubleshooting steps.

JoshX
  • 49
  • 4
  • Happened for me after a system update. VSCode was open on reboot, so it restarted it, but apparently not correctly. Restart VSCode solved the issue, as stated. – rickb Nov 03 '21 at 21:13
  • 3
    I had to close VSCode, restart my Mac and then open VSCode, then close it and reopen it again before it started working properly. But it is now working. Don't give up fellow restarters - if at first you don't succeed, restart, restart, restart again! – Geoff Davids Nov 11 '21 at 17:56
2

If you don't want to mess with your environment, the easiest path to fixing this is to change the Application: Shell Environment Resolution Timeout setting on VSCode to make it longer.

Bumping it up from 10 seconds to 30 fixed my problem.

Ethan
  • 5,660
  • 9
  • 44
  • 51
1

For Linux:

if [[ "$VSCODE_PID" = "" ]]; then
  echo "slow or blocking operations"
fi

Thanks to Hemisu's answer.

M Imam Pratama
  • 998
  • 11
  • 26
1

For those who uses zsh, .ohmyzsh and powerlevel10k prompt:

  1. Get all your exports EXCEPT export ZSH= and nvm stuff and put them in the bottom of .zshrc file.

  2. Insert this line at the top: if [[ "$VSCODE_PID" = "" ]]; then

  3. Before your exports at the bottom, insert an fi (end if)

Now, all your ohmyzsh, nvm and ohmyzsh themes are not loaded when VSCode initializes (the built in terminal still works fine, with ohmyzsh).

In my case, outside that if/fi there is only exports for PATH, LDFLAGS and CPPFLAGS.

Thanks to M Imam Pratama answer.

JCKödel
  • 723
  • 9
  • 19
0

Go to project location and open with code . in terminal (if you are in linux) or cmd (if you are in windows.) That should resolve this issue.

When directly opened from vscode app it happens, even I have faced this.

0

it's a very common error or we can say complain by vs code so here is the solution - do not open vs code directly from its icon or directly from application instead you can do is just open vs code from terminal/cmd obviously for linux/windows respectively by typing this command -> $ code . if this command is not working then reinstall the vs code and check all the boxes before installing it will also allows you to open vs code from folder directly by clicking right side of mouse and just click open this folder with other application and select vs code.

E_net4
  • 27,810
  • 13
  • 101
  • 139
-1

In Linux, you can open "alacarte" and make a new shortcut with the command "code" and click the box "Open in a terminal".

enter image description here

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77