Skip to content

Instantly share code, notes, and snippets.

@chhoumann
Last active March 25, 2024 16:03
Show Gist options
  • Save chhoumann/47f38e5855ac35a478e3761a2a6eb64b to your computer and use it in GitHub Desktop.
Save chhoumann/47f38e5855ac35a478e3761a2a6eb64b to your computer and use it in GitHub Desktop.
Fixing Cursor on WSL
#!/usr/bin/env sh
#
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
if [ "$VSCODE_WSL_DEBUG_INFO" = true ]; then
set -x
fi
COMMIT="502aa640fa223830082ace6c194b46b55e390fa0" # update this to the actual version
APP_NAME="cursor"
QUALITY="stable"
NAME="Cursor"
SERVERDATAFOLDER=".cursor-server"
VSCODE_PATH="$(dirname "$(dirname "$(realpath "$0")")")/../.."
ELECTRON="$VSCODE_PATH/$NAME.exe"
IN_WSL=false
if [ -n "$WSL_DISTRO_NAME" ]; then
# $WSL_DISTRO_NAME is available since WSL builds 18362, also for WSL2
IN_WSL=true
else
WSL_BUILD=$(uname -r | sed -E 's/^[0-9.]+-([0-9]+)-Microsoft.*|.*/\1/')
if [ -n "$WSL_BUILD" ]; then
if [ "$WSL_BUILD" -ge 17063 ]; then
# WSLPATH is available since WSL build 17046
# WSLENV is available since WSL build 17063
IN_WSL=true
else
# If running under older WSL, don't pass cli.js to Electron as
# environment vars cannot be transferred from WSL to Windows
# See: https://github.com/microsoft/BashOnWindows/issues/1363
# https://github.com/microsoft/BashOnWindows/issues/1494
"$ELECTRON" "$@"
exit $?
fi
fi
fi
if [ $IN_WSL = true ]; then
export WSLENV="ELECTRON_RUN_AS_NODE/w:$WSLENV"
CLI=$(wslpath -m "$VSCODE_PATH/resources/app/out/cli.js")
# use the Remote WSL extension if installed
WSL_EXT_ID="ms-vscode-remote.remote-wsl"
ELECTRON_RUN_AS_NODE=1 "$ELECTRON" "$CLI" --ms-enable-electron-run-as-node --locate-extension $WSL_EXT_ID >/tmp/remote-wsl-loc.txt 2>/dev/null </dev/null
WSL_EXT_WLOC=$(tail -n 1 /tmp/remote-wsl-loc.txt)
WSL_CODE=$(wslpath -u "${WSL_EXT_WLOC%%[[:cntrl:]]}")/scripts/wslCode.sh
if [ -n "$WSL_EXT_WLOC" ]; then
# replace \r\n with \n in WSL_EXT_WLOC
WSL_CODE=$(wslpath -u "${WSL_EXT_WLOC%%[[:cntrl:]]}")/scripts/wslCode.sh
"$WSL_CODE" "$COMMIT" "$QUALITY" "$ELECTRON" "$APP_NAME" "$SERVERDATAFOLDER" "$@"
exit $?
fi
elif [ -x "$(command -v cygpath)" ]; then
CLI=$(cygpath -m "$VSCODE_PATH/resources/app/out/cli.js")
else
CLI="$VSCODE_PATH/resources/app/out/cli.js"
fi
ELECTRON_RUN_AS_NODE=1 "$ELECTRON" "$CLI" --ms-enable-electron-run-as-node "$@"
exit $?
@chhoumann
Copy link
Author

chhoumann commented Mar 25, 2024

I was getting a bit annoyed by cursor not working via WSL - the command just doesn't open Cursor. So here's a fix for WSL users.

which cursor will give you the path to a script. That's the script that doesn't work (at least, not for me and seemingly others).
If you use which code, you'll find another script that opens code through wsl. This does seem to work. Users in the github issues have found that this works to open Cursor, if you modify it slightly.

So here's what I've done.
I've copied the script that which cursor pointed to. I renamed it cursor.bk. The script is on your Windows machine (uses your Windows install). Mine is /mnt/c/Users/chhou/AppData/Local/Programs/Cursor/resources/app/bin/cursor, and I renamed it to /mnt/c/Users/chhou/AppData/Local/Programs/Cursor/resources/app/bin/cursor.bk.

Now we'll copy the code script that works. There are a few things to modify, so you can just copy mine:
https://gist.github.com/chhoumann/47f38e5855ac35a478e3761a2a6eb64b

Now, the vscode server that facilitates the communication between your Windows Cursor client and the code server on WSL is installed in ~/.cursor-server. You'll need to have run Cursor, connected to WSL, and opened some folder at least once for it to be installed.
The current version (by commit) is 502aa640fa223830082ace6c194b46b55e390fa0. But don't just copy that. You can find your version (probably the latest one) with ls ~/.cursor-server/bin -a. Copy that commit hash into the cursor script and put it in the COMMIT variable. Like COMMIT="502aa640fa223830082ace6c194b46b55e390fa0".

Now go ahead and save cursor. It should now work.
I don't recommend using the Fix WSL install via the command palette in Cursor. All it's done for me is nuke my extensions. I think it just removes the vscode-server (cursor-server).

Please don't credit me with this fix, I've just pieced together something that works.

In summary:

  1. Replace the script located at which cursor with the one I provided.
  2. Update the commit hash to the folder you have installed. Find it with ls ~/.cursor-server/bin -a.

Sources:
getcursor/cursor#870
https://gist.github.com/ddwang/0046da801bcb29d241869d37ad719394
getcursor/cursor#660
https://forum.cursor.sh/t/is-there-wsl2-support/97/36

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment