From 8559be2fffa3343db8a76e46974430dde1363862 Mon Sep 17 00:00:00 2001 From: Daniel Wagner-Hall Date: Mon, 30 Jan 2023 16:01:45 +0000 Subject: [PATCH 1/4] Allow setting env vars in cargo_metadata command Just like with `other_options`, this is a reasonably common use-case, and needing to re-implement `exec` just to pass an extra env var is a bit annoying. --- src/lib.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 5ab6030b..a03da125 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -592,6 +592,9 @@ pub struct MetadataCommand { /// Arbitrary command line flags to pass to `cargo`. These will be added /// to the end of the command line invocation. other_options: Vec, + /// Arbitrary environment variables to set when running `cargo`. These will be merged into + /// the calling environment, overriding any which clash. + env: HashMap, /// Show stderr verbose: bool, } @@ -689,6 +692,13 @@ impl MetadataCommand { self } + /// Arbitrary environment variables to set when running `cargo`. These will be merged into + /// the calling environment, overriding any which clash. + pub fn env(&mut self, key: String, val: String) -> &mut MetadataCommand { + self.env.insert(key, val); + self + } + /// Set whether to show stderr pub fn verbose(&mut self, verbose: bool) -> &mut MetadataCommand { self.verbose = verbose; @@ -729,6 +739,8 @@ impl MetadataCommand { } cmd.args(&self.other_options); + cmd.envs(&self.env); + cmd } From 48c27536745daefd7f428be72912f5ee75d84e78 Mon Sep 17 00:00:00 2001 From: Daniel Wagner-Hall Date: Mon, 30 Jan 2023 22:53:36 +0000 Subject: [PATCH 2/4] Add examples --- src/lib.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index a03da125..dc52009b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -694,6 +694,21 @@ impl MetadataCommand { /// Arbitrary environment variables to set when running `cargo`. These will be merged into /// the calling environment, overriding any which clash. + /// + /// Some examples of when you may want to use this: + /// 1. Setting cargo config values without needing a .cargo/config.toml file, e.g. to set + /// `CARGO_NET_GIT_FETCH_WITH_CLI=true` + /// 2. To specify a custom path to RUSTC if your rust toolchain components aren't laid out in + /// the way cargo expects by default. + /// + /// ```no_run + /// # use cargo_metadata::{CargoOpt, MetadataCommand}; + /// MetadataCommand::new() + /// .env(String::from("CARGO_NET_GIT_FETCH_WITH_CLI"), String::from("true")) + /// .env(String::from("RUSTC"), String::from("/path/to/rustc")) + /// // ... + /// # ; + /// ``` pub fn env(&mut self, key: String, val: String) -> &mut MetadataCommand { self.env.insert(key, val); self From 3da1b27d185783a42f2b76566df1aa09b2ad1804 Mon Sep 17 00:00:00 2001 From: Daniel Wagner-Hall Date: Tue, 31 Jan 2023 10:41:05 +0000 Subject: [PATCH 3/4] Use Into not String --- src/lib.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index dc52009b..919ee2fc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -83,6 +83,7 @@ use camino::Utf8PathBuf; use derive_builder::Builder; use std::collections::HashMap; use std::env; +use std::ffi::OsString; use std::fmt; use std::hash::Hash; use std::path::PathBuf; @@ -594,7 +595,7 @@ pub struct MetadataCommand { other_options: Vec, /// Arbitrary environment variables to set when running `cargo`. These will be merged into /// the calling environment, overriding any which clash. - env: HashMap, + env: HashMap, /// Show stderr verbose: bool, } @@ -704,13 +705,13 @@ impl MetadataCommand { /// ```no_run /// # use cargo_metadata::{CargoOpt, MetadataCommand}; /// MetadataCommand::new() - /// .env(String::from("CARGO_NET_GIT_FETCH_WITH_CLI"), String::from("true")) - /// .env(String::from("RUSTC"), String::from("/path/to/rustc")) + /// .env("CARGO_NET_GIT_FETCH_WITH_CLI", "true") + /// .env("RUSTC", "/path/to/rustc") /// // ... /// # ; /// ``` - pub fn env(&mut self, key: String, val: String) -> &mut MetadataCommand { - self.env.insert(key, val); + pub fn env, V: Into>(&mut self, key: K, val: V) -> &mut MetadataCommand { + self.env.insert(key.into(), val.into()); self } From 29c17e5e1b4c71144477e85d1fb47f6f9e199d7a Mon Sep 17 00:00:00 2001 From: Daniel Wagner-Hall Date: Tue, 31 Jan 2023 10:49:34 +0000 Subject: [PATCH 4/4] fmt --- src/lib.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 919ee2fc..27c72da0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -710,7 +710,11 @@ impl MetadataCommand { /// // ... /// # ; /// ``` - pub fn env, V: Into>(&mut self, key: K, val: V) -> &mut MetadataCommand { + pub fn env, V: Into>( + &mut self, + key: K, + val: V, + ) -> &mut MetadataCommand { self.env.insert(key.into(), val.into()); self }