Unhandled exception - An error occurred trying to start process when using .NET CLI in VS Code snap on Ubuntu
I install VS Code in Ubuntu using snap. This has generally been fine, but with the new-ish dotnet run app.cs, I hit an interesting problem. When I try to run some .NET CLI applications using a terminal within VS Code, I get an error that looks like this -
Unhandled exception: An error occurred trying to start process '/home/bryan/snap/code/237/.local/share/dotnet/runfile/SimpleWebServer-6a1d7e4454421e2378bbc9c66e68b9889d32b9b306e8503dd754e1248aa787cd/bin/debug/SimpleWebServer' with working directory '/home/bryan/tools/bin'. No such file or directoryThe application I was trying to run is called SimpleWebServer.cs and it is located in the /home/bryan/tools/bin, so why am I getting an error in the snap subfolder for VS Code?
When I use a “normal” terminal outside VS Code, it works just fine.
Behind the scenes
When I first run my SimpleWebServer.cs application using the .NET CLI a build is performed, and the output is placed in a cache folder.
The first time I run the application outside of VS Code, the output goes to a cache folder under ~/.local/share/dotnet/.
But when I do this within VS Code, the output goes to a subfolder of the snap folder for my current version of VS Code, for example, ~/snap/code/247/.local/share/dotnet/runfile/. Where 247 is the version of VS Code I have installed.
The problem
When I install a new version of VS Code, .NET CLI application are just copied to the new version. Say my previous version was 244, then the ~/snap/code/244/.local/share/dotnet/runfile/ directory is copied to the new version of VS Code. But the .NET CLI applications still point to the previous version of VS Code, when they were built.
Inside the folder for each .NET CLI application, there is a file named build-success.cache. If you open this, you will see it has full paths defined, and these will point to the earlier version of VS Code, 244 in this example.
When VS Code tries to execute the application, it finds the cached binaries under the current version of VS Code, but the build-success.cache file points to the old version of VS Code. This causes the error because the old version of VS Code has been removed, and the path no longer exists.
The fix - wipe `em all away
Here is a simple script to delete all cached versions of an application inside the ~/snap/code/*/.local/share/dotnet/runfile/ folders.
Just pass the name of your application to the script, and it will print out the rm -rf commands to remove all the cached versions of the application.
1#/bin/bash
2
3if [[ $# -ne 1 ]]; then
4 echo "Usage: $(basename "$0") <app-name>" >&2
5 exit 1
6fi
7
8app_name="$1"
9
10find ~/snap/code -type d \
11 -path '*/.local/share/dotnet/runfile/*' \
12 -name "${app_name}-*" \
13 -prune \
14 | sort \
15 | while IFS= read -r dir; do
16 echo "rm -rf \"${dir}\""
17 doneHere’s how to use the script -
./cleanup-dotnet-cache.sh SimpleWebServerThe output will look something like this -
rm -rf "/home/bryan/snap/code/244/.local/share/dotnet/runfile/SimpleWebServer-6a1d7e4454421e2378bbc9c66e68b9889d32b9b306e8503dd754e1248aa787cd"
rm -rf "/home/bryan/snap/code/244/.local/share/dotnet/runfile/SimpleWebServer-83dd72e5c46d2016d117e6965e75daeea2f109439a9d32cb04351d6aac27b933"
rm -rf "/home/bryan/snap/code/247/.local/share/dotnet/runfile/SimpleWebServer-6a1d7e4454421e2378bbc9c66e68b9889d32b9b306e8503dd754e1248aa787cd"
rm -rf "/home/bryan/snap/code/247/.local/share/dotnet/runfile/SimpleWebServer-83dd72e5c46d2016d117e6965e75daeea2f109439a9d32cb04351d6aac27b933"There you go, hope that helps someone.