Skip to content

Instantly share code, notes, and snippets.

@swayducky
Last active February 13, 2025 09:14
Show Gist options
  • Save swayducky/8ba8f2db156c7f445d562cdc12c0ddb4 to your computer and use it in GitHub Desktop.
Save swayducky/8ba8f2db156c7f445d562cdc12c0ddb4 to your computer and use it in GitHub Desktop.
This fixes using "cursor" command in WSL
#!/usr/bin/env sh
# LATEST VERSION OF THIS SCRIPT: https://gist.github.com/swayducky/8ba8f2db156c7f445d562cdc12c0ddb4
# FORKED FROM: https://gist.github.com/ddwang/0046da801bcb29d241869d37ad719394
# 1) No longer has a hard-coded COMMIT
# 2) Auto-symlinks a "code" script to avoid wslCode.sh breaking
# HOW TO INSTALL:
# 1) Remove "c:\Users\<USER_NAME>\AppData\Local\Programs\cursor\resources\app\bin" from Windows Environment Settings
# 2) Modify this script with your Windows <USER_NAME> (NOT your WSL username) in the VSCODE_PATH variable
# 3) Save this script as ~/.local/bin/cursor
# 4) chmod +x ~/.local/bin/cursor
# See DISCUSSION:
# Github Issue: https://github.com/getcursor/cursor/issues/807
# Forum Thread: https://forum.cursor.com/t/is-there-wsl2-support/97/42
# 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
SERVER_BIN="${HOME}/.cursor-server/bin/"
COMMIT="$(ls -t1 $SERVER_BIN 2> /dev/null | head -n 1)" # dynamically figure out the COMMIT :)
APP_NAME="code"
QUALITY="stable"
NAME="Cursor"
SERVERDATAFOLDER=".cursor-server"
VSCODE_PATH="/mnt/c/Users/<YOUR_USERNAME>/AppData/Local/Programs/cursor/"
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
MY_CLI_DIR_YO="$SERVER_BIN/$COMMIT/bin/remote-cli"
# if /code doesn't exist, symlink it
if [ ! -d "$MY_CLI_DIR_YO/code" ]; then
ln -s "$MY_CLI_DIR_YO/cursor" "$MY_CLI_DIR_YO/code"
fi
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 $?
@jl-massey
Copy link

Fantastic! Works a treat, thank you! I was about to give cursor a miss....mainly because env variables in WSL2 weren't being respected when starting from Windows....but it's all better now! THANK YOU!
As for people's comments about the symlink throwing an error....my fix was to just ad an f:

[line 72]
was: ln -s "$MY_CLI_DIR_YO/cursor" "$MY_CLI_DIR_YO/code"
now: ln -sf "$MY_CLI_DIR_YO/cursor" "$MY_CLI_DIR_YO/code"

@toml01
Copy link

toml01 commented Sep 5, 2024

Awesome stuff ❤️

Created this gist just to make things a bit smoother (and also to automate after every freakin update 🥲):

wget https://gist.githubusercontent.com/toml01/b413bd736562a5dd6f3d2754ff24facc/raw/7f619d1c0a89637f207d2764f40ab023bdc96075/cursor_fix.sh
chmod +x cursor_fix.sh
./cursor_fix.sh

Or just take this snippet:

#! /bin/bash

wget -O cursor_fix_template https://gist.githubusercontent.com/swayducky/8ba8f2db156c7f445d562cdc12c0ddb4/raw/6c3a3c0be667ed487de5c6fd94fad7a7d8e5d61f/cursor
perl -i -pe 's/<YOUR_USERNAME>/`cmd.exe \/c echo %USERNAME% 2>\/dev\/null | tr -d "\r\n"`/ge' cursor_fix_template
perl -i -pe 's/ln -s "\$MY_CLI_DIR_YO\/cursor" "\$MY_CLI_DIR_YO\/code"/ln -sf "\$MY_CLI_DIR_YO\/cursor" "\$MY_CLI_DIR_YO\/code"/g' cursor_fix_template
mv cursor_fix_template $(which cursor)

@littleV
Copy link

littleV commented Sep 24, 2024

A small fix to this: for line 71, the condition [ ! -d "$MY_CLI_DIR_YO/code" ] is not sufficient to check for the existence of the symlink. This is because -d checks if the path is a directory, not if it is a symlink.
Replace line 71 ~ 73 to:

if [ ! -e "$MY_CLI_DIR_YO/code" ]; then
    # If the path does not exist (neither a file nor a directory nor a valid symlink)
    ln -s "$MY_CLI_DIR_YO/cursor" "$MY_CLI_DIR_YO/code"
elif [ -L "$MY_CLI_DIR_YO/code" ] && [ ! -e "$MY_CLI_DIR_YO/code" ]; then
    # If the path is a symlink but it is broken
    rm "$MY_CLI_DIR_YO/code"
    ln -s "$MY_CLI_DIR_YO/cursor" "$MY_CLI_DIR_YO/code"
fi

@BradKML
Copy link

BradKML commented Jan 17, 2025

Can someone help out on this one? Cus once Cursor is updated using the pop-up the same issues will happen chocolatey-community/chocolatey-package-requests#1576 https://scottspence.com/posts/cursor-setup-for-wsl

@BradKML
Copy link

BradKML commented Feb 13, 2025

Okay made something new (very likely broken) https://gist.github.com/BradKML/b84675c356007c8029ce927c484f4cc6

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