Open6

WSLあれこれ

えんぶんえんぶん

WSLで躓いたこと困ったこと解決したことをつらつらり

えんぶんえんぶん

WSLの再起動

# ディストリビューションの一覧を表示
> wsl -l -v
# 停止
> wsl -t <dist_name>
# 再度terminalからWSLにつなぐなりすれば動き出す

# or

# 管理者権限のPowershellから実行
> Restart-Service LxssManager
えんぶんえんぶん

Windowsの環境変数がUbuntuの$PATHに含まれてる

https://docs.microsoft.com/ja-jp/windows/wsl/wsl-config#interop

Ubuntu側の/etc/wsl.confファイル(なければ作る)に以下を記述

wsl.conf
[interop]
enabled = false
appendWindowsPath = false

ubuntuを再起動すると反映される。
ディストリビューションを閉じて8秒ほどたつと再起動されるのでwsl --list --runningコマンドで逐一起動しているディストリビューションがあるか確認し、起動しているディストリビューションがなければ再度ディストリビューションを起動すると設定が反映されてる

❯ wsl --list --running
実行中のディストリビューションはありません。

https://learn.microsoft.com/ja-jp/windows/wsl/wsl-config#the-8-second-rule

codeコマンドでVSCodeが起動しない

$ code .
Command is only available in WSL or inside a Visual Studio Code terminal.

codeコマンドで呼び出されているのは、vscode-serverでWindows側に入っているVSCodeではない。
これはappendWindowsPath = falseに設定してWindows側のPathがUbuntuに通っていないため。
従ってWindows側のVSCodeのパスをUbuntuの$PATHに追加してやればよい。

~/.bashrc
# パスは適宜変えてね
export PATH=$PATH:"/mnt/c/Users/username/AppData/Local/Programs/Microsoft VS Code/bin"

appendWindowsPath = trueに戻せない???

appendWindowsPathをfalseにした後trueに戻したけどPATHが変わらない。元に戻せない???

えんぶんえんぶん

Proxy

https://qiita.com/Gushi_maru/items/5ba23d997e32abc98620

.bash_profile

export http_proxy="http://proxy.example.com:port"
export https_proxy="http://proxy.example.com:port"
export no_proxy="http://proxy.example.com:port"

/etc/apt/apt.conf

Acquire::http::Proxy "http://proxy.example.com:port";
Acquire::https::Proxy "http://proxy.example.com:port";

/etc/wgetrc

https_proxy = http://proxy.example.com:port
http_proxy = http://proxy.example.com:port
ftp_proxy = http://proxy.example.com:port
えんぶんえんぶん

VSCode

https://code.visualstudio.com/docs/remote/wsl

WSL拡張を入れる

これ入れてからWSL上でCode実行しないとうまいことしてくれない
https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-wsl

terminal

https://code.visualstudio.com/docs/terminal/profiles#_wsl

bash_profileが読み込まれない

https://code.visualstudio.com/docs/terminal/basics#_why-is-nvm-complaining-about-a-prefix-option-when-the-integrated-terminal-is-launched

To get the development tools on the $PATH, VS Code will launch a bash login shell on startup. This means that your ~/.bash_profile has already run and when an integrated terminal launches, it will run another login shell, reordering the $PATH potentially in unexpected ways.
[翻訳] VS Codeは、$PATH上の開発ツールを取得するために、起動時にbashログインシェルを起動します。つまり、~/.bash_profileは既に実行されており、統合ターミナルを起動すると別のログインシェルが実行され、$PATHが予期せぬ順序で変更される可能性があります。

ohmyposhの起動を.bash_profileに書いていたがVSCodeでは起動しなかったため.bashrcにも書いた。2重で書いてていいかよくわかってないのでちゃんと理解したい

# .bashrc と .bash_profile の両方に書いた
eval "$(oh-my-posh init bash --config $(brew --prefix oh-my-posh)/themes/takuya.omp.json)"

terminal起動すると.bashrcが2回呼び出されてる気がする