cargo-rail 0.19.0

Graph-aware CI, dependency unification, changesets-style releases, and crate split/sync for Rust workspaces
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
//! Transparent compiler proxies for exact invocation observations.
//!
//! Cargo invokes rustc mode through `RUSTC_WORKSPACE_WRAPPER`, so the unused
//! dependency lint is applied only to workspace members. Rustdoc observation
//! uses Cargo's selected `RUSTDOC` slot and retains that program as the inner
//! executable because Cargo has no rustdoc-wrapper setting.

use std::ffi::OsString;
use std::path::{Path, PathBuf};
use std::process::Command;

/// Marker set when this executable is the cache-disabled outer compiler wrapper.
pub const CACHE_WRAPPER_MARKER: &str = "CARGO_RAIL_COMPILER_CACHE_WRAPPER";

/// Marker set by the diagnostics collector when this executable is acting as a
/// rustc workspace wrapper.
pub const WRAPPER_MARKER: &str = "CARGO_RAIL_RUSTC_WRAPPER";

/// Existing workspace wrapper saved by the collector for transparent chaining.
pub const INNER_WRAPPER_ENV: &str = "CARGO_RAIL_INNER_WORKSPACE_WRAPPER";

/// Marker set when this executable is transparently proxying rustdoc.
pub const RUSTDOC_WRAPPER_MARKER: &str = "CARGO_RAIL_RUSTDOC_WRAPPER";

/// Selected rustdoc executable retained behind the cargo-rail observation proxy.
pub const INNER_RUSTDOC_ENV: &str = "CARGO_RAIL_INNER_RUSTDOC";

/// Private directory where diagnostics wrappers publish immutable invocation evidence.
pub const OBSERVATION_DIRECTORY_ENV: &str = "CARGO_RAIL_COMPILER_OBSERVATION_DIRECTORY";

/// Physical source root used only to normalize and revalidate observation paths.
pub const OBSERVATION_SOURCE_ROOT_ENV: &str = "CARGO_RAIL_COMPILER_OBSERVATION_SOURCE_ROOT";

/// Record invocations without enabling cargo-rail's workspace diagnostic lint.
pub const OBSERVATION_ONLY_ENV: &str = "CARGO_RAIL_COMPILER_OBSERVATION_ONLY";

/// Whether cargo-rail may install its cache-disabled compiler wrapper.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum CacheWrapperPlan {
  /// No external wrapper exists, so the transparent boundary can be exercised.
  DisabledPassThrough,
  /// An existing sccache wrapper keeps Cargo's original position in the chain.
  PreserveSccache,
  /// An existing wrapper of unknown cache behavior is preserved fail-closed.
  PreserveExisting,
}

impl CacheWrapperPlan {
  pub(crate) fn for_chain(
    rustc_wrapper: Option<&std::ffi::OsStr>,
    workspace_wrapper: Option<&std::ffi::OsStr>,
  ) -> Self {
    if [rustc_wrapper, workspace_wrapper]
      .into_iter()
      .flatten()
      .any(is_sccache_selection)
    {
      Self::PreserveSccache
    } else if rustc_wrapper.is_some() || workspace_wrapper.is_some() {
      Self::PreserveExisting
    } else {
      Self::DisabledPassThrough
    }
  }

  pub(crate) fn installs_cargo_rail(self) -> bool {
    self == Self::DisabledPassThrough
  }

  pub(crate) fn reason(self) -> &'static str {
    match self {
      Self::DisabledPassThrough => "native_compiler_cache_per_invocation",
      Self::PreserveSccache => "sccache_wrapper_preserved",
      Self::PreserveExisting => "existing_compiler_wrapper_preserved",
    }
  }
}

fn is_sccache_selection(program: &std::ffi::OsStr) -> bool {
  Path::new(program)
    .file_stem()
    .and_then(std::ffi::OsStr::to_str)
    .is_some_and(|name| name.eq_ignore_ascii_case("sccache"))
}

/// Compose Cargo's stable wrapper order: global wrapper, workspace wrapper, rustc.
pub(crate) fn rustc_command(
  rustc: &std::ffi::OsStr,
  rustc_wrapper: Option<&std::ffi::OsStr>,
  workspace_wrapper: Option<&std::ffi::OsStr>,
) -> Command {
  match (rustc_wrapper, workspace_wrapper) {
    (Some(wrapper), Some(workspace_wrapper)) => {
      let mut command = Command::new(wrapper);
      command.arg(workspace_wrapper).arg(rustc);
      command
    }
    (Some(wrapper), None) => {
      let mut command = Command::new(wrapper);
      command.arg(rustc);
      command
    }
    (None, Some(workspace_wrapper)) => {
      let mut command = Command::new(workspace_wrapper);
      command.arg(rustc);
      command
    }
    (None, None) => Command::new(rustc),
  }
}

/// Run rustc wrapper mode when requested by the diagnostics collector.
///
/// Returns `None` during normal cargo-rail CLI execution.
#[must_use]
pub fn run_if_requested() -> Option<i32> {
  if let Some(context) = crate::compiler::native_cache::NativeCacheContext::from_direct_invocation() {
    return Some(match context {
      Ok(context) => run_cache_disabled(Some(context)),
      Err(error) => {
        eprintln!("cargo-rail compiler cache wrapper: {error}");
        2
      }
    });
  }
  if std::env::var_os(CACHE_WRAPPER_MARKER).is_some() {
    return Some(run_cache_disabled(
      crate::compiler::native_cache::NativeCacheContext::from_environment(),
    ));
  }
  if std::env::var_os(WRAPPER_MARKER).is_some() {
    return Some(run_rustc());
  }
  if std::env::var_os(RUSTDOC_WRAPPER_MARKER).is_some() {
    return Some(run_rustdoc());
  }
  if is_unmarked_recursive_wrapper_invocation() {
    eprintln!("cargo-rail rustc wrapper: recursive cargo-rail rustc wrapper configuration");
    return Some(2);
  }
  None
}

fn run_cache_disabled(context: Option<crate::compiler::native_cache::NativeCacheContext>) -> i32 {
  if let Some(context) = context
    && let Err(error) = context.activate()
  {
    eprintln!("cargo-rail compiler cache wrapper: {error}");
    return 2;
  }
  let mut args = std::env::args_os().skip(1);
  let Some(program) = args.next() else {
    eprintln!("cargo-rail compiler cache wrapper: missing compiler executable");
    return 1;
  };
  let arguments = args.collect::<Vec<_>>();
  let mut command = Command::new(&program);
  command.args(&arguments).env_remove(CACHE_WRAPPER_MARKER);
  match crate::compiler::native_cache::configure_outer(&program, &arguments, &mut command) {
    crate::compiler::native_cache::OuterCacheAction::Hit(exit_code) => exit_code,
    crate::compiler::native_cache::OuterCacheAction::Store(recorder) => {
      crate::compiler::native_cache::run_and_store(command, recorder, "cargo-rail compiler cache wrapper")
    }
    crate::compiler::native_cache::OuterCacheAction::Execute => {
      run_transparently(command, "cargo-rail compiler cache wrapper")
    }
  }
}

#[cfg(unix)]
fn run_transparently(mut command: Command, context: &str) -> i32 {
  use std::os::unix::process::CommandExt as _;

  let error = command.exec();
  eprintln!("{context}: failed to execute compiler: {error}");
  1
}

#[cfg(not(unix))]
fn run_transparently(mut command: Command, context: &str) -> i32 {
  match command.status() {
    Ok(status) => status.code().unwrap_or(1),
    Err(error) => {
      eprintln!("{context}: failed to execute compiler: {error}");
      1
    }
  }
}

fn is_unmarked_recursive_wrapper_invocation() -> bool {
  std::env::var_os("CARGO").is_some()
    && std::env::args_os().nth(1).is_some_and(|program| {
      Path::new(&program)
        .file_stem()
        .and_then(std::ffi::OsStr::to_str)
        .is_some_and(|name| name.eq_ignore_ascii_case("rustc"))
    })
}

fn run_rustc() -> i32 {
  let mut args = std::env::args_os().skip(1);
  let Some(rustc) = args.next() else {
    eprintln!("cargo-rail rustc wrapper: missing rustc executable");
    return 1;
  };

  let remaining: Vec<OsString> = args.collect();
  let inner_wrapper = std::env::var_os(INNER_WRAPPER_ENV);
  let recorder = (!is_rustc_information_request(&remaining))
    .then(|| {
      std::env::var_os(OBSERVATION_DIRECTORY_ENV)
        .zip(std::env::var_os(OBSERVATION_SOURCE_ROOT_ENV))
        .and_then(|(directory, source_root)| {
          crate::compiler::observation::begin_invocation(
            &PathBuf::from(directory),
            &PathBuf::from(source_root),
            &rustc,
            &remaining,
          )
          .ok()
        })
    })
    .flatten();
  let mut command = rustc_command(&rustc, None, inner_wrapper.as_deref());
  command.args(&remaining);
  if std::env::var_os(OBSERVATION_ONLY_ENV).is_none() {
    command.arg("--warn=unused-crate-dependencies");
  }
  command
    .env_remove(WRAPPER_MARKER)
    .env_remove(INNER_WRAPPER_ENV)
    .env_remove(OBSERVATION_DIRECTORY_ENV)
    .env_remove(OBSERVATION_SOURCE_ROOT_ENV)
    .env_remove(OBSERVATION_ONLY_ENV);
  crate::compiler::native_cache::remove_private_environment(&mut command);

  let status = command.status();

  if let Some(recorder) = recorder {
    let _ = recorder.finish(status.as_ref().is_ok_and(std::process::ExitStatus::success));
  }

  match status {
    Ok(status) => status.code().unwrap_or(1),
    Err(error) => {
      eprintln!("cargo-rail rustc wrapper: failed to execute compiler: {error}");
      1
    }
  }
}

fn is_rustc_information_request(arguments: &[OsString]) -> bool {
  arguments.iter().any(|argument| {
    matches!(
      argument.to_str(),
      Some("-h" | "--help" | "-V" | "--version" | "-vV" | "--print")
    ) || argument
      .to_str()
      .is_some_and(|argument| argument.starts_with("--print="))
  })
}

fn run_rustdoc() -> i32 {
  let Some(rustdoc) = std::env::var_os(INNER_RUSTDOC_ENV) else {
    eprintln!("cargo-rail rustdoc proxy: missing selected rustdoc executable");
    return 1;
  };
  let original_arguments = std::env::args_os().skip(1).collect::<Vec<_>>();
  let records_compilation = !is_rustdoc_information_request(&original_arguments);
  let arguments = rustdoc_observation_arguments(&rustdoc, original_arguments);
  let recorder = if records_compilation {
    std::env::var_os(OBSERVATION_DIRECTORY_ENV)
      .zip(std::env::var_os(OBSERVATION_SOURCE_ROOT_ENV))
      .and_then(|(directory, source_root)| {
        crate::compiler::observation::begin_rustdoc_invocation(
          &PathBuf::from(directory),
          &PathBuf::from(source_root),
          &arguments,
        )
        .ok()
      })
  } else {
    None
  };
  let status = Command::new(&rustdoc)
    .args(&arguments)
    .env("RUSTDOC", &rustdoc)
    .env_remove(RUSTDOC_WRAPPER_MARKER)
    .env_remove(INNER_RUSTDOC_ENV)
    .env_remove(OBSERVATION_DIRECTORY_ENV)
    .env_remove(OBSERVATION_SOURCE_ROOT_ENV)
    .status();

  if let Some(recorder) = recorder {
    let _ = recorder.finish(status.as_ref().is_ok_and(std::process::ExitStatus::success));
  }

  match status {
    Ok(status) => status.code().unwrap_or(1),
    Err(error) => {
      eprintln!("cargo-rail rustdoc proxy: failed to execute rustdoc: {error}");
      1
    }
  }
}

fn rustdoc_observation_arguments(rustdoc: &std::ffi::OsStr, mut arguments: Vec<OsString>) -> Vec<OsString> {
  if is_rustdoc_information_request(&arguments)
    || arguments
      .iter()
      .any(|argument| argument.as_encoded_bytes().starts_with(b"@"))
    || arguments
      .iter()
      .any(|argument| matches!(argument.to_str(), Some("--test" | "--check")))
    || uses_non_html_output_format(&arguments)
    || !is_cargo_rustdoc_crate_invocation(&arguments)
  {
    return arguments;
  }

  let mut index = 0usize;
  while index < arguments.len() {
    if arguments[index] == "--emit" {
      if let Some(value) = arguments.get_mut(index + 1)
        && let Some(value) = value.to_str()
        && let Some(extended) = rustdoc_emit_with_dep_info(rustdoc, value)
      {
        arguments[index + 1] = extended.into();
      }
      return arguments;
    }
    if let Some(value) = arguments[index]
      .to_str()
      .and_then(|argument| argument.strip_prefix("--emit="))
    {
      if let Some(extended) = rustdoc_emit_with_dep_info(rustdoc, value) {
        arguments[index] = format!("--emit={extended}").into();
      }
      return arguments;
    }
    index += 1;
  }

  if let Some(modes) = rustdoc_default_emit_modes(rustdoc) {
    arguments.push(format!("--emit={modes}").into());
  }
  arguments
}

fn is_cargo_rustdoc_crate_invocation(arguments: &[OsString]) -> bool {
  has_option_value(arguments, "--crate-name")
    && (has_option_value(arguments, "-o")
      || has_option_value(arguments, "--out-dir")
      || has_option_value(arguments, "--output"))
    && arguments.iter().any(|argument| {
      argument
        .to_str()
        .is_some_and(|argument| !argument.starts_with('-') && argument.ends_with(".rs"))
    })
}

fn has_option_value(arguments: &[OsString], option: &str) -> bool {
  arguments.iter().enumerate().any(|(index, argument)| {
    (argument == option && arguments.get(index + 1).is_some_and(|value| !value.is_empty()))
      || argument
        .to_str()
        .and_then(|argument| argument.strip_prefix(option))
        .is_some_and(|value| value.starts_with('=') && value.len() > 1)
  })
}

fn is_rustdoc_information_request(arguments: &[OsString]) -> bool {
  arguments
    .iter()
    .any(|argument| matches!(argument.to_str(), Some("-h" | "--help" | "-V" | "--version" | "-vV")))
}

fn uses_non_html_output_format(arguments: &[OsString]) -> bool {
  arguments.iter().enumerate().any(|(index, argument)| {
    let Some(argument) = argument.to_str() else {
      return true;
    };
    match argument {
      "-w" | "--output-format" => arguments
        .get(index + 1)
        .and_then(|value| value.to_str())
        .is_none_or(|value| value != "html"),
      _ => argument
        .strip_prefix("--output-format=")
        .is_some_and(|value| value != "html"),
    }
  })
}

fn rustdoc_emit_with_dep_info(rustdoc: &std::ffi::OsStr, value: &str) -> Option<String> {
  if emit_contains_dep_info(value) {
    return extend_rustdoc_emit(value, "");
  }
  extend_rustdoc_emit(value, &rustdoc_default_emit_modes(rustdoc)?)
}

fn emit_contains_dep_info(value: &str) -> bool {
  value
    .split(',')
    .map(str::trim)
    .any(|mode| mode.split_once('=').map_or(mode, |(name, _)| name) == "dep-info")
}

fn extend_rustdoc_emit(value: &str, supported: &str) -> Option<String> {
  let mut modes = value.split(',').map(str::trim).collect::<Vec<_>>();
  if modes.is_empty() || modes.iter().any(|mode| mode.is_empty()) {
    return None;
  }
  if emit_contains_dep_info(value) {
    return Some(modes.join(","));
  }
  let supported = supported.split(',').collect::<Vec<_>>();
  if !modes.iter().all(|mode| {
    let name = mode.split_once('=').map_or(*mode, |(name, _)| name);
    supported.contains(&name)
  }) {
    return None;
  }
  modes.push("dep-info");
  Some(modes.join(","))
}

fn rustdoc_default_emit_modes(rustdoc: &std::ffi::OsStr) -> Option<String> {
  let output = Command::new(rustdoc)
    .arg("--help")
    .env("RUSTDOC", rustdoc)
    .env_remove(RUSTDOC_WRAPPER_MARKER)
    .env_remove(INNER_RUSTDOC_ENV)
    .env_remove(OBSERVATION_DIRECTORY_ENV)
    .env_remove(OBSERVATION_SOURCE_ROOT_ENV)
    .output()
    .ok()?;
  if !output.status.success() {
    return None;
  }
  rustdoc_emit_modes_from_help(&String::from_utf8(output.stdout).ok()?)
}

fn rustdoc_emit_modes_from_help(help: &str) -> Option<String> {
  let modes = help.lines().find_map(|line| {
    let (_, remainder) = line.split_once("--emit [")?;
    remainder.split_once(']').map(|(modes, _)| modes)
  })?;
  let modes = modes.split(',').map(str::trim).collect::<Vec<_>>();
  matches!(
    modes.as_slice(),
    ["toolchain-shared-resources", "invocation-specific", "dep-info"]
      | ["html-static-files", "html-non-static-files", "dep-info"]
  )
  .then(|| modes.join(","))
}

#[cfg(test)]
mod tests {
  use std::ffi::OsStr;

  use super::*;

  #[test]
  fn rustc_information_requests_do_not_become_compilation_units() {
    for arguments in [
      vec![OsString::from("-vV")],
      vec![OsString::from("--print"), OsString::from("cfg")],
      vec![OsString::from("--print=file-names")],
    ] {
      assert!(is_rustc_information_request(&arguments));
    }
    assert!(!is_rustc_information_request(&[
      OsString::from("--crate-name"),
      OsString::from("unit"),
      OsString::from("src/lib.rs"),
    ]));
  }

  #[test]
  fn cache_wrapper_plan_never_layers_over_existing_or_sccache_wrappers() {
    assert_eq!(
      CacheWrapperPlan::for_chain(None, None),
      CacheWrapperPlan::DisabledPassThrough
    );
    assert_eq!(
      CacheWrapperPlan::for_chain(Some(OsStr::new("sccache")), None),
      CacheWrapperPlan::PreserveSccache
    );
    assert_eq!(
      CacheWrapperPlan::for_chain(None, Some(OsStr::new("/tools/SCCACHE.exe"))),
      CacheWrapperPlan::PreserveSccache
    );
    assert_eq!(
      CacheWrapperPlan::for_chain(Some(OsStr::new("custom-wrapper")), None),
      CacheWrapperPlan::PreserveExisting
    );
    assert!(!CacheWrapperPlan::PreserveSccache.installs_cargo_rail());
    assert!(!CacheWrapperPlan::PreserveExisting.installs_cargo_rail());
  }

  #[test]
  fn rustdoc_emit_discovery_accepts_both_msrv_and_current_names() {
    assert_eq!(
      rustdoc_emit_modes_from_help("        --emit [toolchain-shared-resources,invocation-specific,dep-info]\n"),
      Some("toolchain-shared-resources,invocation-specific,dep-info".to_string())
    );
    assert_eq!(
      rustdoc_emit_modes_from_help("        --emit [html-static-files,html-non-static-files,dep-info]\n"),
      Some("html-static-files,html-non-static-files,dep-info".to_string())
    );
    assert_eq!(rustdoc_emit_modes_from_help("        --emit [html]\n"), None);
    assert_eq!(
      rustdoc_emit_modes_from_help("        --emit [html-static-files,html-non-static-files,json,dep-info]\n"),
      None
    );
  }

  #[test]
  fn existing_rustdoc_emit_modes_gain_dep_info_without_losing_outputs() {
    assert_eq!(
      extend_rustdoc_emit(
        "html-static-files,html-non-static-files",
        "html-static-files,html-non-static-files,dep-info"
      ),
      Some("html-static-files,html-non-static-files,dep-info".to_string())
    );
    assert_eq!(
      rustdoc_emit_with_dep_info(OsStr::new("missing-rustdoc"), "html-static-files,dep-info"),
      Some("html-static-files,dep-info".to_string())
    );
    assert_eq!(
      rustdoc_emit_with_dep_info(OsStr::new("missing-rustdoc"), "dep-info=unit.d"),
      Some("dep-info=unit.d".to_string())
    );
    assert_eq!(
      extend_rustdoc_emit("json", "html-static-files,html-non-static-files,dep-info"),
      None
    );
    assert_eq!(extend_rustdoc_emit("", "html-static-files,dep-info"), None);
  }

  #[test]
  fn non_html_and_non_rendering_rustdoc_invocations_remain_unchanged() {
    let original = vec!["--crate-name".into(), "unit".into(), "--output-format=json".into()];
    assert_eq!(
      rustdoc_observation_arguments(OsStr::new("rustdoc"), original.clone()),
      original
    );

    let original = vec!["--crate-name".into(), "unit".into(), "--test".into()];
    assert_eq!(
      rustdoc_observation_arguments(OsStr::new("rustdoc"), original.clone()),
      original
    );

    let original = vec!["@arguments".into()];
    assert_eq!(
      rustdoc_observation_arguments(OsStr::new("rustdoc"), original.clone()),
      original
    );

    let original = vec!["README.md".into(), "-o".into(), "doc".into()];
    assert_eq!(
      rustdoc_observation_arguments(OsStr::new("rustdoc"), original.clone()),
      original
    );
  }
}