Skip to main content

supercov_engine/
go_run.rs

1//! Public, isolated Go coverage run lifecycle.
2//!
3//! Supercov owns the probes here, which means it rewrites the module's own
4//! sources. That cannot happen in the user's tree, so the run copies the
5//! project into an isolated workspace, instruments it there, and runs the
6//! user's command against the copy. Nothing in the tree the author edits is
7//! touched.
8//!
9//! Two facts about `go test` shape the rest:
10//!
11//! - It builds and runs **one test binary per package**, each its own process
12//!   with its own probe array. So each test package writes its own evidence
13//!   file and the run merges them, rather than every process racing to write
14//!   one path.
15//! - It **caches** successful results. A cached package does not run, so it
16//!   records nothing, and a run that silently measured half a suite is worse
17//!   than one that took longer. The run adds `-count=1` when the command does
18//!   not already say otherwise, and says so.
19
20use std::{
21    collections::{BTreeMap, BTreeSet},
22    ffi::OsString,
23    fs,
24    io::Write,
25    path::{Path, PathBuf},
26    time::Instant,
27};
28
29use serde::{Deserialize, Serialize};
30
31use crate::{
32    evidence_archive::write_archive,
33    frontend_protocol::validate_frontend_report_request,
34    go_instrumenter::{RUNTIME_ALIAS, RUNTIME_IMPORT, rewrite},
35    go_project::{PreparedGoProject, go_integrity_inputs, module_path, prepare_go_project},
36    go_test_harness::{instrument_test_file, probe_array_file, synthesized_harness},
37    integrity::{FrontendIntegrityInputs, create_explicit_run_integrity},
38    lifecycle::{
39        ProjectLock, finalize_published_run, publish_run, recover_abandoned_runs,
40        remove_stored_tree_deferred,
41    },
42    orchestration::{ExecutionPhase, ExecutionPlan, PhaseKind, execute_plan},
43    owned_evidence::{
44        OwnedRunInputs, OwnedTestOutcome, build_frontend_run, go_coverage_model, go_declaration,
45        merge_evidence, read_evidence,
46    },
47    process_supervision::{CommandSpec, SupervisionOptions},
48    run_store::{RawEvidenceMetadata, RunMetadata, RunTimings},
49    workspace::{canonicalize_simplified, prepare_cached_workspace, recover_cached_workspace},
50};
51
52/// Where the runtime package lands inside the workspace.
53///
54/// A plain directory rather than something hidden: the Go tool ignores any
55/// directory whose name begins with `.` or `_`, so a runtime placed out of
56/// sight would also be out of the build.
57const RUNTIME_DIRECTORY: &str = "supercov_runtime";
58
59const RUNTIME_SOURCE: &str = include_str!("../runtime-assets/go/supercov/supercov.go");
60
61/// The generated file that declares a package's probe array.
62const PROBE_FILE: &str = "supercov_probes.go";
63
64/// The generated file that arms the runtime and binds each test.
65const HARNESS_FILE: &str = "supercov_generated_test.go";
66
67#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
68#[serde(rename_all = "camelCase", deny_unknown_fields)]
69pub struct DirectGoRunRequest {
70    pub root: PathBuf,
71    pub command: Vec<String>,
72    pub run_id: String,
73    pub started_at: String,
74    /// Credit every test with what it reached, by running the package's tests
75    /// one at a time.
76    ///
77    /// Probes are a store into one array the whole process shares, so a test
78    /// that called `t.Parallel()` can only be credited with its own work if
79    /// nothing else is running while it does it. `-parallel 1` is what buys
80    /// that, and what it costs is the suite's own parallelism -- which is why
81    /// it is asked for rather than assumed.
82    #[serde(default)]
83    pub exact_attribution: bool,
84}
85
86#[derive(Debug, Clone, PartialEq)]
87pub struct DirectGoRunResult {
88    pub run_id: String,
89    pub run_directory: PathBuf,
90    pub exit_code: i32,
91    pub tests: usize,
92    /// Test functions `go test` ran that no evidence can be credited to: one
93    /// that called `t.Parallel()`, and every Example or Fuzz target. What they
94    /// reached counts run-wide, so a run of nothing but these is a measured
95    /// run with no per-test attribution -- which is a different thing from an
96    /// empty suite, and the summary has to be able to say which it was.
97    pub unattributed: usize,
98    pub source_files: usize,
99    /// Source files that did not parse, and so carry no obligations. A hole in
100    /// the denominator travels with the number it was taken out of.
101    pub unparseable_sources: usize,
102    /// Test files that did not parse. Their tests still run and still reach
103    /// what they reach; what is lost is the announcement that would name them
104    /// for it.
105    pub unparseable_tests: usize,
106    pub packages: usize,
107    pub recovered_runs: Vec<String>,
108    pub metadata: RunMetadata,
109}
110
111fn elapsed_ms(started: Instant) -> f64 {
112    (started.elapsed().as_secs_f64() * 10_000.0).round() / 10.0
113}
114
115/// One package whose tests will run, and where its evidence lands.
116#[derive(Debug, Clone)]
117struct GoTestPackage {
118    /// Directory relative to the workspace root, `/`-separated, `.` for the
119    /// module root.
120    directory: String,
121    evidence: PathBuf,
122    /// Which file declared each test, so a result can point at its source.
123    declared_in: BTreeMap<String, String>,
124    /// What ran here and announced nothing: parallel tests, Examples, Fuzz
125    /// targets.
126    unattributed: BTreeSet<String>,
127}
128
129struct InstrumentedWorkspace {
130    project: PreparedGoProject,
131    packages: Vec<GoTestPackage>,
132    /// Test files that did not parse, with the reason. They cost the tests
133    /// they declare their attribution and nothing else.
134    unparseable_tests: Vec<(String, String)>,
135}
136
137fn directory_of(relative: &str) -> String {
138    match relative.rsplit_once('/') {
139        Some((directory, _)) => directory.to_owned(),
140        None => ".".to_owned(),
141    }
142}
143
144/// The `package` a Go file declares.
145///
146/// Scanned rather than parsed: the clause is the first thing in a Go file that
147/// is not a comment, so stripping comments and reading the next word gets the
148/// same answer as a parse for a fraction of the cost — and preparing a project
149/// already parses every file once, which is enough.
150fn package_name(source: &str) -> Option<String> {
151    let bytes = source.as_bytes();
152    let mut at = 0;
153    while at < bytes.len() {
154        let rest = &source[at..];
155        if rest.starts_with("//") {
156            at += rest.find('\n').map_or(rest.len(), |end| end + 1);
157        } else if rest.starts_with("/*") {
158            // An unterminated comment is not Go; there is no clause to find.
159            at += rest.find("*/").map_or(rest.len(), |end| end + 2);
160        } else if bytes[at].is_ascii_whitespace() {
161            at += 1;
162        } else {
163            return rest
164                .strip_prefix("package")
165                .and_then(|rest| rest.strip_prefix(|c: char| c.is_ascii_whitespace()))
166                .and_then(|rest| rest.split_whitespace().next())
167                .map(str::to_owned);
168        }
169    }
170    None
171}
172
173fn read(path: &Path) -> Result<String, String> {
174    fs::read_to_string(path).map_err(|error| format!("{}: {error}", path.display()))
175}
176
177fn write(path: &Path, contents: &str) -> Result<(), String> {
178    if let Some(parent) = path.parent() {
179        fs::create_dir_all(parent).map_err(|error| format!("{}: {error}", parent.display()))?;
180    }
181    fs::write(path, contents).map_err(|error| format!("{}: {error}", path.display()))
182}
183
184/// A file-system-safe name for a package's evidence, so two packages cannot
185/// collide on one path.
186fn evidence_name(directory: &str) -> String {
187    let slug = directory
188        .chars()
189        .map(|c| if c.is_ascii_alphanumeric() { c } else { '_' })
190        .collect::<String>();
191    format!("{slug}.bin")
192}
193
194/// Rewrite the workspace in place: instrumented sources, the runtime package
195/// they import, a probe array per package, and a harness per test package.
196fn instrument_workspace(
197    workspace: &Path,
198    evidence_directory: &Path,
199    serialised: bool,
200    roots: Option<&crate::source_discovery::ExplicitSourceRoots>,
201) -> Result<InstrumentedWorkspace, String> {
202    let mut project = prepare_go_project(workspace, roots)?;
203    let mut unparseable_tests: Vec<(String, String)> = Vec::new();
204
205    // A go.work repository has several modules and no module at its root, so
206    // each gets a runtime of its own: an import is resolved against the module
207    // that names it, and one module cannot import a package inside another.
208    let members = crate::go_project::workspace_modules(workspace);
209    let directories = if members.is_empty() {
210        vec![".".to_owned()]
211    } else {
212        members.into_iter().collect::<Vec<_>>()
213    };
214    let mut runtimes: Vec<(String, String)> = Vec::new();
215    for directory in &directories {
216        let at = workspace.join(directory);
217        let module = module_path(&at).ok_or_else(|| {
218            format!(
219                "{}: no module path in go.mod, so Supercov cannot name the package its runtime is imported from",
220                at.display()
221            )
222        })?;
223        write(
224            &at.join(RUNTIME_DIRECTORY).join("supercov.go"),
225            RUNTIME_SOURCE,
226        )?;
227        runtimes.push((directory.clone(), format!("{module}/{RUNTIME_DIRECTORY}")));
228    }
229    // Longest first, so a nested module wins over the root it sits under.
230    runtimes.sort_by_key(|(directory, _)| std::cmp::Reverse(directory.len()));
231    let import_for = |relative: &str| -> &str {
232        runtimes
233            .iter()
234            .find(|(directory, _)| {
235                directory == "."
236                    || relative == directory
237                    || relative.starts_with(&format!("{directory}/"))
238            })
239            .map(|(_, import)| import.as_str())
240            .unwrap_or_default()
241    };
242
243    // Probe ids are handed out across the whole module, so every package
244    // reserves the same total: a probe from one package is the same index in
245    // the binary that links it as a dependency.
246    let probe_count = project
247        .probes
248        .keys()
249        .max()
250        .map_or(0, |highest| *highest as usize + 1);
251
252    let mut source_packages: BTreeMap<String, String> = BTreeMap::new();
253    for (relative, instrumented) in &project.instrumented {
254        let instrumented = instrumented.replace(RUNTIME_IMPORT, import_for(relative));
255        let path = workspace.join(relative);
256        if let Some(name) = package_name(&instrumented) {
257            source_packages
258                .entry(directory_of(relative))
259                .or_insert(name);
260        }
261        write(&path, &instrumented)?;
262    }
263    for (directory, package) in &source_packages {
264        write(
265            &workspace.join(directory).join(PROBE_FILE),
266            &probe_array_file(
267                package,
268                RUNTIME_ALIAS,
269                import_for(&format!("{directory}/x.go")),
270                probe_count,
271            ),
272        )?;
273    }
274
275    let mut by_directory: BTreeMap<String, Vec<&String>> = BTreeMap::new();
276    for relative in &project.files.tests {
277        by_directory
278            .entry(directory_of(relative))
279            .or_default()
280            .push(relative);
281    }
282
283    let mut packages = Vec::new();
284    for (directory, tests) in by_directory {
285        let evidence = evidence_directory.join(evidence_name(&directory));
286        let evidence_literal = evidence.to_string_lossy().replace('\\', "\\\\");
287        let mut declared_in = BTreeMap::new();
288        let mut unattributed = BTreeSet::new();
289        let mut declares_test_main = false;
290        // The harness joins whichever package the tests are in. A directory
291        // can hold both `foo` and `foo_test` files; the internal one wins,
292        // because that is where the instrumented sources are.
293        let mut harness_package: Option<String> = None;
294        for relative in tests {
295            let path = workspace.join(relative);
296            let source = read(&path)?;
297            let Some(name) = package_name(&source) else {
298                continue;
299            };
300            // The package a harness joins is read from the text, so it is
301            // known whether or not the file parses -- and a package whose only
302            // test file is unreadable still needs one.
303            let internal = source_packages.get(&directory) == Some(&name);
304            if internal || harness_package.is_none() {
305                harness_package = Some(name);
306            }
307            // A file the toolchain never builds declares nothing Supercov has
308            // to work around, and reading its `TestMain` as the package's own
309            // stood the harness down for a package that had none -- so the
310            // runtime was never armed and a passing suite published nothing.
311            let built = !build_ignored(&source);
312            let file =
313                match instrument_test_file(&source, RUNTIME_ALIAS, &evidence_literal, serialised) {
314                    Ok(file) => file,
315                    Err(error) => {
316                        // Taking the run down with it is the reflex a source file
317                        // was corrected for, and it is worse here: a test file
318                        // need not even be in the build for Supercov to refuse the
319                        // suite the toolchain just passed. What it costs is the
320                        // attribution of the tests it declares, because there is
321                        // nowhere to put the announcement. What they reach still
322                        // counts run-wide, like any execution no test could claim.
323                        project.manifest.limitations.push(
324                            crate::go_project::unparseable_limitation(relative, &error.to_string()),
325                        );
326                        unparseable_tests.push((relative.clone(), error.to_string()));
327                        // A TestMain nobody could read is still a TestMain, and Go
328                        // permits one per package. Synthesising a second does not
329                        // fail to measure, it fails to build -- so this is scanned
330                        // for rather than parsed for, which is the one question
331                        // that cannot go unanswered.
332                        declares_test_main |= built && declares_test_main_textually(&source);
333                        continue;
334                    }
335                };
336            declares_test_main |= built && file.declares_test_main;
337            for test in &file.tests {
338                declared_in.insert(test.clone(), relative.clone());
339            }
340            unattributed.extend(file.unattributed.iter().cloned());
341            if !file.edits.is_empty() {
342                write(
343                    &path,
344                    &rewrite(&source, &file.edits).replace(RUNTIME_IMPORT, import_for(relative)),
345                )?;
346            }
347        }
348        let Some(package) = harness_package else {
349            continue;
350        };
351        write(
352            &workspace.join(&directory).join(HARNESS_FILE),
353            &synthesized_harness(
354                &package,
355                RUNTIME_ALIAS,
356                import_for(&format!("{directory}/x.go")),
357                probe_count,
358                &project.decision_widths,
359                &evidence_literal,
360                declares_test_main,
361            ),
362        )?;
363        packages.push(GoTestPackage {
364            directory,
365            evidence,
366            declared_in,
367            unattributed,
368        });
369    }
370    Ok(InstrumentedWorkspace {
371        project,
372        packages,
373        unparseable_tests,
374    })
375}
376
377/// Whether the toolchain will refuse to build this file whatever it is asked.
378///
379/// Build constraints in general cannot be answered without knowing the GOOS,
380/// GOARCH and tags a build will use, and guessing at them would drop files a
381/// run does measure. `ignore` needs none of that: it is not a GOOS, not a
382/// GOARCH, and nothing defines it, which is exactly why it is the convention
383/// for Go kept beside Go without being compiled -- a reference harness, a
384/// generator's input. So it is the one constraint that can be read alone, and
385/// the only one that is.
386fn build_ignored(source: &str) -> bool {
387    // Constraints live above the package clause and nowhere else: the
388    // toolchain stops reading them there, so a `//go:build ignore` in a
389    // comment further down is text about Go rather than a constraint on it.
390    let header = if source.starts_with("package ") {
391        // Nothing precedes a clause on the first line, and looking for a
392        // newline before it would find none and read the whole file.
393        ""
394    } else {
395        source
396            .find("\npackage ")
397            .map_or(source, |clause| &source[..clause])
398    };
399    header
400        .lines()
401        .map(str::trim)
402        .filter_map(|line| {
403            line.strip_prefix("//go:build")
404                // The spelling Go used before 1.17, still in trees today.
405                .or_else(|| line.strip_prefix("// +build"))
406        })
407        .any(|constraint| {
408            constraint
409                .split(|c: char| !(c.is_alphanumeric() || c == '_'))
410                .any(|term| term == "ignore")
411        })
412}
413
414/// Whether a file declares `TestMain`, read from the text rather than a tree.
415///
416/// Only ever asked of a file that did not parse, which is exactly when there
417/// is no tree to ask. It errs towards seeing one: believing in a `TestMain`
418/// that is not there costs the package Supercov's own harness, while missing
419/// one that is there declares it twice and the package stops compiling.
420fn declares_test_main_textually(source: &str) -> bool {
421    source.split("func TestMain").skip(1).any(|rest| {
422        rest.trim_start()
423            .strip_prefix('(')
424            .is_some_and(|arguments| arguments.contains("testing.M"))
425    })
426}
427
428/// `go test` caches a package that passed, and a cached package does not run.
429/// The command with Go's test parallelism turned down to one.
430///
431/// `-parallel` bounds how many tests calling `t.Parallel()` run at once. At 1
432/// they resume one at a time, so each is the only test running when its probes
433/// fire and the harness can announce it like any serial test.
434fn command_run_in_order(command: &[String]) -> (Vec<String>, bool) {
435    if command
436        .iter()
437        .any(|argument| argument == "-parallel" || argument.starts_with("-parallel="))
438    {
439        // The author already said what they wanted; saying it louder is not
440        // Supercov's call.
441        return (command.to_vec(), false);
442    }
443    let mut updated = command.to_vec();
444    let position = updated
445        .iter()
446        .position(|argument| argument == "test")
447        .map_or(updated.len(), |index| index + 1);
448    updated.insert(position, "-parallel=1".into());
449    (updated, true)
450}
451
452fn command_with_fresh_results(command: &[String]) -> (Vec<String>, bool) {
453    if command
454        .iter()
455        .any(|argument| argument == "-count" || argument.starts_with("-count="))
456    {
457        return (command.to_vec(), false);
458    }
459    let mut updated = command.to_vec();
460    // After the subcommand, so `go -count=1 test` is never produced.
461    let position = updated
462        .iter()
463        .position(|argument| argument == "test")
464        .map_or(updated.len(), |index| index + 1);
465    updated.insert(position, "-count=1".into());
466    (updated, true)
467}
468
469/// The fingerprint a later query compares against the stored run.
470pub fn current_go_integrity(
471    root: &Path,
472    command: &[String],
473    source_roots: Option<&[String]>,
474) -> Result<crate::run_store::RunIntegrity, String> {
475    let root = canonicalize_simplified(root).map_err(|error| error.to_string())?;
476    let files = crate::go_project::discover_go_files(&root)?;
477    let mut inputs = go_integrity_inputs(&files, command);
478    crate::source_discovery::fold_roots_into_configuration(
479        &mut inputs.execution_configuration,
480        source_roots,
481    );
482    create_explicit_run_integrity(&root, &inputs, &FrontendIntegrityInputs::embedded_go())
483        .map_err(|error| error.to_string())
484}
485
486pub fn run_direct_go(
487    request: &DirectGoRunRequest,
488    diagnostics: &mut dyn Write,
489) -> Result<DirectGoRunResult, String> {
490    if request.command.is_empty() {
491        return Err("test command must not be empty".into());
492    }
493    let total_started = Instant::now();
494    let initialization_started = Instant::now();
495    let root = canonicalize_simplified(&request.root)
496        .map_err(|error| format!("{}: {error}", request.root.display()))?;
497    let mut lock = ProjectLock::acquire(&root, &request.run_id, &request.started_at)
498        .map_err(|error| error.to_string())?;
499    let initialization_ms = elapsed_ms(initialization_started);
500    let work_directory = root.join(".supercov/work").join(&request.run_id);
501    let result = (|| {
502        let recovered_runs = recover_abandoned_runs(&root, &request.started_at)
503            .map_err(|error| error.to_string())?;
504        if !recovered_runs.is_empty() {
505            writeln!(
506                diagnostics,
507                "[supercov] recovered abandoned run(s): {}",
508                recovered_runs.join(", ")
509            )
510            .map_err(|error| error.to_string())?;
511        }
512
513        let adapter_started = Instant::now();
514        let files = crate::go_project::discover_go_files(&root)?;
515        let source_roots = crate::source_discovery::configured_source_roots(
516            &std::env::vars().collect::<std::collections::BTreeMap<_, _>>(),
517        );
518        let mut integrity_inputs = go_integrity_inputs(&files, &request.command);
519        crate::source_discovery::fold_roots_into_configuration(
520            &mut integrity_inputs.execution_configuration,
521            source_roots.as_deref(),
522        );
523        let assertion_inputs =
524            crate::assertion_inputs::capture(&root, "go", integrity_inputs.assertion_paths())?;
525        let integrity = create_explicit_run_integrity(
526            &root,
527            &integrity_inputs,
528            &FrontendIntegrityInputs::embedded_go(),
529        )
530        .map_err(|error| error.to_string())?;
531
532        let workspace_started = Instant::now();
533        recover_cached_workspace(&root, &lock).map_err(|error| error.to_string())?;
534        let workspace =
535            prepare_cached_workspace(&root, &lock, &[]).map_err(|error| error.to_string())?;
536        let evidence_directory = work_directory.join("go/evidence");
537        fs::create_dir_all(&evidence_directory).map_err(|error| error.to_string())?;
538        // Decided before instrumenting, because whether a parallel test can be
539        // announced depends on whether anything will be running beside it.
540        let (command, serialised) = if request.exact_attribution {
541            command_run_in_order(&request.command)
542        } else {
543            (request.command.clone(), false)
544        };
545        let ambient = std::env::vars().collect::<std::collections::BTreeMap<_, _>>();
546        let roots = crate::source_discovery::ExplicitSourceRoots::from_environment_in(
547            &root, &workspace, &ambient,
548        )
549        .map_err(|error| error.to_string())?;
550        let instrumented =
551            instrument_workspace(&workspace, &evidence_directory, serialised, roots.as_ref())?;
552        let workspace_preparation_ms = elapsed_ms(workspace_started);
553        let adapter_setup_ms = (elapsed_ms(adapter_started) - workspace_preparation_ms).max(0.0);
554        writeln!(
555            diagnostics,
556            "[supercov] detected Go; instrumenting {} source file(s) across {} test package(s) in isolated workspace {}",
557            instrumented.project.instrumented.len(),
558            instrumented.packages.len(),
559            workspace.display()
560        )
561        .map_err(|error| error.to_string())?;
562        for (file, reason) in &instrumented.project.unparseable {
563            writeln!(
564                diagnostics,
565                "[supercov] could not parse {file}: {reason}; it carries no obligations"
566            )
567            .map_err(|error| error.to_string())?;
568        }
569        for (file, reason) in &instrumented.unparseable_tests {
570            writeln!(
571                diagnostics,
572                "[supercov] could not parse {file}: {reason}; the tests it declares run unattributed and what they reach counts run-wide"
573            )
574            .map_err(|error| error.to_string())?;
575        }
576        if instrumented.packages.is_empty() {
577            return Err(
578                "no Go test packages were found, so a run would measure nothing: Supercov needs at least one _test.go file"
579                    .into(),
580            );
581        }
582        // A file that does not parse is a hole in the denominator, and one
583        // hole is reported and measured around. Every file being a hole is not
584        // a hole: there is no denominator left, and a run of nothing satisfies
585        // every floor there is. Reporting that as success with exit 0 is the
586        // failure mode most likely to go unnoticed, so it is refused here the
587        // same way a project with no test package is.
588        if instrumented.project.instrumented.is_empty()
589            && !instrumented.project.files.sources.is_empty()
590        {
591            return Err(format!(
592                "none of the {} Go source file(s) could be parsed, so the run would measure a denominator of nothing: {}",
593                instrumented.project.files.sources.len(),
594                instrumented
595                    .project
596                    .unparseable
597                    .iter()
598                    .map(|(file, reason)| format!("{file}: {reason}"))
599                    .collect::<Vec<_>>()
600                    .join("; ")
601            ));
602        }
603
604        if serialised {
605            writeln!(
606                diagnostics,
607                "[supercov] added -parallel=1 for --exact-attribution: a test that calls t.Parallel() can only be credited with what it reached when nothing else is running. Your suite runs in order, which is slower."
608            )
609            .map_err(|error| error.to_string())?;
610        }
611        let (command, forced_fresh) = command_with_fresh_results(&command);
612        if forced_fresh {
613            writeln!(
614                diagnostics,
615                "[supercov] added -count=1: `go test` caches passing packages, and a cached package does not run, so it would record no coverage"
616            )
617            .map_err(|error| error.to_string())?;
618        }
619        let test_started = Instant::now();
620        let plan = ExecutionPlan {
621            preparation: Vec::new(),
622            test: ExecutionPhase {
623                name: "test".into(),
624                kind: PhaseKind::Test,
625                command: CommandSpec {
626                    program: command[0].clone().into(),
627                    arguments: command[1..].iter().map(OsString::from).collect(),
628                    cwd: workspace.clone(),
629                    environment: None,
630                    captured_output: None,
631                },
632            },
633        };
634        let options = SupervisionOptions::from_environment().map_err(|error| error.to_string())?;
635        let execution = execute_plan(&plan, options, diagnostics, |_, _| Ok(()))
636            .map_err(|error| error.to_string())?;
637        let test_command_ms = elapsed_ms(test_started);
638        if let Some(signal) = execution.interrupted_signal {
639            return Err(format!(
640                "the test command was interrupted by {signal:?}; no run was published"
641            ));
642        }
643        let exit_code = execution.exit_code;
644
645        let publication_started = Instant::now();
646        let mut parts = Vec::new();
647        let mut outcomes = Vec::new();
648        let mut silent = Vec::new();
649        for package in &instrumented.packages {
650            let Ok(bytes) = fs::read(&package.evidence) else {
651                silent.push(package.directory.clone());
652                continue;
653            };
654            let evidence = read_evidence(&bytes)
655                .map_err(|error| format!("{}: {error}", package.evidence.display()))?;
656            for test in &evidence.tests {
657                outcomes.push(OwnedTestOutcome {
658                    name: test.name.clone(),
659                    runner: test.runner.clone(),
660                    package: package.directory.clone(),
661                    file: package.declared_in.get(&test.name).cloned(),
662                    status: test.status.clone(),
663                    attributed: !test.unattributed,
664                });
665            }
666            parts.push(evidence);
667        }
668        if !silent.is_empty() {
669            writeln!(
670                diagnostics,
671                "[supercov] {} test package(s) wrote no evidence and are absent from this run: {}",
672                silent.len(),
673                silent.join(", ")
674            )
675            .map_err(|error| error.to_string())?;
676        }
677        let evidence = merge_evidence(parts);
678        let unattributed = instrumented
679            .packages
680            .iter()
681            .map(|package| package.unattributed.len())
682            .sum::<usize>();
683        // What the run reached, whether or not a test can be named for it. A
684        // package whose every test calls `t.Parallel()` announces nothing by
685        // design -- the coverage is real and simply has no owner -- and
686        // discarding the run there threw away the lines, branches and MC/DC of
687        // a fully measured suite. `t.Parallel()` is idiomatic Go: one reported
688        // codebase calls it in 935 of its 1144 test files.
689        let reached_something = evidence.global.iter().any(|total| *total != 0);
690        if outcomes.is_empty() && !reached_something {
691            let because = if unattributed > 0 {
692                format!(
693                    "; {unattributed} test(s) ran without being able to announce themselves (t.Parallel, Example or Fuzz) and reached nothing measured either"
694                )
695            } else {
696                String::new()
697            };
698            return Err(format!(
699                "no Go test recorded evidence (the command exited {exit_code}); a run that measured nothing is not published{because}"
700            ));
701        }
702        let run = build_frontend_run(OwnedRunInputs {
703            declaration: go_declaration(),
704            environment: "go",
705            manifest: &instrumented.project.manifest,
706            probes: &instrumented.project.probes,
707            evidence: &evidence,
708            outcomes: &outcomes,
709            run_id: &request.run_id,
710            generated_at: &request.started_at,
711            test_exit_code: exit_code,
712            coverage_model: go_coverage_model(),
713        })
714        .map_err(|error| error.to_string())?;
715        // Before anything is written: a run that cannot be read back is not a
716        // run, and finding that out at publication is far better than finding
717        // it out when someone asks for the report.
718        validate_frontend_report_request(&run.declaration, &run.request)
719            .map_err(|error| error.to_string())?;
720        let archive_path = work_directory.join("evidence.raw.gz");
721        let raw = write_archive(
722            crate::assertion_inputs::append(
723                run.archive_entries().map_err(|error| error.to_string())?,
724                &assertion_inputs,
725            )?,
726            &archive_path,
727        )
728        .map_err(|error| error.to_string())?;
729        let evidence_publication_ms = elapsed_ms(publication_started);
730
731        let timings = RunTimings {
732            initialization_ms,
733            workspace_preparation_ms,
734            adapter_setup_ms,
735            instrumented_build_ms: 0.0,
736            test_command_ms,
737            evidence_publication_ms,
738        };
739        let metadata = RunMetadata {
740            id: request.run_id.clone(),
741            started_at: request.started_at.clone(),
742            duration_ms: elapsed_ms(total_started),
743            command: request.command.clone(),
744            test_exit_code: Some(exit_code),
745            integrity,
746            raw_evidence: RawEvidenceMetadata {
747                schema_version: raw.schema_version,
748                format: raw.format.into(),
749                file: raw.file.into(),
750                files: raw.files,
751                uncompressed_bytes: raw.uncompressed_bytes,
752                compressed_bytes: raw.compressed_bytes,
753            },
754            isolated_build: Some(true),
755            instrumented_build_cache: None,
756            timings: Some(timings),
757            merged: None,
758            parents: None,
759            source_roots: source_roots.clone(),
760        };
761        let run_directory =
762            publish_run(&root, &metadata, &archive_path).map_err(|error| error.to_string())?;
763        finalize_published_run(&root, &request.run_id).map_err(|error| error.to_string())?;
764        Ok(DirectGoRunResult {
765            run_id: request.run_id.clone(),
766            run_directory,
767            exit_code,
768            tests: outcomes
769                .iter()
770                .map(|outcome| outcome.name.as_str())
771                .collect::<BTreeSet<_>>()
772                .len(),
773            unattributed,
774            source_files: instrumented.project.instrumented.len(),
775            unparseable_sources: instrumented.project.unparseable.len(),
776            unparseable_tests: instrumented.unparseable_tests.len(),
777            packages: instrumented.packages.len(),
778            recovered_runs,
779            metadata,
780        })
781    })();
782    if result.is_err() {
783        let _ = remove_stored_tree_deferred(&root, &work_directory);
784    }
785    let release = lock.release().map_err(|error| error.to_string());
786    match (result, release) {
787        (Ok(result), Ok(())) => Ok(result),
788        (Err(error), _) => Err(error),
789        (Ok(_), Err(error)) => Err(error),
790    }
791}
792
793#[cfg(test)]
794mod tests {
795    use super::*;
796
797    #[test]
798    fn the_package_clause_is_found_past_whatever_precedes_it() {
799        // A build tag, a licence header, a block comment: all of them sit
800        // above the clause in real Go, and a reader that took the first line
801        // would find none of them.
802        assert_eq!(package_name("package main\n").as_deref(), Some("main"));
803        assert_eq!(
804            package_name("//go:build linux\n\n// Copyright.\npackage worker\n").as_deref(),
805            Some("worker")
806        );
807        assert_eq!(
808            package_name("/*\nA package comment\nspanning lines.\n*/\npackage doc\n").as_deref(),
809            Some("doc")
810        );
811        assert_eq!(
812            package_name("/* one line */ package inline\n").as_deref(),
813            Some("inline")
814        );
815        // A trailing comment is not part of the name.
816        assert_eq!(
817            package_name("package api // the public surface\n").as_deref(),
818            Some("api")
819        );
820        assert_eq!(package_name("import \"fmt\"\n"), None);
821    }
822
823    #[test]
824    fn caching_is_disabled_unless_the_author_already_chose() {
825        // `go test` skips a package whose result it has cached, and a package
826        // that does not run records nothing. A run that measured half a suite
827        // without saying so would be worse than a slow one.
828        let (command, forced) =
829            command_with_fresh_results(&["go".into(), "test".into(), "./...".into()]);
830        assert!(forced);
831        assert_eq!(command, ["go", "test", "-count=1", "./..."]);
832
833        // An author who already said how many times to run means it.
834        let (command, forced) =
835            command_with_fresh_results(&["go".into(), "test".into(), "-count=3".into()]);
836        assert!(!forced);
837        assert_eq!(command, ["go", "test", "-count=3"]);
838    }
839
840    #[test]
841    fn each_package_gets_an_evidence_file_of_its_own() {
842        // One test binary per package, each its own process: sharing a path
843        // would mean the last one to finish overwrote everyone else.
844        assert_ne!(evidence_name("internal/auth"), evidence_name("internal/db"));
845        assert_eq!(evidence_name("."), "_.bin");
846        assert!(!evidence_name("internal/auth").contains('/'));
847    }
848
849    #[test]
850    fn only_the_constraint_nothing_ever_defines_is_read_alone() {
851        // `ignore` is not a GOOS, not a GOARCH, and nothing sets it, which is
852        // why it is the convention for Go kept beside Go without being
853        // compiled. Every other constraint needs a GOOS, a GOARCH and a tag
854        // set to answer, and guessing at those would drop files a run does
855        // measure.
856        assert!(build_ignored("//go:build ignore\n\npackage p\n"));
857        assert!(build_ignored("// +build ignore\n\npackage p\n"));
858        assert!(build_ignored(
859            "// Copyright.\n\n//go:build ignore\n\npackage p\n"
860        ));
861        // Constraints combine, and one of the terms is still the term.
862        assert!(build_ignored("//go:build ignore || linux\n\npackage p\n"));
863
864        // Everything else is a question this cannot answer, so it does not.
865        assert!(!build_ignored("//go:build linux\n\npackage p\n"));
866        assert!(!build_ignored("//go:build !windows\n\npackage p\n"));
867        assert!(!build_ignored("package p\n"));
868        // A tag that merely starts with the word is a different tag.
869        assert!(!build_ignored("//go:build ignored\n\npackage p\n"));
870        assert!(!build_ignored("//go:build ignore_me\n\npackage p\n"));
871        // And a constraint has to be above the package clause to be one. The
872        // toolchain ignores the text below it, so reading it there would drop
873        // a file the build takes.
874        assert!(!build_ignored(
875            "package p\n\n//go:build ignore\n\nfunc f() {}\n"
876        ));
877        assert!(!build_ignored(
878            "package p\n\nvar doc = \"//go:build ignore\"\n"
879        ));
880    }
881
882    #[test]
883    fn a_test_main_is_seen_in_a_file_nothing_could_parse() {
884        // Only ever asked of a file that did not parse, which is exactly when
885        // there is no tree to ask. Missing a real one declares a second
886        // TestMain and the package stops compiling, so this errs towards
887        // seeing one -- but not so far that any mention of the word counts.
888        assert!(declares_test_main_textually(
889            "package p\n\nfunc TestMain(m *testing.M) {\n\tos.Exit(m.Run())\n}\n"
890        ));
891        // Spacing is the author's business.
892        assert!(declares_test_main_textually(
893            "package p\n\nfunc TestMain( m  *testing.M ) {}\n"
894        ));
895        assert!(declares_test_main_textually(
896            "package p\n\nfunc TestMain(m *testing.M) { broken( }\n"
897        ));
898
899        // `TestMainHelper` is an ordinary function whose name starts with the
900        // word, exactly as `Testify` is not a test.
901        assert!(!declares_test_main_textually(
902            "package p\n\nfunc TestMainHelper(t *testing.T) {}\n"
903        ));
904        // A test that merely takes a *testing.T is not the package's harness.
905        assert!(!declares_test_main_textually(
906            "package p\n\nfunc TestMain(t *testing.T) {}\n"
907        ));
908        assert!(!declares_test_main_textually(
909            "package p\n\n// func TestMain\n"
910        ));
911        assert!(!declares_test_main_textually("package p\n"));
912    }
913}