Skip to content

Pass Process around explicitly #3871

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
46 changes: 24 additions & 22 deletions src/bin/rustup-init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use rustup::cli::rustup_mode;
#[cfg(windows)]
use rustup::cli::self_update;
use rustup::cli::setup_mode;
use rustup::currentprocess::{process, with_runtime, Process};
use rustup::currentprocess::{with_runtime, Process};
use rustup::env_var::RUST_RECURSION_COUNT_MAX;
use rustup::errors::RustupError;
use rustup::is_proxyable_tools;
Expand All @@ -40,11 +40,11 @@ fn main() {
let process = Process::os();
let mut builder = Builder::new_multi_thread();
builder.enable_all();
with_runtime(process, builder, {
async {
match maybe_trace_rustup().await {
with_runtime(process.clone(), builder, {
async move {
match maybe_trace_rustup(&process).await {
Err(e) => {
common::report_error(&e);
common::report_error(&e, &process);
std::process::exit(1);
}
Ok(utils::ExitCode(c)) => std::process::exit(c),
Expand All @@ -53,68 +53,70 @@ fn main() {
});
}

async fn maybe_trace_rustup() -> Result<utils::ExitCode> {
async fn maybe_trace_rustup(process: &Process) -> Result<utils::ExitCode> {
#[cfg(feature = "otel")]
opentelemetry::global::set_text_map_propagator(
opentelemetry_sdk::propagation::TraceContextPropagator::new(),
);
let subscriber = rustup::cli::log::tracing_subscriber(process());
let subscriber = rustup::cli::log::tracing_subscriber(process);
tracing::subscriber::set_global_default(subscriber)?;
let result = run_rustup().await;
let result = run_rustup(process).await;
// We're tracing, so block until all spans are exported.
#[cfg(feature = "otel")]
opentelemetry::global::shutdown_tracer_provider();
result
}

#[cfg_attr(feature = "otel", tracing::instrument)]
async fn run_rustup() -> Result<utils::ExitCode> {
if let Ok(dir) = process().var("RUSTUP_TRACE_DIR") {
async fn run_rustup(process: &Process) -> Result<utils::ExitCode> {
if let Ok(dir) = process.var("RUSTUP_TRACE_DIR") {
open_trace_file!(dir)?;
}
let result = run_rustup_inner().await;
if process().var("RUSTUP_TRACE_DIR").is_ok() {
let result = run_rustup_inner(process).await;
if process.var("RUSTUP_TRACE_DIR").is_ok() {
close_trace_file!();
}
result
}

#[cfg_attr(feature = "otel", tracing::instrument(err))]
async fn run_rustup_inner() -> Result<utils::ExitCode> {
async fn run_rustup_inner(process: &Process) -> Result<utils::ExitCode> {
// Guard against infinite proxy recursion. This mostly happens due to
// bugs in rustup.
do_recursion_guard()?;
do_recursion_guard(process)?;

// Before we do anything else, ensure we know where we are and who we
// are because otherwise we cannot proceed usefully.
let current_dir = process()
let current_dir = process
.current_dir()
.context(RustupError::LocatingWorkingDir)?;
utils::current_exe()?;

match process().name().as_deref() {
Some("rustup") => rustup_mode::main(current_dir).await,
match process.name().as_deref() {
Some("rustup") => rustup_mode::main(current_dir, process).await,
Some(n) if n.starts_with("rustup-setup") || n.starts_with("rustup-init") => {
// NB: The above check is only for the prefix of the file
// name. Browsers rename duplicates to
// e.g. rustup-setup(2), and this allows all variations
// to work.
setup_mode::main(current_dir).await
setup_mode::main(current_dir, process).await
}
Some(n) if n.starts_with("rustup-gc-") => {
// This is the final uninstallation stage on windows where
// rustup deletes its own exe
cfg_if! {
if #[cfg(windows)] {
self_update::complete_windows_uninstall()
self_update::complete_windows_uninstall(process)
} else {
unreachable!("Attempted to use Windows-specific code on a non-Windows platform. Aborting.")
}
}
}
Some(n) => {
is_proxyable_tools(n)?;
proxy_mode::main(n, current_dir).await.map(ExitCode::from)
proxy_mode::main(n, current_dir, process)
.await
.map(ExitCode::from)
}
None => {
// Weird case. No arg0, or it's unparsable.
Expand All @@ -123,8 +125,8 @@ async fn run_rustup_inner() -> Result<utils::ExitCode> {
}
}

fn do_recursion_guard() -> Result<()> {
let recursion_count = process()
fn do_recursion_guard(process: &Process) -> Result<()> {
let recursion_count = process
.var("RUST_RECURSION_COUNT")
.ok()
.and_then(|s| s.parse().ok())
Expand Down
Loading
Loading