diff --git a/src/lib.rs b/src/lib.rs index fae4719..1b72307 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -45,7 +45,7 @@ pub struct Context { impl Context { /* Constructors */ /// Get a context structure from a built artifact. - fn from_artifact(metadata: Metadata, artifact: &Artifact) -> Result { + fn from_artifact(metadata: &Metadata, artifact: &Artifact) -> Result { // Currently there is no clean way to get the target triple from cargo so we can only make // an approximation, we do this by extracting the target triple from the artifacts path. // For more info on the path structure see: https://doc.rust-lang.org/cargo/guide/build-cache.html @@ -54,7 +54,7 @@ impl Context { // See: https://github.com/rust-lang/cargo/issues/5579, https://github.com/rust-lang/cargo/issues/8002 // Should always succeed. - let target_path = artifact.filenames[0].strip_prefix(metadata.target_directory)?; + let target_path = artifact.filenames[0].strip_prefix(&metadata.target_directory)?; let target_name = if let Some(Utf8Component::Normal(path)) = target_path.components().next() { // TODO: How will custom profiles impact this? @@ -279,8 +279,9 @@ To see all the flags the proxied tool accepts run `cargo-{} -- --help`.{}", } } -pub fn run(tool: Tool, matches: ArgMatches) -> Result { +fn get_metadata(tool: &Tool, matches: &ArgMatches) -> Result { let mut metadata_command = MetadataCommand::new(); + metadata_command.no_deps(); if tool.needs_build() { if let Some(features) = matches.get_many::("features") { metadata_command.features(CargoOpt::SomeFeatures( @@ -302,6 +303,10 @@ pub fn run(tool: Tool, matches: ArgMatches) -> Result { bail!("Unable to find workspace members"); } + Ok(metadata) +} + +pub fn run(tool: Tool, matches: ArgMatches) -> Result { let mut tool_args = vec![]; if let Some(args) = matches.get_many::("args") { tool_args.extend(args.map(|s| s.as_str())); @@ -310,7 +315,8 @@ pub fn run(tool: Tool, matches: ArgMatches) -> Result { let tool_help = tool_args.first() == Some(&"--help"); let target_artifact = if tool.needs_build() && !tool_help { - cargo_build(&matches, &metadata)? + let metadata = get_metadata(&tool, &matches)?; + cargo_build(&matches, &metadata)?.map(|a| (a, metadata)) } else { None }; @@ -318,11 +324,11 @@ pub fn run(tool: Tool, matches: ArgMatches) -> Result { let mut lltool = Command::new(format!("rust-{}", tool.name())); if tool == Tool::Objdump { - let ctxt = if let Some(artifact) = &target_artifact { + let ctxt = if let Some((artifact, metadata)) = &target_artifact { Context::from_artifact(metadata, artifact)? } else { Context::from_flag( - metadata, + get_metadata(&tool, &matches)?, matches.get_one::("target").map(|s| s.as_str()), )? }; @@ -347,7 +353,7 @@ pub fn run(tool: Tool, matches: ArgMatches) -> Result { if tool.needs_build() { // Artifact - if let Some(artifact) = &target_artifact { + if let Some((artifact, _)) = &target_artifact { let file = match &artifact.executable { // Example and bins have an executable Some(val) => val,