11//! Get "compiler" args from cargo
22
33use crate :: errors:: * ;
4+ use anyhow:: anyhow;
45use cargo_manifest:: { Edition , Manifest , MaybeInherited :: Local } ;
56use log:: { debug, info} ;
67use std:: fs;
@@ -27,9 +28,9 @@ use std::process::Command;
2728///
2829/// # fn main() -> Result<()> {
2930/// // Get cargo to say what the compiler args need to be...
30- /// let proj_root = std::env::current_dir()?; // or other path to `Cargo.toml`
31+ /// let manifest_file = std::env::current_dir()?.join("Cargo.toml") ; // or other path to `Cargo.toml`
3132/// let mut extern_args = ExternArgs::new();
32- /// extern_args.load(&proj_root )?;
33+ /// extern_args.load(&manifest_file )?;
3334///
3435/// // then, when actually invoking rustdoc or some other compiler-like tool...
3536///
@@ -61,11 +62,14 @@ impl ExternArgs {
6162 /// Run a `cargo build` to see what args Cargo is using for library paths and extern crates.
6263 /// Touch a source file in the crate to ensure something is compiled and the args will be visible.
6364
64- pub fn load ( & mut self , proj_root : & Path ) -> Result < & Self > {
65+ pub fn load ( & mut self , cargo_path : & Path ) -> Result < & Self > {
6566 // find Cargo.toml and determine the package name and lib or bin source file.
66- let cargo_path = proj_root. join ( "Cargo.toml" ) ;
67+ let proj_root = cargo_path
68+ . canonicalize ( ) ?
69+ . parent ( )
70+ . ok_or ( anyhow ! ( "can't find parent of {:?}" , cargo_path) ) ?. to_owned ( ) ;
6771 let mut manifest = Manifest :: from_path ( & cargo_path) ?;
68- manifest. complete_from_path ( proj_root) ?; // try real hard to determine bin or lib
72+ manifest. complete_from_path ( & proj_root) ?; // try real hard to determine bin or lib
6973 let package = manifest
7074 . package
7175 . expect ( "doctest Cargo.toml must include a [package] section" ) ;
@@ -75,7 +79,7 @@ impl ExternArgs {
7579 self . edition = if let Some ( Local ( edition) ) = package. edition {
7680 my_display_edition ( edition)
7781 } else {
78- "" . to_owned ( ) //
82+ "" . to_owned ( ) //
7983 } ;
8084
8185 debug ! (
@@ -91,7 +95,7 @@ impl ExternArgs {
9195 let try_path: PathBuf = proj_root. join ( "src" ) . join ( fname) ;
9296 if try_path. exists ( ) {
9397 touch ( & try_path) ?;
94- self . run_cargo ( proj_root, & cargo_path) ?;
98+ self . run_cargo ( & proj_root, & cargo_path) ?;
9599 return Ok ( self ) ;
96100 // file should be closed when f goes out of scope at bottom of this loop
97101 }
0 commit comments