warrensbox/terraform-switcher

Very slow on Ubuntu/WSL2 due to PATH containing entries from Windows

Closed this issue ยท 13 comments

Running on Ubuntu in WSL2 (Windows Subsystem for Linux), tfswitch takes a very long time (10+ seconds) to complete, after the version has been selected/detected.
Reducing PATH to Linux-only paths, i.e. omitting anything under /mnt/c/, makes the command run almost instantly.
I have not looked at the code, but I am guessing it somehow does or triggers a traversal of all PATH entries, which can be quite slow for paths residing on the Windows filesystem(s), especially under WSL2.

As a workaround, I have this in a script called tf (shortcut for running both tfswitch and terraform):

#!/bin/sh
basePath="$HOME/.local/bin"
binPath="$basePath/terraform"
if command -v tfswitch > /dev/null
then
  PATH="$basePath" tfswitch -b "$binPath" 1>&2
  eval "$binPath $@"
else
  terraform $@
fi

I can confirm this. I came here to report that running tfswitch was very slow:

$ time tfswitch 0.15.0
Switched terraform to version "0.15.0"

real    0m32.086s
user    0m0.683s
sys     0m6.349s

I've lived with it for months now, so it's not new, but after reading @tachylatus post, I tried pruning the "/mnt/c" parts of the path and lo and behold:

$ time tfswitch 0.15.0
Switched terraform to version "0.15.0"

real    0m0.100s
user    0m0.026s
sys     0m0.009s

Using strace it looks like most of the time is spent waiting for a mutex of some sort, but it's definitely related to the Windows paths.

Strace outputs lots of these:

futex(0xd1cfb0, FUTEX_WAIT_PRIVATE, 0, NULL) = 0

This is my workaround:

PATH="" tfswitch 0.15.0

@tachylatus or @mhvelplund can you provide the OS and ARCH of the container that you are using?

I'm running "Ubuntu 18.04.5 LTS" inside WSL2 under Windows 10.

The PATH vairable is bananas :)

$ echo $PATH | wc -c
1295

There are 41 path elements.

you can configure wsl2 to not add windows paths:
set /etc/wsl.conf to

[interop]
enabled=true
appendWindowsPath=false

you're right @plenti-chris-fowles but by doing that you will lose the ability to call windows executables from wsl right?

I'm also experiencing really slow times using tfswitch and don't wanna go back to tfenv :P

@fgbaezp - you can manually control your path in your profile within wsl.

for example I add :/mnt/c/ProgramData/chocolatey/bin:/mnt/c/Program Files/Microsoft VS Code/bin to my path to launch vs code and things installed via chocolatey from within wsl

this still is much faster than having your entire windows PATH scanned, but gives you access to some windows tools from within wsl

This is my workaround:

PATH="" tfswitch 0.15.0

Thank you very much! it helped in my case (I use the command this way):
PATH="" > /dev/null /home/rob/.bin/tfswitch

Thanks for this.
On my side, the workaround is a wrapper than nullifies the PATH when running tfswitch and an alias to this wrapper.

From 35s to 70ms is a nice improvement ^^

Any idea why tfswitch loops over all folders from the path, performing a newnfstatat() on each file ? (including System32 /o)

Shameless shilling, but if you don't want to bother with the $PATH workaround, try out my Rust reimplementation of tfswitch called tfswitcher. I've been using it for a while for my job and it's been working well, even with Windows directories in $PATH.

I cannot seem to reproduce it with a current Ubuntu WSL

root@PLATON:~# time tfswitch 1.7.5
Creating directory for terraform binary at: /root/.terraform.versions
Downloading to: /root/.terraform.versions
25959930 bytes downloaded
Switched terraform to version "1.7.5"

real    0m5,371s
user    0m0,676s
sys     0m0,450s
root@PLATON:~# time tfswitch 1.7.4
Downloading to: /root/.terraform.versions
25940915 bytes downloaded
Switched terraform to version "1.7.4"

real    0m2,626s
user    0m0,553s
sys     0m0,216s
root@PLATON:~# time tfswitch 1.7.5
Switched terraform to version "1.7.5"

real    0m0,012s
user    0m0,001s
sys     0m0,013s
root@PLATON:~#

Can someone please verify this?

As for the time being i cannot reproduce this issue i will close it.
Feel free to open a new issue if this problem still persists.