@@ -15,7 +15,14 @@ use crate::cargo::{CargoCmd as _, cargo};
1515/// A process builder for cargo commands, providing a similar API to `std::process::Command`.
1616///
1717/// `CargoCommand` is a wrapper around `std::process::Command` specifically designed for
18- /// executing cargo commands with additional functionality like sysroot preparation.
18+ /// executing cargo commands targeting [hyperlight](https://github.com/hyperlight-dev/hyperlight)
19+ /// guest code.
20+ /// Before executing the desired command, `CargoCommand` takes care of setting up the
21+ /// appropriate environment. It:
22+ /// * creates a custom rust target for hyperlight guest code
23+ /// * creates a sysroot with Rust's libs core and alloc
24+ /// * finds the appropriate compiler and archiver for any C dependencies
25+ /// * sets up necessary environment variables for `cc-rs` and `bindgen` to work correctly.
1926///
2027/// # Examples
2128///
@@ -60,15 +67,15 @@ impl Default for CargoCommand {
6067impl CargoCommand {
6168 /// Constructs a new `CargoCommand` for launching the cargo program.
6269 ///
70+ /// The value of the `CARGO` environment variable is used if it is set; otherwise, the
71+ /// default `cargo` from binary `PATH` is used.
72+ /// If `RUSTUP_TOOLCHAIN` is set in the environment, it is also propagated to the
73+ /// child process to ensure correct functioning of the rustup wrappers.
74+ ///
6375 /// The default configuration is:
6476 /// - No arguments to the program
6577 /// - Inherits the current process's environment
6678 /// - Inherits the current process's working directory
67- /// - Inherits stdin/stdout/stderr for [`spawn`] or [`status`], but creates pipes for [`output`]
68- ///
69- /// [`spawn`]: CargoCommand::spawn
70- /// [`status`]: CargoCommand::status
71- /// [`output`]: CargoCommand::output
7279 ///
7380 /// # Examples
7481 ///
@@ -242,8 +249,16 @@ impl CargoCommand {
242249 ///
243250 /// [`env`]: CargoCommand::env
244251 pub fn env_clear ( & mut self ) -> & mut Self {
252+ let rust_toolchain = self
253+ . get_envs ( )
254+ . find_map ( |( k, v) | ( k == "RUSTUP_TOOLCHAIN" ) . then_some ( v) )
255+ . flatten ( )
256+ . map ( |v| v. to_os_string ( ) ) ;
245257 self . clear_env = true ;
246258 self . command . env_clear ( ) ;
259+ if let Some ( rust_toolchain) = rust_toolchain {
260+ self . command . env ( "RUSTUP_TOOLCHAIN" , rust_toolchain) ;
261+ }
247262 self
248263 }
249264
@@ -521,6 +536,10 @@ impl CargoCommand {
521536 self . prepare_sysroot ( )
522537 . context ( "Failed to prepare sysroot" ) ?;
523538
539+ if let Some ( cwd) = self . get_current_dir ( ) {
540+ env:: set_current_dir ( cwd) . context ( "Failed to change current directory" ) ?;
541+ }
542+
524543 Ok ( exec (
525544 self . get_program ( ) ,
526545 self . get_args ( ) ,
0 commit comments