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