Skip to main content

rustdoc/
doctest.rs

1mod extracted;
2mod make;
3mod markdown;
4mod runner;
5mod rust;
6
7use std::fs::File;
8use std::hash::{Hash, Hasher};
9use std::io::{self, Write};
10use std::path::{Path, PathBuf};
11use std::process::{self, Command, Stdio};
12use std::str::FromStr;
13use std::sync::atomic::{AtomicUsize, Ordering};
14use std::sync::{Arc, Mutex};
15use std::time::{Duration, Instant};
16use std::{panic, str};
17
18pub(crate) use make::{BuildDocTestBuilder, DocTestBuilder};
19pub(crate) use markdown::test as test_markdown;
20use proc_macro2::{TokenStream, TokenTree};
21use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxHasher, FxIndexMap, FxIndexSet};
22use rustc_errors::emitter::HumanReadableErrorType;
23use rustc_errors::{ColorConfig, DiagCtxtHandle};
24use rustc_hir::attrs::AttributeKind;
25use rustc_hir::def_id::LOCAL_CRATE;
26use rustc_hir::{Attribute, CRATE_HIR_ID};
27use rustc_interface::interface;
28use rustc_middle::ty::TyCtxt;
29use rustc_session::config::{self, CrateType, ErrorOutputType, Input};
30use rustc_session::lint;
31use rustc_span::edition::Edition;
32use rustc_span::{FileName, RemapPathScopeComponents, Span};
33use rustc_target::spec::{Target, TargetTuple};
34use tempfile::{Builder as TempFileBuilder, TempDir};
35use tracing::debug;
36
37use self::rust::HirCollector;
38use crate::config::{MergeDoctests, Options as RustdocOptions, OutputFormat};
39use crate::html::markdown::{ErrorCodes, Ignore, LangString, MdRelLine};
40use crate::lint::init_lints;
41
42/// Type used to display times (compilation and total) information for merged doctests.
43struct MergedDoctestTimes {
44    total_time: Instant,
45    /// Total time spent compiling all merged doctests.
46    compilation_time: Duration,
47    /// This field is used to keep track of how many merged doctests we (tried to) compile.
48    added_compilation_times: usize,
49}
50
51impl MergedDoctestTimes {
52    fn new() -> Self {
53        Self {
54            total_time: Instant::now(),
55            compilation_time: Duration::default(),
56            added_compilation_times: 0,
57        }
58    }
59
60    fn add_compilation_time(&mut self, duration: Duration) {
61        self.compilation_time += duration;
62        self.added_compilation_times += 1;
63    }
64
65    /// Returns `(total_time, compilation_time)`.
66    fn times_in_secs(&self) -> Option<(f64, f64)> {
67        // If no merged doctest was compiled, then there is nothing to display since the numbers
68        // displayed by `libtest` for standalone tests are already accurate (they include both
69        // compilation and runtime).
70        if self.added_compilation_times == 0 {
71            return None;
72        }
73        Some((self.total_time.elapsed().as_secs_f64(), self.compilation_time.as_secs_f64()))
74    }
75}
76
77/// Options that apply to all doctests in a crate or Markdown file (for `rustdoc foo.md`).
78#[derive(Clone)]
79pub(crate) struct GlobalTestOptions {
80    /// Name of the crate (for regular `rustdoc`) or Markdown file (for `rustdoc foo.md`).
81    pub(crate) crate_name: String,
82    /// Whether to disable the default `extern crate my_crate;` when creating doctests.
83    pub(crate) no_crate_inject: bool,
84    /// Whether inserting extra indent spaces in code block,
85    /// default is `false`, only `true` for generating code link of Rust playground
86    pub(crate) insert_indent_space: bool,
87    /// Path to file containing arguments for the invocation of rustc.
88    pub(crate) args_file: PathBuf,
89}
90
91pub(crate) fn generate_args_file(file_path: &Path, options: &RustdocOptions) -> Result<(), String> {
92    let mut file = File::create(file_path)
93        .map_err(|error| format!("failed to create args file: {error:?}"))?;
94
95    // We now put the common arguments into the file we created.
96    let mut content = vec![];
97
98    for cfg in &options.cfgs {
99        content.push(format!("--cfg={cfg}"));
100    }
101    for check_cfg in &options.check_cfgs {
102        content.push(format!("--check-cfg={check_cfg}"));
103    }
104
105    for lib_str in &options.lib_strs {
106        content.push(format!("-L{lib_str}"));
107    }
108    for extern_str in &options.extern_strs {
109        content.push(format!("--extern={extern_str}"));
110    }
111    content.push("-Ccodegen-units=1".to_string());
112    for codegen_options_str in &options.codegen_options_strs {
113        content.push(format!("-C{codegen_options_str}"));
114    }
115    for unstable_option_str in &options.unstable_opts_strs {
116        content.push(format!("-Z{unstable_option_str}"));
117    }
118
119    content.extend(options.doctest_build_args.clone());
120
121    let content = content.join("\n");
122
123    file.write_all(content.as_bytes())
124        .map_err(|error| format!("failed to write arguments to temporary file: {error:?}"))?;
125    Ok(())
126}
127
128fn get_doctest_dir(opts: &RustdocOptions) -> io::Result<TempDir> {
129    let mut builder = TempFileBuilder::new();
130    builder.prefix("rustdoctest");
131    if opts.codegen_options.save_temps {
132        builder.disable_cleanup(true);
133    }
134    builder.tempdir()
135}
136
137pub(crate) fn run(dcx: DiagCtxtHandle<'_>, input: Input, options: RustdocOptions) {
138    let invalid_codeblock_attributes_name = crate::lint::INVALID_CODEBLOCK_ATTRIBUTES.name;
139
140    // See core::create_config for what's going on here.
141    let allowed_lints = vec![
142        invalid_codeblock_attributes_name.to_owned(),
143        lint::builtin::UNKNOWN_LINTS.name.to_owned(),
144        lint::builtin::RENAMED_AND_REMOVED_LINTS.name.to_owned(),
145    ];
146
147    let (lint_opts, lint_caps) = init_lints(allowed_lints, options.lint_opts.clone(), |lint| {
148        if lint.name == invalid_codeblock_attributes_name {
149            None
150        } else {
151            Some((lint.name_lower(), lint::Allow))
152        }
153    });
154
155    debug!(?lint_opts);
156
157    let crate_types =
158        if options.proc_macro_crate { vec![CrateType::ProcMacro] } else { vec![CrateType::Rlib] };
159
160    let sessopts = config::Options {
161        sysroot: options.sysroot.clone(),
162        search_paths: options.libs.clone(),
163        crate_types,
164        lint_opts,
165        lint_cap: Some(options.lint_cap.unwrap_or(lint::Forbid)),
166        cg: options.codegen_options.clone(),
167        externs: options.externs.clone(),
168        unstable_features: options.unstable_features,
169        actually_rustdoc: true,
170        edition: options.edition,
171        target_triple: options.target.clone(),
172        crate_name: options.crate_name.clone(),
173        remap_path_prefix: options.remap_path_prefix.clone(),
174        unstable_opts: options.unstable_opts.clone(),
175        error_format: options.error_format.clone(),
176        target_modifiers: options.target_modifiers.clone(),
177        ..config::Options::default()
178    };
179
180    let mut cfgs = options.cfgs.clone();
181    cfgs.push("doc".to_owned());
182    cfgs.push("doctest".to_owned());
183    let config = interface::Config {
184        opts: sessopts,
185        crate_cfg: cfgs,
186        crate_check_cfg: options.check_cfgs.clone(),
187        input: input.clone(),
188        output_file: None,
189        output_dir: None,
190        file_loader: None,
191        locale_resources: rustc_driver::DEFAULT_LOCALE_RESOURCES.to_vec(),
192        lint_caps,
193        psess_created: None,
194        hash_untracked_state: None,
195        register_lints: Some(Box::new(crate::lint::register_lints)),
196        override_queries: None,
197        extra_symbols: Vec::new(),
198        make_codegen_backend: None,
199        ice_file: None,
200        using_internal_features: &rustc_driver::USING_INTERNAL_FEATURES,
201    };
202
203    let externs = options.externs.clone();
204    let json_unused_externs = options.json_unused_externs;
205
206    let temp_dir = match get_doctest_dir(&options)
207        .map_err(|error| format!("failed to create temporary directory: {error:?}"))
208    {
209        Ok(temp_dir) => temp_dir,
210        Err(error) => return crate::wrap_return(dcx, Err(error)),
211    };
212    let args_path = temp_dir.path().join("rustdoc-cfgs");
213    crate::wrap_return(dcx, generate_args_file(&args_path, &options));
214
215    let extract_doctests = options.output_format == OutputFormat::Doctest;
216    let save_temps = options.codegen_options.save_temps;
217    let result = interface::run_compiler(config, |compiler| {
218        let krate = rustc_interface::passes::parse(&compiler.sess);
219
220        let collector = rustc_interface::create_and_enter_global_ctxt(compiler, krate, |tcx| {
221            let crate_name = tcx.crate_name(LOCAL_CRATE).to_string();
222            let opts = scrape_test_config(tcx, crate_name, args_path);
223
224            let hir_collector = HirCollector::new(
225                ErrorCodes::from(compiler.sess.opts.unstable_features.is_nightly_build()),
226                tcx,
227            );
228            let tests = hir_collector.collect_crate();
229            if extract_doctests {
230                let mut collector = extracted::ExtractedDocTests::new();
231                tests.into_iter().for_each(|t| collector.add_test(t, &opts, &options));
232
233                let stdout = std::io::stdout();
234                let mut stdout = stdout.lock();
235                if let Err(error) = serde_json::ser::to_writer(&mut stdout, &collector) {
236                    eprintln!();
237                    Err(format!("Failed to generate JSON output for doctests: {error:?}"))
238                } else {
239                    Ok(None)
240                }
241            } else {
242                let mut collector = CreateRunnableDocTests::new(options, opts);
243                tests.into_iter().for_each(|t| collector.add_test(t, Some(compiler.sess.dcx())));
244
245                Ok(Some(collector))
246            }
247        });
248        compiler.sess.dcx().abort_if_errors();
249
250        collector
251    });
252
253    let CreateRunnableDocTests {
254        standalone_tests,
255        mergeable_tests,
256        rustdoc_options,
257        opts,
258        unused_extern_reports,
259        compiling_test_count,
260        ..
261    } = match result {
262        Ok(Some(collector)) => collector,
263        Ok(None) => return,
264        Err(error) => {
265            eprintln!("{error}");
266            // Since some files in the temporary folder are still owned and alive, we need
267            // to manually remove the folder.
268            if !save_temps {
269                let _ = std::fs::remove_dir_all(temp_dir.path());
270            }
271            std::process::exit(1);
272        }
273    };
274
275    run_tests(
276        dcx,
277        opts,
278        &rustdoc_options,
279        &unused_extern_reports,
280        standalone_tests,
281        mergeable_tests,
282        Some(temp_dir),
283    );
284
285    let compiling_test_count = compiling_test_count.load(Ordering::SeqCst);
286
287    // Collect and warn about unused externs, but only if we've gotten
288    // reports for each doctest
289    if json_unused_externs.is_enabled() {
290        let unused_extern_reports: Vec<_> =
291            std::mem::take(&mut unused_extern_reports.lock().unwrap());
292        if unused_extern_reports.len() == compiling_test_count {
293            let extern_names =
294                externs.iter().map(|(name, _)| name).collect::<FxIndexSet<&String>>();
295            let mut unused_extern_names = unused_extern_reports
296                .iter()
297                .map(|uexts| uexts.unused_extern_names.iter().collect::<FxIndexSet<&String>>())
298                .fold(extern_names, |uextsa, uextsb| {
299                    uextsa.intersection(&uextsb).copied().collect::<FxIndexSet<&String>>()
300                })
301                .iter()
302                .map(|v| (*v).clone())
303                .collect::<Vec<String>>();
304            unused_extern_names.sort();
305            // Take the most severe lint level
306            let lint_level = unused_extern_reports
307                .iter()
308                .map(|uexts| uexts.lint_level.as_str())
309                .max_by_key(|v| match *v {
310                    "warn" => 1,
311                    "deny" => 2,
312                    "forbid" => 3,
313                    // The allow lint level is not expected,
314                    // as if allow is specified, no message
315                    // is to be emitted.
316                    v => unreachable!("Invalid lint level '{v}'"),
317                })
318                .unwrap_or("warn")
319                .to_string();
320            let uext = UnusedExterns { lint_level, unused_extern_names };
321            let unused_extern_json = serde_json::to_string(&uext).unwrap();
322            eprintln!("{unused_extern_json}");
323        }
324    }
325}
326
327pub(crate) fn run_tests(
328    dcx: DiagCtxtHandle<'_>,
329    opts: GlobalTestOptions,
330    rustdoc_options: &Arc<RustdocOptions>,
331    unused_extern_reports: &Arc<Mutex<Vec<UnusedExterns>>>,
332    mut standalone_tests: Vec<test::TestDescAndFn>,
333    mergeable_tests: FxIndexMap<MergeableTestKey, Vec<(DocTestBuilder, ScrapedDocTest)>>,
334    // We pass this argument so we can drop it manually before using `exit`.
335    mut temp_dir: Option<TempDir>,
336) {
337    let mut test_args = Vec::with_capacity(rustdoc_options.test_args.len() + 1);
338    test_args.insert(0, "rustdoctest".to_string());
339    test_args.extend_from_slice(&rustdoc_options.test_args);
340    if rustdoc_options.no_capture {
341        test_args.push("--no-capture".to_string());
342    }
343
344    let mut nb_errors = 0;
345    let mut ran_edition_tests = 0;
346    let mut times = MergedDoctestTimes::new();
347    let target_str = rustdoc_options.target.to_string();
348
349    for (MergeableTestKey { edition, global_crate_attrs_hash }, mut doctests) in mergeable_tests {
350        if doctests.is_empty() {
351            continue;
352        }
353        doctests.sort_by(|(_, a), (_, b)| a.name.cmp(&b.name));
354
355        let mut tests_runner = runner::DocTestRunner::new();
356
357        let rustdoc_test_options = IndividualTestOptions::new(
358            rustdoc_options,
359            &Some(format!("merged_doctest_{edition}_{global_crate_attrs_hash}")),
360            PathBuf::from(format!("doctest_{edition}_{global_crate_attrs_hash}.rs")),
361        );
362
363        for (doctest, scraped_test) in &doctests {
364            tests_runner.add_test(doctest, scraped_test, &target_str);
365        }
366        let (duration, ret) = tests_runner.run_merged_tests(
367            rustdoc_test_options,
368            edition,
369            &opts,
370            &test_args,
371            rustdoc_options,
372        );
373        times.add_compilation_time(duration);
374        if let Ok(success) = ret {
375            ran_edition_tests += 1;
376            if !success {
377                nb_errors += 1;
378            }
379            continue;
380        }
381
382        if rustdoc_options.merge_doctests == MergeDoctests::Always {
383            let mut diag = dcx.struct_fatal("failed to merge doctests");
384            diag.note("requested explicitly on the command line with `--merge-doctests=yes`");
385            diag.emit();
386        }
387
388        // We failed to compile all compatible tests as one so we push them into the
389        // `standalone_tests` doctests.
390        debug!("Failed to compile compatible doctests for edition {} all at once", edition);
391        for (doctest, scraped_test) in doctests {
392            doctest.generate_unique_doctest(
393                &scraped_test.text,
394                scraped_test.langstr.test_harness,
395                &opts,
396                Some(&opts.crate_name),
397            );
398            standalone_tests.push(generate_test_desc_and_fn(
399                doctest,
400                scraped_test,
401                opts.clone(),
402                Arc::clone(rustdoc_options),
403                unused_extern_reports.clone(),
404            ));
405        }
406    }
407
408    // We need to call `test_main` even if there is no doctest to run to get the output
409    // `running 0 tests...`.
410    if ran_edition_tests == 0 || !standalone_tests.is_empty() {
411        standalone_tests.sort_by(|a, b| a.desc.name.as_slice().cmp(b.desc.name.as_slice()));
412        test::test_main_with_exit_callback(&test_args, standalone_tests, None, || {
413            let times = times.times_in_secs();
414            // We ensure temp dir destructor is called.
415            std::mem::drop(temp_dir.take());
416            if let Some((total_time, compilation_time)) = times {
417                test::print_merged_doctests_times(&test_args, total_time, compilation_time);
418            }
419        });
420    } else {
421        // If the first condition branch exited successfully, `test_main_with_exit_callback` will
422        // not exit the process. So to prevent displaying the times twice, we put it behind an
423        // `else` condition.
424        if let Some((total_time, compilation_time)) = times.times_in_secs() {
425            test::print_merged_doctests_times(&test_args, total_time, compilation_time);
426        }
427    }
428    // We ensure temp dir destructor is called.
429    std::mem::drop(temp_dir);
430    if nb_errors != 0 {
431        std::process::exit(test::ERROR_EXIT_CODE);
432    }
433}
434
435// Look for `#![doc(test(no_crate_inject))]`, used by crates in the std facade.
436fn scrape_test_config(
437    tcx: TyCtxt<'_>,
438    crate_name: String,
439    args_file: PathBuf,
440) -> GlobalTestOptions {
441    let mut opts = GlobalTestOptions {
442        crate_name,
443        no_crate_inject: false,
444        insert_indent_space: false,
445        args_file,
446    };
447
448    let source_map = tcx.sess.source_map();
449    'main: for attr in tcx.hir_attrs(CRATE_HIR_ID) {
450        let Attribute::Parsed(AttributeKind::Doc(d)) = attr else { continue };
451        for attr_span in &d.test_attrs {
452            // FIXME: This is ugly, remove when `test_attrs` has been ported to new attribute API.
453            if let Ok(snippet) = source_map.span_to_snippet(*attr_span)
454                && let Ok(stream) = TokenStream::from_str(&snippet)
455            {
456                // NOTE: `test(attr(..))` is handled when discovering the individual tests
457                if stream.into_iter().any(|token| {
458                    matches!(
459                        token,
460                        TokenTree::Ident(i) if i.to_string() == "no_crate_inject",
461                    )
462                }) {
463                    opts.no_crate_inject = true;
464                    break 'main;
465                }
466            }
467        }
468    }
469
470    opts
471}
472
473/// Documentation test failure modes.
474enum TestFailure {
475    /// The test failed to compile.
476    CompileError,
477    /// The test is marked `compile_fail` but compiled successfully.
478    UnexpectedCompilePass,
479    /// The test failed to compile (as expected) but the compiler output did not contain all
480    /// expected error codes.
481    MissingErrorCodes(Vec<String>),
482    /// The test binary was unable to be executed.
483    ExecutionError(io::Error),
484    /// The test binary exited with a non-zero exit code.
485    ///
486    /// This typically means an assertion in the test failed or another form of panic occurred.
487    ExecutionFailure(process::Output),
488    /// The test is marked `should_panic` but the test binary executed successfully.
489    UnexpectedRunPass,
490}
491
492enum DirState {
493    Temp(TempDir),
494    Perm(PathBuf),
495}
496
497impl DirState {
498    fn path(&self) -> &std::path::Path {
499        match self {
500            DirState::Temp(t) => t.path(),
501            DirState::Perm(p) => p.as_path(),
502        }
503    }
504}
505
506// NOTE: Keep this in sync with the equivalent structs in rustc
507// and cargo.
508// We could unify this struct the one in rustc but they have different
509// ownership semantics, so doing so would create wasteful allocations.
510#[derive(serde::Serialize, serde::Deserialize)]
511pub(crate) struct UnusedExterns {
512    /// Lint level of the unused_crate_dependencies lint
513    lint_level: String,
514    /// List of unused externs by their names.
515    unused_extern_names: Vec<String>,
516}
517
518fn add_exe_suffix(input: String, target: &TargetTuple) -> String {
519    let exe_suffix = match target {
520        TargetTuple::TargetTuple(_) => Target::expect_builtin(target).options.exe_suffix,
521        TargetTuple::TargetJson { contents, .. } => {
522            Target::from_json(contents).unwrap().0.options.exe_suffix
523        }
524    };
525    input + &exe_suffix
526}
527
528fn wrapped_rustc_command(rustc_wrappers: &[PathBuf], rustc_binary: &Path) -> Command {
529    let mut args = rustc_wrappers.iter().map(PathBuf::as_path).chain([rustc_binary]);
530
531    let exe = args.next().expect("unable to create rustc command");
532    let mut command = Command::new(exe);
533    for arg in args {
534        command.arg(arg);
535    }
536
537    command
538}
539
540/// Information needed for running a bundle of doctests.
541///
542/// This data structure contains the "full" test code, including the wrappers
543/// (if multiple doctests are merged), `main` function,
544/// and everything needed to calculate the compiler's command-line arguments.
545/// The `# ` prefix on boring lines has also been stripped.
546pub(crate) struct RunnableDocTest {
547    full_test_code: String,
548    full_test_line_offset: usize,
549    test_opts: IndividualTestOptions,
550    global_opts: GlobalTestOptions,
551    langstr: LangString,
552    line: usize,
553    edition: Edition,
554    no_run: bool,
555    merged_test_code: Option<String>,
556}
557
558impl RunnableDocTest {
559    fn path_for_merged_doctest_bundle(&self) -> PathBuf {
560        self.test_opts.outdir.path().join(format!("doctest_bundle_{}.rs", self.edition))
561    }
562    fn path_for_merged_doctest_runner(&self) -> PathBuf {
563        self.test_opts.outdir.path().join(format!("doctest_runner_{}.rs", self.edition))
564    }
565    fn is_multiple_tests(&self) -> bool {
566        self.merged_test_code.is_some()
567    }
568}
569
570/// Execute a `RunnableDoctest`.
571///
572/// This is the function that calculates the compiler command line, invokes the compiler, then
573/// invokes the test or tests in a separate executable (if applicable).
574///
575/// Returns a tuple containing the `Duration` of the compilation and the `Result` of the test.
576fn run_test(
577    doctest: RunnableDocTest,
578    rustdoc_options: &RustdocOptions,
579    supports_color: bool,
580    report_unused_externs: impl Fn(UnusedExterns),
581) -> (Duration, Result<(), TestFailure>) {
582    let langstr = &doctest.langstr;
583    // Make sure we emit well-formed executable names for our target.
584    let rust_out = add_exe_suffix("rust_out".to_owned(), &rustdoc_options.target);
585    let output_file = doctest.test_opts.outdir.path().join(rust_out);
586    let instant = Instant::now();
587
588    // Common arguments used for compiling the doctest runner.
589    // On merged doctests, the compiler is invoked twice: once for the test code itself,
590    // and once for the runner wrapper (which needs to use `#![feature]` on stable).
591    let mut compiler_args = vec![];
592
593    compiler_args.push(format!("@{}", doctest.global_opts.args_file.display()));
594
595    let sysroot = &rustdoc_options.sysroot;
596    if let Some(explicit_sysroot) = &sysroot.explicit {
597        compiler_args.push(format!("--sysroot={}", explicit_sysroot.display()));
598    }
599
600    compiler_args.extend_from_slice(&["--edition".to_owned(), doctest.edition.to_string()]);
601    if langstr.test_harness {
602        compiler_args.push("--test".to_owned());
603    }
604    if rustdoc_options.json_unused_externs.is_enabled() && !langstr.compile_fail {
605        compiler_args.push("--error-format=json".to_owned());
606        compiler_args.extend_from_slice(&["--json".to_owned(), "unused-externs".to_owned()]);
607        compiler_args.extend_from_slice(&["-W".to_owned(), "unused_crate_dependencies".to_owned()]);
608        compiler_args.extend_from_slice(&["-Z".to_owned(), "unstable-options".to_owned()]);
609    }
610
611    if doctest.no_run && !langstr.compile_fail && rustdoc_options.persist_doctests.is_none() {
612        // FIXME: why does this code check if it *shouldn't* persist doctests
613        //        -- shouldn't it be the negation?
614        compiler_args.push("--emit=metadata".to_owned());
615    }
616    compiler_args.extend_from_slice(&[
617        "--target".to_owned(),
618        match &rustdoc_options.target {
619            TargetTuple::TargetTuple(s) => s.clone(),
620            TargetTuple::TargetJson { path_for_rustdoc, .. } => {
621                path_for_rustdoc.to_str().expect("target path must be valid unicode").to_owned()
622            }
623        },
624    ]);
625    if let ErrorOutputType::HumanReadable { kind, color_config } = rustdoc_options.error_format {
626        let short = kind.short();
627        let unicode = kind == HumanReadableErrorType { unicode: true, short };
628
629        if short {
630            compiler_args.extend_from_slice(&["--error-format".to_owned(), "short".to_owned()]);
631        }
632        if unicode {
633            compiler_args
634                .extend_from_slice(&["--error-format".to_owned(), "human-unicode".to_owned()]);
635        }
636
637        match color_config {
638            ColorConfig::Never => {
639                compiler_args.extend_from_slice(&["--color".to_owned(), "never".to_owned()]);
640            }
641            ColorConfig::Always => {
642                compiler_args.extend_from_slice(&["--color".to_owned(), "always".to_owned()]);
643            }
644            ColorConfig::Auto => {
645                compiler_args.extend_from_slice(&[
646                    "--color".to_owned(),
647                    if supports_color { "always" } else { "never" }.to_owned(),
648                ]);
649            }
650        }
651    }
652
653    let rustc_binary = rustdoc_options
654        .test_builder
655        .as_deref()
656        .unwrap_or_else(|| rustc_interface::util::rustc_path(sysroot).expect("found rustc"));
657    let mut compiler = wrapped_rustc_command(&rustdoc_options.test_builder_wrappers, rustc_binary);
658
659    compiler.args(&compiler_args);
660
661    // If this is a merged doctest, we need to write it into a file instead of using stdin
662    // because if the size of the merged doctests is too big, it'll simply break stdin.
663    if doctest.is_multiple_tests() {
664        // It makes the compilation failure much faster if it is for a combined doctest.
665        compiler.arg("--error-format=short");
666        let input_file = doctest.path_for_merged_doctest_bundle();
667        if std::fs::write(&input_file, &doctest.full_test_code).is_err() {
668            // If we cannot write this file for any reason, we leave. All combined tests will be
669            // tested as standalone tests.
670            return (Duration::default(), Err(TestFailure::CompileError));
671        }
672        if !rustdoc_options.no_capture && rustdoc_options.merge_doctests == MergeDoctests::Auto {
673            // If `no_capture` is disabled, and we might fallback to standalone tests, then we don't
674            // display rustc's output when compiling the merged doctests.
675            compiler.stderr(Stdio::null());
676        }
677        // bundled tests are an rlib, loaded by a separate runner executable
678        compiler
679            .arg("--crate-type=lib")
680            .arg("--out-dir")
681            .arg(doctest.test_opts.outdir.path())
682            .arg(input_file);
683    } else {
684        compiler.arg("--crate-type=bin").arg("-o").arg(&output_file);
685        // Setting these environment variables is unneeded if this is a merged doctest.
686        compiler.env("UNSTABLE_RUSTDOC_TEST_PATH", &doctest.test_opts.path);
687        compiler.env(
688            "UNSTABLE_RUSTDOC_TEST_LINE",
689            format!("{}", doctest.line as isize - doctest.full_test_line_offset as isize),
690        );
691        compiler.arg("-");
692        compiler.stdin(Stdio::piped());
693        compiler.stderr(Stdio::piped());
694    }
695
696    debug!("compiler invocation for doctest: {compiler:?}");
697
698    let mut child = match compiler.spawn() {
699        Ok(child) => child,
700        Err(error) => {
701            eprintln!("Failed to spawn {:?}: {error:?}", compiler.get_program());
702            return (Duration::default(), Err(TestFailure::CompileError));
703        }
704    };
705    let output = if let Some(merged_test_code) = &doctest.merged_test_code {
706        // compile-fail tests never get merged, so this should always pass
707        let status = child.wait().expect("Failed to wait");
708
709        // the actual test runner is a separate component, built with nightly-only features;
710        // build it now
711        let runner_input_file = doctest.path_for_merged_doctest_runner();
712
713        let mut runner_compiler =
714            wrapped_rustc_command(&rustdoc_options.test_builder_wrappers, rustc_binary);
715        // the test runner does not contain any user-written code, so this doesn't allow
716        // the user to exploit nightly-only features on stable
717        runner_compiler.env("RUSTC_BOOTSTRAP", "1");
718        runner_compiler.args(compiler_args);
719        runner_compiler.args(["--crate-type=bin", "-o"]).arg(&output_file);
720        let mut extern_path = std::ffi::OsString::from(format!(
721            "--extern=doctest_bundle_{edition}=",
722            edition = doctest.edition
723        ));
724
725        // Deduplicate passed -L directory paths, since usually all dependencies will be in the
726        // same directory (e.g. target/debug/deps from Cargo).
727        let mut seen_search_dirs = FxHashSet::default();
728        for extern_str in &rustdoc_options.extern_strs {
729            if let Some((_cratename, path)) = extern_str.split_once('=') {
730                // Direct dependencies of the tests themselves are
731                // indirect dependencies of the test runner.
732                // They need to be in the library search path.
733                let dir = Path::new(path)
734                    .parent()
735                    .filter(|x| x.components().count() > 0)
736                    .unwrap_or(Path::new("."));
737                if seen_search_dirs.insert(dir) {
738                    runner_compiler.arg("-L").arg(dir);
739                }
740            }
741        }
742        let output_bundle_file = doctest
743            .test_opts
744            .outdir
745            .path()
746            .join(format!("libdoctest_bundle_{edition}.rlib", edition = doctest.edition));
747        extern_path.push(&output_bundle_file);
748        runner_compiler.arg(extern_path);
749        runner_compiler.arg(&runner_input_file);
750        if std::fs::write(&runner_input_file, merged_test_code).is_err() {
751            // If we cannot write this file for any reason, we leave. All combined tests will be
752            // tested as standalone tests.
753            return (instant.elapsed(), Err(TestFailure::CompileError));
754        }
755        if !rustdoc_options.no_capture && rustdoc_options.merge_doctests == MergeDoctests::Auto {
756            // If `no_capture` is disabled and we're autodetecting whether to merge,
757            // we don't display rustc's output when compiling the merged doctests.
758            runner_compiler.stderr(Stdio::null());
759        } else {
760            runner_compiler.stderr(Stdio::inherit());
761        }
762        runner_compiler.arg("--error-format=short");
763        debug!("compiler invocation for doctest runner: {runner_compiler:?}");
764
765        let status = if !status.success() {
766            status
767        } else {
768            let mut child_runner = match runner_compiler.spawn() {
769                Ok(child) => child,
770                Err(error) => {
771                    eprintln!("Failed to spawn {:?}: {error:?}", runner_compiler.get_program());
772                    return (Duration::default(), Err(TestFailure::CompileError));
773                }
774            };
775            child_runner.wait().expect("Failed to wait")
776        };
777
778        process::Output { status, stdout: Vec::new(), stderr: Vec::new() }
779    } else {
780        let stdin = child.stdin.as_mut().expect("Failed to open stdin");
781        stdin.write_all(doctest.full_test_code.as_bytes()).expect("could write out test sources");
782        child.wait_with_output().expect("Failed to read stdout")
783    };
784
785    struct Bomb<'a>(&'a str);
786    impl Drop for Bomb<'_> {
787        fn drop(&mut self) {
788            eprint!("{}", self.0);
789        }
790    }
791    let mut out = str::from_utf8(&output.stderr)
792        .unwrap()
793        .lines()
794        .filter(|l| {
795            if let Ok(uext) = serde_json::from_str::<UnusedExterns>(l) {
796                report_unused_externs(uext);
797                false
798            } else {
799                true
800            }
801        })
802        .intersperse_with(|| "\n")
803        .collect::<String>();
804
805    // Add a \n to the end to properly terminate the last line,
806    // but only if there was output to be printed
807    if !out.is_empty() {
808        out.push('\n');
809    }
810
811    let _bomb = Bomb(&out);
812    match (output.status.success(), langstr.compile_fail) {
813        (true, true) => {
814            return (instant.elapsed(), Err(TestFailure::UnexpectedCompilePass));
815        }
816        (true, false) => {}
817        (false, true) => {
818            if !langstr.error_codes.is_empty() {
819                // We used to check if the output contained "error[{}]: " but since we added the
820                // colored output, we can't anymore because of the color escape characters before
821                // the ":".
822                let missing_codes: Vec<String> = langstr
823                    .error_codes
824                    .iter()
825                    .filter(|err| !out.contains(&format!("error[{err}]")))
826                    .cloned()
827                    .collect();
828
829                if !missing_codes.is_empty() {
830                    return (instant.elapsed(), Err(TestFailure::MissingErrorCodes(missing_codes)));
831                }
832            }
833        }
834        (false, false) => {
835            return (instant.elapsed(), Err(TestFailure::CompileError));
836        }
837    }
838
839    let duration = instant.elapsed();
840    if doctest.no_run {
841        return (duration, Ok(()));
842    }
843
844    // Run the code!
845    let mut cmd;
846
847    let output_file = make_maybe_absolute_path(output_file);
848    if let Some(tool) = &rustdoc_options.test_runtool {
849        let tool = make_maybe_absolute_path(tool.into());
850        cmd = Command::new(tool);
851        cmd.args(&rustdoc_options.test_runtool_args);
852        cmd.arg(&output_file);
853    } else {
854        cmd = Command::new(&output_file);
855        if doctest.is_multiple_tests() {
856            cmd.env("RUSTDOC_DOCTEST_BIN_PATH", &output_file);
857        }
858    }
859    if let Some(run_directory) = &rustdoc_options.test_run_directory {
860        cmd.current_dir(run_directory);
861    }
862
863    let result = if doctest.is_multiple_tests() || rustdoc_options.no_capture {
864        cmd.status().map(|status| process::Output {
865            status,
866            stdout: Vec::new(),
867            stderr: Vec::new(),
868        })
869    } else {
870        cmd.output()
871    };
872    match result {
873        Err(e) => return (duration, Err(TestFailure::ExecutionError(e))),
874        Ok(out) => {
875            if langstr.should_panic && out.status.success() {
876                return (duration, Err(TestFailure::UnexpectedRunPass));
877            } else if !langstr.should_panic && !out.status.success() {
878                return (duration, Err(TestFailure::ExecutionFailure(out)));
879            }
880        }
881    }
882
883    (duration, Ok(()))
884}
885
886/// Converts a path intended to use as a command to absolute if it is
887/// relative, and not a single component.
888///
889/// This is needed to deal with relative paths interacting with
890/// `Command::current_dir` in a platform-specific way.
891fn make_maybe_absolute_path(path: PathBuf) -> PathBuf {
892    if path.components().count() == 1 {
893        // Look up process via PATH.
894        path
895    } else {
896        std::env::current_dir().map(|c| c.join(&path)).unwrap_or_else(|_| path)
897    }
898}
899struct IndividualTestOptions {
900    outdir: DirState,
901    path: PathBuf,
902}
903
904impl IndividualTestOptions {
905    fn new(options: &RustdocOptions, test_id: &Option<String>, test_path: PathBuf) -> Self {
906        let outdir = if let Some(ref path) = options.persist_doctests {
907            let mut path = path.clone();
908            path.push(test_id.as_deref().unwrap_or("<doctest>"));
909
910            if let Err(err) = std::fs::create_dir_all(&path) {
911                eprintln!("Couldn't create directory for doctest executables: {err}");
912                panic::resume_unwind(Box::new(()));
913            }
914
915            DirState::Perm(path)
916        } else {
917            DirState::Temp(get_doctest_dir(options).expect("rustdoc needs a tempdir"))
918        };
919
920        Self { outdir, path: test_path }
921    }
922}
923
924/// A doctest scraped from the code, ready to be turned into a runnable test.
925///
926/// The pipeline goes: [`clean`] AST -> `ScrapedDoctest` -> `RunnableDoctest`.
927/// [`run_merged_tests`] converts a bunch of scraped doctests to a single runnable doctest,
928/// while [`generate_unique_doctest`] does the standalones.
929///
930/// [`clean`]: crate::clean
931/// [`run_merged_tests`]: crate::doctest::runner::DocTestRunner::run_merged_tests
932/// [`generate_unique_doctest`]: crate::doctest::make::DocTestBuilder::generate_unique_doctest
933#[derive(Debug)]
934pub(crate) struct ScrapedDocTest {
935    filename: FileName,
936    line: usize,
937    langstr: LangString,
938    text: String,
939    name: String,
940    span: Span,
941    global_crate_attrs: Vec<String>,
942}
943
944impl ScrapedDocTest {
945    fn new(
946        filename: FileName,
947        line: usize,
948        logical_path: Vec<String>,
949        langstr: LangString,
950        text: String,
951        span: Span,
952        global_crate_attrs: Vec<String>,
953    ) -> Self {
954        let mut item_path = logical_path.join("::");
955        item_path.retain(|c| c != ' ');
956        if !item_path.is_empty() {
957            item_path.push(' ');
958        }
959        let name = format!(
960            "{} - {item_path}(line {line})",
961            filename.display(RemapPathScopeComponents::DOCUMENTATION)
962        );
963
964        Self { filename, line, langstr, text, name, span, global_crate_attrs }
965    }
966    fn edition(&self, opts: &RustdocOptions) -> Edition {
967        self.langstr.edition.unwrap_or(opts.edition)
968    }
969
970    fn no_run(&self, opts: &RustdocOptions) -> bool {
971        self.langstr.no_run || opts.no_run
972    }
973
974    fn path(&self) -> PathBuf {
975        match &self.filename {
976            FileName::Real(name) => {
977                name.path(RemapPathScopeComponents::DOCUMENTATION).to_path_buf()
978            }
979            _ => PathBuf::from(r"doctest.rs"),
980        }
981    }
982}
983
984pub(crate) trait DocTestVisitor {
985    fn visit_test(&mut self, test: String, config: LangString, rel_line: MdRelLine);
986    fn visit_header(&mut self, _name: &str, _level: u32) {}
987}
988
989#[derive(Clone, Debug, Hash, Eq, PartialEq)]
990pub(crate) struct MergeableTestKey {
991    edition: Edition,
992    global_crate_attrs_hash: u64,
993}
994
995struct CreateRunnableDocTests {
996    standalone_tests: Vec<test::TestDescAndFn>,
997    mergeable_tests: FxIndexMap<MergeableTestKey, Vec<(DocTestBuilder, ScrapedDocTest)>>,
998
999    rustdoc_options: Arc<RustdocOptions>,
1000    opts: GlobalTestOptions,
1001    visited_tests: FxHashMap<(String, usize), usize>,
1002    unused_extern_reports: Arc<Mutex<Vec<UnusedExterns>>>,
1003    compiling_test_count: AtomicUsize,
1004    can_merge_doctests: MergeDoctests,
1005}
1006
1007impl CreateRunnableDocTests {
1008    fn new(rustdoc_options: RustdocOptions, opts: GlobalTestOptions) -> CreateRunnableDocTests {
1009        CreateRunnableDocTests {
1010            standalone_tests: Vec::new(),
1011            mergeable_tests: FxIndexMap::default(),
1012            opts,
1013            visited_tests: FxHashMap::default(),
1014            unused_extern_reports: Default::default(),
1015            compiling_test_count: AtomicUsize::new(0),
1016            can_merge_doctests: rustdoc_options.merge_doctests,
1017            rustdoc_options: Arc::new(rustdoc_options),
1018        }
1019    }
1020
1021    fn add_test(&mut self, scraped_test: ScrapedDocTest, dcx: Option<DiagCtxtHandle<'_>>) {
1022        // For example `module/file.rs` would become `module_file_rs`
1023        //
1024        // Note that we are kind-of extending the definition of the MACRO scope here, but
1025        // after all `#[doc]` is kind-of a macro.
1026        let file = scraped_test
1027            .filename
1028            .display(RemapPathScopeComponents::MACRO)
1029            .to_string_lossy()
1030            .chars()
1031            .map(|c| if c.is_ascii_alphanumeric() { c } else { '_' })
1032            .collect::<String>();
1033        let test_id = format!(
1034            "{file}_{line}_{number}",
1035            file = file,
1036            line = scraped_test.line,
1037            number = {
1038                // Increases the current test number, if this file already
1039                // exists or it creates a new entry with a test number of 0.
1040                self.visited_tests
1041                    .entry((file.clone(), scraped_test.line))
1042                    .and_modify(|v| *v += 1)
1043                    .or_insert(0)
1044            },
1045        );
1046
1047        let edition = scraped_test.edition(&self.rustdoc_options);
1048        let doctest = BuildDocTestBuilder::new(&scraped_test.text)
1049            .crate_name(&self.opts.crate_name)
1050            .global_crate_attrs(scraped_test.global_crate_attrs.clone())
1051            .edition(edition)
1052            .can_merge_doctests(self.can_merge_doctests)
1053            .test_id(test_id)
1054            .lang_str(&scraped_test.langstr)
1055            .span(scraped_test.span)
1056            .build(dcx);
1057        let is_standalone = !doctest.can_be_merged
1058            || self.rustdoc_options.no_capture
1059            || self.rustdoc_options.test_args.iter().any(|arg| arg == "--show-output");
1060        if is_standalone {
1061            let test_desc = self.generate_test_desc_and_fn(doctest, scraped_test);
1062            self.standalone_tests.push(test_desc);
1063        } else {
1064            self.mergeable_tests
1065                .entry(MergeableTestKey {
1066                    edition,
1067                    global_crate_attrs_hash: {
1068                        let mut hasher = FxHasher::default();
1069                        scraped_test.global_crate_attrs.hash(&mut hasher);
1070                        hasher.finish()
1071                    },
1072                })
1073                .or_default()
1074                .push((doctest, scraped_test));
1075        }
1076    }
1077
1078    fn generate_test_desc_and_fn(
1079        &mut self,
1080        test: DocTestBuilder,
1081        scraped_test: ScrapedDocTest,
1082    ) -> test::TestDescAndFn {
1083        if !scraped_test.langstr.compile_fail {
1084            self.compiling_test_count.fetch_add(1, Ordering::SeqCst);
1085        }
1086
1087        generate_test_desc_and_fn(
1088            test,
1089            scraped_test,
1090            self.opts.clone(),
1091            Arc::clone(&self.rustdoc_options),
1092            self.unused_extern_reports.clone(),
1093        )
1094    }
1095}
1096
1097fn generate_test_desc_and_fn(
1098    test: DocTestBuilder,
1099    scraped_test: ScrapedDocTest,
1100    opts: GlobalTestOptions,
1101    rustdoc_options: Arc<RustdocOptions>,
1102    unused_externs: Arc<Mutex<Vec<UnusedExterns>>>,
1103) -> test::TestDescAndFn {
1104    let target_str = rustdoc_options.target.to_string();
1105    let rustdoc_test_options =
1106        IndividualTestOptions::new(&rustdoc_options, &test.test_id, scraped_test.path());
1107
1108    debug!("creating test {}: {}", scraped_test.name, scraped_test.text);
1109    test::TestDescAndFn {
1110        desc: test::TestDesc {
1111            name: test::DynTestName(scraped_test.name.clone()),
1112            ignore: match scraped_test.langstr.ignore {
1113                Ignore::All => true,
1114                Ignore::None => false,
1115                Ignore::Some(ref ignores) => ignores.iter().any(|s| target_str.contains(s)),
1116            },
1117            ignore_message: None,
1118            source_file: "",
1119            start_line: 0,
1120            start_col: 0,
1121            end_line: 0,
1122            end_col: 0,
1123            // compiler failures are test failures
1124            should_panic: test::ShouldPanic::No,
1125            compile_fail: scraped_test.langstr.compile_fail,
1126            no_run: scraped_test.no_run(&rustdoc_options),
1127            test_type: test::TestType::DocTest,
1128        },
1129        testfn: test::DynTestFn(Box::new(move || {
1130            doctest_run_fn(
1131                rustdoc_test_options,
1132                opts,
1133                test,
1134                scraped_test,
1135                rustdoc_options,
1136                unused_externs,
1137            )
1138        })),
1139    }
1140}
1141
1142fn doctest_run_fn(
1143    test_opts: IndividualTestOptions,
1144    global_opts: GlobalTestOptions,
1145    doctest: DocTestBuilder,
1146    scraped_test: ScrapedDocTest,
1147    rustdoc_options: Arc<RustdocOptions>,
1148    unused_externs: Arc<Mutex<Vec<UnusedExterns>>>,
1149) -> Result<(), String> {
1150    let report_unused_externs = |uext| {
1151        unused_externs.lock().unwrap().push(uext);
1152    };
1153    let (wrapped, full_test_line_offset) = doctest.generate_unique_doctest(
1154        &scraped_test.text,
1155        scraped_test.langstr.test_harness,
1156        &global_opts,
1157        Some(&global_opts.crate_name),
1158    );
1159    let runnable_test = RunnableDocTest {
1160        full_test_code: wrapped.to_string(),
1161        full_test_line_offset,
1162        test_opts,
1163        global_opts,
1164        langstr: scraped_test.langstr.clone(),
1165        line: scraped_test.line,
1166        edition: scraped_test.edition(&rustdoc_options),
1167        no_run: scraped_test.no_run(&rustdoc_options),
1168        merged_test_code: None,
1169    };
1170    let (_, res) =
1171        run_test(runnable_test, &rustdoc_options, doctest.supports_color, report_unused_externs);
1172
1173    if let Err(err) = res {
1174        match err {
1175            TestFailure::CompileError => {
1176                eprint!("Couldn't compile the test.");
1177            }
1178            TestFailure::UnexpectedCompilePass => {
1179                eprint!("Test compiled successfully, but it's marked `compile_fail`.");
1180            }
1181            TestFailure::UnexpectedRunPass => {
1182                eprint!("Test executable succeeded, but it's marked `should_panic`.");
1183            }
1184            TestFailure::MissingErrorCodes(codes) => {
1185                eprint!("Some expected error codes were not found: {codes:?}");
1186            }
1187            TestFailure::ExecutionError(err) => {
1188                eprint!("Couldn't run the test: {err}");
1189                if err.kind() == io::ErrorKind::PermissionDenied {
1190                    eprint!(" - maybe your tempdir is mounted with noexec?");
1191                }
1192            }
1193            TestFailure::ExecutionFailure(out) => {
1194                eprintln!("Test executable failed ({reason}).", reason = out.status);
1195
1196                // FIXME(#12309): An unfortunate side-effect of capturing the test
1197                // executable's output is that the relative ordering between the test's
1198                // stdout and stderr is lost. However, this is better than the
1199                // alternative: if the test executable inherited the parent's I/O
1200                // handles the output wouldn't be captured at all, even on success.
1201                //
1202                // The ordering could be preserved if the test process' stderr was
1203                // redirected to stdout, but that functionality does not exist in the
1204                // standard library, so it may not be portable enough.
1205                let stdout = str::from_utf8(&out.stdout).unwrap_or_default();
1206                let stderr = str::from_utf8(&out.stderr).unwrap_or_default();
1207
1208                if !stdout.is_empty() || !stderr.is_empty() {
1209                    eprintln!();
1210
1211                    if !stdout.is_empty() {
1212                        eprintln!("stdout:\n{stdout}");
1213                    }
1214
1215                    if !stderr.is_empty() {
1216                        eprintln!("stderr:\n{stderr}");
1217                    }
1218                }
1219            }
1220        }
1221
1222        panic::resume_unwind(Box::new(()));
1223    }
1224    Ok(())
1225}
1226
1227#[cfg(test)] // used in tests
1228impl DocTestVisitor for Vec<usize> {
1229    fn visit_test(&mut self, _test: String, _config: LangString, rel_line: MdRelLine) {
1230        self.push(1 + rel_line.offset());
1231    }
1232}
1233
1234#[cfg(test)]
1235mod tests;