cargo/ops/fix/
mod.rs

1//! High-level overview of how `fix` works:
2//!
3//! The main goal is to run `cargo check` to get rustc to emit JSON
4//! diagnostics with suggested fixes that can be applied to the files on the
5//! filesystem, and validate that those changes didn't break anything.
6//!
7//! Cargo begins by launching a [`LockServer`] thread in the background to
8//! listen for network connections to coordinate locking when multiple targets
9//! are built simultaneously. It ensures each package has only one fix running
10//! at once.
11//!
12//! The [`RustfixDiagnosticServer`] is launched in a background thread (in
13//! `JobQueue`) to listen for network connections to coordinate displaying
14//! messages to the user on the console (so that multiple processes don't try
15//! to print at the same time).
16//!
17//! Cargo begins a normal `cargo check` operation with itself set as a proxy
18//! for rustc by setting `BuildConfig::primary_unit_rustc` in the build config. When
19//! cargo launches rustc to check a crate, it is actually launching itself.
20//! The `FIX_ENV_INTERNAL` environment variable is set to the value of the [`LockServer`]'s
21//! address so that cargo knows it is in fix-proxy-mode.
22//!
23//! Each proxied cargo-as-rustc detects it is in fix-proxy-mode (via `FIX_ENV_INTERNAL`
24//! environment variable in `main`) and does the following:
25//!
26//! - Acquire a lock from the [`LockServer`] from the master cargo process.
27//! - Launches the real rustc ([`rustfix_and_fix`]), looking at the JSON output
28//!   for suggested fixes.
29//! - Uses the `rustfix` crate to apply the suggestions to the files on the
30//!   file system.
31//! - If rustfix fails to apply any suggestions (for example, they are
32//!   overlapping), but at least some suggestions succeeded, it will try the
33//!   previous two steps up to 4 times as long as some suggestions succeed.
34//! - Assuming there's at least one suggestion applied, and the suggestions
35//!   applied cleanly, rustc is run again to verify the suggestions didn't
36//!   break anything. The change will be backed out if it fails (unless
37//!   `--broken-code` is used).
38
39use std::collections::{BTreeSet, HashMap, HashSet};
40use std::ffi::OsString;
41use std::io::Write;
42use std::path::{Path, PathBuf};
43use std::process::{self, ExitStatus, Output};
44use std::{env, fs, str};
45
46use anyhow::{bail, Context as _};
47use cargo_util::{exit_status_to_string, is_simple_exit_code, paths, ProcessBuilder};
48use cargo_util_schemas::manifest::TomlManifest;
49use rustfix::diagnostics::Diagnostic;
50use rustfix::CodeFix;
51use semver::Version;
52use tracing::{debug, trace, warn};
53
54pub use self::fix_edition::fix_edition;
55use crate::core::compiler::CompileKind;
56use crate::core::compiler::RustcTargetData;
57use crate::core::resolver::features::{DiffMap, FeatureOpts, FeatureResolver, FeaturesFor};
58use crate::core::resolver::{HasDevUnits, Resolve, ResolveBehavior};
59use crate::core::PackageIdSpecQuery as _;
60use crate::core::{Edition, MaybePackage, Package, PackageId, Workspace};
61use crate::ops::resolve::WorkspaceResolve;
62use crate::ops::{self, CompileOptions};
63use crate::util::diagnostic_server::{Message, RustfixDiagnosticServer};
64use crate::util::errors::CargoResult;
65use crate::util::toml_mut::manifest::LocalManifest;
66use crate::util::GlobalContext;
67use crate::util::{existing_vcs_repo, LockServer, LockServerClient};
68use crate::{drop_eprint, drop_eprintln};
69
70mod fix_edition;
71
72/// **Internal only.**
73/// Indicates Cargo is in fix-proxy-mode if presents.
74/// The value of it is the socket address of the [`LockServer`] being used.
75/// See the [module-level documentation](mod@super::fix) for more.
76const FIX_ENV_INTERNAL: &str = "__CARGO_FIX_PLZ";
77/// **Internal only.**
78/// For passing [`FixOptions::broken_code`] through to cargo running in proxy mode.
79const BROKEN_CODE_ENV_INTERNAL: &str = "__CARGO_FIX_BROKEN_CODE";
80/// **Internal only.**
81/// For passing [`FixOptions::edition`] through to cargo running in proxy mode.
82const EDITION_ENV_INTERNAL: &str = "__CARGO_FIX_EDITION";
83/// **Internal only.**
84/// For passing [`FixOptions::idioms`] through to cargo running in proxy mode.
85const IDIOMS_ENV_INTERNAL: &str = "__CARGO_FIX_IDIOMS";
86/// **Internal only.**
87/// The sysroot path.
88///
89/// This is for preventing `cargo fix` from fixing rust std/core libs. See
90///
91/// * <https://github.com/rust-lang/cargo/issues/9857>
92/// * <https://github.com/rust-lang/rust/issues/88514#issuecomment-2043469384>
93const SYSROOT_INTERNAL: &str = "__CARGO_FIX_RUST_SRC";
94
95pub struct FixOptions {
96    pub edition: Option<EditionFixMode>,
97    pub idioms: bool,
98    pub compile_opts: CompileOptions,
99    pub allow_dirty: bool,
100    pub allow_no_vcs: bool,
101    pub allow_staged: bool,
102    pub broken_code: bool,
103    pub requested_lockfile_path: Option<PathBuf>,
104}
105
106/// The behavior of `--edition` migration.
107#[derive(Clone, Copy)]
108pub enum EditionFixMode {
109    /// Migrates the package from the current edition to the next.
110    ///
111    /// This is the normal (stable) behavior of `--edition`.
112    NextRelative,
113    /// Migrates to a specific edition.
114    ///
115    /// This is used by `-Zfix-edition` to force a specific edition like
116    /// `future`, which does not have a relative value.
117    OverrideSpecific(Edition),
118}
119
120impl EditionFixMode {
121    /// Returns the edition to use for the given current edition.
122    pub fn next_edition(&self, current_edition: Edition) -> Edition {
123        match self {
124            EditionFixMode::NextRelative => current_edition.saturating_next(),
125            EditionFixMode::OverrideSpecific(edition) => *edition,
126        }
127    }
128
129    /// Serializes to a string.
130    fn to_string(&self) -> String {
131        match self {
132            EditionFixMode::NextRelative => "1".to_string(),
133            EditionFixMode::OverrideSpecific(edition) => edition.to_string(),
134        }
135    }
136
137    /// Deserializes from the given string.
138    fn from_str(s: &str) -> EditionFixMode {
139        match s {
140            "1" => EditionFixMode::NextRelative,
141            edition => EditionFixMode::OverrideSpecific(edition.parse().unwrap()),
142        }
143    }
144}
145
146pub fn fix(
147    gctx: &GlobalContext,
148    original_ws: &Workspace<'_>,
149    opts: &mut FixOptions,
150) -> CargoResult<()> {
151    check_version_control(gctx, opts)?;
152
153    let mut target_data =
154        RustcTargetData::new(original_ws, &opts.compile_opts.build_config.requested_kinds)?;
155    if let Some(edition_mode) = opts.edition {
156        let specs = opts.compile_opts.spec.to_package_id_specs(&original_ws)?;
157        let members: Vec<&Package> = original_ws
158            .members()
159            .filter(|m| specs.iter().any(|spec| spec.matches(m.package_id())))
160            .collect();
161        migrate_manifests(original_ws, &members, edition_mode)?;
162
163        check_resolver_change(&original_ws, &mut target_data, opts)?;
164    }
165    let ws = original_ws.reload(gctx)?;
166
167    // Spin up our lock server, which our subprocesses will use to synchronize fixes.
168    let lock_server = LockServer::new()?;
169    let mut wrapper = ProcessBuilder::new(env::current_exe()?);
170    wrapper.env(FIX_ENV_INTERNAL, lock_server.addr().to_string());
171    let _started = lock_server.start()?;
172
173    opts.compile_opts.build_config.force_rebuild = true;
174
175    if opts.broken_code {
176        wrapper.env(BROKEN_CODE_ENV_INTERNAL, "1");
177    }
178
179    if let Some(mode) = &opts.edition {
180        wrapper.env(EDITION_ENV_INTERNAL, mode.to_string());
181    }
182    if opts.idioms {
183        wrapper.env(IDIOMS_ENV_INTERNAL, "1");
184    }
185
186    let sysroot = &target_data.info(CompileKind::Host).sysroot;
187    if sysroot.is_dir() {
188        wrapper.env(SYSROOT_INTERNAL, sysroot);
189    }
190
191    *opts
192        .compile_opts
193        .build_config
194        .rustfix_diagnostic_server
195        .borrow_mut() = Some(RustfixDiagnosticServer::new()?);
196
197    if let Some(server) = opts
198        .compile_opts
199        .build_config
200        .rustfix_diagnostic_server
201        .borrow()
202        .as_ref()
203    {
204        server.configure(&mut wrapper);
205    }
206
207    let rustc = ws.gctx().load_global_rustc(Some(&ws))?;
208    wrapper.arg(&rustc.path);
209    // This is calling rustc in cargo fix-proxy-mode, so it also need to retry.
210    // The argfile handling are located at `FixArgs::from_args`.
211    wrapper.retry_with_argfile(true);
212
213    // primary crates are compiled using a cargo subprocess to do extra work of applying fixes and
214    // repeating build until there are no more changes to be applied
215    opts.compile_opts.build_config.primary_unit_rustc = Some(wrapper);
216
217    ops::compile(&ws, &opts.compile_opts)?;
218    Ok(())
219}
220
221fn check_version_control(gctx: &GlobalContext, opts: &FixOptions) -> CargoResult<()> {
222    if opts.allow_no_vcs {
223        return Ok(());
224    }
225    if !existing_vcs_repo(gctx.cwd(), gctx.cwd()) {
226        bail!(
227            "no VCS found for this package and `cargo fix` can potentially \
228             perform destructive changes; if you'd like to suppress this \
229             error pass `--allow-no-vcs`"
230        )
231    }
232
233    if opts.allow_dirty && opts.allow_staged {
234        return Ok(());
235    }
236
237    let mut dirty_files = Vec::new();
238    let mut staged_files = Vec::new();
239    if let Ok(repo) = git2::Repository::discover(gctx.cwd()) {
240        let mut repo_opts = git2::StatusOptions::new();
241        repo_opts.include_ignored(false);
242        repo_opts.include_untracked(true);
243        for status in repo.statuses(Some(&mut repo_opts))?.iter() {
244            if let Some(path) = status.path() {
245                match status.status() {
246                    git2::Status::CURRENT => (),
247                    git2::Status::INDEX_NEW
248                    | git2::Status::INDEX_MODIFIED
249                    | git2::Status::INDEX_DELETED
250                    | git2::Status::INDEX_RENAMED
251                    | git2::Status::INDEX_TYPECHANGE => {
252                        if !opts.allow_staged {
253                            staged_files.push(path.to_string())
254                        }
255                    }
256                    _ => {
257                        if !opts.allow_dirty {
258                            dirty_files.push(path.to_string())
259                        }
260                    }
261                };
262            }
263        }
264    }
265
266    if dirty_files.is_empty() && staged_files.is_empty() {
267        return Ok(());
268    }
269
270    let mut files_list = String::new();
271    for file in dirty_files {
272        files_list.push_str("  * ");
273        files_list.push_str(&file);
274        files_list.push_str(" (dirty)\n");
275    }
276    for file in staged_files {
277        files_list.push_str("  * ");
278        files_list.push_str(&file);
279        files_list.push_str(" (staged)\n");
280    }
281
282    bail!(
283        "the working directory of this package has uncommitted changes, and \
284         `cargo fix` can potentially perform destructive changes; if you'd \
285         like to suppress this error pass `--allow-dirty`, \
286         or commit the changes to these files:\n\
287         \n\
288         {}\n\
289         ",
290        files_list
291    );
292}
293
294fn migrate_manifests(
295    ws: &Workspace<'_>,
296    pkgs: &[&Package],
297    edition_mode: EditionFixMode,
298) -> CargoResult<()> {
299    // HACK: Duplicate workspace migration logic between virtual manifests and real manifests to
300    // reduce multiple Migrating messages being reported for the same file to the user
301    if matches!(ws.root_maybe(), MaybePackage::Virtual(_)) {
302        // Warning: workspaces do not have an edition so this should only include changes needed by
303        // packages that preserve the behavior of the workspace on all editions
304        let highest_edition = pkgs
305            .iter()
306            .map(|p| p.manifest().edition())
307            .max()
308            .unwrap_or_default();
309        let prepare_for_edition = edition_mode.next_edition(highest_edition);
310        if highest_edition == prepare_for_edition
311            || (!prepare_for_edition.is_stable() && !ws.gctx().nightly_features_allowed)
312        {
313            //
314        } else {
315            let mut manifest_mut = LocalManifest::try_new(ws.root_manifest())?;
316            let document = &mut manifest_mut.data;
317            let mut fixes = 0;
318
319            if Edition::Edition2024 <= prepare_for_edition {
320                let root = document.as_table_mut();
321
322                if let Some(workspace) = root
323                    .get_mut("workspace")
324                    .and_then(|t| t.as_table_like_mut())
325                {
326                    // strictly speaking, the edition doesn't apply to this table but it should be safe
327                    // enough
328                    fixes += rename_dep_fields_2024(workspace, "dependencies");
329                }
330            }
331
332            if 0 < fixes {
333                // HACK: As workspace migration is a special case, only report it if something
334                // happened
335                let file = ws.root_manifest();
336                let file = file.strip_prefix(ws.root()).unwrap_or(file);
337                let file = file.display();
338                ws.gctx().shell().status(
339                    "Migrating",
340                    format!("{file} from {highest_edition} edition to {prepare_for_edition}"),
341                )?;
342
343                let verb = if fixes == 1 { "fix" } else { "fixes" };
344                let msg = format!("{file} ({fixes} {verb})");
345                ws.gctx().shell().status("Fixed", msg)?;
346
347                manifest_mut.write()?;
348            }
349        }
350    }
351
352    for pkg in pkgs {
353        let existing_edition = pkg.manifest().edition();
354        let prepare_for_edition = edition_mode.next_edition(existing_edition);
355        if existing_edition == prepare_for_edition
356            || (!prepare_for_edition.is_stable() && !ws.gctx().nightly_features_allowed)
357        {
358            continue;
359        }
360        let file = pkg.manifest_path();
361        let file = file.strip_prefix(ws.root()).unwrap_or(file);
362        let file = file.display();
363        ws.gctx().shell().status(
364            "Migrating",
365            format!("{file} from {existing_edition} edition to {prepare_for_edition}"),
366        )?;
367
368        let mut manifest_mut = LocalManifest::try_new(pkg.manifest_path())?;
369        let document = &mut manifest_mut.data;
370        let mut fixes = 0;
371
372        let ws_original_toml = match ws.root_maybe() {
373            MaybePackage::Package(package) => package.manifest().original_toml(),
374            MaybePackage::Virtual(manifest) => manifest.original_toml(),
375        };
376        if Edition::Edition2024 <= prepare_for_edition {
377            let root = document.as_table_mut();
378
379            if let Some(workspace) = root
380                .get_mut("workspace")
381                .and_then(|t| t.as_table_like_mut())
382            {
383                // strictly speaking, the edition doesn't apply to this table but it should be safe
384                // enough
385                fixes += rename_dep_fields_2024(workspace, "dependencies");
386            }
387
388            fixes += rename_table(root, "project", "package");
389            if let Some(target) = root.get_mut("lib").and_then(|t| t.as_table_like_mut()) {
390                fixes += rename_target_fields_2024(target);
391            }
392            fixes += rename_array_of_target_fields_2024(root, "bin");
393            fixes += rename_array_of_target_fields_2024(root, "example");
394            fixes += rename_array_of_target_fields_2024(root, "test");
395            fixes += rename_array_of_target_fields_2024(root, "bench");
396            fixes += rename_dep_fields_2024(root, "dependencies");
397            fixes += remove_ignored_default_features_2024(root, "dependencies", ws_original_toml);
398            fixes += rename_table(root, "dev_dependencies", "dev-dependencies");
399            fixes += rename_dep_fields_2024(root, "dev-dependencies");
400            fixes +=
401                remove_ignored_default_features_2024(root, "dev-dependencies", ws_original_toml);
402            fixes += rename_table(root, "build_dependencies", "build-dependencies");
403            fixes += rename_dep_fields_2024(root, "build-dependencies");
404            fixes +=
405                remove_ignored_default_features_2024(root, "build-dependencies", ws_original_toml);
406            for target in root
407                .get_mut("target")
408                .and_then(|t| t.as_table_like_mut())
409                .iter_mut()
410                .flat_map(|t| t.iter_mut())
411                .filter_map(|(_k, t)| t.as_table_like_mut())
412            {
413                fixes += rename_dep_fields_2024(target, "dependencies");
414                fixes +=
415                    remove_ignored_default_features_2024(target, "dependencies", ws_original_toml);
416                fixes += rename_table(target, "dev_dependencies", "dev-dependencies");
417                fixes += rename_dep_fields_2024(target, "dev-dependencies");
418                fixes += remove_ignored_default_features_2024(
419                    target,
420                    "dev-dependencies",
421                    ws_original_toml,
422                );
423                fixes += rename_table(target, "build_dependencies", "build-dependencies");
424                fixes += rename_dep_fields_2024(target, "build-dependencies");
425                fixes += remove_ignored_default_features_2024(
426                    target,
427                    "build-dependencies",
428                    ws_original_toml,
429                );
430            }
431        }
432
433        if 0 < fixes {
434            let verb = if fixes == 1 { "fix" } else { "fixes" };
435            let msg = format!("{file} ({fixes} {verb})");
436            ws.gctx().shell().status("Fixed", msg)?;
437
438            manifest_mut.write()?;
439        }
440    }
441
442    Ok(())
443}
444
445fn rename_dep_fields_2024(parent: &mut dyn toml_edit::TableLike, dep_kind: &str) -> usize {
446    let mut fixes = 0;
447    for target in parent
448        .get_mut(dep_kind)
449        .and_then(|t| t.as_table_like_mut())
450        .iter_mut()
451        .flat_map(|t| t.iter_mut())
452        .filter_map(|(_k, t)| t.as_table_like_mut())
453    {
454        fixes += rename_table(target, "default_features", "default-features");
455    }
456    fixes
457}
458
459fn remove_ignored_default_features_2024(
460    parent: &mut dyn toml_edit::TableLike,
461    dep_kind: &str,
462    ws_original_toml: &TomlManifest,
463) -> usize {
464    let mut fixes = 0;
465    for (name_in_toml, target) in parent
466        .get_mut(dep_kind)
467        .and_then(|t| t.as_table_like_mut())
468        .iter_mut()
469        .flat_map(|t| t.iter_mut())
470        .filter_map(|(k, t)| t.as_table_like_mut().map(|t| (k, t)))
471    {
472        let name_in_toml: &str = &name_in_toml;
473        let ws_deps = ws_original_toml
474            .workspace
475            .as_ref()
476            .and_then(|ws| ws.dependencies.as_ref());
477        if let Some(ws_dep) = ws_deps.and_then(|ws_deps| ws_deps.get(name_in_toml)) {
478            if ws_dep.default_features() == Some(false) {
479                continue;
480            }
481        }
482        if target
483            .get("workspace")
484            .and_then(|i| i.as_value())
485            .and_then(|i| i.as_bool())
486            == Some(true)
487            && target
488                .get("default-features")
489                .and_then(|i| i.as_value())
490                .and_then(|i| i.as_bool())
491                == Some(false)
492        {
493            target.remove("default-features");
494            fixes += 1;
495        }
496    }
497    fixes
498}
499
500fn rename_array_of_target_fields_2024(root: &mut dyn toml_edit::TableLike, kind: &str) -> usize {
501    let mut fixes = 0;
502    for target in root
503        .get_mut(kind)
504        .and_then(|t| t.as_array_of_tables_mut())
505        .iter_mut()
506        .flat_map(|t| t.iter_mut())
507    {
508        fixes += rename_target_fields_2024(target);
509    }
510    fixes
511}
512
513fn rename_target_fields_2024(target: &mut dyn toml_edit::TableLike) -> usize {
514    let mut fixes = 0;
515    fixes += rename_table(target, "crate_type", "crate-type");
516    fixes += rename_table(target, "proc_macro", "proc-macro");
517    fixes
518}
519
520fn rename_table(parent: &mut dyn toml_edit::TableLike, old: &str, new: &str) -> usize {
521    let Some(old_key) = parent.key(old).cloned() else {
522        return 0;
523    };
524
525    let project = parent.remove(old).expect("returned early");
526    if !parent.contains_key(new) {
527        parent.insert(new, project);
528        let mut new_key = parent.key_mut(new).expect("just inserted");
529        *new_key.dotted_decor_mut() = old_key.dotted_decor().clone();
530        *new_key.leaf_decor_mut() = old_key.leaf_decor().clone();
531    }
532    1
533}
534
535fn check_resolver_change<'gctx>(
536    ws: &Workspace<'gctx>,
537    target_data: &mut RustcTargetData<'gctx>,
538    opts: &FixOptions,
539) -> CargoResult<()> {
540    let root = ws.root_maybe();
541    match root {
542        MaybePackage::Package(root_pkg) => {
543            if root_pkg.manifest().resolve_behavior().is_some() {
544                // If explicitly specified by the user, no need to check.
545                return Ok(());
546            }
547            // Only trigger if updating the root package from 2018.
548            let pkgs = opts.compile_opts.spec.get_packages(ws)?;
549            if !pkgs.contains(&root_pkg) {
550                // The root is not being migrated.
551                return Ok(());
552            }
553            if root_pkg.manifest().edition() != Edition::Edition2018 {
554                // V1 to V2 only happens on 2018 to 2021.
555                return Ok(());
556            }
557        }
558        MaybePackage::Virtual(_vm) => {
559            // Virtual workspaces don't have a global edition to set (yet).
560            return Ok(());
561        }
562    }
563    // 2018 without `resolver` set must be V1
564    assert_eq!(ws.resolve_behavior(), ResolveBehavior::V1);
565    let specs = opts.compile_opts.spec.to_package_id_specs(ws)?;
566    let mut resolve_differences = |has_dev_units| -> CargoResult<(WorkspaceResolve<'_>, DiffMap)> {
567        let dry_run = false;
568        let ws_resolve = ops::resolve_ws_with_opts(
569            ws,
570            target_data,
571            &opts.compile_opts.build_config.requested_kinds,
572            &opts.compile_opts.cli_features,
573            &specs,
574            has_dev_units,
575            crate::core::resolver::features::ForceAllTargets::No,
576            dry_run,
577        )?;
578
579        let feature_opts = FeatureOpts::new_behavior(ResolveBehavior::V2, has_dev_units);
580        let v2_features = FeatureResolver::resolve(
581            ws,
582            target_data,
583            &ws_resolve.targeted_resolve,
584            &ws_resolve.pkg_set,
585            &opts.compile_opts.cli_features,
586            &specs,
587            &opts.compile_opts.build_config.requested_kinds,
588            feature_opts,
589        )?;
590
591        let diffs = v2_features.compare_legacy(&ws_resolve.resolved_features);
592        Ok((ws_resolve, diffs))
593    };
594    let (_, without_dev_diffs) = resolve_differences(HasDevUnits::No)?;
595    let (ws_resolve, mut with_dev_diffs) = resolve_differences(HasDevUnits::Yes)?;
596    if without_dev_diffs.is_empty() && with_dev_diffs.is_empty() {
597        // Nothing is different, nothing to report.
598        return Ok(());
599    }
600    // Only display unique changes with dev-dependencies.
601    with_dev_diffs.retain(|k, vals| without_dev_diffs.get(k) != Some(vals));
602    let gctx = ws.gctx();
603    gctx.shell().note(
604        "Switching to Edition 2021 will enable the use of the version 2 feature resolver in Cargo.",
605    )?;
606    drop_eprintln!(
607        gctx,
608        "This may cause some dependencies to be built with fewer features enabled than previously."
609    );
610    drop_eprintln!(
611        gctx,
612        "More information about the resolver changes may be found \
613         at https://doc.rust-lang.org/nightly/edition-guide/rust-2021/default-cargo-resolver.html"
614    );
615    drop_eprintln!(
616        gctx,
617        "When building the following dependencies, \
618         the given features will no longer be used:\n"
619    );
620    let show_diffs = |differences: DiffMap| {
621        for ((pkg_id, features_for), removed) in differences {
622            drop_eprint!(gctx, "  {}", pkg_id);
623            if let FeaturesFor::HostDep = features_for {
624                drop_eprint!(gctx, " (as host dependency)");
625            }
626            drop_eprint!(gctx, " removed features: ");
627            let joined: Vec<_> = removed.iter().map(|s| s.as_str()).collect();
628            drop_eprintln!(gctx, "{}", joined.join(", "));
629        }
630        drop_eprint!(gctx, "\n");
631    };
632    if !without_dev_diffs.is_empty() {
633        show_diffs(without_dev_diffs);
634    }
635    if !with_dev_diffs.is_empty() {
636        drop_eprintln!(
637            gctx,
638            "The following differences only apply when building with dev-dependencies:\n"
639        );
640        show_diffs(with_dev_diffs);
641    }
642    report_maybe_diesel(gctx, &ws_resolve.targeted_resolve)?;
643    Ok(())
644}
645
646fn report_maybe_diesel(gctx: &GlobalContext, resolve: &Resolve) -> CargoResult<()> {
647    fn is_broken_diesel(pid: PackageId) -> bool {
648        pid.name() == "diesel" && pid.version() < &Version::new(1, 4, 8)
649    }
650
651    fn is_broken_diesel_migration(pid: PackageId) -> bool {
652        pid.name() == "diesel_migrations" && pid.version().major <= 1
653    }
654
655    if resolve.iter().any(is_broken_diesel) && resolve.iter().any(is_broken_diesel_migration) {
656        gctx.shell().note(
657            "\
658This project appears to use both diesel and diesel_migrations. These packages have
659a known issue where the build may fail due to the version 2 resolver preventing
660feature unification between those two packages. Please update to at least diesel 1.4.8
661to prevent this issue from happening.
662",
663        )?;
664    }
665    Ok(())
666}
667
668/// Provide the lock address when running in proxy mode
669///
670/// Returns `None` if `fix` is not being run (not in proxy mode). Returns
671/// `Some(...)` if in `fix` proxy mode
672pub fn fix_get_proxy_lock_addr() -> Option<String> {
673    // ALLOWED: For the internal mechanism of `cargo fix` only.
674    // Shouldn't be set directly by anyone.
675    #[allow(clippy::disallowed_methods)]
676    env::var(FIX_ENV_INTERNAL).ok()
677}
678
679/// Entry point for `cargo` running as a proxy for `rustc`.
680///
681/// This is called every time `cargo` is run to check if it is in proxy mode.
682///
683/// If there are warnings or errors, this does not return,
684/// and the process exits with the corresponding `rustc` exit code.
685///
686/// See [`fix_get_proxy_lock_addr`]
687pub fn fix_exec_rustc(gctx: &GlobalContext, lock_addr: &str) -> CargoResult<()> {
688    let args = FixArgs::get()?;
689    trace!("cargo-fix as rustc got file {:?}", args.file);
690
691    let workspace_rustc = gctx
692        .get_env("RUSTC_WORKSPACE_WRAPPER")
693        .map(PathBuf::from)
694        .ok();
695    let mut rustc = ProcessBuilder::new(&args.rustc).wrapped(workspace_rustc.as_ref());
696    rustc.retry_with_argfile(true);
697    rustc.env_remove(FIX_ENV_INTERNAL);
698    args.apply(&mut rustc);
699    // Removes `FD_CLOEXEC` set by `jobserver::Client` to ensure that the
700    // compiler can access the jobserver.
701    if let Some(client) = gctx.jobserver_from_env() {
702        rustc.inherit_jobserver(client);
703    }
704
705    trace!("start rustfixing {:?}", args.file);
706    let fixes = rustfix_crate(&lock_addr, &rustc, &args.file, &args, gctx)?;
707
708    if fixes.last_output.status.success() {
709        for (path, file) in fixes.files.iter() {
710            Message::Fixed {
711                file: path.clone(),
712                fixes: file.fixes_applied,
713            }
714            .post(gctx)?;
715        }
716        // Display any remaining diagnostics.
717        emit_output(&fixes.last_output)?;
718        return Ok(());
719    }
720
721    let allow_broken_code = gctx.get_env_os(BROKEN_CODE_ENV_INTERNAL).is_some();
722
723    // There was an error running rustc during the last run.
724    //
725    // Back out all of the changes unless --broken-code was used.
726    if !allow_broken_code {
727        for (path, file) in fixes.files.iter() {
728            debug!("reverting {:?} due to errors", path);
729            paths::write(path, &file.original_code)?;
730        }
731    }
732
733    // If there were any fixes, let the user know that there was a failure
734    // attempting to apply them, and to ask for a bug report.
735    //
736    // FIXME: The error message here is not correct with --broken-code.
737    //        https://github.com/rust-lang/cargo/issues/10955
738    if fixes.files.is_empty() {
739        // No fixes were available. Display whatever errors happened.
740        emit_output(&fixes.last_output)?;
741        exit_with(fixes.last_output.status);
742    } else {
743        let krate = {
744            let mut iter = rustc.get_args();
745            let mut krate = None;
746            while let Some(arg) = iter.next() {
747                if arg == "--crate-name" {
748                    krate = iter.next().and_then(|s| s.to_owned().into_string().ok());
749                }
750            }
751            krate
752        };
753        log_failed_fix(
754            gctx,
755            krate,
756            &fixes.last_output.stderr,
757            fixes.last_output.status,
758        )?;
759        // Display the diagnostics that appeared at the start, before the
760        // fixes failed. This can help with diagnosing which suggestions
761        // caused the failure.
762        emit_output(&fixes.first_output)?;
763        // Exit with whatever exit code we initially started with. `cargo fix`
764        // treats this as a warning, and shouldn't return a failure code
765        // unless the code didn't compile in the first place.
766        exit_with(fixes.first_output.status);
767    }
768}
769
770fn emit_output(output: &Output) -> CargoResult<()> {
771    // Unfortunately if there is output on stdout, this does not preserve the
772    // order of output relative to stderr. In practice, rustc should never
773    // print to stdout unless some proc-macro does it.
774    std::io::stderr().write_all(&output.stderr)?;
775    std::io::stdout().write_all(&output.stdout)?;
776    Ok(())
777}
778
779struct FixedCrate {
780    /// Map of file path to some information about modifications made to that file.
781    files: HashMap<String, FixedFile>,
782    /// The output from rustc from the first time it was called.
783    ///
784    /// This is needed when fixes fail to apply, so that it can display the
785    /// original diagnostics to the user which can help with diagnosing which
786    /// suggestions caused the failure.
787    first_output: Output,
788    /// The output from rustc from the last time it was called.
789    ///
790    /// This will be displayed to the user to show any remaining diagnostics
791    /// or errors.
792    last_output: Output,
793}
794
795#[derive(Debug)]
796struct FixedFile {
797    errors_applying_fixes: Vec<String>,
798    fixes_applied: u32,
799    original_code: String,
800}
801
802/// Attempts to apply fixes to a single crate.
803///
804/// This runs `rustc` (possibly multiple times) to gather suggestions from the
805/// compiler and applies them to the files on disk.
806fn rustfix_crate(
807    lock_addr: &str,
808    rustc: &ProcessBuilder,
809    filename: &Path,
810    args: &FixArgs,
811    gctx: &GlobalContext,
812) -> CargoResult<FixedCrate> {
813    // First up, we want to make sure that each crate is only checked by one
814    // process at a time. If two invocations concurrently check a crate then
815    // it's likely to corrupt it.
816    //
817    // Historically this used per-source-file locking, then per-package
818    // locking. It now uses a single, global lock as some users do things like
819    // #[path] or include!() of shared files between packages. Serializing
820    // makes it slower, but is the only safe way to prevent concurrent
821    // modification.
822    let _lock = LockServerClient::lock(&lock_addr.parse()?, "global")?;
823
824    // Map of files that have been modified.
825    let mut files = HashMap::new();
826
827    if !args.can_run_rustfix(gctx)? {
828        // This fix should not be run. Skipping...
829        // We still need to run rustc at least once to make sure any potential
830        // rmeta gets generated, and diagnostics get displayed.
831        debug!("can't fix {filename:?}, running rustc: {rustc}");
832        let last_output = rustc.output()?;
833        let fixes = FixedCrate {
834            files,
835            first_output: last_output.clone(),
836            last_output,
837        };
838        return Ok(fixes);
839    }
840
841    // Next up, this is a bit suspicious, but we *iteratively* execute rustc and
842    // collect suggestions to feed to rustfix. Once we hit our limit of times to
843    // execute rustc or we appear to be reaching a fixed point we stop running
844    // rustc.
845    //
846    // This is currently done to handle code like:
847    //
848    //      ::foo::<::Bar>();
849    //
850    // where there are two fixes to happen here: `crate::foo::<crate::Bar>()`.
851    // The spans for these two suggestions are overlapping and its difficult in
852    // the compiler to **not** have overlapping spans here. As a result, a naive
853    // implementation would feed the two compiler suggestions for the above fix
854    // into `rustfix`, but one would be rejected because it overlaps with the
855    // other.
856    //
857    // In this case though, both suggestions are valid and can be automatically
858    // applied! To handle this case we execute rustc multiple times, collecting
859    // fixes each time we do so. Along the way we discard any suggestions that
860    // failed to apply, assuming that they can be fixed the next time we run
861    // rustc.
862    //
863    // Naturally, we want a few protections in place here though to avoid looping
864    // forever or otherwise losing data. To that end we have a few termination
865    // conditions:
866    //
867    // * Do this whole process a fixed number of times. In theory we probably
868    //   need an infinite number of times to apply fixes, but we're not gonna
869    //   sit around waiting for that.
870    // * If it looks like a fix genuinely can't be applied we need to bail out.
871    //   Detect this when a fix fails to get applied *and* no suggestions
872    //   successfully applied to the same file. In that case looks like we
873    //   definitely can't make progress, so bail out.
874    let max_iterations = gctx
875        .get_env("CARGO_FIX_MAX_RETRIES")
876        .ok()
877        .and_then(|n| n.parse().ok())
878        .unwrap_or(4);
879    let mut last_output;
880    let mut last_made_changes;
881    let mut first_output = None;
882    let mut current_iteration = 0;
883    loop {
884        for file in files.values_mut() {
885            // We'll generate new errors below.
886            file.errors_applying_fixes.clear();
887        }
888        (last_output, last_made_changes) =
889            rustfix_and_fix(&mut files, rustc, filename, args, gctx)?;
890        if current_iteration == 0 {
891            first_output = Some(last_output.clone());
892        }
893        let mut progress_yet_to_be_made = false;
894        for (path, file) in files.iter_mut() {
895            if file.errors_applying_fixes.is_empty() {
896                continue;
897            }
898            debug!("had rustfix apply errors in {path:?} {file:?}");
899            // If anything was successfully fixed *and* there's at least one
900            // error, then assume the error was spurious and we'll try again on
901            // the next iteration.
902            if last_made_changes {
903                progress_yet_to_be_made = true;
904            }
905        }
906        if !progress_yet_to_be_made {
907            break;
908        }
909        current_iteration += 1;
910        if current_iteration >= max_iterations {
911            break;
912        }
913    }
914    if last_made_changes {
915        debug!("calling rustc one last time for final results: {rustc}");
916        last_output = rustc.output()?;
917    }
918
919    // Any errors still remaining at this point need to be reported as probably
920    // bugs in Cargo and/or rustfix.
921    for (path, file) in files.iter_mut() {
922        for error in file.errors_applying_fixes.drain(..) {
923            Message::ReplaceFailed {
924                file: path.clone(),
925                message: error,
926            }
927            .post(gctx)?;
928        }
929    }
930
931    Ok(FixedCrate {
932        files,
933        first_output: first_output.expect("at least one iteration"),
934        last_output,
935    })
936}
937
938/// Executes `rustc` to apply one round of suggestions to the crate in question.
939///
940/// This will fill in the `fixes` map with original code, suggestions applied,
941/// and any errors encountered while fixing files.
942fn rustfix_and_fix(
943    files: &mut HashMap<String, FixedFile>,
944    rustc: &ProcessBuilder,
945    filename: &Path,
946    args: &FixArgs,
947    gctx: &GlobalContext,
948) -> CargoResult<(Output, bool)> {
949    // If not empty, filter by these lints.
950    // TODO: implement a way to specify this.
951    let only = HashSet::new();
952
953    debug!("calling rustc to collect suggestions and validate previous fixes: {rustc}");
954    let output = rustc.output()?;
955
956    // If rustc didn't succeed for whatever reasons then we're very likely to be
957    // looking at otherwise broken code. Let's not make things accidentally
958    // worse by applying fixes where a bug could cause *more* broken code.
959    // Instead, punt upwards which will reexec rustc over the original code,
960    // displaying pretty versions of the diagnostics we just read out.
961    if !output.status.success() && gctx.get_env_os(BROKEN_CODE_ENV_INTERNAL).is_none() {
962        debug!(
963            "rustfixing `{:?}` failed, rustc exited with {:?}",
964            filename,
965            output.status.code()
966        );
967        return Ok((output, false));
968    }
969
970    let fix_mode = gctx
971        .get_env_os("__CARGO_FIX_YOLO")
972        .map(|_| rustfix::Filter::Everything)
973        .unwrap_or(rustfix::Filter::MachineApplicableOnly);
974
975    // Sift through the output of the compiler to look for JSON messages.
976    // indicating fixes that we can apply.
977    let stderr = str::from_utf8(&output.stderr).context("failed to parse rustc stderr as UTF-8")?;
978
979    let suggestions = stderr
980        .lines()
981        .filter(|x| !x.is_empty())
982        .inspect(|y| trace!("line: {}", y))
983        // Parse each line of stderr, ignoring errors, as they may not all be JSON.
984        .filter_map(|line| serde_json::from_str::<Diagnostic>(line).ok())
985        // From each diagnostic, try to extract suggestions from rustc.
986        .filter_map(|diag| rustfix::collect_suggestions(&diag, &only, fix_mode));
987
988    // Collect suggestions by file so we can apply them one at a time later.
989    let mut file_map = HashMap::new();
990    let mut num_suggestion = 0;
991    // It's safe since we won't read any content under home dir.
992    let home_path = gctx.home().as_path_unlocked();
993    for suggestion in suggestions {
994        trace!("suggestion");
995        // Make sure we've got a file associated with this suggestion and all
996        // snippets point to the same file. Right now it's not clear what
997        // we would do with multiple files.
998        let file_names = suggestion
999            .solutions
1000            .iter()
1001            .flat_map(|s| s.replacements.iter())
1002            .map(|r| &r.snippet.file_name);
1003
1004        let file_name = if let Some(file_name) = file_names.clone().next() {
1005            file_name.clone()
1006        } else {
1007            trace!("rejecting as it has no solutions {:?}", suggestion);
1008            continue;
1009        };
1010
1011        let file_path = Path::new(&file_name);
1012        // Do not write into registry cache. See rust-lang/cargo#9857.
1013        if file_path.starts_with(home_path) {
1014            continue;
1015        }
1016        // Do not write into standard library source. See rust-lang/cargo#9857.
1017        if let Some(sysroot) = args.sysroot.as_deref() {
1018            if file_path.starts_with(sysroot) {
1019                continue;
1020            }
1021        }
1022
1023        if !file_names.clone().all(|f| f == &file_name) {
1024            trace!("rejecting as it changes multiple files: {:?}", suggestion);
1025            continue;
1026        }
1027
1028        trace!("adding suggestion for {:?}: {:?}", file_name, suggestion);
1029        file_map
1030            .entry(file_name)
1031            .or_insert_with(Vec::new)
1032            .push(suggestion);
1033        num_suggestion += 1;
1034    }
1035
1036    debug!(
1037        "collected {} suggestions for `{}`",
1038        num_suggestion,
1039        filename.display(),
1040    );
1041
1042    let mut made_changes = false;
1043    for (file, suggestions) in file_map {
1044        // Attempt to read the source code for this file. If this fails then
1045        // that'd be pretty surprising, so log a message and otherwise keep
1046        // going.
1047        let code = match paths::read(file.as_ref()) {
1048            Ok(s) => s,
1049            Err(e) => {
1050                warn!("failed to read `{}`: {}", file, e);
1051                continue;
1052            }
1053        };
1054        let num_suggestions = suggestions.len();
1055        debug!("applying {} fixes to {}", num_suggestions, file);
1056
1057        // If this file doesn't already exist then we just read the original
1058        // code, so save it. If the file already exists then the original code
1059        // doesn't need to be updated as we've just read an interim state with
1060        // some fixes but perhaps not all.
1061        let fixed_file = files.entry(file.clone()).or_insert_with(|| FixedFile {
1062            errors_applying_fixes: Vec::new(),
1063            fixes_applied: 0,
1064            original_code: code.clone(),
1065        });
1066        let mut fixed = CodeFix::new(&code);
1067
1068        for suggestion in suggestions.iter().rev() {
1069            // As mentioned above in `rustfix_crate`,
1070            // we don't immediately warn about suggestions that fail to apply here,
1071            // and instead we save them off for later processing.
1072            //
1073            // However, we don't bother reporting conflicts that exactly match prior replacements.
1074            // This is currently done to reduce noise for things like rust-lang/rust#51211,
1075            // although it may be removed if that's fixed deeper in the compiler.
1076            match fixed.apply(suggestion) {
1077                Ok(()) => fixed_file.fixes_applied += 1,
1078                Err(rustfix::Error::AlreadyReplaced {
1079                    is_identical: true, ..
1080                }) => continue,
1081                Err(e) => fixed_file.errors_applying_fixes.push(e.to_string()),
1082            }
1083        }
1084        if fixed.modified() {
1085            made_changes = true;
1086            let new_code = fixed.finish()?;
1087            paths::write(&file, new_code)?;
1088        }
1089    }
1090
1091    Ok((output, made_changes))
1092}
1093
1094fn exit_with(status: ExitStatus) -> ! {
1095    #[cfg(unix)]
1096    {
1097        use std::os::unix::prelude::*;
1098        if let Some(signal) = status.signal() {
1099            drop(writeln!(
1100                std::io::stderr().lock(),
1101                "child failed with signal `{}`",
1102                signal
1103            ));
1104            process::exit(2);
1105        }
1106    }
1107    process::exit(status.code().unwrap_or(3));
1108}
1109
1110fn log_failed_fix(
1111    gctx: &GlobalContext,
1112    krate: Option<String>,
1113    stderr: &[u8],
1114    status: ExitStatus,
1115) -> CargoResult<()> {
1116    let stderr = str::from_utf8(stderr).context("failed to parse rustc stderr as utf-8")?;
1117
1118    let diagnostics = stderr
1119        .lines()
1120        .filter(|x| !x.is_empty())
1121        .filter_map(|line| serde_json::from_str::<Diagnostic>(line).ok());
1122    let mut files = BTreeSet::new();
1123    let mut errors = Vec::new();
1124    for diagnostic in diagnostics {
1125        errors.push(diagnostic.rendered.unwrap_or(diagnostic.message));
1126        for span in diagnostic.spans.into_iter() {
1127            files.insert(span.file_name);
1128        }
1129    }
1130    // Include any abnormal messages (like an ICE or whatever).
1131    errors.extend(
1132        stderr
1133            .lines()
1134            .filter(|x| !x.starts_with('{'))
1135            .map(|x| x.to_string()),
1136    );
1137
1138    let files = files.into_iter().collect();
1139    let abnormal_exit = if status.code().map_or(false, is_simple_exit_code) {
1140        None
1141    } else {
1142        Some(exit_status_to_string(status))
1143    };
1144    Message::FixFailed {
1145        files,
1146        krate,
1147        errors,
1148        abnormal_exit,
1149    }
1150    .post(gctx)?;
1151
1152    Ok(())
1153}
1154
1155/// Various command-line options and settings used when `cargo` is running as
1156/// a proxy for `rustc` during the fix operation.
1157struct FixArgs {
1158    /// This is the `.rs` file that is being fixed.
1159    file: PathBuf,
1160    /// If `--edition` is used to migrate to the next edition, this is the
1161    /// edition we are migrating towards.
1162    prepare_for_edition: Option<Edition>,
1163    /// `true` if `--edition-idioms` is enabled.
1164    idioms: bool,
1165    /// The current edition.
1166    ///
1167    /// `None` if on 2015.
1168    enabled_edition: Option<Edition>,
1169    /// Other command-line arguments not reflected by other fields in
1170    /// `FixArgs`.
1171    other: Vec<OsString>,
1172    /// Path to the `rustc` executable.
1173    rustc: PathBuf,
1174    /// Path to host sysroot.
1175    sysroot: Option<PathBuf>,
1176}
1177
1178impl FixArgs {
1179    fn get() -> CargoResult<FixArgs> {
1180        Self::from_args(env::args_os())
1181    }
1182
1183    // This is a separate function so that we can use it in tests.
1184    fn from_args(argv: impl IntoIterator<Item = OsString>) -> CargoResult<Self> {
1185        let mut argv = argv.into_iter();
1186        let mut rustc = argv
1187            .nth(1)
1188            .map(PathBuf::from)
1189            .ok_or_else(|| anyhow::anyhow!("expected rustc or `@path` as first argument"))?;
1190        let mut file = None;
1191        let mut enabled_edition = None;
1192        let mut other = Vec::new();
1193
1194        let mut handle_arg = |arg: OsString| -> CargoResult<()> {
1195            let path = PathBuf::from(arg);
1196            if path.extension().and_then(|s| s.to_str()) == Some("rs") && path.exists() {
1197                file = Some(path);
1198                return Ok(());
1199            }
1200            if let Some(s) = path.to_str() {
1201                if let Some(edition) = s.strip_prefix("--edition=") {
1202                    enabled_edition = Some(edition.parse()?);
1203                    return Ok(());
1204                }
1205            }
1206            other.push(path.into());
1207            Ok(())
1208        };
1209
1210        if let Some(argfile_path) = rustc.to_str().unwrap_or_default().strip_prefix("@") {
1211            // Because cargo in fix-proxy-mode might hit the command line size limit,
1212            // cargo fix need handle `@path` argfile for this special case.
1213            if argv.next().is_some() {
1214                bail!("argfile `@path` cannot be combined with other arguments");
1215            }
1216            let contents = fs::read_to_string(argfile_path)
1217                .with_context(|| format!("failed to read argfile at `{argfile_path}`"))?;
1218            let mut iter = contents.lines().map(OsString::from);
1219            rustc = iter
1220                .next()
1221                .map(PathBuf::from)
1222                .ok_or_else(|| anyhow::anyhow!("expected rustc as first argument"))?;
1223            for arg in iter {
1224                handle_arg(arg)?;
1225            }
1226        } else {
1227            for arg in argv {
1228                handle_arg(arg)?;
1229            }
1230        }
1231
1232        let file = file.ok_or_else(|| anyhow::anyhow!("could not find .rs file in rustc args"))?;
1233        // ALLOWED: For the internal mechanism of `cargo fix` only.
1234        // Shouldn't be set directly by anyone.
1235        #[allow(clippy::disallowed_methods)]
1236        let idioms = env::var(IDIOMS_ENV_INTERNAL).is_ok();
1237
1238        // ALLOWED: For the internal mechanism of `cargo fix` only.
1239        // Shouldn't be set directly by anyone.
1240        #[allow(clippy::disallowed_methods)]
1241        let prepare_for_edition = env::var(EDITION_ENV_INTERNAL).ok().map(|v| {
1242            let enabled_edition = enabled_edition.unwrap_or(Edition::Edition2015);
1243            let mode = EditionFixMode::from_str(&v);
1244            mode.next_edition(enabled_edition)
1245        });
1246
1247        // ALLOWED: For the internal mechanism of `cargo fix` only.
1248        // Shouldn't be set directly by anyone.
1249        #[allow(clippy::disallowed_methods)]
1250        let sysroot = env::var_os(SYSROOT_INTERNAL).map(PathBuf::from);
1251
1252        Ok(FixArgs {
1253            file,
1254            prepare_for_edition,
1255            idioms,
1256            enabled_edition,
1257            other,
1258            rustc,
1259            sysroot,
1260        })
1261    }
1262
1263    fn apply(&self, cmd: &mut ProcessBuilder) {
1264        cmd.arg(&self.file);
1265        cmd.args(&self.other);
1266        if self.prepare_for_edition.is_some() {
1267            // When migrating an edition, we don't want to fix other lints as
1268            // they can sometimes add suggestions that fail to apply, causing
1269            // the entire migration to fail. But those lints aren't needed to
1270            // migrate.
1271            cmd.arg("--cap-lints=allow");
1272        } else {
1273            // This allows `cargo fix` to work even if the crate has #[deny(warnings)].
1274            cmd.arg("--cap-lints=warn");
1275        }
1276        if let Some(edition) = self.enabled_edition {
1277            cmd.arg("--edition").arg(edition.to_string());
1278            if self.idioms && edition.supports_idiom_lint() {
1279                cmd.arg(format!("-Wrust-{}-idioms", edition));
1280            }
1281        }
1282
1283        if let Some(edition) = self.prepare_for_edition {
1284            edition.force_warn_arg(cmd);
1285        }
1286    }
1287
1288    /// Validates the edition, and sends a message indicating what is being
1289    /// done. Returns a flag indicating whether this fix should be run.
1290    fn can_run_rustfix(&self, gctx: &GlobalContext) -> CargoResult<bool> {
1291        let Some(to_edition) = self.prepare_for_edition else {
1292            return Message::Fixing {
1293                file: self.file.display().to_string(),
1294            }
1295            .post(gctx)
1296            .and(Ok(true));
1297        };
1298        // Unfortunately determining which cargo targets are being built
1299        // isn't easy, and each target can be a different edition. The
1300        // cargo-as-rustc fix wrapper doesn't know anything about the
1301        // workspace, so it can't check for the `cargo-features` unstable
1302        // opt-in. As a compromise, this just restricts to the nightly
1303        // toolchain.
1304        //
1305        // Unfortunately this results in a pretty poor error message when
1306        // multiple jobs run in parallel (the error appears multiple
1307        // times). Hopefully this doesn't happen often in practice.
1308        if !to_edition.is_stable() && !gctx.nightly_features_allowed {
1309            let message = format!(
1310                "`{file}` is on the latest edition, but trying to \
1311                 migrate to edition {to_edition}.\n\
1312                 Edition {to_edition} is unstable and not allowed in \
1313                 this release, consider trying the nightly release channel.",
1314                file = self.file.display(),
1315                to_edition = to_edition
1316            );
1317            return Message::EditionAlreadyEnabled {
1318                message,
1319                edition: to_edition.previous().unwrap(),
1320            }
1321            .post(gctx)
1322            .and(Ok(false)); // Do not run rustfix for this the edition.
1323        }
1324        let from_edition = self.enabled_edition.unwrap_or(Edition::Edition2015);
1325        if from_edition == to_edition {
1326            let message = format!(
1327                "`{}` is already on the latest edition ({}), \
1328                 unable to migrate further",
1329                self.file.display(),
1330                to_edition
1331            );
1332            Message::EditionAlreadyEnabled {
1333                message,
1334                edition: to_edition,
1335            }
1336            .post(gctx)
1337        } else {
1338            Message::Migrating {
1339                file: self.file.display().to_string(),
1340                from_edition,
1341                to_edition,
1342            }
1343            .post(gctx)
1344        }
1345        .and(Ok(true))
1346    }
1347}
1348
1349#[cfg(test)]
1350mod tests {
1351    use super::FixArgs;
1352    use std::ffi::OsString;
1353    use std::io::Write as _;
1354    use std::path::PathBuf;
1355
1356    #[test]
1357    fn get_fix_args_from_argfile() {
1358        let mut temp = tempfile::Builder::new().tempfile().unwrap();
1359        let main_rs = tempfile::Builder::new().suffix(".rs").tempfile().unwrap();
1360
1361        let content = format!("/path/to/rustc\n{}\nfoobar\n", main_rs.path().display());
1362        temp.write_all(content.as_bytes()).unwrap();
1363
1364        let argfile = format!("@{}", temp.path().display());
1365        let args = ["cargo", &argfile];
1366        let fix_args = FixArgs::from_args(args.map(|x| x.into())).unwrap();
1367        assert_eq!(fix_args.rustc, PathBuf::from("/path/to/rustc"));
1368        assert_eq!(fix_args.file, main_rs.path());
1369        assert_eq!(fix_args.other, vec![OsString::from("foobar")]);
1370    }
1371
1372    #[test]
1373    fn get_fix_args_from_argfile_with_extra_arg() {
1374        let mut temp = tempfile::Builder::new().tempfile().unwrap();
1375        let main_rs = tempfile::Builder::new().suffix(".rs").tempfile().unwrap();
1376
1377        let content = format!("/path/to/rustc\n{}\nfoobar\n", main_rs.path().display());
1378        temp.write_all(content.as_bytes()).unwrap();
1379
1380        let argfile = format!("@{}", temp.path().display());
1381        let args = ["cargo", &argfile, "boo!"];
1382        match FixArgs::from_args(args.map(|x| x.into())) {
1383            Err(e) => assert_eq!(
1384                e.to_string(),
1385                "argfile `@path` cannot be combined with other arguments"
1386            ),
1387            Ok(_) => panic!("should fail"),
1388        }
1389    }
1390}