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 plan = ExecutionPlan {
745 preparation: Vec::new(),
746 test: ExecutionPhase {
747 name: "test".into(),
748 kind: PhaseKind::Test,
749 command: CommandSpec {
750 program: request.command[0].clone().into(),
751 arguments: request.command[1..].iter().map(OsString::from).collect(),
752 cwd: workspace.clone(),
753 environment: Some(environment_with(overrides)),
754 captured_output: None,
755 },
756 },
757 };
758 let options = supervision_options()?;
759 let watchdog_program = request.watchdog_program.as_ref().ok_or_else(|| {
760 DirectJavascriptRunError::Failed(
761 "the JavaScript run is missing its crash-containment executable".into(),
762 )
763 })?;
764 let supervisor = ProcessSupervisor::new_crash_safe(watchdog_program)
765 .map_err(|error| DirectJavascriptRunError::Failed(error.to_string()))?;
766 let output_baseline = std::cell::RefCell::new(None);
767 let execution = match execute_plan_with_supervisor(
768 &supervisor,
769 &plan,
770 options,
771 diagnostics,
772 |phase, diagnostics| {
773 {
774 *output_baseline.borrow_mut() =
778 Some(workspace_output_baseline(&workspace).map_err(|error| {
779 OrchestrationError::PhaseSetup {
780 phase: phase.name.clone(),
781 reason: error.to_string(),
782 }
783 })?);
784 if frontend.assertion_calls > 0 {
785 writeln!(
786 diagnostics,
787 "[supercov] attributed {} native node:assert call(s)",
788 frontend.assertion_calls
789 )
790 .map_err(|error| OrchestrationError::PhaseSetup {
791 phase: phase.name.clone(),
792 reason: error.to_string(),
793 })?;
794 }
795 writeln!(
796 diagnostics,
797 "[supercov] running in isolated workspace: {}",
798 request.command.join(" ")
799 )
800 .map_err(|error| OrchestrationError::PhaseSetup {
801 phase: phase.name.clone(),
802 reason: error.to_string(),
803 })?;
804 }
805 update_run_state(&root, &run_id, RunStateStatus::Testing, &started_at, None).map_err(
806 |error| OrchestrationError::PhaseSetup {
807 phase: phase.name.clone(),
808 reason: error.to_string(),
809 },
810 )?;
811 Ok(())
812 },
813 ) {
814 Ok(execution) => execution,
815 Err(error) => {
816 let message = error.to_string();
817 let state_updated = update_run_state(
818 &root,
819 &run_id,
820 RunStateStatus::Failed,
821 &started_at,
822 Some(message.clone()),
823 )
824 .is_ok();
825 if state_updated {
826 cleanup.mark_terminal();
827 }
828 return Err(message.into());
829 }
830 };
831 let test_command_ms = execution
832 .phases
833 .iter()
834 .find(|phase| phase.kind == PhaseKind::Test)
835 .map_or(0.0, |phase| phase.duration_ms as f64);
836 if execution.exit_code != 0
841 && execution.interrupted_signal.is_none()
842 && !project.unrun_build.is_empty()
843 {
844 let build = project.unrun_build.join(" ");
845 let _ = writeln!(
846 diagnostics,
847 "[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} && {}\"`",
848 request.command.join(" ")
849 );
850 }
851 if let Some(signal) = execution.interrupted_signal {
852 interrupt_run_state(&root, &run_id, &started_at, signal_name(signal))
853 .map_err(|error| error.to_string())?;
854 cleanup.mark_terminal();
855 return Err(DirectJavascriptRunError::Interrupted {
856 signal,
857 exit_code: execution.exit_code,
858 timings: RunTimings {
859 initialization_ms: rounded_millisecond(initialization_ms),
860 workspace_preparation_ms: rounded_millisecond(workspace_preparation_ms),
861 adapter_setup_ms: rounded_millisecond(adapter_setup_ms),
862 instrumented_build_ms: 0.0,
863 test_command_ms: rounded_millisecond(test_command_ms),
864 evidence_publication_ms: 0.0,
865 },
866 total_ms: rounded_millisecond(elapsed_ms(total_started)),
867 });
868 }
869 if let Some(baseline) = output_baseline.borrow().as_ref() {
874 let protected = project
875 .source_files
876 .iter()
877 .map(PathBuf::from)
878 .collect::<std::collections::BTreeSet<_>>();
879 let outputs = sync_command_outputs(&root, &workspace, baseline, &protected)
880 .map_err(|error| error.to_string())?;
881 if outputs.synced > 0 {
882 let _ = writeln!(
883 diagnostics,
884 "[supercov] synced {} file(s) the command created or changed back to the project: {}",
885 outputs.synced,
886 crate::workspace::summarize_paths(&outputs.synced_paths)
887 );
888 }
889 if !outputs.skipped_instrumented.is_empty() {
890 let _ = writeln!(
891 diagnostics,
892 "[supercov] {} file(s) stayed in the isolated workspace: they are instrumented copies, or were built from them, and must not overwrite your project: {}",
893 outputs.skipped_instrumented.len(),
894 crate::workspace::summarize_paths(&outputs.skipped_instrumented)
895 );
896 }
897 if !outputs.deleted_in_workspace.is_empty() {
898 let _ = writeln!(
899 diagnostics,
900 "[supercov] the command deleted {} file(s) in the isolated workspace; deletions are not propagated to the project: {}",
901 outputs.deleted_in_workspace.len(),
902 crate::workspace::summarize_paths(&outputs.deleted_in_workspace)
903 );
904 }
905 }
906 update_run_state(
907 &root,
908 &run_id,
909 RunStateStatus::Publishing,
910 &started_at,
911 None,
912 )
913 .map_err(|error| error.to_string())?;
914
915 let publication_started = Instant::now();
916 let archive_path = root
917 .join(".supercov/work")
918 .join(&run_id)
919 .join("evidence.raw.gz");
920 let entries = collect_sources(&[
921 EvidenceArchiveSource::File {
922 file: frontend.manifest_path,
923 path: "manifest.json".into(),
924 },
925 EvidenceArchiveSource::File {
926 file: workspace.join(".supercov/statement-exclusions.json"),
927 path: "statement-exclusions.json".into(),
928 },
929 EvidenceArchiveSource::Directory {
930 directory: evidence_directory,
931 prefix: None,
932 },
933 EvidenceArchiveSource::Directory {
934 directory: server_evidence_root.join(&run_id),
935 prefix: Some("server".into()),
936 },
937 ])
938 .map_err(|error| error.to_string())?;
939 let entries =
940 javascript_archive_entries(entries, &frontend.manifest, &run_id, execution.exit_code)?;
941 let entries = crate::source_capture::append(entries, &source_inputs)?;
942 let raw = write_archive(entries, &archive_path).map_err(|error| error.to_string())?;
943 remove_stored_tree_deferred(&root, &workspace.join(".supercov/evidence"))
944 .map_err(|error| error.to_string())?;
945 remove_stored_tree_deferred(&root, &server_evidence_root).map_err(|error| error.to_string())?;
946 let evidence_publication_ms = elapsed_ms(publication_started);
947 let timings = RunTimings {
948 initialization_ms: rounded_millisecond(initialization_ms),
949 workspace_preparation_ms: rounded_millisecond(workspace_preparation_ms),
950 adapter_setup_ms: rounded_millisecond(adapter_setup_ms),
951 instrumented_build_ms: 0.0,
952 test_command_ms: rounded_millisecond(test_command_ms),
953 evidence_publication_ms: rounded_millisecond(evidence_publication_ms),
954 };
955 let metadata = RunMetadata {
956 id: run_id.clone(),
957 started_at: started_at.clone(),
958 duration_ms: rounded_millisecond(elapsed_ms(total_started)),
959 command: request.command.clone(),
960 test_exit_code: Some(execution.exit_code),
961 integrity,
962 raw_evidence: RawEvidenceMetadata {
963 schema_version: raw.schema_version,
964 format: raw.format.into(),
965 file: raw.file.into(),
966 files: raw.files,
967 uncompressed_bytes: raw.uncompressed_bytes,
968 compressed_bytes: raw.compressed_bytes,
969 },
970 isolated_build: Some(true),
971 instrumented_build_cache: None,
972 timings: Some(timings),
973 merged: None,
974 parents: None,
975 source_roots: source_roots.clone(),
976 };
977 let run_directory =
978 publish_run(&root, &metadata, &archive_path).map_err(|error| error.to_string())?;
979 let terminal_status = if execution.exit_code == 0 {
980 RunStateStatus::Complete
981 } else {
982 RunStateStatus::Failed
983 };
984 update_run_state(&root, &run_id, terminal_status, &started_at, None)
985 .map_err(|error| error.to_string())?;
986 finalize_published_run(&root, &run_id).map_err(|error| error.to_string())?;
987 cleanup.mark_terminal();
988 Ok(DirectJavascriptRunResult {
989 run_id,
990 run_directory,
991 workspace,
992 exit_code: execution.exit_code,
993 assertion_calls: frontend.assertion_calls,
994 recovered_runs,
995 metadata,
996 })
997}
998
999#[cfg(test)]
1000mod environment_tests {
1001 use super::*;
1002
1003 #[test]
1004 fn nested_npm_drops_only_pnpm_derived_environment_aliases() {
1005 let mut environment = BTreeMap::from([
1006 ("npm_config_auto_install_peers".into(), "true".into()),
1007 ("NPM_CONFIG_SHAMEFULLY_HOIST".into(), "true".into()),
1008 (
1009 "npm_config_registry".into(),
1010 "https://registry.npmjs.org".into(),
1011 ),
1012 ("USER_VALUE".into(), "kept".into()),
1013 ]);
1014 remove_derived_pnpm_config(&mut environment);
1015 assert_eq!(
1016 environment,
1017 BTreeMap::from([
1018 ("USER_VALUE".into(), "kept".into()),
1019 (
1020 "npm_config_registry".into(),
1021 "https://registry.npmjs.org".into()
1022 ),
1023 ])
1024 );
1025 }
1026}