Search code examples
bashclionwindows-subsystem-for-linux

How to set up WSL toolchain in CLion to behave like interactive WSL?


I've got a Debian WSL2 install that I'm using to write some cross platform C code. As part of the CMake setup, I'm calling a non-global install of npm (which I installed using nvm). This works fine when running interactively, but when run from the CLion Run configuration, it isn't aware of the NVM install - likely because it's not sourcing the bash_profile before running. How can I get a non-interactive shell to run interactively in this context?


Solution

  • To correctly find your NPM install through NVM, you can use a combination of sudo and source. The key is to always source nvm.sh before attempting to use anything installed by npm

    In your CMake

    add_custom_command(
      COMMAND sudo -u <your user> /path/to/script.sh
      ...
    )
    

    In your build script, /path/to/script.sh

    #!/bin/bash
    
    # other init stuff
    
    # before your first npm/nvm call
    source "$HOME/.nvm/nvm.sh"
    
    # now you can use anything installed by npm