Check CARGO_TARGET_DIR and CARGO_BUILD_TARGET_DIR

Previously, we assumed that the documentation generated by cargo is
placed in the target directory relative to the current working
directory.  This is only true if the CARGO_TARGET_DIR and
CARGO_BUILD_TARGET_DIR environment variables are not set.

With this patch, we check the CARGO_TARGET_DIR and
CARGO_BUILD_TARGET_DIR environment variables to determine the target
directory.  It could also be set in the Cargo configuration file, but
that would be too complicated to check.  In the future, we might be able
to use the cargo config subcommand to determie the target directory.
master
Robin Krahl 3 years ago
parent 601ec251f8
commit 04432813bd
No known key found for this signature in database
GPG Key ID: 8E9B0870524F69D8

@ -47,8 +47,9 @@ pub struct Args {
/// Do not search the default documentation sources
///
/// If this option is not set, rusty-man appends `$sysroot/share/doc/rust{,-doc}/html` and
/// `target/doc` to the list of sources if they exist. `$sysroot` is the output of `rustc
/// --print sysroot` or `/usr` if that command does not output a valid path.
/// `$target/doc` to the list of sources if they exist. `$sysroot` is the output of `rustc
/// --print sysroot` or `/usr` if that command does not output a valid path. `$target` is
/// `$CARGO_TARGET_DIR`, `$CARGO_BUILD_TARGET_DIR` or `./target`.
#[merge(strategy = merge::bool::overwrite_false)]
#[structopt(long)]
pub no_default_sources: bool,

@ -8,9 +8,10 @@
//! 1. The sources, currently only local directories, are loaded, see the `load_sources` function
//! and the `source` module. Per default, we look for documentation in the directory
//! `share/doc/rust{,-doc}/html` relative to the Rust installation path (`rustc --print sysroot`
//! or `usr`) and in `./target/doc`.
//! 2. We try to look up the given keyword in all acailable sources, see the and the `source`
//! module for the lookup logic and the `doc` module for the loaded documentation.
//! or `usr`) and the `doc` directory relative to the Cargo target directory
//! (`$CARGO_TARGET_DIR`, `$CARGO_BUILD_TARGET_DIR` or `./target`).
//! 2. We try to look up the given keyword in all available sources, see the `parser` and the
//! `source` module for the lookup logic and the `doc` module for the loaded documentation.
//! 3. If we didnt find a match in the previous step, we load the search index from the
//! `search-index.js` file for all sources and try to find a matching item. If we find one, we
//! open the documentation for that item as in step 2. See the `search_doc` function and the
@ -48,6 +49,7 @@ mod source;
mod test_utils;
mod viewer;
use std::env;
use std::io;
use std::path;
@ -111,13 +113,15 @@ fn load_sources(sources: &[String], load_default_sources: bool) -> anyhow::Resul
}
fn get_default_sources() -> Vec<path::PathBuf> {
let mut default_sources: Vec<path::PathBuf> = Vec::new();
let mut default_sources = Vec::new();
let sysroot = get_sysroot().unwrap_or_else(|| path::PathBuf::from("/usr"));
default_sources.push(sysroot.join("share/doc/rust/html"));
default_sources.push(sysroot.join("share/doc/rust-doc/html"));
default_sources.push(path::PathBuf::from("./target/doc"));
let mut target_dir = get_target_dir();
target_dir.push("doc");
default_sources.push(target_dir);
default_sources
}
@ -133,6 +137,13 @@ fn get_sysroot() -> Option<path::PathBuf> {
.map(|s| s.trim().into())
}
fn get_target_dir() -> path::PathBuf {
env::var_os("CARGO_TARGET_DIR")
.or_else(|| env::var_os("CARGO_BUILD_TARGET_DIR"))
.map(From::from)
.unwrap_or_else(|| "./target".into())
}
/// Use the search index to find the documentation for an item that partially matches the given
/// keyword.
fn search_doc(sources: &source::Sources, name: &doc::Name) -> anyhow::Result<Option<doc::Doc>> {

Loading…
Cancel
Save