0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

cursor入れたらvscodeのcode .が使えなくなった時の対処法

Last updated at Posted at 2025-05-18

cursor試してたらvscodeのcode .が使えなくなった

先日windows環境でcursorを適当に試していたところ、code .を実行したらcursorが起動した。
単純にcursorのインストール時にcodeコマンドをインストールしたせいで競合していたのが原因だったが、cursorコマンドでの起動をインストールしたのにcodeでもcursorが起動するのは普通に困るので何とかしたい。

vscode側でcodeの再インストールが出来ない問題

他記事ではvscode側でcodeを再インストールすることで解決していたが自分の環境では再インストールの表示がされなかった(cursor側では表示されたのに…)
そのためcursor側のコマンド自体を削除することでごり押しして解決することにした

image.png

cursor側のcodeコマンド自体を削除する

whereコマンドでcodeコマンドのバッチスクリプト(シェルスクリプト)を探す

where code

今回は以下のように複数表示された。cursor内のcodeコマンドとvscode側のcodeコマンドが確認できる

C:\Users\user\AppData\Local\Programs\cursor\resources\app\bin\code
C:\Users\user\AppData\Local\Programs\cursor\resources\app\bin\code.cmd
C:\Microsoft VS Code\bin\code
C:\Microsoft VS Code\bin\code.cmd

後は単純にcursor側のコマンドをrmで削除する

rm C:/Users/p-user/AppData/Local/Programs/cursor/resources/app/bin/code
rm C:/Users/p-user/AppData/Local/Programs/cursor/resources/app/bin/code.cmd

もう一度whereを実行して消えたか確認する

where code
C:\Microsoft VS Code\bin\code
C:\Microsoft VS Code\bin\code.cmd

これでcodeコマンドを実行しvscodeの起動が確認できた。

おまけ: codeとcode.cmdについて

そもそもcodeコマンドってなんぞやという疑問が生まれたので初学者なりに調査してみた

code.cmdの中身

vscodeのcode.cmdはバッチファイルっぽい中身をしていた。

@echo off
setlocal
set VSCODE_DEV=
set ELECTRON_RUN_AS_NODE=1
"%~dp0..\Code.exe" "%~dp0..\resources\app\out\cli.js" %*
IF %ERRORLEVEL% NEQ 0 EXIT /b %ERRORLEVEL%
endlocal

読み方が分からないけどこんな感じになっている

  1. VSCODE_DEVとelectronのnodeモードでの起動(ELECTRON_RUN_AS_NODE=1)
  2. 自身のパスから上の階層のcode.exeを起動する
  3. cli.jsに引数としてcodeコマンドの引数を渡している
  4. エラー出力が検出できる

ただ、jsを実行しているということはnodeが入っていないと使えないのでは?という疑問が浮かぶ。
調べてみるとvscodeはElectronというデスクトップアプリフレームワークで作られていてchromiumとnode.jsが埋め込まれているらしい。ELECTRON_RUN_AS_NODEがtrueなのもそのためで、結果としてcli.jsが実行できるのだそう。すごい。

codeの中身

shebangが見えるので多分UNIX系で使えるようにするやつっぽい?

#!/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="848b80aeb52026648a8ff9f7c45a9b0a80641e2e"
APP_NAME="code"
QUALITY="stable"
NAME="Code"
SERVERDATAFOLDER=".vscode-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" --locate-extension $WSL_EXT_ID >/tmp/remote-wsl-loc.txt 2>/dev/null </dev/null
	WSL_EXT_WLOC=$(cat /tmp/remote-wsl-loc.txt)

	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" "$@"
exit $?

詳しく見てみると

  1. Electron を Node モードで起動する(ELECTRON_RUN_AS_NODE=1)
  2. resources/app/out/cli.js を Node.js で実行する
  3. WSL環境かどうかを判定し、リモート接続(Remote-WSL)にする

というような機能が実装されていた。
codeとcode.cmdどちらにもelectronをnodeモードで起動する文とcli.jsの起動文が含まれているためここが大事な部分っぽい。

ゆるまとめ: codeコマンドの正体

codeコマンドの正体はnodeモードでElectronを起動してcli.jsを実行し、引数にvscodeを起動するディレクトリを渡すバッチファイル(シェルスクリプト)であった。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?