VS Code - Fix a task automation issue ‘The terminal process failed to launch (exit code: 127)’
I like to run a web server automatically whenever I open a web project in VS Code. I use VS Code tasks to do this. Suddenly, this has stopped working, without rhyme or reason! 😓
I was greeted with the following error in the integrated terminal:
* Executing task: pnpm dev
zsh:1: command not found: pnpm
* The terminal process “/usr/bin/zsh ‘-c’, ‘pnpm dev’” failed to launch (exit code: 127).
* Press any key to close the terminal
Nice to meet you exit code 127! Let’s get to know each other! 🤗
The source of the issue
If you are not familiar with tasks in VS Code, check out this post that covers this very use case.
The task is running the (p)npm script called “dev”. You can see the tasks.json file below.
The “dev” script is running the eleventy server in dev mode. The details of the script are not important for this discussion, but to round out the background here is an abbreviated version of my package.json:
If I open a new integrated terminal and run pnpm dev
, it runs the command successfully! It looks like it is specific to tasks.
The problem is that tasks are executed with Zsh using /bin/zsh -c
, which starts a non-login, non-interactive session. This means that ~/.zshrc and ~/.zprofile are not sourced by Zsh. In my case, this means that I can’t run the necessary local npm packages in a task. The solution should be to have tasks run in an interative shell session. How do we do that?
The fix
To start an interative Zsh shell session we want to pass the i
flag to Zsh. After some digging, I found that there is a task/debug profile setting that let’s you specify the environment for tasks. The setting has a version for each platform (windows/linux/osx) in the form of terminal.integrated.automationProfile.<platform>
. For example, it would be terminal.integrated.automationProfile.linux
for linux.
The fix for my linux setup was to add the following to the settings.json: