Skip to main content

qk_executor/
lib.rs

1//! Prepare finite tasks, then execute shell commands in managed process groups.
2
3mod capture;
4pub mod dotenv;
5mod env_flags;
6mod interpolate;
7mod process;
8#[cfg(unix)]
9mod pty;
10pub mod readiness;
11pub mod ready_signal;
12pub mod report;
13pub mod tokens;
14
15use std::collections::BTreeMap;
16use std::ffi::OsString;
17use std::path::{Path, PathBuf};
18
19use anyhow::{Context, Result, bail};
20use qk_config::Workspace;
21use qk_taskgraph::Task;
22use serde_json::{Map, Value};
23
24pub use capture::{
25    Capture, Decoration, Display, MAX_LOG_BYTES, OutputStyle, RecordedLog, Shown, collapsed,
26    printable, read_capture, replay,
27};
28pub use env_flags::{force_colour, is_ci, is_ci_with};
29pub use process::{Outcome, execute, execute_captured, shell};
30
31#[derive(Debug)]
32struct NodeRuntime {
33    root: PathBuf,
34    env: BTreeMap<OsString, OsString>,
35    paths: Vec<PathBuf>,
36    directory: std::sync::OnceLock<Option<PathBuf>>,
37}
38
39/// The runner's Node selection, resolved only when a command starts.
40#[derive(Clone, Debug)]
41pub struct NodePath {
42    runtime: std::sync::Arc<NodeRuntime>,
43    local_entries: usize,
44}
45
46impl NodePath {
47    fn new(root: &Path, env: &BTreeMap<OsString, OsString>, local_entries: usize) -> Self {
48        type Key = (PathBuf, BTreeMap<OsString, OsString>);
49        static RUNTIMES: std::sync::OnceLock<
50            std::sync::Mutex<BTreeMap<Key, std::sync::Arc<NodeRuntime>>>,
51        > = std::sync::OnceLock::new();
52        let mut runtimes = RUNTIMES.get_or_init(Default::default).lock().unwrap();
53        let runtime = runtimes
54            .entry((root.to_owned(), env.clone()))
55            .or_insert_with(|| {
56                let paths = env
57                    .get(&path_key(env))
58                    .map(|path| std::env::split_paths(path).collect())
59                    .unwrap_or_default();
60                std::sync::Arc::new(NodeRuntime {
61                    root: root.to_owned(),
62                    env: env.clone(),
63                    paths,
64                    directory: Default::default(),
65                })
66            })
67            .clone();
68        Self {
69            runtime,
70            local_entries,
71        }
72    }
73
74    fn path(&self, env: &BTreeMap<OsString, OsString>) -> Result<Option<(OsString, OsString)>> {
75        // Select before task overrides, like process.execPath in Nx. Cache hits
76        // never call this, and concurrent commands share the same proxy probe.
77        let Some(directory) = self.runtime.directory.get_or_init(|| {
78            node_directory(&self.runtime.root, &self.runtime.paths, &self.runtime.env)
79        }) else {
80            return Ok(None);
81        };
82        let key = path_key(env);
83        let mut paths: Vec<_> = env
84            .get(&key)
85            .map(|path| std::env::split_paths(path).collect())
86            .unwrap_or_default();
87        paths.insert(self.local_entries.min(paths.len()), directory.clone());
88        Ok(Some((
89            key,
90            std::env::join_paths(paths).context("cannot construct task PATH")?,
91        )))
92    }
93}
94
95fn path_key(env: &BTreeMap<OsString, OsString>) -> OsString {
96    if cfg!(windows) {
97        env.keys()
98            .find(|key| key.to_string_lossy().eq_ignore_ascii_case("PATH"))
99            .cloned()
100            .unwrap_or_else(|| "PATH".into())
101    } else {
102        "PATH".into()
103    }
104}
105
106fn environment_key(env: &BTreeMap<OsString, OsString>, name: OsString) -> OsString {
107    if cfg!(windows) && name.to_string_lossy().eq_ignore_ascii_case("PATH") {
108        path_key(env)
109    } else {
110        name
111    }
112}
113
114fn node_directory(
115    cwd: &Path,
116    paths: &[PathBuf],
117    env: &BTreeMap<OsString, OsString>,
118) -> Option<PathBuf> {
119    let name = if cfg!(windows) { "node.exe" } else { "node" };
120    for directory in paths {
121        let candidate = cwd.join(directory).join(name);
122        let Ok(metadata) = candidate.metadata() else {
123            continue;
124        };
125        if !metadata.is_file() {
126            continue;
127        }
128        #[cfg(unix)]
129        {
130            use std::os::unix::fs::PermissionsExt;
131            if metadata.permissions().mode() & 0o111 == 0 {
132                continue;
133            }
134        }
135        // The first executable PATH match is authoritative, even if discovery fails.
136        let actual = candidate.canonicalize().ok()?;
137        if actual.file_name()? == name {
138            use std::io::Read;
139            let mut header = [0; 2];
140            let script = std::fs::File::open(&actual)
141                .and_then(|mut file| file.read_exact(&mut header))
142                .is_ok()
143                && header == *b"#!";
144            if !script {
145                return actual.parent().map(Path::to_owned);
146            }
147        }
148        return probe_node(&candidate, cwd, env);
149    }
150    None
151}
152
153fn probe_node(candidate: &Path, cwd: &Path, env: &BTreeMap<OsString, OsString>) -> Option<PathBuf> {
154    use std::io::{Read, Seek};
155    use std::process::{Command, Stdio};
156    use std::time::{Duration, Instant};
157    let mut output = tempfile::tempfile().ok()?;
158    let mut child = Command::new(candidate)
159        .args(["-p", "process.execPath"])
160        .current_dir(cwd)
161        .env_clear()
162        .envs(env)
163        .stdout(Stdio::from(output.try_clone().ok()?))
164        .stderr(Stdio::null())
165        .spawn()
166        .ok()?;
167    let started = Instant::now();
168    loop {
169        match child.try_wait() {
170            Ok(Some(status)) => {
171                if !status.success() {
172                    return None;
173                }
174                let mut bytes = Vec::new();
175                output.rewind().ok()?;
176                output.take(4096).read_to_end(&mut bytes).ok()?;
177                let path = PathBuf::from(std::str::from_utf8(&bytes).ok()?.trim());
178                return (path.is_absolute() && path.is_file())
179                    .then(|| path.parent().unwrap().to_owned());
180            }
181            Ok(None) if started.elapsed() < Duration::from_secs(2) => {
182                std::thread::sleep(Duration::from_millis(5))
183            }
184            _ => {
185                let _ = child.kill();
186                let _ = child.wait();
187                return None;
188            }
189        }
190    }
191}
192
193/// Reports a `qk:` warning to the installed sink; by default it is written to
194/// stderr in one write, so task processes sharing stderr cannot split it.
195#[macro_export]
196macro_rules! status {
197    ($($arg:tt)*) => {{
198        $crate::report::warning(&::std::format!($($arg)*));
199    }};
200}
201
202/// How a sandboxed task's commands are held to what it declares.
203#[derive(Clone, Debug)]
204pub enum Confinement {
205    /// macOS: a Seatbelt profile, through `sandbox-exec`.
206    Seatbelt(PathBuf),
207    /// Linux: a Landlock rule set's descriptor, which each command restricts
208    /// itself to before it starts. It stays open while the run lasts.
209    Landlock(i32),
210}
211
212/// An external syscall recorder wrapping each command, with separate trace files.
213#[derive(Clone, Debug)]
214pub struct Recording {
215    pub program: PathBuf,
216    pub directory: PathBuf,
217}
218
219impl Recording {
220    /// The same invocation is used by preflight and actual task commands.
221    pub fn command(&self, index: usize, text: &str) -> std::process::Command {
222        let mut command = std::process::Command::new(&self.program);
223        // No read/write buffers or exec argument/environment arrays.
224        command
225            .args([
226                "-ff",
227                "-ttt",
228                "-yy",
229                "-qq",
230                "-s",
231                "4096",
232                "-e",
233                "trace=%file,%process,getdents64,fchdir,close,dup,dup2,dup3,getcwd",
234                "-e",
235                "raw=execve,execveat",
236                "-o",
237            ])
238            .arg(self.directory.join(format!("command-{index}")))
239            .args(["--", "/bin/sh", "-c", text]);
240        command
241    }
242}
243
244#[derive(Clone, Debug)]
245pub struct PreparedTask {
246    pub id: String,
247    pub commands: Vec<String>,
248    pub parallel: bool,
249    pub cwd: PathBuf,
250    pub env: BTreeMap<OsString, OsString>,
251    /// Dotenv, explicit settings and local binary lookup without task metadata.
252    pub runtime_env: BTreeMap<OsString, OsString>,
253    pub node_path: Option<NodePath>,
254    /// Variables set for the process after `env` that are not part of what
255    /// the task is: nothing reads them to key it, such as a thread count.
256    pub execution: BTreeMap<OsString, OsString>,
257    /// Nx's `readyWhen`: text whose appearance in the output, every piece of
258    /// it, makes the task ready while its commands keep running.
259    pub ready_when: Vec<String>,
260    /// Set once all of `ready_when` has appeared.
261    pub ready: std::sync::Arc<std::sync::atomic::AtomicBool>,
262    /// A continuous target's own `readyWhen`, which tasks depending on it
263    /// with `waitFor: "ready"` wait for, or why it is invalid.
264    pub ready_check: Option<std::result::Result<readiness::ReadyCheck, String>>,
265    /// What `ready_check` concluded.
266    pub verdict: std::sync::Arc<readiness::Verdict>,
267    /// The process ids of its commands while they run, from which the runner
268    /// measures the memory the task uses.
269    pub processes: std::sync::Arc<std::sync::Mutex<std::collections::BTreeSet<u32>>>,
270    /// Each command's prefix and colours, by position; empty without any.
271    pub decorations: Vec<Decoration>,
272    /// Whether the task reads qk's stdin, and has the terminal while it runs.
273    /// The runner sets it for the one task a run is for, when that task has a
274    /// single command whose output goes straight to the terminal.
275    pub interactive: bool,
276    /// Whether captured commands may run under a pseudo-terminal, which they
277    /// do when qk's output is a terminal: Nx's `usePty`. Commands whose
278    /// output is hashed set it to false, so keys never depend on a terminal.
279    pub pseudo_terminal: bool,
280    /// Nx's `tty`: whether qk's terminal is in raw input mode while the
281    /// interactive task reads it through a pseudo-terminal.
282    pub tty: bool,
283    /// What the commands run under, for `--sandbox`.
284    pub sandbox: Option<Confinement>,
285    pub recording: Option<Recording>,
286    /// How the task's output is shown; the runner sets it per output style.
287    pub display: Display,
288}
289
290/// The dotenv files Nx loads for a task, most specific first: in the
291/// project root, then the workspace root, for the target and configuration
292/// (`.env.<target>.<configuration>`, `.env.<configuration>`, `.env.<target>`),
293/// then `.env.local`, `.local.env` and `.env`, each also in its `.local` and
294/// `.<name>.env` spellings. An atomized target also reads the files of the
295/// target it was split from, after its own of the same kind.
296fn dotenv_files(
297    project_root: &str,
298    target: &str,
299    configuration: Option<&str>,
300    non_atomized: Option<&str>,
301) -> Vec<String> {
302    let mut identifiers = Vec::new();
303    if let Some(configuration) = configuration {
304        identifiers.push(format!("{target}.{configuration}"));
305        if let Some(non_atomized) = non_atomized {
306            identifiers.push(format!("{non_atomized}.{configuration}"));
307        }
308        identifiers.push(configuration.to_owned());
309    }
310    identifiers.push(target.to_owned());
311    if let Some(non_atomized) = non_atomized {
312        identifiers.push(non_atomized.to_owned());
313    }
314    identifiers.push(String::new());
315    let variants = |identifier: &str, root: &str| -> Vec<String> {
316        let prefix = if root.is_empty() || root == "." {
317            String::new()
318        } else {
319            format!("{root}/")
320        };
321        if identifier.is_empty() {
322            [".env.local", ".local.env", ".env"]
323                .map(|name| format!("{prefix}{name}"))
324                .to_vec()
325        } else {
326            [
327                format!(".env.{identifier}.local"),
328                format!(".env.{identifier}"),
329                format!(".{identifier}.local.env"),
330                format!(".{identifier}.env"),
331            ]
332            .map(|name| format!("{prefix}{name}"))
333            .to_vec()
334        }
335    };
336    let mut files = Vec::new();
337    for root in [project_root, ""] {
338        for identifier in &identifiers {
339            // Colons in target names cannot name regular dotenv files on Windows.
340            #[cfg(windows)]
341            if identifier.contains(':') {
342                continue;
343            }
344            for file in variants(identifier, root) {
345                if !files.contains(&file) {
346                    files.push(file);
347                }
348            }
349        }
350    }
351    files
352}
353
354/// The target whose dotenv files a task reads, and the target it was
355/// atomized from, as Nx's getOwnerTargetForTask finds them: through the
356/// project's `metadata.targetGroups`, a target of the task's group that
357/// names a `metadata.nonAtomizedTarget`. Unlike Nx, a task that names one
358/// itself is its own owner, so each atomized target reads its own files
359/// rather than the first of its group's.
360fn owner_target<'a>(
361    project: &'a qk_config::Project,
362    target: &'a str,
363) -> (&'a str, Option<&'a str>) {
364    let non_atomized = |definition: &'a qk_config::Target| {
365        definition
366            .extra
367            .get("metadata")
368            .and_then(|metadata| metadata.get("nonAtomizedTarget"))
369            .and_then(Value::as_str)
370    };
371    if let Some((name, definition)) = project.targets.get_key_value(target)
372        && let Some(non_atomized) = non_atomized(definition)
373    {
374        return (name, Some(non_atomized));
375    }
376    let groups = project
377        .extra
378        .get("metadata")
379        .and_then(|metadata| metadata.get("targetGroups"))
380        .and_then(Value::as_object);
381    for group in groups.into_iter().flat_map(|groups| groups.values()) {
382        let members: Vec<&str> = group
383            .as_array()
384            .into_iter()
385            .flatten()
386            .filter_map(Value::as_str)
387            .collect();
388        if !members.contains(&target) {
389            continue;
390        }
391        for member in members {
392            if let Some((name, definition)) = project.targets.get_key_value(member)
393                && let Some(non_atomized) = non_atomized(definition)
394            {
395                return (name, Some(non_atomized));
396            }
397        }
398    }
399    (target, None)
400}
401
402/// The variable naming the tasks a task runs inside, one invocation a line.
403pub const INVOCATIONS: &str = "QK_TASK_INVOCATIONS";
404
405/// A task as an invocation: its id and arguments, which a task running inside
406/// it repeats only by recursing, as Nx's task invocation tracker tells it.
407pub fn invocation(task: &Task) -> String {
408    let mut text = task.id.clone();
409    for arg in &task.args {
410        text.push('\u{1f}');
411        text.push_str(arg);
412    }
413    text.replace('\n', " ")
414}
415
416/// The tasks this process runs inside, from [`INVOCATIONS`].
417pub fn enclosing_invocations() -> Vec<String> {
418    std::env::var(INVOCATIONS)
419        .map(|chain| chain.lines().map(str::to_owned).collect())
420        .unwrap_or_default()
421}
422
423/// The root dotenv files Nx loads into its own process at startup, in the
424/// order they take precedence.
425pub const ROOT_DOTENV_FILES: [&str; 3] = [".local.env", ".env.local", ".env"];
426
427/// A task's environment as Nx builds it: the process environment, without
428/// what the root dotenv files set in it at startup, then the task's dotenv
429/// files, loaded and expanded together, then the variables Nx sets for
430/// every task. A target's `env` applies on top.
431fn task_environment(
432    workspace: &Workspace,
433    task: &Task,
434    base_env: &BTreeMap<OsString, OsString>,
435) -> Result<(BTreeMap<OsString, OsString>, BTreeMap<OsString, OsString>)> {
436    let mut env = base_env.clone();
437    let project = &workspace.projects[&task.project];
438    if loads_dotenv(base_env) {
439        // As Nx's unloadDotEnvFiles: a variable whose value is a root file's
440        // goes, so the task's own files decide it. Nx unloads them even when
441        // it did not load them; qk keeps what the shell set then.
442        for file in [".env", ".local.env", ".env.local"] {
443            for (name, value) in dotenv::read_alone(&workspace.root.join(file))? {
444                if env.get(&name) == Some(&value) {
445                    env.remove(&name);
446                }
447            }
448        }
449        let (target, non_atomized) = owner_target(project, &task.target);
450        let files: Vec<PathBuf> = dotenv_files(
451            &project.root,
452            target,
453            task.configuration.as_deref(),
454            non_atomized,
455        )
456        .into_iter()
457        .map(|file| workspace.root.join(file))
458        .collect();
459        dotenv::load(&files, &mut env)?;
460    }
461    let runtime_env = env.clone();
462    // Nx's own variables from an enclosing run, which describe that run.
463    for name in [
464        "NX_SET_CLI",
465        "NX_ORIGINAL_FORCE_COLOR",
466        "NX_TERMINAL_OUTPUT_PATH",
467        "NX_STREAM_OUTPUT",
468        "NX_PREFIX_OUTPUT",
469        // A task of an enclosing run's signal socket and reported address:
470        // a task here neither signals for that task nor reads its address.
471        ready_signal::SOCKET_ENV,
472        ready_signal::TASK_ENV,
473        ready_signal::URL_ENV,
474    ] {
475        env.remove(std::ffi::OsStr::new(name));
476    }
477    let mut set = |name: &str, value: OsString| {
478        env.insert(name.into(), value);
479    };
480    set("NX_WORKSPACE_ROOT", workspace.root.clone().into_os_string());
481    set("NX_TASK_TARGET_PROJECT", task.project.clone().into());
482    set("NX_TASK_TARGET_TARGET", task.target.clone().into());
483    set("LERNA_PACKAGE_NAME", task.project.clone().into());
484    set("NX_TUI", "false".into());
485    // The qk and Nx processes this task runs inside, outermost first.
486    let ancestors = match base_env.get(std::ffi::OsStr::new("NX_INVOCATION_ANCESTOR_PIDS")) {
487        Some(inherited) if !inherited.is_empty() => {
488            format!("{},{}", inherited.to_string_lossy(), std::process::id())
489        }
490        _ => std::process::id().to_string(),
491    };
492    set("NX_INVOCATION_ANCESTOR_PIDS", ancestors.into());
493    // The tasks this one runs inside, outermost first, so a run it starts can
494    // tell when it would run one of them again.
495    let mut chain = base_env
496        .get(std::ffi::OsStr::new(INVOCATIONS))
497        .map(|chain| chain.to_string_lossy().into_owned())
498        .unwrap_or_default();
499    if !chain.is_empty() {
500        chain.push('\n');
501    }
502    chain.push_str(&invocation(task));
503    set(INVOCATIONS, chain.into());
504    if task.target == "test" {
505        set("NX_TERMINAL_CAPTURE_STDERR", "true".into());
506    }
507    match &task.configuration {
508        Some(configuration) => set("NX_TASK_TARGET_CONFIGURATION", configuration.clone().into()),
509        None => {
510            env.remove(std::ffi::OsStr::new("NX_TASK_TARGET_CONFIGURATION"));
511        }
512    }
513    Ok((env, runtime_env))
514}
515
516/// Whether dotenv files load: as in Nx, `NX_LOAD_DOT_ENV_FILES=false` turns
517/// them all off.
518fn loads_dotenv(env: &BTreeMap<OsString, OsString>) -> bool {
519    env.get(std::ffi::OsStr::new("NX_LOAD_DOT_ENV_FILES"))
520        .is_none_or(|value| value != "false")
521}
522
523/// The process environment with the root dotenv files loaded into it, as
524/// Nx loads them into its own process at startup, unless
525/// `NX_LOAD_DOT_ENV_FILES=false`. Variables already set win.
526pub fn environment(workspace: &Path) -> Result<BTreeMap<OsString, OsString>> {
527    let mut values: BTreeMap<OsString, OsString> = std::env::vars_os().collect();
528    if loads_dotenv(&values) {
529        let files: Vec<PathBuf> = ROOT_DOTENV_FILES
530            .iter()
531            .map(|file| workspace.join(file))
532            .collect();
533        dotenv::load(&files, &mut values)?;
534    }
535    Ok(values)
536}
537
538pub fn prepare(
539    workspace: &Workspace,
540    task: &Task,
541    base_env: &BTreeMap<OsString, OsString>,
542) -> Result<PreparedTask> {
543    let definition = &task.definition;
544    let executor = definition
545        .executor
546        .as_deref()
547        .context("target has no executor or command")?;
548    let project = &workspace.projects[&task.project];
549    let run_commands = executor == "nx:run-commands";
550    if !matches!(executor, "nx:run-commands" | "nx:run-script" | "nx:noop") {
551        bail!("unsupported executor {executor:?}");
552    }
553    // As in Nx, run-script reads only `script`, and noop nothing.
554    let options = if run_commands {
555        run_commands_overrides(&definition.options, &task.args, project)?
556    } else {
557        definition.options.clone()
558    };
559    let options = &options;
560    let empty = Map::new();
561    let mut interpolation = if run_commands {
562        interpolate::Interpolation::new(&task.args, options)?
563    } else {
564        interpolate::Interpolation::new(&[], &empty)?
565    };
566    let (mut env, mut runtime_env) = task_environment(workspace, task, base_env)?;
567    // As in Nx 24, `envFile` sets what the task's environment, dotenv files
568    // included, leaves unset or empty, beneath `env`; and the task's
569    // environment says whether it loads.
570    if let Some(file) = options.get("envFile").filter(|_| run_commands) {
571        let file = file.as_str().context("envFile must be a string")?;
572        let path = workspace.root.join(file);
573        if loads_dotenv(&runtime_env) {
574            if !path.is_file() {
575                bail!("envFile {file} does not exist");
576            }
577            dotenv::load(std::slice::from_ref(&path), &mut runtime_env)?;
578            dotenv::load(std::slice::from_ref(&path), &mut env)?;
579        }
580    }
581    // A target-level `env` is a qk extension, for every executor.
582    let option_env = options.get("env").filter(|_| run_commands);
583    for values in [definition.extra.get("env"), option_env]
584        .into_iter()
585        .flatten()
586    {
587        for (name, value) in values.as_object().context("env must be an object")? {
588            if name.is_empty() || name.contains(['=', '\0']) {
589                bail!("invalid environment variable name");
590            }
591            let value: OsString = value
592                .as_str()
593                .context("environment values must be strings")?
594                .into();
595            let name = environment_key(&env, name.into());
596            runtime_env.insert(name.clone(), value.clone());
597            env.insert(name, value);
598        }
599    }
600    interpolation.set_env(&env);
601    let mut execution = BTreeMap::new();
602    execution.insert(
603        "FORCE_COLOR".into(),
604        env.get(std::ffi::OsStr::new("FORCE_COLOR"))
605            .cloned()
606            .unwrap_or_else(|| "true".into()),
607    );
608    if run_commands && boolean(options.get("color"), false, "color")? {
609        execution.insert("FORCE_COLOR".into(), "true".into());
610    }
611    // run-commands runs where `cwd` says, the workspace root by default, and
612    // run-script in the project root.
613    let cwd = match options.get("cwd").filter(|_| run_commands) {
614        Some(cwd) => cwd.as_str().context("cwd must be a string")?,
615        None if run_commands => ".",
616        None => &project.root,
617    };
618    let cwd = workspace.root.join(cwd);
619    let mut resolved = PathBuf::new();
620    for part in cwd.components() {
621        match part {
622            std::path::Component::ParentDir => {
623                resolved.pop();
624            }
625            std::path::Component::CurDir => {}
626            _ => resolved.push(part.as_os_str()),
627        }
628    }
629    // Expose local binaries even for plain run-commands targets.
630    let mut paths = Vec::new();
631    for directory in resolved.ancestors() {
632        paths.push(directory.join("node_modules/.bin"));
633    }
634    let node_path = NodePath::new(&workspace.root, base_env, paths.len());
635    let path_key = path_key(&env);
636    if let Some(path) = env.get(&path_key) {
637        paths.extend(std::env::split_paths(path));
638    }
639    let path = std::env::join_paths(paths).context("cannot construct task PATH")?;
640    env.insert(path_key.clone(), path.clone());
641    runtime_env.insert(path_key, path);
642    let ready_when: Vec<String> = match options.get("readyWhen").filter(|_| run_commands) {
643        None | Some(Value::Null) => Vec::new(),
644        Some(Value::String(text)) => vec![text.clone()],
645        Some(value) => serde_json::from_value(value.clone())
646            .context("readyWhen must be a string or an array of strings")?,
647    };
648    // As in Nx, a single `command` runs in parallel exactly when it has
649    // `readyWhen`, whatever `parallel` says.
650    let parallel = if run_commands && options.get("command").is_some_and(merge_truthy) {
651        !ready_when.is_empty()
652    } else if run_commands {
653        boolean(options.get("parallel"), true, "parallel")?
654    } else {
655        true
656    };
657    let (mut pseudo_terminal, tty) = if run_commands {
658        (
659            boolean(options.get("usePty"), true, "usePty")?
660                && base_env
661                    .get(std::ffi::OsStr::new("NX_NATIVE_COMMAND_RUNNER"))
662                    .is_none_or(|value| value != "false"),
663            boolean(options.get("tty"), true, "tty")?,
664        )
665    } else {
666        (true, true)
667    };
668    let mut decorations = Vec::new();
669    if !ready_when.is_empty() && !parallel {
670        bail!("readyWhen can only be used when parallel is true");
671    }
672    let mut commands = Vec::new();
673    match executor {
674        "nx:noop" => {}
675        "nx:run-script" => {
676            let script = options
677                .get("script")
678                .and_then(Value::as_str)
679                .context("run-script requires options.script")?;
680            let package = workspace
681                .packages
682                .get(&task.project)
683                .context("run-script requires a package.json")?;
684            if !package.scripts.contains_key(script) {
685                bail!("package.json has no script {script:?}");
686            }
687            let manager = package_manager(workspace)?;
688            // Nx joins the arguments as they are; qk quotes each, so that
689            // one with a space or a shell character stays one argument.
690            let args = interpolate::quoted_arguments(&task.args)?;
691            let script = interpolate::quote(script)?;
692            // Nx's `run` for each: npm and bun need `--` before arguments,
693            // and yarn runs a script by name.
694            let mut command = match manager {
695                "yarn" => format!("yarn {script}"),
696                manager => format!("{manager} run {script}"),
697            };
698            if !args.is_empty() {
699                if matches!(manager, "npm" | "bun") {
700                    command.push_str(" --");
701                }
702                command.push(' ');
703                command.push_str(&args);
704            }
705            commands.push(command);
706        }
707        "nx:run-commands" => {
708            let forward = boolean(options.get("forwardAllArgs"), true, "forwardAllArgs")?;
709            // As in Nx, `command` wins over `commands`.
710            if let Some(command) = options
711                .get("command")
712                .filter(|command| merge_truthy(command))
713            {
714                // Nx also takes an array, joined with spaces.
715                let command = match command {
716                    Value::String(command) => command.clone(),
717                    Value::Array(words) => words
718                        .iter()
719                        .map(|word| word.as_str().context("command words must be strings"))
720                        .collect::<Result<Vec<_>>>()?
721                        .join(" "),
722                    _ => bail!("command must be a string or an array of strings"),
723                };
724                commands.push(interpolation.command(&command, forward)?);
725            } else if let Some(values) = options.get("commands") {
726                for value in values.as_array().context("commands must be an array")? {
727                    let (command, forward) = if let Some(command) = value.as_str() {
728                        (command, forward)
729                    } else {
730                        let value = value
731                            .as_object()
732                            .context("commands entries must be strings or objects")?;
733                        for key in value.keys() {
734                            if !matches!(
735                                key.as_str(),
736                                "command"
737                                    | "forwardAllArgs"
738                                    | "prefix"
739                                    | "prefixColor"
740                                    | "color"
741                                    | "bgColor"
742                                    | "description"
743                            ) {
744                                bail!("unsupported command entry field {key:?}");
745                            }
746                        }
747                        let field =
748                            |name: &str, background: Option<bool>| -> Result<Option<String>> {
749                                let Some(value) = value.get(name) else {
750                                    return Ok(None);
751                                };
752                                let text = value.as_str().with_context(|| {
753                                    format!("command entry {name} must be a string")
754                                })?;
755                                if let Some(background) = background
756                                    && !Decoration::valid(text, background)
757                                {
758                                    bail!("command entry {name} {text:?} is not a colour");
759                                }
760                                Ok(Some(text.to_owned()))
761                            };
762                        let decoration = Decoration {
763                            prefix: field("prefix", None)?,
764                            prefix_color: field("prefixColor", Some(false))?,
765                            color: field("color", Some(false))?,
766                            bg_color: field("bgColor", Some(true))?,
767                        };
768                        if !decoration.is_empty() && !parallel {
769                            bail!(
770                                "prefix, prefixColor, color and bgColor can only be set when parallel is true"
771                            );
772                        }
773                        decorations.resize(commands.len(), Decoration::default());
774                        decorations.push(decoration);
775                        (
776                            value
777                                .get("command")
778                                .and_then(Value::as_str)
779                                .context("command entry requires a command string")?,
780                            boolean(value.get("forwardAllArgs"), forward, "forwardAllArgs")?,
781                        )
782                    };
783                    commands.push(interpolation.command(command, forward)?);
784                }
785            } else {
786                bail!("run-commands requires command or commands");
787            }
788            // An empty `commands` succeeds without running anything, as in Nx.
789            if commands.iter().any(|command| command.trim().is_empty()) {
790                bail!("run-commands requires nonempty commands");
791            }
792        }
793        _ => bail!("unsupported executor {executor:?}"),
794    }
795    // As in Nx, commands run side by side share qk's output through pipes.
796    pseudo_terminal &= commands.len() == 1 || !parallel;
797    let ready_check = ready_check(workspace, task, options, &env)?;
798    Ok(PreparedTask {
799        id: task.id.clone(),
800        commands,
801        parallel,
802        cwd,
803        env,
804        runtime_env,
805        node_path: Some(node_path),
806        execution,
807        ready_when,
808        ready: Default::default(),
809        ready_check,
810        verdict: Default::default(),
811        processes: Default::default(),
812        decorations,
813        interactive: false,
814        pseudo_terminal,
815        tty,
816        sandbox: None,
817        recording: None,
818        display: Display::default(),
819    })
820}
821
822/// A continuous target's `readyWhen`, resolved against the options and the
823/// environment the task runs with, as Nx 24 resolves it. `null` turns off one
824/// a target default sets. A target that is not continuous has none.
825fn ready_check(
826    workspace: &Workspace,
827    task: &Task,
828    options: &Map<String, Value>,
829    env: &BTreeMap<OsString, OsString>,
830) -> Result<Option<std::result::Result<readiness::ReadyCheck, String>>> {
831    let Some(ready_when) = task
832        .definition
833        .extra
834        .get("readyWhen")
835        .filter(|value| !value.is_null() && task.definition.continuous == Some(true))
836    else {
837        return Ok(None);
838    };
839    let mut options = options.clone();
840    let project = &workspace.projects[&task.project];
841    for (key, value) in interpolate::yargs(&task.args, false, false) {
842        if key == "_" {
843            continue;
844        }
845        // As in Nx, an argument's tokens resolve for the task's project.
846        let value = match value {
847            Value::String(text) => Value::String(qk_config::interpolate_override(&text, project)?),
848            value => value,
849        };
850        if key.contains('-') {
851            options.insert(interpolate::camel_case(&key), value.clone());
852        }
853        options.insert(key, value);
854    }
855    let full_env = env;
856    let env: BTreeMap<String, String> = env
857        .iter()
858        .filter_map(|(name, value)| Some((name.to_str()?.to_owned(), value.to_str()?.to_owned())))
859        .collect();
860    let scope = readiness::Scope {
861        workspace_root: workspace
862            .root
863            .to_str()
864            .context("workspace path must be UTF-8")?,
865        project_root: &workspace.projects[&task.project].root,
866        project_name: &task.project,
867        options: &options,
868        env: &env,
869    };
870    let mut check = readiness::resolve(&task.id, ready_when, &scope);
871    // As in Nx 24, a command check runs as the task's commands do, and
872    // reads the options they run with.
873    if let Ok(Some(readiness::ReadyCheck {
874        probe: readiness::Probe::Command { env: probe_env, .. },
875        ..
876    })) = &mut check
877    {
878        *probe_env = full_env.clone();
879        probe_env.insert(
880            "NX_TASK_OPTIONS".into(),
881            serde_json::to_string(&options)?.into(),
882        );
883    }
884    Ok(check.map_err(|error| format!("{error:#}")).transpose())
885}
886
887/// The run-commands options Nx reads, which a task's arguments may set too.
888const OPTION_FLAGS: &[&str] = &[
889    "command",
890    "commands",
891    "color",
892    "parallel",
893    "readyWhen",
894    "cwd",
895    "args",
896    "envFile",
897    "__unparsed__",
898    "env",
899    "usePty",
900    "streamOutput",
901    "verbose",
902    "forwardAllArgs",
903    "tty",
904];
905
906/// The target's options with the task's arguments that name a run-commands
907/// option applied. As in Nx, `--cwd=dir`, `--no-parallel` or
908/// `--env.NAME=value` set the option rather than reaching the command.
909fn run_commands_overrides(
910    options: &serde_json::Map<String, Value>,
911    args: &[String],
912    project: &qk_config::Project,
913) -> Result<serde_json::Map<String, Value>> {
914    const BOOLEAN: &[&str] = &[
915        "color",
916        "parallel",
917        "usePty",
918        "streamOutput",
919        "verbose",
920        "forwardAllArgs",
921        "tty",
922    ];
923    let mut options = options.clone();
924    let mut ready = Vec::new();
925    let mut args = args.iter().peekable();
926    while let Some(arg) = args.next() {
927        let Some(flag) = arg.strip_prefix("--") else {
928            continue;
929        };
930        let (key, value) = match flag.split_once('=') {
931            Some((key, value)) => (key, Some(value.to_owned())),
932            None => (flag, None),
933        };
934        let (key, negated) = match key.strip_prefix("no-") {
935            Some(key) if OPTION_FLAGS.contains(&key) => (key, true),
936            _ => (key, false),
937        };
938        let (base, field) = match key.split_once('.') {
939            Some((base, field)) => (base, Some(field)),
940            None => (key, None),
941        };
942        if !OPTION_FLAGS.contains(&base) {
943            continue;
944        }
945        let boolean = BOOLEAN.contains(&base);
946        let value = match value {
947            Some(value) => value,
948            None if negated => "false".into(),
949            None if !boolean && args.peek().is_some_and(|next| !next.starts_with('-')) => {
950                args.next().unwrap().clone()
951            }
952            None => "true".into(),
953        };
954        let value = if boolean {
955            Value::Bool(value != "false")
956        } else {
957            // As in Nx, an argument's tokens resolve for the task's project.
958            Value::String(qk_config::interpolate_override(&value, project)?)
959        };
960        match (base, field) {
961            ("env", Some(name)) => {
962                let env = options
963                    .entry("env")
964                    .or_insert_with(|| Value::Object(Default::default()));
965                if let Value::Object(env) = env {
966                    env.insert(name.to_owned(), value);
967                }
968            }
969            ("readyWhen", None) => ready.push(value),
970            // Nx sets these itself, and a list cannot come from a flag.
971            ("__unparsed__" | "commands", _) | (_, Some(_)) => {}
972            (base, None) => {
973                options.insert(base.to_owned(), value);
974            }
975        }
976    }
977    if !ready.is_empty() {
978        let ready = if ready.len() == 1 {
979            ready.pop().unwrap()
980        } else {
981            Value::Array(ready)
982        };
983        options.insert("readyWhen".into(), ready);
984    }
985    Ok(options)
986}
987
988/// JavaScript truthiness, which decides what Nx reads as set.
989fn merge_truthy(value: &Value) -> bool {
990    match value {
991        Value::Null => false,
992        Value::Bool(flag) => *flag,
993        Value::Number(number) => number.as_f64().is_some_and(|number| number != 0.0),
994        Value::String(text) => !text.is_empty(),
995        Value::Array(_) | Value::Object(_) => true,
996    }
997}
998
999fn boolean(value: Option<&Value>, default: bool, name: &str) -> Result<bool> {
1000    value
1001        .map(|value| {
1002            value
1003                .as_bool()
1004                .with_context(|| format!("{name} must be boolean"))
1005        })
1006        .transpose()
1007        .map(|value| value.unwrap_or(default))
1008}
1009
1010fn package_manager(workspace: &Workspace) -> Result<&'static str> {
1011    workspace.package_manager_name()
1012}
1013
1014#[cfg(all(test, windows))]
1015mod dotenv_tests {
1016    #[test]
1017    fn colon_targets_keep_configuration_and_generic_dotenv_files() {
1018        let files = super::dotenv_files("app", "test:unit:watch", Some("ci"), None);
1019        assert!(files.iter().all(|file| !file.contains(':')));
1020        assert!(files.contains(&"app/.env.ci".to_owned()));
1021        assert!(files.contains(&"app/.env".to_owned()));
1022        assert!(files.contains(&".env.ci".to_owned()));
1023        assert!(files.contains(&".env".to_owned()));
1024    }
1025}