developing in wsl

122 阅读2分钟

需求要点

  • 喜爱 正常使用 windows 环境。
  • 喜爱 在基于Linux的环境中进行开发,请使用特定于Linux的工具链和实用程序,并运行和调试基于Linux的应用程序

理论基础

The Visual Studio Code WSL extension lets you use the Windows Subsystem for Linux (WSL) as your full-time development environment right from VS Code.

操作指南

Install the Windows Subsystem for Linux along with your preferred Linux distribution.

installing wsl and setting


done


Install Visual Studio Code on the Windows side (not in WSL).

installing development tools on window

scoop install vscode

done


Install the WSL extension. If you plan to work with other remote extensions in VS Code, you may choose to install the Remote Development extension pack

# lst:
# code --list-extensions
# code --list-extensions --show-versions


# add:
# code --install-extension ms-vscode.PowerShell
# code --install-extension EditorConfig.EditorConfig
code --install-extension ms-vscode-remote.remote-wsl
code --install-extension ms-vscode-remote.vscode-remote-extensionpack

done


Open a remote folder or workspace -> from-wsl-terminals

Opening a folder inside the Windows Subsystem for Linux in VS Code

  • Open a WSL terminal window (using the start menu item or by typing wsl from a command prompt / PowerShell).
  • Navigate to a folder you'd like to open in VS Code (including, but not limited to, Windows filesystem mounts like /mnt/c)
  • Type code .  in the terminal. When doing this for the first time, you should see VS Code fetching components needed to run in WSL. This should only take a short while, and is only needed once
# start and login wsl:
wsl

# Navigate to a folder:
cd /mnt/d/code/shell

# open folder  with vscode cli
code .

Open a remote folder or workspace -> from-vscode

  • open vscode in window
  • F1 -> typing WSL: Connect to WSL or WSL: Connect to WSL using Distro
  • Use the File menu to open your folder

If you already have a folder open, you can also use the WSL: Reopen Folder in WSL command.

If you are in a WSL window and want to open the current input in a local window, use WSL: Reopen in Windows.

Open a remote folder or workspace -> From the Windows command prompt

# wsl -l -v

# code --remote wsl+<distro name> <path in WSL>
# eg:
# code --remote wsl+ubuntu_2204 /mnt/d/code/shell
# code --folder-uri vscode-remote://wsl+ubuntu_2204//mnt/d/code/shell

done


working with git -> setting-line-ending

If you are working with the same repository in WSL and Windows, be sure to set up consistent line endings. See tips and tricks for details.

# If you would prefer to still always upload Unix-style line endings (LF), you can use the `input` option.
git config --global core.autocrlf input

# If you'd prefer to disable line-ending conversion entirely
git config --global core.autocrlf false

# set xx.bat and xx.md file line-ending for window
cat > .gitattributes <<EOF

* text=auto eol=lf
*.{cmd,[cC][mM][dD]} text eol=crlf
*.{bat,[bB][aA][tT]} text eol=crlf
EOF

done


working with git -> Sharing Git credentials between Windows and WSL

If you use HTTPS to clone your repositories and have a credential helper configured in Windows, you can share this with WSL so that passwords you enter are persisted on both sides. (Note that this does not apply to using SSH keys.)

  • git config --global credential.helper wincred in widnow powershell
  • in wsl terminal:
git config --global credential.helper "/mnt/c/Program\ Files/Git/mingw64/bin/git-credential-manager-core.exe"

done


Opening a WSL 2 folder in a container

If you are using WSL 2 and Docker Desktop's WSL 2 back-end, you can use the Dev Containers extension to work with source code stored inside WSL!

  • install and setup Docker Desktop's WSL 2 support. (must docker Desktop?)
  • install the Dev Containers extension along with the WSL extension.
  • open your source code folder in WSL as you would normally.
  • Once your folder is open in WSL, F1 -> select Dev Containers: Reopen in Container 
  • If the folder does not have a .devcontainer/devcontainer.json file in it, you'll be asked to pick a starting point from a filterable list or an existing Dockerfile or Docker Compose file (if one exists).
# code --install-extension ms-vscode-remote.remote-containers
.devcontainer/devcontainer.json
Dockerfile
docker-compose.yaml

参考文献

Developing in the Windows Subsystem for Linux with Visual Studio Code