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

🐞 VSC TTK running Azure Function nested project ignores Node version in environment variable PATH (set via NVM) **SOLVED** #10785

Closed
andrewconnell opened this issue Jan 29, 2024 · 3 comments
Assignees
Labels
investigating needs attention This issue needs the attention of a contributor. TA:Compute Team Area: Compute

Comments

@andrewconnell
Copy link
Contributor

andrewconnell commented Jan 29, 2024

Describe the bug
When using the VSC startup tasks on a project that includes an Azure Function for the backend, TTK doesn't respect if the user's environment is using NVM for Node with a supported version set at the default & rather defaults to system. If the system version of Node that's installed isn't a valid version, it causes the Azure Function backend to enter a fail/crash/recycle loop making it unusable.

If you manually run the startup task from the console (either within the integrated console in VSC or from your shell of choice), it works. BUT, if you run using the startup task Start Backend, it fails.

To Reproduce
Steps to reproduce the behavior:

  1. Setup environment to use NVM

  2. Install an Azure Functions-supported version of Node via NVM (ie: Node v18.19)

  3. Install an unsupported version of Node via a Node.js provided installer (ie: Node v21.*)

  4. Create a new TTK project: Tab > React with Fluent UI

  5. Start the project using Run & Debug > Debug in Teams ([browser])

  6. Once the build & deployment completes, pick [integrated Terminal] > Start Backend & observe sea of failures...

    Screenshot 2024-01-29 at 4 35 52 PM

    Notice the two highlighted portions: I'm using the latest Azure Functions Core Tools (v4.0.5455) and it's picking up Node.js v21.

  7. Kill the Node terminal (so ports aren't locked up)

  8. In a new Terminal window, verify you have good versions of Node installed & configured via NVM.

    Screenshot 2024-01-29 at 4 44 00 PM

    NOTE: This shows that the system-installed version of Node {{1}} is an unsupported version, the same one being picked up by the custom task in the project template, but a supported version of Node is the current and default one{{2}} and is also reflected in the PATH environment variable {{3}}... and just for good measure, my entire NVM config {{4}} so no magic up my sleeve.

  9. Prove it's an issue with the custom task Start backend by manually running the exact same command the task runs from the API folder and observe it works this time compared to the first screenshot:

    Screenshot 2024-01-29 at 4 52 47 PM

    Notice it's working with zero errors, because it's picking up Node v18.* from NVM.

    CORRECTION: The screenshot above shows I'm running the Azure Function Core Tools func command directly, but I really use the NPM script to inject the environment variables: npm start dev:teamsfx. This works as well.

Expected behavior
The custom task Start backend from TTK respects the version of NVM in the envvar $PATH & not the one installed as system so it uses what the developer has set as their preference.

Screenshots
see above

VS Code Extension Information (please complete the following information):

  • OS: macOS v14.2.1
  • Version v5.4

CLI Information (please complete the following information):

  • OS: macOS v14.2.1
  • Version v2.0.2

Additional context
The only workaround I've figured out was to start the build, but before testing the app was to kill the Start Backend terminal/process, open a new process, and start the Azure Function project manually from the command line.

Furthermore...

This may not be a popular/widespread issue, but it will affect developers who use a package manager like Homebrew (macos) or chocolatey (windows)... which isn't uncommon.

The problem is these package managers, when a package has a dependency on Node.js, it installs upgraded versions of node using the provided Node.js installers. This installs Node in what Node version managers (NVM) recognize as system which are popular for developers.

@Siglud
Copy link
Member

Siglud commented Jan 30, 2024

I think the PATH variable your echo $PATH command output is your zsh's path, not the system path.

The TTK will use bash not zsh to run the Node commands by default. So maybe you should add your node PATH to your bash.

Please refer to this link to find solutions.

@andrewconnell
Copy link
Contributor Author

You're correct in noting I'm using ZSH & NVM is loaded from there, but I also have the ~/.bashrc profile config with the NVM loading script in it as well. Yet I still have the same issue.

Is it TTK that's defaulting to bash to run Node commands, or is it VSC?

If it's TTK like you said, you said (my emphasis):

The TTK will use bash not zsh to run the Node commands by default .

Does that imply you can override it to use zsh instead of bash?

Either way, if the user has configured their terminal to use a specific shell (which loads all sorts of things they configured), seems strange to ignore their setting and force bash.

@andrewconnell
Copy link
Contributor Author

andrewconnell commented Jan 30, 2024

@Siglud - I did a bit more digging based on your last response & realized this isn't a TTK thing, rather it's a VSC thing.

For others who stumble across this...

The issue stemmed from the Start backend task in ./vscode/tasks.json:

{
  "label": "Start backend",
  "type": "shell",
  "command": "npm run dev:teamsfx",
  "isBackground": true,
  "options": {
      "cwd": "${workspaceFolder}/api",
      "env": {
          "PATH": "${workspaceFolder}/devTools/func:${env:PATH}"
      }
  },
  // ...
  "dependsOn": "Watch backend"
},

This is a type=shell task, which defaults to your shell. For me (macOS) the default is ZSH (since macOS v13), but VSC defaults to bash. The problem was that my configuration was in my ZSH config (~/.zshrc) including the NVM setup. However, because VSC defaults to bash, it wasn't picking up the config (even after I duped it to the bash config (~/.bashrc)).

The solution is to configure VSC to default to ZSH which you can do from your VSC settings. I added the following:

"terminal.integrated.profiles.osx": {
  "zsh": {
    "path": "zsh",
    "args": []
  },
},
"terminal.integrated.defaultProfile.osx": "zsh",

Now it works as expected.

Thanks for the pointer @Siglud 🎩

@andrewconnell andrewconnell changed the title 🐞 VSC TTK running Azure Function nested project ignores Node version in environment variable PATH (set via NVM) 🐞 VSC TTK running Azure Function nested project ignores Node version in environment variable PATH (set via NVM) **SOLVED** Jan 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
investigating needs attention This issue needs the attention of a contributor. TA:Compute Team Area: Compute
Projects
None yet
Development

No branches or pull requests

4 participants