Cargo doc --open and compiler errors

Often, I want to open the docs for a project to read documentation for some dependencies, but when I do cargo doc --open, I get a compiler error. I have to either

  1. Fix the compiler errors, or
  2. Do pushd target/doc/ && pwd && popd, copy the directory into my search bar, and then use the folder view to select some documentation.

It would be much easier for me if, when cargo doc --open failed to build all the documentation, it opened the browser to the document root anyway ($project_root/target/doc/). Really would save me effort. Is this something other people have come across, or is there a solution I should be aware of? :slight_smile:

lynx target/doc/index.html. Or whatever file you want to open.

So I use a graphical browser for viewing my docs (firefox). If I do firefox target/doc/some_package_dont_care_which/index.html, then the behavior is different depending on whether I already have a window open or not. If I have a window open, a new tab will be added with the docs (great), but if I don’t have any window open a new one will be created, and attached as the foreground process in my terminal (not so great).

Even without this, I think I’d still rather choose my file from the browser rather than just tab-complete to get any and then select the one I want from the sidebar.

I’d also kinda like an index.html in target/doc that is blank but has the sidebar, so you can choose your documentation.

EDIT If the answer is it’s work and no-one has time to do it then great I can do it, that’s different from it has these disadvantages and there is this better alternative.

You can always use & to background the job. Or even use xdg-open if configured correctly. I just wanted to give hints for quicker workarounds that the push/pop-dance and copy-pasting the folder name…

You could also use git stash/git stash pop, hoping that that the last commit was compilable.

1 Like

If it’s a specific dependency you care about you could use cargo doc -p foo --open to only build foos docs. If it’s only a few I think you can specify multiple -p as well. Otherwise you could run cargo doc first to build as many docs as it can before it fails, then cargo doc -p foo --open to open one of the ones that did build.

1 Like

I think this is the solution I should learn how to use. :slight_smile:

This is a great idea too!

Thanks for the help.

I opened an issue a long time ago for the same problem.

1 Like