cargo/core/compiler/job_queue/
mod.rs

1//! Management of the interaction between the main `cargo` and all spawned jobs.
2//!
3//! ## Overview
4//!
5//! This module implements a job queue. A job here represents a unit of work,
6//! which is roughly a rustc invocation, a build script run, or just a no-op.
7//! The job queue primarily handles the following things:
8//!
9//! * Spawns concurrent jobs. Depending on its [`Freshness`], a job could be
10//!     either executed on a spawned thread or ran on the same thread to avoid
11//!     the threading overhead.
12//! * Controls the number of concurrency. It allocates and manages [`jobserver`]
13//!     tokens to each spawned off rustc and build scripts.
14//! * Manages the communication between the main `cargo` process and its
15//!     spawned jobs. Those [`Message`]s are sent over a [`Queue`] shared
16//!     across threads.
17//! * Schedules the execution order of each [`Job`]. Priorities are determined
18//!     when calling [`JobQueue::enqueue`] to enqueue a job. The scheduling is
19//!     relatively rudimentary and could likely be improved.
20//!
21//! A rough outline of building a queue and executing jobs is:
22//!
23//! 1. [`JobQueue::new`] to simply create one queue.
24//! 2. [`JobQueue::enqueue`] to add new jobs onto the queue.
25//! 3. Consumes the queue and executes all jobs via [`JobQueue::execute`].
26//!
27//! The primary loop happens insides [`JobQueue::execute`], which is effectively
28//! [`DrainState::drain_the_queue`]. [`DrainState`] is, as its name tells,
29//! the running state of the job queue getting drained.
30//!
31//! ## Jobserver
32//!
33//! As of Feb. 2023, Cargo and rustc have a relatively simple jobserver
34//! relationship with each other. They share a single jobserver amongst what
35//! is potentially hundreds of threads of work on many-cored systems.
36//! The jobserver could come from either the environment (e.g., from a `make`
37//! invocation), or from Cargo creating its own jobserver server if there is no
38//! jobserver to inherit from.
39//!
40//! Cargo wants to complete the build as quickly as possible, fully saturating
41//! all cores (as constrained by the `-j=N`) parameter. Cargo also must not spawn
42//! more than N threads of work: the total amount of tokens we have floating
43//! around must always be limited to N.
44//!
45//! It is not really possible to optimally choose which crate should build
46//! first or last; nor is it possible to decide whether to give an additional
47//! token to rustc first or rather spawn a new crate of work. The algorithm in
48//! Cargo prioritizes spawning as many crates (i.e., rustc processes) as
49//! possible. In short, the jobserver relationship among Cargo and rustc
50//! processes is **1 `cargo` to N `rustc`**. Cargo knows nothing beyond rustc
51//! processes in terms of parallelism[^parallel-rustc].
52//!
53//! We integrate with the [jobserver] crate, originating from GNU make
54//! [POSIX jobserver], to make sure that build scripts which use make to
55//! build C code can cooperate with us on the number of used tokens and
56//! avoid overfilling the system we're on.
57//!
58//! ## Scheduling
59//!
60//! The current scheduling algorithm is not really polished. It is simply based
61//! on a dependency graph [`DependencyQueue`]. We continue adding nodes onto
62//! the graph until we finalize it. When the graph gets finalized, it finds the
63//! sum of the cost of each dependencies of each node, including transitively.
64//! The sum of dependency cost turns out to be the cost of each given node.
65//!
66//! At the time being, the cost is just passed as a fixed placeholder in
67//! [`JobQueue::enqueue`]. In the future, we could explore more possibilities
68//! around it. For instance, we start persisting timing information for each
69//! build somewhere. For a subsequent build, we can look into the historical
70//! data and perform a PGO-like optimization to prioritize jobs, making a build
71//! fully pipelined.
72//!
73//! ## Message queue
74//!
75//! Each spawned thread running a process uses the message queue [`Queue`] to
76//! send messages back to the main thread (the one running `cargo`).
77//! The main thread coordinates everything, and handles printing output.
78//!
79//! It is important to be careful which messages use [`push`] vs [`push_bounded`].
80//! `push` is for priority messages (like tokens, or "finished") where the
81//! sender shouldn't block. We want to handle those so real work can proceed
82//! ASAP.
83//!
84//! `push_bounded` is only for messages being printed to stdout/stderr. Being
85//! bounded prevents a flood of messages causing a large amount of memory
86//! being used.
87//!
88//! `push` also avoids blocking which helps avoid deadlocks. For example, when
89//! the diagnostic server thread is dropped, it waits for the thread to exit.
90//! But if the thread is blocked on a full queue, and there is a critical
91//! error, the drop will deadlock. This should be fixed at some point in the
92//! future. The jobserver thread has a similar problem, though it will time
93//! out after 1 second.
94//!
95//! To access the message queue, each running `Job` is given its own [`JobState`],
96//! containing everything it needs to communicate with the main thread.
97//!
98//! See [`Message`] for all available message kinds.
99//!
100//! [^parallel-rustc]: In fact, `jobserver` that Cargo uses also manages the
101//!     allocation of tokens to rustc beyond the implicit token each rustc owns
102//!     (i.e., the ones used for parallel LLVM work and parallel rustc threads).
103//!     See also ["Rust Compiler Development Guide: Parallel Compilation"]
104//!     and [this comment][rustc-codegen] in rust-lang/rust.
105//!
106//! ["Rust Compiler Development Guide: Parallel Compilation"]: https://rustc-dev-guide.rust-lang.org/parallel-rustc.html
107//! [rustc-codegen]: https://github.com/rust-lang/rust/blob/5423745db8b434fcde54888b35f518f00cce00e4/compiler/rustc_codegen_ssa/src/back/write.rs#L1204-L1217
108//! [jobserver]: https://docs.rs/jobserver
109//! [POSIX jobserver]: https://www.gnu.org/software/make/manual/html_node/POSIX-Jobserver.html
110//! [`push`]: Queue::push
111//! [`push_bounded`]: Queue::push_bounded
112
113mod job;
114mod job_state;
115
116use std::cell::RefCell;
117use std::collections::{HashMap, HashSet};
118use std::fmt::Write as _;
119use std::io;
120use std::path::{Path, PathBuf};
121use std::sync::Arc;
122use std::thread::{self, Scope};
123use std::time::Duration;
124
125use anyhow::{Context as _, format_err};
126use jobserver::{Acquired, HelperThread};
127use semver::Version;
128use tracing::{debug, trace};
129
130pub use self::job::Freshness::{self, Dirty, Fresh};
131pub use self::job::{Job, Work};
132pub use self::job_state::JobState;
133use super::custom_build::Severity;
134use super::timings::{SectionTiming, Timings};
135use super::{BuildContext, BuildRunner, CompileMode, Unit};
136use crate::core::compiler::descriptive_pkg_name;
137use crate::core::compiler::future_incompat::{
138    self, FutureBreakageItem, FutureIncompatReportPackage,
139};
140use crate::core::resolver::ResolveBehavior;
141use crate::core::{PackageId, Shell, TargetKind};
142use crate::util::CargoResult;
143use crate::util::context::WarningHandling;
144use crate::util::diagnostic_server::{self, DiagnosticPrinter};
145use crate::util::errors::AlreadyPrintedError;
146use crate::util::machine_message::{self, Message as _};
147use crate::util::{self, internal};
148use crate::util::{DependencyQueue, GlobalContext, Progress, ProgressStyle, Queue};
149
150/// This structure is backed by the `DependencyQueue` type and manages the
151/// queueing of compilation steps for each package. Packages enqueue units of
152/// work and then later on the entire graph is converted to `DrainState` and
153/// executed.
154pub struct JobQueue<'gctx> {
155    queue: DependencyQueue<Unit, Artifact, Job>,
156    counts: HashMap<PackageId, usize>,
157    timings: Timings<'gctx>,
158}
159
160/// This structure is backed by the `DependencyQueue` type and manages the
161/// actual compilation step of each package. Packages enqueue units of work and
162/// then later on the entire graph is processed and compiled.
163///
164/// It is created from `JobQueue` when we have fully assembled the crate graph
165/// (i.e., all package dependencies are known).
166struct DrainState<'gctx> {
167    // This is the length of the DependencyQueue when starting out
168    total_units: usize,
169
170    queue: DependencyQueue<Unit, Artifact, Job>,
171    messages: Arc<Queue<Message>>,
172    /// Diagnostic deduplication support.
173    diag_dedupe: DiagDedupe<'gctx>,
174    /// Count of warnings, used to print a summary after the job succeeds
175    warning_count: HashMap<JobId, WarningCount>,
176    active: HashMap<JobId, Unit>,
177    compiled: HashSet<PackageId>,
178    documented: HashSet<PackageId>,
179    scraped: HashSet<PackageId>,
180    counts: HashMap<PackageId, usize>,
181    progress: Progress<'gctx>,
182    next_id: u32,
183    timings: Timings<'gctx>,
184
185    /// Tokens that are currently owned by this Cargo, and may be "associated"
186    /// with a rustc process. They may also be unused, though if so will be
187    /// dropped on the next loop iteration.
188    ///
189    /// Note that the length of this may be zero, but we will still spawn work,
190    /// as we share the implicit token given to this Cargo process with a
191    /// single rustc process.
192    tokens: Vec<Acquired>,
193
194    /// The list of jobs that we have not yet started executing, but have
195    /// retrieved from the `queue`. We eagerly pull jobs off the main queue to
196    /// allow us to request jobserver tokens pretty early.
197    pending_queue: Vec<(Unit, Job, usize)>,
198    print: DiagnosticPrinter<'gctx>,
199
200    /// How many jobs we've finished
201    finished: usize,
202    per_package_future_incompat_reports: Vec<FutureIncompatReportPackage>,
203}
204
205/// Count of warnings, used to print a summary after the job succeeds
206#[derive(Default)]
207pub struct WarningCount {
208    /// total number of warnings
209    pub total: usize,
210    /// number of lint warnings
211    pub lints: usize,
212    /// number of warnings that were suppressed because they
213    /// were duplicates of a previous warning
214    pub duplicates: usize,
215    /// number of fixable warnings set to `NotAllowed`
216    /// if any errors have been seen for the current
217    /// target
218    pub fixable: FixableWarnings,
219}
220
221impl WarningCount {
222    /// If an error is seen this should be called
223    /// to set `fixable` to `NotAllowed`
224    fn disallow_fixable(&mut self) {
225        self.fixable = FixableWarnings::NotAllowed;
226    }
227
228    /// Checks fixable if warnings are allowed
229    /// fixable warnings are allowed if no
230    /// errors have been seen for the current
231    /// target. If an error was seen `fixable`
232    /// will be `NotAllowed`.
233    fn fixable_allowed(&self) -> bool {
234        match &self.fixable {
235            FixableWarnings::NotAllowed => false,
236            _ => true,
237        }
238    }
239}
240
241/// Used to keep track of how many fixable warnings there are
242/// and if fixable warnings are allowed
243#[derive(Default)]
244pub enum FixableWarnings {
245    NotAllowed,
246    #[default]
247    Zero,
248    Positive(usize),
249}
250
251pub struct ErrorsDuringDrain {
252    pub count: usize,
253}
254
255struct ErrorToHandle {
256    error: anyhow::Error,
257
258    /// This field is true for "interesting" errors and false for "mundane"
259    /// errors. If false, we print the above error only if it's the first one
260    /// encountered so far while draining the job queue.
261    ///
262    /// At most places that an error is propagated, we set this to false to
263    /// avoid scenarios where Cargo might end up spewing tons of redundant error
264    /// messages. For example if an i/o stream got closed somewhere, we don't
265    /// care about individually reporting every thread that it broke; just the
266    /// first is enough.
267    ///
268    /// The exception where `print_always` is true is that we do report every
269    /// instance of a rustc invocation that failed with diagnostics. This
270    /// corresponds to errors from `Message::Finish`.
271    print_always: bool,
272}
273
274impl<E> From<E> for ErrorToHandle
275where
276    anyhow::Error: From<E>,
277{
278    fn from(error: E) -> Self {
279        ErrorToHandle {
280            error: anyhow::Error::from(error),
281            print_always: false,
282        }
283    }
284}
285
286#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
287pub struct JobId(pub u32);
288
289impl std::fmt::Display for JobId {
290    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
291        write!(f, "{}", self.0)
292    }
293}
294
295/// Handler for deduplicating diagnostics.
296struct DiagDedupe<'gctx> {
297    seen: RefCell<HashSet<u64>>,
298    gctx: &'gctx GlobalContext,
299}
300
301impl<'gctx> DiagDedupe<'gctx> {
302    fn new(gctx: &'gctx GlobalContext) -> Self {
303        DiagDedupe {
304            seen: RefCell::new(HashSet::new()),
305            gctx,
306        }
307    }
308
309    /// Emits a diagnostic message.
310    ///
311    /// Returns `true` if the message was emitted, or `false` if it was
312    /// suppressed for being a duplicate.
313    fn emit_diag(&self, diag: &str) -> CargoResult<bool> {
314        let h = util::hash_u64(diag);
315        if !self.seen.borrow_mut().insert(h) {
316            return Ok(false);
317        }
318        let mut shell = self.gctx.shell();
319        shell.print_ansi_stderr(diag.as_bytes())?;
320        shell.err().write_all(b"\n")?;
321        Ok(true)
322    }
323}
324
325/// Possible artifacts that can be produced by compilations, used as edge values
326/// in the dependency graph.
327///
328/// As edge values we can have multiple kinds of edges depending on one node,
329/// for example some units may only depend on the metadata for an rlib while
330/// others depend on the full rlib. This `Artifact` enum is used to distinguish
331/// this case and track the progress of compilations as they proceed.
332#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
333enum Artifact {
334    /// A generic placeholder for "depends on everything run by a step" and
335    /// means that we can't start the next compilation until the previous has
336    /// finished entirely.
337    All,
338
339    /// A node indicating that we only depend on the metadata of a compilation,
340    /// but the compilation is typically also producing an rlib. We can start
341    /// our step, however, before the full rlib is available.
342    Metadata,
343}
344
345enum Message {
346    Run(JobId, String),
347    Stdout(String),
348    Stderr(String),
349
350    // This is for general stderr output from subprocesses
351    Diagnostic {
352        id: JobId,
353        level: String,
354        diag: String,
355        lint: bool,
356        fixable: bool,
357    },
358    // This handles duplicate output that is suppressed, for showing
359    // only a count of duplicate messages instead
360    WarningCount {
361        id: JobId,
362        lint: bool,
363        emitted: bool,
364        fixable: bool,
365    },
366    // This is for warnings generated by Cargo's interpretation of the
367    // subprocess output, e.g. scrape-examples prints a warning if a
368    // unit fails to be scraped
369    Warning {
370        id: JobId,
371        warning: String,
372    },
373
374    FixDiagnostic(diagnostic_server::Message),
375    Token(io::Result<Acquired>),
376    Finish(JobId, Artifact, CargoResult<()>),
377    FutureIncompatReport(JobId, Vec<FutureBreakageItem>),
378    SectionTiming(JobId, SectionTiming),
379}
380
381impl<'gctx> JobQueue<'gctx> {
382    pub fn new(bcx: &BuildContext<'_, 'gctx>) -> JobQueue<'gctx> {
383        JobQueue {
384            queue: DependencyQueue::new(),
385            counts: HashMap::new(),
386            timings: Timings::new(bcx, &bcx.roots),
387        }
388    }
389
390    pub fn enqueue(
391        &mut self,
392        build_runner: &BuildRunner<'_, 'gctx>,
393        unit: &Unit,
394        job: Job,
395    ) -> CargoResult<()> {
396        let dependencies = build_runner.unit_deps(unit);
397        let mut queue_deps = dependencies
398            .iter()
399            .filter(|dep| {
400                // Binaries aren't actually needed to *compile* tests, just to run
401                // them, so we don't include this dependency edge in the job graph.
402                // But we shouldn't filter out dependencies being scraped for Rustdoc.
403                (!dep.unit.target.is_test() && !dep.unit.target.is_bin())
404                    || dep.unit.artifact.is_true()
405                    || dep.unit.mode.is_doc_scrape()
406            })
407            .map(|dep| {
408                // Handle the case here where our `unit -> dep` dependency may
409                // only require the metadata, not the full compilation to
410                // finish. Use the tables in `build_runner` to figure out what
411                // kind of artifact is associated with this dependency.
412                let artifact = if build_runner.only_requires_rmeta(unit, &dep.unit) {
413                    Artifact::Metadata
414                } else {
415                    Artifact::All
416                };
417                (dep.unit.clone(), artifact)
418            })
419            .collect::<HashMap<_, _>>();
420
421        // This is somewhat tricky, but we may need to synthesize some
422        // dependencies for this target if it requires full upstream
423        // compilations to have completed. Because of pipelining, some
424        // dependency edges may be `Metadata` due to the above clause (as
425        // opposed to everything being `All`). For example consider:
426        //
427        //    a (binary)
428        //    └ b (lib)
429        //        └ c (lib)
430        //
431        // Here the dependency edge from B to C will be `Metadata`, and the
432        // dependency edge from A to B will be `All`. For A to be compiled,
433        // however, it currently actually needs the full rlib of C. This means
434        // that we need to synthesize a dependency edge for the dependency graph
435        // from A to C. That's done here.
436        //
437        // This will walk all dependencies of the current target, and if any of
438        // *their* dependencies are `Metadata` then we depend on the `All` of
439        // the target as well. This should ensure that edges changed to
440        // `Metadata` propagate upwards `All` dependencies to anything that
441        // transitively contains the `Metadata` edge.
442        if unit.requires_upstream_objects() {
443            for dep in dependencies {
444                depend_on_deps_of_deps(build_runner, &mut queue_deps, dep.unit.clone());
445            }
446
447            fn depend_on_deps_of_deps(
448                build_runner: &BuildRunner<'_, '_>,
449                deps: &mut HashMap<Unit, Artifact>,
450                unit: Unit,
451            ) {
452                for dep in build_runner.unit_deps(&unit) {
453                    if deps.insert(dep.unit.clone(), Artifact::All).is_none() {
454                        depend_on_deps_of_deps(build_runner, deps, dep.unit.clone());
455                    }
456                }
457            }
458        }
459
460        // For now we use a fixed placeholder value for the cost of each unit, but
461        // in the future this could be used to allow users to provide hints about
462        // relative expected costs of units, or this could be automatically set in
463        // a smarter way using timing data from a previous compilation.
464        self.queue.queue(unit.clone(), job, queue_deps, 100);
465        *self.counts.entry(unit.pkg.package_id()).or_insert(0) += 1;
466        Ok(())
467    }
468
469    /// Executes all jobs necessary to build the dependency graph.
470    ///
471    /// This function will spawn off `config.jobs()` workers to build all of the
472    /// necessary dependencies, in order. Freshness is propagated as far as
473    /// possible along each dependency chain.
474    #[tracing::instrument(skip_all)]
475    pub fn execute(mut self, build_runner: &mut BuildRunner<'_, '_>) -> CargoResult<()> {
476        self.queue.queue_finished();
477
478        let progress =
479            Progress::with_style("Building", ProgressStyle::Ratio, build_runner.bcx.gctx);
480        let state = DrainState {
481            total_units: self.queue.len(),
482            queue: self.queue,
483            // 100 here is somewhat arbitrary. It is a few screenfulls of
484            // output, and hopefully at most a few megabytes of memory for
485            // typical messages. If you change this, please update the test
486            // caching_large_output, too.
487            messages: Arc::new(Queue::new(100)),
488            diag_dedupe: DiagDedupe::new(build_runner.bcx.gctx),
489            warning_count: HashMap::new(),
490            active: HashMap::new(),
491            compiled: HashSet::new(),
492            documented: HashSet::new(),
493            scraped: HashSet::new(),
494            counts: self.counts,
495            progress,
496            next_id: 0,
497            timings: self.timings,
498            tokens: Vec::new(),
499            pending_queue: Vec::new(),
500            print: DiagnosticPrinter::new(
501                build_runner.bcx.gctx,
502                &build_runner.bcx.rustc().workspace_wrapper,
503            ),
504            finished: 0,
505            per_package_future_incompat_reports: Vec::new(),
506        };
507
508        // Create a helper thread for acquiring jobserver tokens
509        let messages = state.messages.clone();
510        let helper = build_runner
511            .jobserver
512            .clone()
513            .into_helper_thread(move |token| {
514                messages.push(Message::Token(token));
515            })
516            .context("failed to create helper thread for jobserver management")?;
517
518        // Create a helper thread to manage the diagnostics for rustfix if
519        // necessary.
520        let messages = state.messages.clone();
521        // It is important that this uses `push` instead of `push_bounded` for
522        // now. If someone wants to fix this to be bounded, the `drop`
523        // implementation needs to be changed to avoid possible deadlocks.
524        let _diagnostic_server = build_runner
525            .bcx
526            .build_config
527            .rustfix_diagnostic_server
528            .borrow_mut()
529            .take()
530            .map(move |srv| srv.start(move |msg| messages.push(Message::FixDiagnostic(msg))));
531
532        thread::scope(
533            move |scope| match state.drain_the_queue(build_runner, scope, &helper) {
534                Some(err) => Err(err),
535                None => Ok(()),
536            },
537        )
538    }
539}
540
541impl<'gctx> DrainState<'gctx> {
542    fn spawn_work_if_possible<'s>(
543        &mut self,
544        build_runner: &mut BuildRunner<'_, '_>,
545        jobserver_helper: &HelperThread,
546        scope: &'s Scope<'s, '_>,
547    ) -> CargoResult<()> {
548        // Dequeue as much work as we can, learning about everything
549        // possible that can run. Note that this is also the point where we
550        // start requesting job tokens. Each job after the first needs to
551        // request a token.
552        while let Some((unit, job, priority)) = self.queue.dequeue() {
553            // We want to keep the pieces of work in the `pending_queue` sorted
554            // by their priorities, and insert the current job at its correctly
555            // sorted position: following the lower priority jobs, and the ones
556            // with the same priority (since they were dequeued before the
557            // current one, we also keep that relation).
558            let idx = self
559                .pending_queue
560                .partition_point(|&(_, _, p)| p <= priority);
561            self.pending_queue.insert(idx, (unit, job, priority));
562            if self.active.len() + self.pending_queue.len() > 1 {
563                jobserver_helper.request_token();
564            }
565        }
566
567        // Now that we've learned of all possible work that we can execute
568        // try to spawn it so long as we've got a jobserver token which says
569        // we're able to perform some parallel work.
570        // The `pending_queue` is sorted in ascending priority order, and we
571        // remove items from its end to schedule the highest priority items
572        // sooner.
573        while self.has_extra_tokens() && !self.pending_queue.is_empty() {
574            let (unit, job, _) = self.pending_queue.pop().unwrap();
575            *self.counts.get_mut(&unit.pkg.package_id()).unwrap() -= 1;
576            // Print out some nice progress information.
577            // NOTE: An error here will drop the job without starting it.
578            // That should be OK, since we want to exit as soon as
579            // possible during an error.
580            self.note_working_on(
581                build_runner.bcx.gctx,
582                build_runner.bcx.ws.root(),
583                &unit,
584                job.freshness(),
585            )?;
586            self.run(&unit, job, build_runner, scope);
587        }
588
589        Ok(())
590    }
591
592    fn has_extra_tokens(&self) -> bool {
593        self.active.len() < self.tokens.len() + 1
594    }
595
596    fn handle_event(
597        &mut self,
598        build_runner: &mut BuildRunner<'_, '_>,
599        event: Message,
600    ) -> Result<(), ErrorToHandle> {
601        let warning_handling = build_runner.bcx.gctx.warning_handling()?;
602        match event {
603            Message::Run(id, cmd) => {
604                build_runner
605                    .bcx
606                    .gctx
607                    .shell()
608                    .verbose(|c| c.status("Running", &cmd))?;
609                self.timings
610                    .unit_start(build_runner, id, self.active[&id].clone());
611            }
612            Message::Stdout(out) => {
613                writeln!(build_runner.bcx.gctx.shell().out(), "{}", out)?;
614            }
615            Message::Stderr(err) => {
616                let mut shell = build_runner.bcx.gctx.shell();
617                shell.print_ansi_stderr(err.as_bytes())?;
618                shell.err().write_all(b"\n")?;
619            }
620            Message::Diagnostic {
621                id,
622                level,
623                diag,
624                lint,
625                fixable,
626            } => {
627                let emitted = self.diag_dedupe.emit_diag(&diag)?;
628                if level == "warning" {
629                    self.bump_warning_count(id, lint, emitted, fixable);
630                }
631                if level == "error" {
632                    let cnts = self.warning_count.entry(id).or_default();
633                    // If there is an error, the `cargo fix` message should not show
634                    cnts.disallow_fixable();
635                }
636            }
637            Message::Warning { id, warning } => {
638                if warning_handling != WarningHandling::Allow {
639                    build_runner.bcx.gctx.shell().warn(warning)?;
640                }
641                let lint = false;
642                let emitted = true;
643                let fixable = false;
644                self.bump_warning_count(id, lint, emitted, fixable);
645            }
646            Message::WarningCount {
647                id,
648                lint,
649                emitted,
650                fixable,
651            } => {
652                self.bump_warning_count(id, lint, emitted, fixable);
653            }
654            Message::FixDiagnostic(msg) => {
655                self.print.print(&msg)?;
656            }
657            Message::Finish(id, artifact, result) => {
658                let unit = match artifact {
659                    // If `id` has completely finished we remove it
660                    // from the `active` map ...
661                    Artifact::All => {
662                        trace!("end: {:?}", id);
663                        self.finished += 1;
664                        self.report_warning_count(
665                            build_runner,
666                            id,
667                            &build_runner.bcx.rustc().workspace_wrapper,
668                        );
669                        self.active.remove(&id).unwrap()
670                    }
671                    // ... otherwise if it hasn't finished we leave it
672                    // in there as we'll get another `Finish` later on.
673                    Artifact::Metadata => {
674                        trace!("end (meta): {:?}", id);
675                        self.active[&id].clone()
676                    }
677                };
678                debug!("end ({:?}): {:?}", unit, result);
679                match result {
680                    Ok(()) => self.finish(id, &unit, artifact, build_runner)?,
681                    Err(_) if build_runner.bcx.unit_can_fail_for_docscraping(&unit) => {
682                        build_runner
683                            .failed_scrape_units
684                            .lock()
685                            .unwrap()
686                            .insert(build_runner.files().metadata(&unit).unit_id());
687                        self.queue.finish(&unit, &artifact);
688                    }
689                    Err(error) => {
690                        let show_warnings = true;
691                        self.emit_log_messages(&unit, build_runner, show_warnings)?;
692                        self.back_compat_notice(build_runner, &unit)?;
693                        return Err(ErrorToHandle {
694                            error,
695                            print_always: true,
696                        });
697                    }
698                }
699            }
700            Message::FutureIncompatReport(id, items) => {
701                let unit = &self.active[&id];
702                let package_id = unit.pkg.package_id();
703                let is_local = unit.is_local();
704                self.per_package_future_incompat_reports
705                    .push(FutureIncompatReportPackage {
706                        package_id,
707                        is_local,
708                        items,
709                    });
710            }
711            Message::Token(acquired_token) => {
712                let token = acquired_token.context("failed to acquire jobserver token")?;
713                self.tokens.push(token);
714            }
715            Message::SectionTiming(id, section) => {
716                self.timings.unit_section_timing(build_runner, id, &section);
717            }
718        }
719
720        Ok(())
721    }
722
723    // This will also tick the progress bar as appropriate
724    fn wait_for_events(&mut self) -> Vec<Message> {
725        // Drain all events at once to avoid displaying the progress bar
726        // unnecessarily. If there's no events we actually block waiting for
727        // an event, but we keep a "heartbeat" going to allow `record_cpu`
728        // to run above to calculate CPU usage over time. To do this we
729        // listen for a message with a timeout, and on timeout we run the
730        // previous parts of the loop again.
731        let mut events = self.messages.try_pop_all();
732        if events.is_empty() {
733            loop {
734                self.tick_progress();
735                self.tokens.truncate(self.active.len() - 1);
736                match self.messages.pop(Duration::from_millis(500)) {
737                    Some(message) => {
738                        events.push(message);
739                        break;
740                    }
741                    None => continue,
742                }
743            }
744        }
745        events
746    }
747
748    /// This is the "main" loop, where Cargo does all work to run the
749    /// compiler.
750    ///
751    /// This returns an Option to prevent the use of `?` on `Result` types
752    /// because it is important for the loop to carefully handle errors.
753    fn drain_the_queue<'s>(
754        mut self,
755        build_runner: &mut BuildRunner<'_, '_>,
756        scope: &'s Scope<'s, '_>,
757        jobserver_helper: &HelperThread,
758    ) -> Option<anyhow::Error> {
759        trace!("queue: {:#?}", self.queue);
760
761        // Iteratively execute the entire dependency graph. Each turn of the
762        // loop starts out by scheduling as much work as possible (up to the
763        // maximum number of parallel jobs we have tokens for). A local queue
764        // is maintained separately from the main dependency queue as one
765        // dequeue may actually dequeue quite a bit of work (e.g., 10 binaries
766        // in one package).
767        //
768        // After a job has finished we update our internal state if it was
769        // successful and otherwise wait for pending work to finish if it failed
770        // and then immediately return (or keep going, if requested by the build
771        // config).
772        let mut errors = ErrorsDuringDrain { count: 0 };
773        // CAUTION! Do not use `?` or break out of the loop early. Every error
774        // must be handled in such a way that the loop is still allowed to
775        // drain event messages.
776        loop {
777            if errors.count == 0 || build_runner.bcx.build_config.keep_going {
778                if let Err(e) = self.spawn_work_if_possible(build_runner, jobserver_helper, scope) {
779                    self.handle_error(&mut build_runner.bcx.gctx.shell(), &mut errors, e);
780                }
781            }
782
783            // If after all that we're not actually running anything then we're
784            // done!
785            if self.active.is_empty() {
786                break;
787            }
788
789            // And finally, before we block waiting for the next event, drop any
790            // excess tokens we may have accidentally acquired. Due to how our
791            // jobserver interface is architected we may acquire a token that we
792            // don't actually use, and if this happens just relinquish it back
793            // to the jobserver itself.
794            for event in self.wait_for_events() {
795                if let Err(event_err) = self.handle_event(build_runner, event) {
796                    self.handle_error(&mut build_runner.bcx.gctx.shell(), &mut errors, event_err);
797                }
798            }
799        }
800        self.progress.clear();
801
802        let profile_name = build_runner.bcx.build_config.requested_profile;
803        // NOTE: this may be a bit inaccurate, since this may not display the
804        // profile for what was actually built. Profile overrides can change
805        // these settings, and in some cases different targets are built with
806        // different profiles. To be accurate, it would need to collect a
807        // list of Units built, and maybe display a list of the different
808        // profiles used. However, to keep it simple and compatible with old
809        // behavior, we just display what the base profile is.
810        let profile = build_runner.bcx.profiles.base_profile();
811        let mut opt_type = String::from(if profile.opt_level.as_str() == "0" {
812            "unoptimized"
813        } else {
814            "optimized"
815        });
816        if profile.debuginfo.is_turned_on() {
817            opt_type += " + debuginfo";
818        }
819
820        let time_elapsed = util::elapsed(build_runner.bcx.gctx.creation_time().elapsed());
821        if let Err(e) = self
822            .timings
823            .finished(build_runner, &errors.to_error())
824            .context("failed to render timing report")
825        {
826            self.handle_error(&mut build_runner.bcx.gctx.shell(), &mut errors, e);
827        }
828        if build_runner.bcx.build_config.emit_json() {
829            let mut shell = build_runner.bcx.gctx.shell();
830            let msg = machine_message::BuildFinished {
831                success: errors.count == 0,
832            }
833            .to_json_string();
834            if let Err(e) = writeln!(shell.out(), "{}", msg) {
835                self.handle_error(&mut shell, &mut errors, e);
836            }
837        }
838
839        if let Some(error) = errors.to_error() {
840            // Any errors up to this point have already been printed via the
841            // `display_error` inside `handle_error`.
842            Some(anyhow::Error::new(AlreadyPrintedError::new(error)))
843        } else if self.queue.is_empty() && self.pending_queue.is_empty() {
844            let profile_link = build_runner.bcx.gctx.shell().err_hyperlink(
845                "https://doc.rust-lang.org/cargo/reference/profiles.html#default-profiles",
846            );
847            let message = format!(
848                "{profile_link}`{profile_name}` profile [{opt_type}]{profile_link:#} target(s) in {time_elapsed}",
849            );
850            // It doesn't really matter if this fails.
851            let _ = build_runner.bcx.gctx.shell().status("Finished", message);
852            future_incompat::save_and_display_report(
853                build_runner.bcx,
854                &self.per_package_future_incompat_reports,
855            );
856
857            None
858        } else {
859            debug!("queue: {:#?}", self.queue);
860            Some(internal("finished with jobs still left in the queue"))
861        }
862    }
863
864    fn handle_error(
865        &mut self,
866        shell: &mut Shell,
867        err_state: &mut ErrorsDuringDrain,
868        new_err: impl Into<ErrorToHandle>,
869    ) {
870        let new_err = new_err.into();
871        if new_err.print_always || err_state.count == 0 {
872            crate::display_error(&new_err.error, shell);
873            if err_state.count == 0 && !self.active.is_empty() {
874                self.progress.indicate_error();
875                let _ = shell.warn("build failed, waiting for other jobs to finish...");
876            }
877            err_state.count += 1;
878        } else {
879            tracing::warn!("{:?}", new_err.error);
880        }
881    }
882
883    // This also records CPU usage and marks concurrency; we roughly want to do
884    // this as often as we spin on the events receiver (at least every 500ms or
885    // so).
886    fn tick_progress(&mut self) {
887        // Record some timing information if `--timings` is enabled, and
888        // this'll end up being a noop if we're not recording this
889        // information.
890        self.timings.mark_concurrency(
891            self.active.len(),
892            self.pending_queue.len(),
893            self.queue.len(),
894        );
895        self.timings.record_cpu();
896
897        let active_names = self
898            .active
899            .values()
900            .map(|u| self.name_for_progress(u))
901            .collect::<Vec<_>>();
902        let _ = self.progress.tick_now(
903            self.finished,
904            self.total_units,
905            &format!(": {}", active_names.join(", ")),
906        );
907    }
908
909    fn name_for_progress(&self, unit: &Unit) -> String {
910        let pkg_name = unit.pkg.name();
911        let target_name = unit.target.name();
912        match unit.mode {
913            CompileMode::Doc { .. } => format!("{}(doc)", pkg_name),
914            CompileMode::RunCustomBuild => format!("{}(build)", pkg_name),
915            CompileMode::Test | CompileMode::Check { test: true } => match unit.target.kind() {
916                TargetKind::Lib(_) => format!("{}(test)", target_name),
917                TargetKind::CustomBuild => panic!("cannot test build script"),
918                TargetKind::Bin => format!("{}(bin test)", target_name),
919                TargetKind::Test => format!("{}(test)", target_name),
920                TargetKind::Bench => format!("{}(bench)", target_name),
921                TargetKind::ExampleBin | TargetKind::ExampleLib(_) => {
922                    format!("{}(example test)", target_name)
923                }
924            },
925            _ => match unit.target.kind() {
926                TargetKind::Lib(_) => pkg_name.to_string(),
927                TargetKind::CustomBuild => format!("{}(build.rs)", pkg_name),
928                TargetKind::Bin => format!("{}(bin)", target_name),
929                TargetKind::Test => format!("{}(test)", target_name),
930                TargetKind::Bench => format!("{}(bench)", target_name),
931                TargetKind::ExampleBin | TargetKind::ExampleLib(_) => {
932                    format!("{}(example)", target_name)
933                }
934            },
935        }
936    }
937
938    /// Executes a job.
939    ///
940    /// Fresh jobs block until finished (which should be very fast!), Dirty
941    /// jobs will spawn a thread in the background and return immediately.
942    fn run<'s>(
943        &mut self,
944        unit: &Unit,
945        job: Job,
946        build_runner: &BuildRunner<'_, '_>,
947        scope: &'s Scope<'s, '_>,
948    ) {
949        let id = JobId(self.next_id);
950        self.next_id = self.next_id.checked_add(1).unwrap();
951
952        debug!("start {}: {:?}", id, unit);
953
954        assert!(self.active.insert(id, unit.clone()).is_none());
955
956        let messages = self.messages.clone();
957        let is_fresh = job.freshness().is_fresh();
958        let rmeta_required = build_runner.rmeta_required(unit);
959
960        let doit = move |diag_dedupe| {
961            let state = JobState::new(id, messages, diag_dedupe, rmeta_required);
962            state.run_to_finish(job);
963        };
964
965        match is_fresh {
966            true => {
967                self.timings.add_fresh();
968                // Running a fresh job on the same thread is often much faster than spawning a new
969                // thread to run the job.
970                doit(Some(&self.diag_dedupe));
971            }
972            false => {
973                self.timings.add_dirty();
974                scope.spawn(move || doit(None));
975            }
976        }
977    }
978
979    fn emit_log_messages(
980        &self,
981        unit: &Unit,
982        build_runner: &mut BuildRunner<'_, '_>,
983        show_warnings: bool,
984    ) -> CargoResult<()> {
985        let outputs = build_runner.build_script_outputs.lock().unwrap();
986        let Some(metadata_vec) = build_runner.find_build_script_metadatas(unit) else {
987            return Ok(());
988        };
989        let bcx = &mut build_runner.bcx;
990        for metadata in metadata_vec {
991            if let Some(output) = outputs.get(metadata) {
992                if !output.log_messages.is_empty()
993                    && (show_warnings
994                        || output
995                            .log_messages
996                            .iter()
997                            .any(|(severity, _)| *severity == Severity::Error))
998                {
999                    let msg_with_package =
1000                        |msg: &str| format!("{}@{}: {}", unit.pkg.name(), unit.pkg.version(), msg);
1001
1002                    for (severity, message) in output.log_messages.iter() {
1003                        match severity {
1004                            Severity::Error => {
1005                                bcx.gctx.shell().error(msg_with_package(message))?;
1006                            }
1007                            Severity::Warning => {
1008                                bcx.gctx.shell().warn(msg_with_package(message))?;
1009                            }
1010                        }
1011                    }
1012                }
1013            }
1014        }
1015
1016        Ok(())
1017    }
1018
1019    fn bump_warning_count(&mut self, id: JobId, lint: bool, emitted: bool, fixable: bool) {
1020        let cnts = self.warning_count.entry(id).or_default();
1021        cnts.total += 1;
1022        if lint {
1023            cnts.lints += 1;
1024        }
1025        if !emitted {
1026            cnts.duplicates += 1;
1027        // Don't add to fixable if it's already been emitted
1028        } else if fixable {
1029            // Do not add anything to the fixable warning count if
1030            // is `NotAllowed` since that indicates there was an
1031            // error while building this `Unit`
1032            if cnts.fixable_allowed() {
1033                cnts.fixable = match cnts.fixable {
1034                    FixableWarnings::NotAllowed => FixableWarnings::NotAllowed,
1035                    FixableWarnings::Zero => FixableWarnings::Positive(1),
1036                    FixableWarnings::Positive(fixable) => FixableWarnings::Positive(fixable + 1),
1037                };
1038            }
1039        }
1040    }
1041
1042    /// Displays a final report of the warnings emitted by a particular job.
1043    fn report_warning_count(
1044        &mut self,
1045        runner: &mut BuildRunner<'_, '_>,
1046        id: JobId,
1047        rustc_workspace_wrapper: &Option<PathBuf>,
1048    ) {
1049        let gctx = runner.bcx.gctx;
1050        let count = match self.warning_count.get(&id) {
1051            // An error could add an entry for a `Unit`
1052            // with 0 warnings but having fixable
1053            // warnings be disallowed
1054            Some(count) if count.total > 0 => count,
1055            None | Some(_) => return,
1056        };
1057        runner.compilation.lint_warning_count += count.lints;
1058        let unit = &self.active[&id];
1059        let mut message = descriptive_pkg_name(&unit.pkg.name(), &unit.target, &unit.mode);
1060        message.push_str(" generated ");
1061        match count.total {
1062            1 => message.push_str("1 warning"),
1063            n => {
1064                let _ = write!(message, "{} warnings", n);
1065            }
1066        };
1067        match count.duplicates {
1068            0 => {}
1069            1 => message.push_str(" (1 duplicate)"),
1070            n => {
1071                let _ = write!(message, " ({} duplicates)", n);
1072            }
1073        }
1074        // Only show the `cargo fix` message if its a local `Unit`
1075        if unit.is_local() {
1076            // Do not show this if there are any errors or no fixable warnings
1077            if let FixableWarnings::Positive(fixable) = count.fixable {
1078                // `cargo fix` doesn't have an option for custom builds
1079                if !unit.target.is_custom_build() {
1080                    // To make sure the correct command is shown for `clippy` we
1081                    // check if `RUSTC_WORKSPACE_WRAPPER` is set and pointing towards
1082                    // `clippy-driver`.
1083                    let clippy = std::ffi::OsStr::new("clippy-driver");
1084                    let command = match rustc_workspace_wrapper.as_ref().and_then(|x| x.file_stem())
1085                    {
1086                        Some(wrapper) if wrapper == clippy => "cargo clippy --fix",
1087                        _ => "cargo fix",
1088                    };
1089                    let mut args =
1090                        format!("{} -p {}", unit.target.description_named(), unit.pkg.name());
1091                    if unit.mode.is_rustc_test()
1092                        && !(unit.target.is_test() || unit.target.is_bench())
1093                    {
1094                        args.push_str(" --tests");
1095                    }
1096                    let mut suggestions = format!("{} suggestion", fixable);
1097                    if fixable > 1 {
1098                        suggestions.push_str("s")
1099                    }
1100                    let _ = write!(
1101                        message,
1102                        " (run `{command} --{args}` to apply {suggestions})"
1103                    );
1104                }
1105            }
1106        }
1107        // Errors are ignored here because it is tricky to handle them
1108        // correctly, and they aren't important.
1109        let _ = gctx.shell().warn(message);
1110    }
1111
1112    fn finish(
1113        &mut self,
1114        id: JobId,
1115        unit: &Unit,
1116        artifact: Artifact,
1117        build_runner: &mut BuildRunner<'_, '_>,
1118    ) -> CargoResult<()> {
1119        if unit.mode.is_run_custom_build() {
1120            self.emit_log_messages(
1121                unit,
1122                build_runner,
1123                unit.show_warnings(build_runner.bcx.gctx),
1124            )?;
1125        }
1126        let unblocked = self.queue.finish(unit, &artifact);
1127        match artifact {
1128            Artifact::All => self.timings.unit_finished(build_runner, id, unblocked),
1129            Artifact::Metadata => self
1130                .timings
1131                .unit_rmeta_finished(build_runner, id, unblocked),
1132        }
1133        Ok(())
1134    }
1135
1136    // This isn't super trivial because we don't want to print loads and
1137    // loads of information to the console, but we also want to produce a
1138    // faithful representation of what's happening. This is somewhat nuanced
1139    // as a package can start compiling *very* early on because of custom
1140    // build commands and such.
1141    //
1142    // In general, we try to print "Compiling" for the first nontrivial task
1143    // run for a package, regardless of when that is. We then don't print
1144    // out any more information for a package after we've printed it once.
1145    fn note_working_on(
1146        &mut self,
1147        gctx: &GlobalContext,
1148        ws_root: &Path,
1149        unit: &Unit,
1150        fresh: &Freshness,
1151    ) -> CargoResult<()> {
1152        if (self.compiled.contains(&unit.pkg.package_id())
1153            && !unit.mode.is_doc()
1154            && !unit.mode.is_doc_scrape())
1155            || (self.documented.contains(&unit.pkg.package_id()) && unit.mode.is_doc())
1156            || (self.scraped.contains(&unit.pkg.package_id()) && unit.mode.is_doc_scrape())
1157        {
1158            return Ok(());
1159        }
1160
1161        match fresh {
1162            // Any dirty stage which runs at least one command gets printed as
1163            // being a compiled package.
1164            Dirty(dirty_reason) => {
1165                if !dirty_reason.is_fresh_build() {
1166                    gctx.shell()
1167                        .verbose(|shell| dirty_reason.present_to(shell, unit, ws_root))?;
1168                }
1169
1170                if unit.mode.is_doc() {
1171                    self.documented.insert(unit.pkg.package_id());
1172                    gctx.shell().status("Documenting", &unit.pkg)?;
1173                } else if unit.mode.is_doc_test() {
1174                    // Skip doc test.
1175                } else if unit.mode.is_doc_scrape() {
1176                    self.scraped.insert(unit.pkg.package_id());
1177                    gctx.shell().status("Scraping", &unit.pkg)?;
1178                } else {
1179                    self.compiled.insert(unit.pkg.package_id());
1180                    if unit.mode.is_check() {
1181                        gctx.shell().status("Checking", &unit.pkg)?;
1182                    } else {
1183                        gctx.shell().status("Compiling", &unit.pkg)?;
1184                    }
1185                }
1186            }
1187            Fresh => {
1188                // If doc test are last, only print "Fresh" if nothing has been printed.
1189                if self.counts[&unit.pkg.package_id()] == 0
1190                    && !(unit.mode.is_doc_test() && self.compiled.contains(&unit.pkg.package_id()))
1191                {
1192                    self.compiled.insert(unit.pkg.package_id());
1193                    gctx.shell().verbose(|c| c.status("Fresh", &unit.pkg))?;
1194                }
1195            }
1196        }
1197        Ok(())
1198    }
1199
1200    fn back_compat_notice(
1201        &self,
1202        build_runner: &BuildRunner<'_, '_>,
1203        unit: &Unit,
1204    ) -> CargoResult<()> {
1205        if unit.pkg.name() != "diesel"
1206            || unit.pkg.version() >= &Version::new(1, 4, 8)
1207            || build_runner.bcx.ws.resolve_behavior() == ResolveBehavior::V1
1208            || !unit.pkg.package_id().source_id().is_registry()
1209            || !unit.features.is_empty()
1210        {
1211            return Ok(());
1212        }
1213        if !build_runner
1214            .bcx
1215            .unit_graph
1216            .keys()
1217            .any(|unit| unit.pkg.name() == "diesel" && !unit.features.is_empty())
1218        {
1219            return Ok(());
1220        }
1221        build_runner.bcx.gctx.shell().note(
1222            "\
1223This error may be due to an interaction between diesel and Cargo's new
1224feature resolver. Try updating to diesel 1.4.8 to fix this error.
1225",
1226        )?;
1227        Ok(())
1228    }
1229}
1230
1231impl ErrorsDuringDrain {
1232    fn to_error(&self) -> Option<anyhow::Error> {
1233        match self.count {
1234            0 => None,
1235            1 => Some(format_err!("1 job failed")),
1236            n => Some(format_err!("{} jobs failed", n)),
1237        }
1238    }
1239}