Skip to content
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 1 addition & 11 deletions tests/testsuite/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::path::PathBuf;
use std::thread;

use crate::prelude::*;
use crate::utils::cargo_process;
use crate::utils::{cargo_process, pkg};
use cargo_test_support::compare::assert_e2e;
use cargo_test_support::cross_compile;
use cargo_test_support::git;
Expand All @@ -21,16 +21,6 @@ use crate::utils::cross_compile::disabled as cross_compile_disabled;
use cargo_test_support::install::{assert_has_installed_exe, assert_has_not_installed_exe, exe};
use cargo_test_support::paths;

fn pkg(name: &str, vers: &str) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For myself, I'm not too much of a fan of us reusing like this as it hides in the test what is happening and gets annoying when you just need a little customization. Just directly publish the needed package inside of your test.

Package::new(name, vers)
.file("src/lib.rs", "")
.file(
"src/main.rs",
&format!("extern crate {}; fn main() {{}}", name),
)
.publish();
}

#[cargo_test]
fn simple() {
pkg("foo", "0.0.1");
Expand Down
11 changes: 11 additions & 0 deletions tests/testsuite/rustup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ fn real_rustc_wrapper(bin_dir: &Path, message: &str) -> PathBuf {
// The toolchain rustc needs to call the real rustc. In order to do that,
// it needs to restore or clear the RUSTUP environment variables so that
// if rustup is installed, it will call the correct rustc.
let rustup_toolchain_source_setup = match std::env::var_os("RUSTUP_TOOLCHAIN_SOURCE") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should be doing anything based on the rustup we are running in

See also #16131 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the risk of sounding like an excuse, I was following the example of the code just below this:

let rustup_toolchain_setup = match std::env::var_os("RUSTUP_TOOLCHAIN") {
Some(t) => format!(
".env(\"RUSTUP_TOOLCHAIN\", \"{}\")",
t.into_string().unwrap()
),
None => format!(".env_remove(\"RUSTUP_TOOLCHAIN\")"),
};

Should that following code be modified?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems suspicious and I'd like to better understand why we do this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just FYI, I'm willing to make changes not directly related #11036.

Copy link
Contributor Author

@smoelius smoelius Oct 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would you feel about rustc's path being determined in Cargo's build script rather than via environment variables in this function?

That is, I'm proposing that real_rustc_wrapper look something like this:

/// Creates an executable which prints a message and then runs the *real* rustc.
fn real_rustc_wrapper(bin_dir: &Path, message: &str) -> PathBuf {
    let real_rustc = PathBuf::from(env!("RUSTC"));
    let env = vec![("CARGO_RUSTUP_TEST_real_rustc", real_rustc)];
    make_exe(
        bin_dir,
        "rustc",
        &format!(
            r#"
                eprintln!("{message}");
                let r = std::process::Command::new(env!("CARGO_RUSTUP_TEST_real_rustc"))
                    .args(std::env::args_os().skip(1))
                    .status();
                std::process::exit(r.unwrap().code().unwrap_or(2));
            "#
        ),
        &env,
    )
}

EDIT: Also, please tell me whether you think a separate PR would be appropriate.

Some(t) => format!(
".env(\"RUSTUP_TOOLCHAIN_SOURCE\", \"{}\")",
t.into_string().unwrap()
),
None => format!(".env_remove(\"RUSTUP_TOOLCHAIN_SOURCE\")"),
};
let rustup_toolchain_setup = match std::env::var_os("RUSTUP_TOOLCHAIN") {
Some(t) => format!(
".env(\"RUSTUP_TOOLCHAIN\", \"{}\")",
Expand All @@ -78,6 +85,7 @@ fn real_rustc_wrapper(bin_dir: &Path, message: &str) -> PathBuf {
eprintln!("{message}");
let r = std::process::Command::new(env!("CARGO_RUSTUP_TEST_real_rustc"))
.args(std::env::args_os().skip(1))
{rustup_toolchain_source_setup}
{rustup_toolchain_setup}
{rustup_home_setup}
.status();
Expand Down Expand Up @@ -162,6 +170,7 @@ fn typical_rustup() {
// `~/.cargo/bin/rustc to use our custom rustup proxies.
let path = prepend_path(&cargo_bin);
p.cargo("check")
.env("RUSTUP_TOOLCHAIN_SOURCE", "default")
.env("RUSTUP_TOOLCHAIN", "test-toolchain")
.env("RUSTUP_HOME", &rustup_home)
.env("PATH", &path)
Expand All @@ -179,6 +188,7 @@ real rustc running
p.build_dir().rm_rf();

p.cargo("check")
.env("RUSTUP_TOOLCHAIN_SOURCE", "default")
.env("RUSTUP_TOOLCHAIN", "test-toolchain")
.env("RUSTUP_HOME", &rustup_home)
.env("PATH", &path)
Expand Down Expand Up @@ -249,6 +259,7 @@ fn custom_calls_other_cargo() {
// Set these to simulate what would happen when running under rustup.
// We want to make sure that cargo-custom does not try to use the
// rustup proxies.
.env("RUSTUP_TOOLCHAIN_SOURCE", "default")
.env("RUSTUP_TOOLCHAIN", "test-toolchain")
.env("RUSTUP_HOME", &rustup_home)
.with_stderr_data(str![[r#"
Expand Down
11 changes: 11 additions & 0 deletions tests/testsuite/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::path::PathBuf;

use cargo_test_support::registry::Package;
use cargo_test_support::{ArgLineCommandExt, Execs, execs, process};

pub mod cross_compile;
Expand All @@ -19,3 +20,13 @@ pub fn cargo_process(arg_line: &str) -> Execs {
pub fn cargo_exe() -> PathBuf {
snapbox::cmd::cargo_bin!("cargo").to_path_buf()
}

pub fn pkg(name: &str, vers: &str) {
Package::new(name, vers)
.file("src/lib.rs", "")
.file(
"src/main.rs",
&format!("extern crate {}; fn main() {{}}", name),
)
.publish();
}