1use std::{
4 collections::BTreeMap,
5 ffi::OsString,
6 path::{Path, PathBuf},
7 time::{Instant, SystemTime, UNIX_EPOCH},
8};
9
10use serde::{Deserialize, Serialize};
11use supercov_contracts::{
12 AttributionPrecision, ExecutionModel, FrontendAttribution, FrontendLimitation,
13 FrontendLimitationScope, FrontendRunDeclaration, FrontendRunnerDeclaration,
14 LANGUAGE_FRONTEND_PROTOCOL_VERSION, StructuralSource,
15};
16
17use crate::{
18 build_cache::{build_cache_key, read_build_cache, reuse_paths, write_build_cache},
19 coverage_report::{PersistedCoverageModel, RawTestResult, javascript_coverage_model},
20 evidence_archive::{
21 EvidenceArchiveEntry, EvidenceArchiveSource, collect_sources, write_archive,
22 },
23 integrity::{FrontendIntegrityInputs, create_run_integrity},
24 javascript_frontend::{
25 javascript_frontend_reuse_paths, load_cached_javascript_frontend,
26 prepare_javascript_frontend, read_javascript_frontend_cache,
27 },
28 lifecycle::{
29 ProjectLock, RunState, RunStateStatus, finalize_published_run, interrupt_run_state,
30 publish_run, recover_abandoned_runs, remove_stored_tree_deferred, update_run_state,
31 write_run_state,
32 },
33 orchestration::{
34 ExecutionPhase, ExecutionPlan, OrchestrationError, PhaseKind, execute_plan_with_supervisor,
35 },
36 process_supervision::{
37 CommandSpec, ForwardedSignal, ProcessSupervisor, SupervisionOptions, positive_milliseconds,
38 },
39 project_discovery::{BuildAdapter, discover_coverage_project},
40 run_store::{
41 InstrumentedBuildCache, RawEvidenceMetadata, RunIntegrity, RunMetadata, RunTimings,
42 },
43 workspace::{
44 cached_workspace_path, prepare_cached_workspace, prune_cached_workspace_sources,
45 sync_command_outputs, workspace_output_baseline,
46 },
47};
48
49#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
50#[serde(rename_all = "camelCase", deny_unknown_fields)]
51pub struct DirectJavascriptRunRequest {
52 pub root: PathBuf,
53 pub command: Vec<String>,
54 #[serde(default, skip_serializing_if = "Option::is_none")]
55 pub run_id: Option<String>,
56 #[serde(default, skip_serializing_if = "Option::is_none")]
57 pub started_at: Option<String>,
58 #[serde(skip)]
59 pub watchdog_program: Option<PathBuf>,
60}
61
62#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
63#[serde(rename_all = "camelCase", deny_unknown_fields)]
64pub struct DirectJavascriptRunResult {
65 pub run_id: String,
66 pub run_directory: PathBuf,
67 pub workspace: PathBuf,
68 pub exit_code: i32,
69 pub assertion_calls: usize,
70 pub recovered_runs: Vec<String>,
71 pub metadata: RunMetadata,
72}
73
74#[derive(Debug)]
75pub enum DirectJavascriptRunError {
76 Interrupted {
77 signal: ForwardedSignal,
78 exit_code: i32,
79 timings: RunTimings,
80 total_ms: f64,
81 },
82 Failed(String),
83}
84
85impl std::fmt::Display for DirectJavascriptRunError {
86 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
87 match self {
88 Self::Interrupted { signal, .. } => {
89 write!(formatter, "interrupted by {}", signal_name(*signal))
90 }
91 Self::Failed(message) => formatter.write_str(message),
92 }
93 }
94}
95
96impl std::error::Error for DirectJavascriptRunError {}
97
98impl From<String> for DirectJavascriptRunError {
99 fn from(value: String) -> Self {
100 Self::Failed(value)
101 }
102}
103
104fn signal_name(signal: ForwardedSignal) -> &'static str {
105 match signal {
106 ForwardedSignal::Sighup => "SIGHUP",
107 ForwardedSignal::Sigint => "SIGINT",
108 ForwardedSignal::Sigterm => "SIGTERM",
109 }
110}
111
112fn javascript_runner_declaration(
113 runner: String,
114 results: &[&RawTestResult],
115) -> FrontendRunnerDeclaration {
116 let has_observations = |raw: &&RawTestResult| {
117 !raw.phases.is_empty()
118 || !raw.server.is_empty()
119 || raw.runtime.iter().chain(&raw.browser).any(|snapshot| {
120 !snapshot.decisions.is_empty()
121 || !snapshot.hits.is_empty()
122 || !snapshot.events.is_empty()
123 })
124 };
125 let exact_test = results.iter().all(|raw| {
126 raw.test_id
127 .as_deref()
128 .is_some_and(|value| !value.is_empty())
129 && raw
130 .scope
131 .as_ref()
132 .is_none_or(|scope| raw.test_id.as_deref() == Some(scope.test_id.as_str()))
133 });
134 let exact_worker = results.iter().all(|raw| {
135 !has_observations(raw)
136 || raw
137 .scope
138 .as_ref()
139 .is_some_and(|scope| !scope.worker_id.is_empty())
140 });
141 let exact_retry = results.iter().all(|raw| {
142 raw.retry.is_some()
143 && raw
144 .scope
145 .as_ref()
146 .is_none_or(|scope| raw.retry == Some(scope.retry))
147 });
148 let contextual = !results.is_empty() && exact_test && exact_worker && exact_retry;
149 let precision = |exact| {
150 if exact {
151 AttributionPrecision::Exact
152 } else {
153 AttributionPrecision::Unavailable
154 }
155 };
156 let attribution = FrontendAttribution {
157 run: AttributionPrecision::Exact,
158 worker: precision(contextual),
159 test: precision(contextual),
160 retry: precision(contextual),
161 phase: precision(contextual),
162 action: precision(contextual),
163 assertion: precision(contextual),
164 };
165 let mut limitations = Vec::new();
166 if !contextual {
167 for (suffix, scope) in [
168 ("worker", FrontendLimitationScope::Worker),
169 ("test", FrontendLimitationScope::Test),
170 ("retry", FrontendLimitationScope::Retry),
171 ("phase", FrontendLimitationScope::Phase),
172 ("action", FrontendLimitationScope::Action),
173 ("assertion", FrontendLimitationScope::Assertion),
174 ] {
175 limitations.push(FrontendLimitation {
176 id: format!("{runner}-no-{suffix}").replace(':', "-"),
177 scopes: vec![scope],
178 reason: format!(
179 "Runner {runner} did not expose exact {suffix} identity for every result"
180 ),
181 });
182 }
183 }
184 FrontendRunnerDeclaration {
185 runner,
186 execution_model: if contextual {
187 ExecutionModel::ParallelContextPropagated
188 } else {
189 ExecutionModel::ParallelUnattributed
190 },
191 attribution,
192 limitations,
193 }
194}
195
196fn javascript_archive_entries(
197 mut entries: Vec<EvidenceArchiveEntry>,
198 manifest: &crate::javascript_frontend::JavascriptManifest,
199 run_id: &str,
200 exit_code: i32,
201) -> Result<Vec<EvidenceArchiveEntry>, String> {
202 let mut results = Vec::new();
203 for entry in &entries {
204 if entry.path == "mcdc.json" || entry.path.ends_with("/mcdc.json") {
205 results.push(
206 serde_json::from_slice::<RawTestResult>(&entry.contents)
207 .map_err(|error| format!("invalid {}: {error}", entry.path))?,
208 );
209 } else if entry.path == "mcdc.jsonl" || entry.path.ends_with(".mcdc.jsonl") {
210 let contents = entry
211 .contents
212 .strip_suffix(b"\n")
213 .ok_or_else(|| format!("{} does not end with a newline", entry.path))?;
214 for (index, line) in contents.split(|byte| *byte == b'\n').enumerate() {
215 if line.is_empty() {
216 return Err(format!(
217 "{} contains a blank record at line {}",
218 entry.path,
219 index + 1
220 ));
221 }
222 results.push(
223 serde_json::from_slice::<RawTestResult>(line).map_err(|error| {
224 format!(
225 "invalid {} record at line {}: {error}",
226 entry.path,
227 index + 1
228 )
229 })?,
230 );
231 }
232 }
233 }
234 if results.is_empty() {
235 let status = match exit_code {
236 0 => "passed",
237 supercov_contracts::COMMAND_TIMEOUT_EXIT_CODE => "timedOut",
238 _ => "failed",
239 };
240 let command_result = RawTestResult {
241 test_id: Some(format!("command:{run_id}")),
242 scope: None,
243 test: "Test command".into(),
244 test_file: None,
245 title: Some("Test command".into()),
246 retry: Some(0),
247 status: Some(status.into()),
248 expected_status: None,
249 flaky: false,
250 provenance: crate::coverage_report::TestProvenance {
251 runner: "command".into(),
252 kind: "setup".into(),
253 project: None,
254 source: "engine".into(),
255 },
256 role: "setup".into(),
257 phases: vec![],
258 runtime: vec![],
259 browser: vec![],
260 server: vec![],
261 };
262 entries.push(EvidenceArchiveEntry {
263 path: "results/command/mcdc.json".into(),
264 contents: serde_json::to_vec(&command_result).map_err(|error| error.to_string())?,
265 });
266 results.push(command_result);
267 }
268 let mut by_runner = BTreeMap::<String, Vec<&RawTestResult>>::new();
269 for result in &results {
270 by_runner
271 .entry(result.provenance.runner.clone())
272 .or_default()
273 .push(result);
274 }
275 if entries
276 .iter()
277 .any(|entry| entry.path.starts_with("server/background/") && entry.path.ends_with(".jsonl"))
278 {
279 by_runner.entry("background".into()).or_default();
280 }
281 let declaration = FrontendRunDeclaration {
282 protocol_version: LANGUAGE_FRONTEND_PROTOCOL_VERSION,
283 frontend_id: "javascript".into(),
284 frontend_version: "javascript-owned-v1".into(),
285 language: "javascript".into(),
286 structural_source: StructuralSource::OwnedProbes,
287 runners: by_runner
288 .into_iter()
289 .map(|(runner, results)| javascript_runner_declaration(runner, &results))
290 .collect(),
291 structural_limitations: manifest
292 .limitations
293 .iter()
294 .map(|limitation| limitation.id.clone())
295 .collect(),
296 };
297 entries.push(EvidenceArchiveEntry {
298 path: "coverage-model.json".into(),
299 contents: serde_json::to_vec(
300 &PersistedCoverageModel::from_declaration(&javascript_coverage_model())
301 .expect("JavaScript coverage model is contract-valid"),
302 )
303 .map_err(|error| error.to_string())?,
304 });
305 entries.push(EvidenceArchiveEntry {
306 path: "frontend.json".into(),
307 contents: serde_json::to_vec(&declaration).map_err(|error| error.to_string())?,
308 });
309 Ok(entries)
310}
311
312struct RunCleanup {
313 root: PathBuf,
314 run_id: String,
315 started_at: String,
316 lock: ProjectLock,
317 workspace: Option<PathBuf>,
318 state_written: bool,
319 terminal_recorded: bool,
320}
321
322impl RunCleanup {
323 fn lock(&self) -> &ProjectLock {
324 &self.lock
325 }
326
327 fn set_workspace(&mut self, workspace: PathBuf) {
328 self.workspace = Some(workspace);
329 }
330
331 fn mark_state_written(&mut self) {
332 self.state_written = true;
333 }
334
335 fn mark_terminal(&mut self) {
336 self.terminal_recorded = true;
337 }
338}
339
340impl Drop for RunCleanup {
341 fn drop(&mut self) {
342 if self.state_written && !self.terminal_recorded {
343 let _ = update_run_state(
344 &self.root,
345 &self.run_id,
346 RunStateStatus::Failed,
347 &self.started_at,
348 Some("Rust run exited before reaching a terminal lifecycle state".into()),
349 );
350 }
351 if let Some(workspace) = &self.workspace {
352 let _ = remove_stored_tree_deferred(
353 &self.root,
354 &workspace.join(".supercov/evidence").join(&self.run_id),
355 );
356 let _ = remove_stored_tree_deferred(
357 &self.root,
358 &workspace
359 .join(".supercov/server-evidence")
360 .join(&self.run_id),
361 );
362 let keep_workspace =
363 std::env::var("SUPERCOV_KEEP_WORKSPACE").is_ok_and(|value| !value.is_empty());
364 if !keep_workspace {
365 let _ = prune_cached_workspace_sources(&self.root, &self.lock);
366 }
367 }
368 let _ = self.lock.release();
369 }
370}
371
372fn now_nonce() -> u128 {
373 SystemTime::now()
374 .duration_since(UNIX_EPOCH)
375 .unwrap_or_default()
376 .as_millis()
377}
378
379fn elapsed_ms(started: Instant) -> f64 {
380 started.elapsed().as_secs_f64() * 1000.0
381}
382
383fn rounded_millisecond(value: f64) -> f64 {
384 (value * 10.0).round() / 10.0
385}
386
387fn supervision_options() -> Result<SupervisionOptions, String> {
388 let defaults = SupervisionOptions::default();
389 Ok(SupervisionOptions {
390 diagnostic_interval: positive_milliseconds(
391 std::env::var("SUPERCOV_DIAGNOSTIC_INTERVAL_MS")
392 .ok()
393 .as_deref(),
394 "SUPERCOV_DIAGNOSTIC_INTERVAL_MS",
395 )
396 .map_err(|error| error.to_string())?
397 .unwrap_or(defaults.diagnostic_interval),
398 timeout: positive_milliseconds(
399 std::env::var("SUPERCOV_COMMAND_TIMEOUT_MS").ok().as_deref(),
400 "SUPERCOV_COMMAND_TIMEOUT_MS",
401 )
402 .map_err(|error| error.to_string())?,
403 termination_grace: defaults.termination_grace,
404 })
405}
406
407fn remove_derived_pnpm_config(environment: &mut BTreeMap<OsString, OsString>) {
408 const PNPM_ONLY_NPM_CONFIG: &[&str] = &[
414 "npm_config_auto_install_peers",
415 "npm_config_enable_pre_post_scripts",
416 "npm_config_shamefully_hoist",
417 ];
418 environment.retain(|key, _| {
419 let key = key.to_string_lossy().to_ascii_lowercase();
420 !PNPM_ONLY_NPM_CONFIG.contains(&key.as_str())
421 });
422}
423
424fn environment_with(values: BTreeMap<String, String>) -> Vec<(OsString, OsString)> {
425 let mut environment = std::env::vars_os().collect::<BTreeMap<_, _>>();
426 remove_derived_pnpm_config(&mut environment);
427 for (key, value) in values {
428 environment.retain(|existing, _| !existing.to_string_lossy().eq_ignore_ascii_case(&key));
429 environment.insert(key.into(), value.into());
430 }
431 environment.into_iter().collect()
432}
433
434fn node_options(preload: &Path) -> String {
435 let preload = std::path::absolute(preload).unwrap_or_else(|_| preload.to_path_buf());
441 [
447 std::env::var("NODE_OPTIONS").ok(),
448 Some("--enable-source-maps".into()),
449 Some(format!("--import={}", crate::workspace::file_url(&preload))),
450 ]
451 .into_iter()
452 .flatten()
453 .filter(|value| !value.is_empty())
454 .collect::<Vec<_>>()
455 .join(" ")
456}
457
458pub fn current_javascript_integrity(
462 root: &Path,
463 command: &[String],
464) -> Result<RunIntegrity, String> {
465 let environment = std::env::vars().collect::<BTreeMap<_, _>>();
466 let project = discover_coverage_project(root, &environment, command)
467 .map_err(|error| error.to_string())?;
468 javascript_integrity_for_project(root, &project)
469}
470
471fn javascript_integrity_for_project(
472 root: &Path,
473 project: &crate::project_discovery::CoverageProject,
474) -> Result<RunIntegrity, String> {
475 let frontend = FrontendIntegrityInputs::embedded_javascript();
476 create_run_integrity(root, project, &frontend).map_err(|error| error.to_string())
477}
478
479pub fn run_direct_javascript(
482 request: &DirectJavascriptRunRequest,
483 diagnostics: &mut dyn std::io::Write,
484) -> Result<DirectJavascriptRunResult, DirectJavascriptRunError> {
485 if request.command.is_empty() {
486 return Err(DirectJavascriptRunError::Failed(
487 "test command must not be empty".into(),
488 ));
489 }
490 let total_started = Instant::now();
491 let initialization_started = Instant::now();
492 let root = crate::workspace::canonicalize_simplified(&request.root)
493 .map_err(|error| format!("{}: {error}", request.root.display()))?;
494 let nonce = now_nonce();
495 let run_id = request
496 .run_id
497 .clone()
498 .unwrap_or_else(|| format!("rust-{nonce}"));
499 let started_at = request
500 .started_at
501 .clone()
502 .unwrap_or_else(|| format!("unix-ms-{nonce}"));
503 let lock =
504 ProjectLock::acquire(&root, &run_id, &started_at).map_err(|error| error.to_string())?;
505 let mut cleanup = RunCleanup {
506 root: root.clone(),
507 run_id: run_id.clone(),
508 started_at: started_at.clone(),
509 lock,
510 workspace: None,
511 state_written: false,
512 terminal_recorded: false,
513 };
514 let recovered_runs =
515 recover_abandoned_runs(&root, &started_at).map_err(|error| error.to_string())?;
516 if !recovered_runs.is_empty() {
517 writeln!(
518 diagnostics,
519 "[supercov] recovered abandoned run(s): {}",
520 recovered_runs.join(", ")
521 )
522 .map_err(|error| error.to_string())?;
523 }
524 let environment = std::env::vars().collect::<BTreeMap<_, _>>();
525 let project = discover_coverage_project(&root, &environment, &request.command)
526 .map_err(|error| error.to_string())?;
527 let integrity = javascript_integrity_for_project(&root, &project)?;
528 let build_cache_key = build_cache_key(&integrity, &project)?;
529 let frontend_cache_key = format!(
530 "{}:{}",
531 integrity.fingerprint.combined, integrity.fingerprint.execution
532 );
533 let prior_workspace = cached_workspace_path(&root).map_err(|error| error.to_string())?;
534 let reusable_build = if project.build_adapter == BuildAdapter::Direct {
535 None
536 } else {
537 read_build_cache(&prior_workspace, &build_cache_key)
538 };
539 let reusable_frontend = read_javascript_frontend_cache(&prior_workspace, &frontend_cache_key);
540 let mut cached_paths = reusable_build.as_ref().map(reuse_paths).unwrap_or_default();
541 if let Some(frontend) = &reusable_frontend {
542 cached_paths.extend(javascript_frontend_reuse_paths(frontend));
543 cached_paths.sort();
544 cached_paths.dedup();
545 }
546 let initialization_ms = elapsed_ms(initialization_started);
547
548 let workspace_started = Instant::now();
549 let workspace_progress = crate::progress::ProgressLine::start("preparing isolated workspace");
550 let workspace = prepare_cached_workspace(&root, cleanup.lock(), &cached_paths)
551 .map_err(|error| error.to_string())?;
552 drop(workspace_progress);
553 cleanup.set_workspace(workspace.clone());
554 let workspace_preparation_ms = elapsed_ms(workspace_started);
555 writeln!(
556 diagnostics,
557 "[supercov] instrumenting isolated workspace {}",
558 workspace.display()
559 )
560 .map_err(|error| error.to_string())?;
561 cleanup.mark_state_written();
562 write_run_state(
563 &root,
564 &RunState {
565 id: run_id.clone(),
566 pid: std::process::id(),
567 root: root.display().to_string(),
568 workspace: workspace.display().to_string(),
569 started_at: started_at.clone(),
570 updated_at: started_at.clone(),
571 status: RunStateStatus::Preparing,
572 signal: None,
573 error: None,
574 },
575 )
576 .map_err(|error| error.to_string())?;
577
578 let adapter_started = Instant::now();
579 let instrumentation_progress = crate::progress::ProgressLine::start("instrumenting sources");
580 let collector_id = format!("collector-{}", integrity.fingerprint.execution);
581 let frontend = if let Some(cache) = &reusable_frontend {
582 load_cached_javascript_frontend(&workspace, cache)
583 } else {
584 prepare_javascript_frontend(&workspace, &project, &collector_id, &frontend_cache_key)
585 }
586 .map_err(|error| error.to_string())?;
587 drop(instrumentation_progress);
588 let adapter_setup_ms = elapsed_ms(adapter_started);
589 if let Some(detail) = crate::javascript_frontend::setup_timing_detail() {
590 writeln!(diagnostics, "[supercov] {detail}").map_err(|error| error.to_string())?;
591 }
592
593 let evidence_relative = format!(".supercov/evidence/{run_id}");
594 let evidence_directory = workspace.join(&evidence_relative);
595 let server_evidence_root = workspace.join(".supercov/server-evidence");
596 let diagnostic_owner = workspace.join(format!(".supercov/diagnostic-owner-{run_id}"));
597 let mut overrides = BTreeMap::from([
598 ("NODE_OPTIONS".into(), node_options(&frontend.preload_path)),
599 ("SUPERCOV_CJS_INTERCEPT".into(), "1".into()),
600 ("SUPERCOV_DIRECT_INSTRUMENTATION".into(), "1".into()),
601 (
605 "SUPERCOV_EVIDENCE_DIR".into(),
606 evidence_directory.display().to_string(),
607 ),
608 (
609 "SUPERCOV_DIAGNOSTIC_OWNER_FILE".into(),
610 diagnostic_owner.display().to_string(),
611 ),
612 (
613 "SUPERCOV_EXECUTION_FINGERPRINT".into(),
614 integrity.fingerprint.execution.clone(),
615 ),
616 (
617 "SUPERCOV_EXECUTION_LOG".into(),
618 evidence_directory
619 .join("execution.jsonl")
620 .display()
621 .to_string(),
622 ),
623 (
624 "SUPERCOV_MANIFEST".into(),
625 frontend.manifest_path.display().to_string(),
626 ),
627 (
628 "SUPERCOV_PROJECT_ROOT".into(),
629 workspace.display().to_string(),
630 ),
631 ("SUPERCOV_RUN_ID".into(), run_id.clone()),
632 (
633 "SUPERCOV_SERVER_EVIDENCE_ROOT".into(),
634 server_evidence_root.display().to_string(),
635 ),
636 (
637 "SUPERCOV_SOURCE_PROJECT_ROOT".into(),
638 root.display().to_string(),
639 ),
640 ]);
641 overrides.insert(
645 "SUPERCOV_GENERATED_VITEST_CONFIG".into(),
646 frontend.vitest_config_path.display().to_string(),
647 );
648 overrides.insert(
649 "SUPERCOV_GENERATED_PLAYWRIGHT_CONFIG".into(),
650 frontend.playwright_config_path.display().to_string(),
651 );
652 overrides.insert(
653 "SUPERCOV_PLAYWRIGHT_MODULE".into(),
654 project.playwright_module.clone(),
655 );
656 overrides.insert(
657 "SUPERCOV_PLAYWRIGHT_TEST_EXPORT".into(),
658 project.playwright_test_export.clone(),
659 );
660 overrides.insert(
661 "SUPERCOV_PLAYWRIGHT_WRAPPER".into(),
662 "./.supercov/node_modules/playwright.mjs".into(),
663 );
664 if let Some(original) = project
665 .playwright_config
666 .as_ref()
667 .and_then(|path| path.strip_prefix(&root).ok())
668 .map(|path| workspace.join(path))
669 {
670 overrides.insert(
671 "SUPERCOV_ORIGINAL_PLAYWRIGHT_CONFIG".into(),
672 original.display().to_string(),
673 );
674 }
675 overrides.extend(project.build_environment.clone());
676 let preparation = if reusable_build.is_some() {
677 writeln!(
678 diagnostics,
679 "[supercov] reusing exact-fingerprint instrumented build {}",
680 &build_cache_key[..12]
681 )
682 .map_err(|error| error.to_string())?;
683 Vec::new()
684 } else if project.build_adapter != BuildAdapter::Direct {
685 let mut arguments = project.build_command[1..]
686 .iter()
687 .map(OsString::from)
688 .collect::<Vec<_>>();
689 if project.build_adapter == BuildAdapter::Vite {
690 arguments.extend([
691 OsString::from("--"),
692 OsString::from("--config"),
693 OsString::from(".supercov/vite.config.mjs"),
694 OsString::from("--logLevel"),
695 OsString::from("error"),
696 ]);
697 }
698 let mut build_overrides = overrides.clone();
699 build_overrides.insert("NODE_ENV".into(), "production".into());
700 build_overrides.insert("npm_config_loglevel".into(), "error".into());
701 let build_environment = environment_with(build_overrides);
702 vec![ExecutionPhase {
703 name: "build".into(),
704 kind: PhaseKind::Build,
705 command: CommandSpec {
706 program: project.build_command[0].clone().into(),
707 arguments,
708 cwd: workspace.clone(),
709 environment: Some(build_environment),
710 captured_output: Some(
711 workspace
712 .join(".supercov")
713 .join(format!("build-output-{run_id}.log")),
714 ),
715 },
716 }]
717 } else {
718 Vec::new()
719 };
720 let plan = ExecutionPlan {
721 preparation,
722 test: ExecutionPhase {
723 name: "test".into(),
724 kind: PhaseKind::Test,
725 command: CommandSpec {
726 program: request.command[0].clone().into(),
727 arguments: request.command[1..].iter().map(OsString::from).collect(),
728 cwd: workspace.clone(),
729 environment: Some(environment_with(overrides)),
730 captured_output: None,
731 },
732 },
733 };
734 let options = supervision_options()?;
735 let watchdog_program = request.watchdog_program.as_ref().ok_or_else(|| {
736 DirectJavascriptRunError::Failed(
737 "the JavaScript run is missing its crash-containment executable".into(),
738 )
739 })?;
740 let supervisor = ProcessSupervisor::new_crash_safe(watchdog_program)
741 .map_err(|error| DirectJavascriptRunError::Failed(error.to_string()))?;
742 let output_baseline = std::cell::RefCell::new(None);
743 let execution = match execute_plan_with_supervisor(
744 &supervisor,
745 &plan,
746 options,
747 diagnostics,
748 |phase, diagnostics| {
749 let status = if phase.kind == PhaseKind::Test {
750 *output_baseline.borrow_mut() =
754 Some(workspace_output_baseline(&workspace).map_err(|error| {
755 OrchestrationError::PhaseSetup {
756 phase: phase.name.clone(),
757 reason: error.to_string(),
758 }
759 })?);
760 if frontend.assertion_calls > 0 {
761 writeln!(
762 diagnostics,
763 "[supercov] attributed {} native node:assert call(s)",
764 frontend.assertion_calls
765 )
766 .map_err(|error| OrchestrationError::PhaseSetup {
767 phase: phase.name.clone(),
768 reason: error.to_string(),
769 })?;
770 }
771 writeln!(
772 diagnostics,
773 "[supercov] running in isolated workspace: {}",
774 request.command.join(" ")
775 )
776 .map_err(|error| OrchestrationError::PhaseSetup {
777 phase: phase.name.clone(),
778 reason: error.to_string(),
779 })?;
780 RunStateStatus::Testing
781 } else {
782 RunStateStatus::Building
783 };
784 update_run_state(&root, &run_id, status, &started_at, None).map_err(|error| {
785 OrchestrationError::PhaseSetup {
786 phase: phase.name.clone(),
787 reason: error.to_string(),
788 }
789 })?;
790 Ok(())
791 },
792 ) {
793 Ok(execution) => execution,
794 Err(error) => {
795 let message = error.to_string();
796 let state_updated = update_run_state(
797 &root,
798 &run_id,
799 RunStateStatus::Failed,
800 &started_at,
801 Some(message.clone()),
802 )
803 .is_ok();
804 if state_updated {
805 cleanup.mark_terminal();
806 }
807 return Err(message.into());
808 }
809 };
810 let instrumented_build_ms = execution
811 .phases
812 .iter()
813 .find(|phase| phase.kind == PhaseKind::Build)
814 .map_or(0.0, |phase| phase.duration_ms as f64);
815 let test_command_ms = execution
816 .phases
817 .iter()
818 .find(|phase| phase.kind == PhaseKind::Test)
819 .map_or(0.0, |phase| phase.duration_ms as f64);
820 let build_succeeded = execution
821 .phases
822 .iter()
823 .find(|phase| phase.kind == PhaseKind::Build)
824 .is_some_and(|phase| phase.result.exit_code() == 0);
825 if project.build_adapter != BuildAdapter::Direct && reusable_build.is_none() && build_succeeded
826 {
827 write_build_cache(&root, &workspace, &build_cache_key, &started_at)?;
828 }
829 if let Some(signal) = execution.interrupted_signal {
830 interrupt_run_state(&root, &run_id, &started_at, signal_name(signal))
831 .map_err(|error| error.to_string())?;
832 cleanup.mark_terminal();
833 return Err(DirectJavascriptRunError::Interrupted {
834 signal,
835 exit_code: execution.exit_code,
836 timings: RunTimings {
837 initialization_ms: rounded_millisecond(initialization_ms),
838 workspace_preparation_ms: rounded_millisecond(workspace_preparation_ms),
839 adapter_setup_ms: rounded_millisecond(adapter_setup_ms),
840 instrumented_build_ms: rounded_millisecond(instrumented_build_ms),
841 test_command_ms: rounded_millisecond(test_command_ms),
842 evidence_publication_ms: 0.0,
843 },
844 total_ms: rounded_millisecond(elapsed_ms(total_started)),
845 });
846 }
847 if let Some(baseline) = output_baseline.borrow().as_ref() {
852 let protected = project
853 .source_files
854 .iter()
855 .map(PathBuf::from)
856 .collect::<std::collections::BTreeSet<_>>();
857 let outputs = sync_command_outputs(&root, &workspace, baseline, &protected)
858 .map_err(|error| error.to_string())?;
859 if outputs.synced > 0 {
860 let _ = writeln!(
861 diagnostics,
862 "[supercov] synced {} file(s) the command created or changed back to the project",
863 outputs.synced
864 );
865 }
866 if !outputs.skipped_instrumented.is_empty() {
867 let _ = writeln!(
868 diagnostics,
869 "[supercov] {} file(s) stayed in the isolated workspace: they are instrumented copies, or were built from them, and must not overwrite your project: {}",
870 outputs.skipped_instrumented.len(),
871 outputs
872 .skipped_instrumented
873 .iter()
874 .take(3)
875 .map(|path| path.display().to_string())
876 .collect::<Vec<_>>()
877 .join(", ")
878 );
879 }
880 if !outputs.deleted_in_workspace.is_empty() {
881 let _ = writeln!(
882 diagnostics,
883 "[supercov] the command deleted {} file(s) in the isolated workspace; deletions are not propagated to the project",
884 outputs.deleted_in_workspace.len()
885 );
886 }
887 }
888 update_run_state(
889 &root,
890 &run_id,
891 RunStateStatus::Publishing,
892 &started_at,
893 None,
894 )
895 .map_err(|error| error.to_string())?;
896
897 let publication_started = Instant::now();
898 let archive_path = root
899 .join(".supercov/work")
900 .join(&run_id)
901 .join("evidence.raw.gz");
902 let entries = collect_sources(&[
903 EvidenceArchiveSource::File {
904 file: frontend.manifest_path,
905 path: "manifest.json".into(),
906 },
907 EvidenceArchiveSource::Directory {
908 directory: evidence_directory,
909 prefix: None,
910 },
911 EvidenceArchiveSource::Directory {
912 directory: server_evidence_root.join(&run_id),
913 prefix: Some("server".into()),
914 },
915 ])
916 .map_err(|error| error.to_string())?;
917 let entries =
918 javascript_archive_entries(entries, &frontend.manifest, &run_id, execution.exit_code)?;
919 let raw = write_archive(entries, &archive_path).map_err(|error| error.to_string())?;
920 remove_stored_tree_deferred(&root, &workspace.join(".supercov/evidence"))
921 .map_err(|error| error.to_string())?;
922 remove_stored_tree_deferred(&root, &server_evidence_root).map_err(|error| error.to_string())?;
923 let evidence_publication_ms = elapsed_ms(publication_started);
924 let timings = RunTimings {
925 initialization_ms: rounded_millisecond(initialization_ms),
926 workspace_preparation_ms: rounded_millisecond(workspace_preparation_ms),
927 adapter_setup_ms: rounded_millisecond(adapter_setup_ms),
928 instrumented_build_ms: rounded_millisecond(instrumented_build_ms),
929 test_command_ms: rounded_millisecond(test_command_ms),
930 evidence_publication_ms: rounded_millisecond(evidence_publication_ms),
931 };
932 let metadata = RunMetadata {
933 id: run_id.clone(),
934 started_at: started_at.clone(),
935 duration_ms: rounded_millisecond(elapsed_ms(total_started)),
936 command: request.command.clone(),
937 test_exit_code: Some(execution.exit_code),
938 integrity,
939 raw_evidence: RawEvidenceMetadata {
940 schema_version: raw.schema_version,
941 format: raw.format.into(),
942 file: raw.file.into(),
943 files: raw.files,
944 uncompressed_bytes: raw.uncompressed_bytes,
945 compressed_bytes: raw.compressed_bytes,
946 },
947 isolated_build: Some(true),
948 instrumented_build_cache: Some(InstrumentedBuildCache {
949 key: build_cache_key,
950 reused: reusable_build.is_some(),
951 }),
952 timings: Some(timings),
953 merged: None,
954 parents: None,
955 };
956 let run_directory =
957 publish_run(&root, &metadata, &archive_path).map_err(|error| error.to_string())?;
958 let terminal_status = if execution.exit_code == 0 {
959 RunStateStatus::Complete
960 } else {
961 RunStateStatus::Failed
962 };
963 update_run_state(&root, &run_id, terminal_status, &started_at, None)
964 .map_err(|error| error.to_string())?;
965 finalize_published_run(&root, &run_id).map_err(|error| error.to_string())?;
966 cleanup.mark_terminal();
967 Ok(DirectJavascriptRunResult {
968 run_id,
969 run_directory,
970 workspace,
971 exit_code: execution.exit_code,
972 assertion_calls: frontend.assertion_calls,
973 recovered_runs,
974 metadata,
975 })
976}
977
978#[cfg(test)]
979mod environment_tests {
980 use super::*;
981
982 #[test]
983 fn nested_npm_drops_only_pnpm_derived_environment_aliases() {
984 let mut environment = BTreeMap::from([
985 ("npm_config_auto_install_peers".into(), "true".into()),
986 ("NPM_CONFIG_SHAMEFULLY_HOIST".into(), "true".into()),
987 (
988 "npm_config_registry".into(),
989 "https://registry.npmjs.org".into(),
990 ),
991 ("USER_VALUE".into(), "kept".into()),
992 ]);
993 remove_derived_pnpm_config(&mut environment);
994 assert_eq!(
995 environment,
996 BTreeMap::from([
997 ("USER_VALUE".into(), "kept".into()),
998 (
999 "npm_config_registry".into(),
1000 "https://registry.npmjs.org".into()
1001 ),
1002 ])
1003 );
1004 }
1005}