cargo: `cargo doc --open` doesn't work in WSL

I’m using ubuntu(WSL) in windows, and I found this problem.

At the beginning, without any configuration, I got some error message. Following the error message, I set BROSWER environmental variable, and the error was gone and browser is opened. However nothing appear, seems it open browser but forget pass the “*.html”.

I’m sure it the BROWSER is set correctly, because follow command /usr/bin/sensible-browser xxx/inndex.html works well in shell.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 13
  • Comments: 20 (6 by maintainers)

Most upvoted comments

A short-term solution is to run explorer.exe . or point it to your project path, navigate to /target/doc/{package_name}, and then double-click index.html.

This can be solved with below

# open_browser.sh
#! /bin/bash
#echo $1
winPath="file://///wsl$//Ubuntu${1}"
#echo ${winPath}
#firefox
/mnt/c/Program\ Files/Mozilla\ Firefox/firefox.exe $winPath
#chrome
#/mnt/c/Program\ Files\ \(x86\)/Google/Chrome/Application/chrome.exe $winPath

and addexport BROWSER='xxx/open_browser.sh' to ~/.bashrc

If you use Brave Browser with Ubuntu 20.04:

  • Create ~/.scripts/open_browser.sh
#!/bin/bash
winPath="file://wsl%24/Ubuntu-20.04${1}"
/mnt/c/Program\ Files/BraveSoftware/Brave-Browser/Application/brave.exe $winPath
  • Mark it as executable chmod +x ~/.scripts/open_browser.sh
  • Add export BROWSER='~/.scripts/open_browser.sh' to ~/.bashrc

Now, cargo doc --open should work as expected.

It works after update Cargo 1.55 without BROWSER env!