Skip to main content

qk_taskgraph/
lib.rs

1//! Expand requested targets into a validated task DAG before executing anything.
2
3use std::collections::{BTreeMap, BTreeSet};
4
5use anyhow::{Context, Result, bail};
6use qk_config::{Target, Workspace};
7use qk_graph::ProjectGraph;
8use serde::Serialize;
9use serde_json::{Map, Value};
10
11#[derive(Clone, Debug)]
12pub struct Request {
13    pub project: String,
14    pub target: String,
15    pub configuration: Option<String>,
16    /// Configuration name propagated to dependencies, before root fallback.
17    pub requested_configuration: Option<String>,
18    pub args: Vec<String>,
19}
20
21impl Request {
22    pub fn parse(value: &str) -> Result<Self> {
23        let parts: Vec<_> = value.split(':').collect();
24        if !(2..=3).contains(&parts.len()) || parts.iter().any(|part| part.is_empty()) {
25            bail!("expected project:target[:configuration], got {value:?}");
26        }
27        Ok(Self {
28            project: parts[0].into(),
29            target: parts[1].into(),
30            configuration: parts.get(2).map(|part| (*part).into()),
31            requested_configuration: parts.get(2).map(|part| (*part).into()),
32            args: Vec::new(),
33        })
34    }
35
36    /// Resolve a literal target name before treating its final segment as a configuration.
37    pub fn parse_in(workspace: &Workspace, value: &str) -> Result<Self> {
38        let Some((project, identifier)) = value.split_once(':') else {
39            return Self::parse(value);
40        };
41        if let Some(project_config) = workspace.projects.get(project) {
42            if project_config.targets.contains_key(identifier) {
43                return Ok(Self {
44                    project: project.into(),
45                    target: identifier.into(),
46                    configuration: None,
47                    requested_configuration: None,
48                    args: Vec::new(),
49                });
50            }
51            if let Some((target, configuration)) = identifier.rsplit_once(':')
52                && project_config.targets.contains_key(target)
53                && !configuration.is_empty()
54            {
55                return Ok(Self {
56                    project: project.into(),
57                    target: target.into(),
58                    configuration: Some(configuration.into()),
59                    requested_configuration: Some(configuration.into()),
60                    args: Vec::new(),
61                });
62            }
63        }
64        Self::parse(value)
65    }
66}
67
68#[derive(Clone, Debug, Serialize)]
69pub struct Task {
70    pub id: String,
71    pub project: String,
72    pub target: String,
73    pub configuration: Option<String>,
74    pub args: Vec<String>,
75    pub definition: Target,
76    pub dependencies: BTreeSet<String>,
77    /// The `inputs` and `waitFor` of the `dependsOn` entries that named each
78    /// dependency, where they differ from the defaults. They only matter for
79    /// a continuous dependency.
80    #[serde(skip_serializing_if = "BTreeMap::is_empty")]
81    pub edges: BTreeMap<String, Edge>,
82}
83
84impl Task {
85    /// How this task depends on `dependency`.
86    pub fn edge(&self, dependency: &str) -> Edge {
87        self.edges.get(dependency).copied().unwrap_or_default()
88    }
89}
90
91/// How a task depends on a continuous task, from its `dependsOn` entries.
92#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
93#[serde(rename_all = "camelCase")]
94pub struct Edge {
95    /// Whether the continuous task's inputs count for this task. Nx adds them
96    /// to the dependent's hash and affected selection unless the entry sets
97    /// `inputs: false`.
98    pub inputs: bool,
99    pub wait_for: WaitFor,
100}
101
102impl Default for Edge {
103    fn default() -> Self {
104        Self {
105            inputs: true,
106            wait_for: WaitFor::Started,
107        }
108    }
109}
110
111impl Edge {
112    /// Two entries naming one dependency, as Nx merges them: waiting for
113    /// readiness wins, and the inputs count unless every entry leaves them out.
114    fn merge(self, other: Self) -> Self {
115        Self {
116            inputs: self.inputs || other.inputs,
117            wait_for: self.wait_for.max(other.wait_for),
118        }
119    }
120}
121
122/// When a task depending on a continuous task starts: once that task has
123/// started, or once its target-level `readyWhen` check has passed.
124#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Serialize)]
125#[serde(rename_all = "camelCase")]
126pub enum WaitFor {
127    #[default]
128    Started,
129    Ready,
130}
131
132/// The dependencies `dependsOn` resolves to, with how each is depended on.
133#[derive(Default)]
134struct Dependencies {
135    ids: BTreeSet<String>,
136    edges: BTreeMap<String, Edge>,
137}
138
139impl Dependencies {
140    fn insert(&mut self, id: String, edge: Edge) {
141        let edge = if self.ids.insert(id.clone()) {
142            edge
143        } else {
144            self.edges.get(&id).copied().unwrap_or_default().merge(edge)
145        };
146        if edge == Edge::default() {
147            self.edges.remove(&id);
148        } else {
149            self.edges.insert(id, edge);
150        }
151    }
152}
153
154#[derive(Debug, Serialize)]
155pub struct TaskGraph {
156    pub roots: BTreeSet<String>,
157    pub tasks: BTreeMap<String, Task>,
158    /// The cycles broken under `ignore_cycles`, each as the tasks around it.
159    #[serde(skip)]
160    pub cycles: Vec<Vec<String>>,
161}
162
163/// Nx's options for building a task graph.
164#[derive(Clone, Copy, Debug, Default)]
165pub struct BuildOptions {
166    /// `--exclude-task-dependencies`: only the requested tasks, without the
167    /// tasks they depend on.
168    pub exclude_task_dependencies: bool,
169    /// `--nx-ignore-cycles`: break each cycle rather than failing, as Nx's
170    /// `makeAcyclic` does, by dropping the dependency that closes it.
171    pub ignore_cycles: bool,
172}
173
174impl TaskGraph {
175    pub fn build(workspace: &Workspace, requests: &[Request]) -> Result<Self> {
176        Self::build_with(workspace, requests, BuildOptions::default())
177    }
178
179    pub fn build_with(
180        workspace: &Workspace,
181        requests: &[Request],
182        options: BuildOptions,
183    ) -> Result<Self> {
184        if requests.is_empty() {
185            bail!("no tasks selected");
186        }
187        let projects = ProjectGraph::build(workspace)?;
188        let mut builder = Builder {
189            workspace,
190            projects,
191            tasks: BTreeMap::new(),
192            visiting: Vec::new(),
193            visited: BTreeSet::new(),
194            ignore_cycles: options.ignore_cycles,
195            cycles: Vec::new(),
196        };
197        let mut roots = BTreeSet::new();
198        for request in requests {
199            let cli = request.args.clone();
200            let visit = Visit {
201                request: request.clone(),
202                overrides: Map::new(),
203                cli: &cli,
204            };
205            roots.insert(builder.visit(visit, true)?.with_context(|| {
206                format!(
207                    "project {:?} has no target {:?}",
208                    request.project, request.target
209                )
210            })?);
211        }
212        let mut graph = Self {
213            roots,
214            tasks: builder.tasks,
215            cycles: builder.cycles,
216        };
217        if !graph.cycles.is_empty() {
218            graph.make_acyclic();
219        }
220        if options.exclude_task_dependencies {
221            let roots = graph.roots.clone();
222            graph.tasks.retain(|id, _| roots.contains(id));
223            for task in graph.tasks.values_mut() {
224                task.dependencies.retain(|id| roots.contains(id));
225                task.edges.retain(|id, _| roots.contains(id));
226            }
227        }
228        Ok(graph)
229    }
230
231    /// Nx's `makeAcyclic`: from each task, depth first, a dependency on a
232    /// task already on the path is dropped.
233    fn make_acyclic(&mut self) {
234        fn visit(
235            graph: &mut BTreeMap<String, Task>,
236            id: &str,
237            visited: &mut BTreeSet<String>,
238            path: &mut Vec<String>,
239        ) {
240            if !visited.insert(id.to_owned()) {
241                return;
242            }
243            let dependencies: Vec<String> = graph[id].dependencies.iter().cloned().collect();
244            for dependency in dependencies {
245                if path.contains(&dependency) {
246                    let task = graph.get_mut(id).unwrap();
247                    task.dependencies.remove(&dependency);
248                    task.edges.remove(&dependency);
249                } else {
250                    path.push(dependency.clone());
251                    visit(graph, &dependency, visited, path);
252                    path.pop();
253                }
254            }
255        }
256        let ids: Vec<String> = self.tasks.keys().cloned().collect();
257        let mut visited = BTreeSet::new();
258        for id in ids {
259            let mut path = vec![id.clone()];
260            visit(&mut self.tasks, &id, &mut visited, &mut path);
261        }
262    }
263}
264
265/// What one `dependsOn` entry declares: its requests, the options it
266/// forwards, whether its projects are dependencies rather than named ones,
267/// and how it depends on them.
268struct Declared {
269    requests: Vec<Request>,
270    overrides: Map<String, Value>,
271    derived: bool,
272    edge: Edge,
273}
274
275/// A task to visit: its request, the options a dependent forwards to it,
276/// and the run's own arguments, which `params: "forward"` passes on.
277struct Visit<'c> {
278    request: Request,
279    overrides: Map<String, Value>,
280    cli: &'c [String],
281}
282
283struct Builder<'a> {
284    workspace: &'a Workspace,
285    projects: ProjectGraph,
286    tasks: BTreeMap<String, Task>,
287    visiting: Vec<String>,
288    visited: BTreeSet<(String, Option<String>)>,
289    ignore_cycles: bool,
290    cycles: Vec<Vec<String>>,
291}
292
293impl Builder<'_> {
294    fn visit(&mut self, visit: Visit, required: bool) -> Result<Option<String>> {
295        let Visit {
296            request,
297            overrides,
298            cli,
299        } = visit;
300        let project = self
301            .workspace
302            .projects
303            .get(&request.project)
304            .with_context(|| format!("unknown project {:?}", request.project))?;
305        let Some(target) = project.targets.get(&request.target) else {
306            if required {
307                bail!(
308                    "project {:?} has no target {:?}",
309                    request.project,
310                    request.target
311                );
312            }
313            return Ok(None);
314        };
315        let requested_configuration = request.requested_configuration.clone();
316        let configuration = match request.configuration {
317            Some(name) if target.configurations.contains_key(&name) => Some(name),
318            Some(name) if required => bail!(
319                "{}:{} has no configuration {name:?}",
320                request.project,
321                request.target
322            ),
323            _ => target.default_configuration.clone(),
324        };
325        let id = format!(
326            "{}:{}{}",
327            request.project,
328            request.target,
329            configuration
330                .as_ref()
331                .map(|c| format!(":{c}"))
332                .unwrap_or_default()
333        );
334        if let Some(position) = self.visiting.iter().position(|task| task == &id) {
335            let mut cycle = self.visiting[position..].to_vec();
336            cycle.push(id.clone());
337            if !self.ignore_cycles {
338                bail!("task dependency cycle: {}", cycle.join(" -> "));
339            }
340            // The edge stays until the graph is complete, then the cycle is broken.
341            self.cycles.push(cycle);
342            return Ok(Some(id));
343        }
344        if self
345            .visited
346            .contains(&(id.clone(), requested_configuration.clone()))
347        {
348            return Ok(Some(id));
349        }
350        // As in Nx, a task reached again keeps the arguments and forwarded
351        // options it was first reached with.
352        let (args, definition) = match self.tasks.get(&id) {
353            Some(existing) => (existing.args.clone(), existing.definition.clone()),
354            None => {
355                let mut definition = target.clone();
356                if let Some(name) = &configuration {
357                    let options = target
358                        .configurations
359                        .get(name)
360                        .with_context(|| format!("{id}: default configuration does not exist"))?;
361                    for (key, value) in options {
362                        if key == "env" {
363                            value.as_object().with_context(|| {
364                                format!("{id}: configuration env must be an object")
365                            })?;
366                        }
367                        definition.options.insert(key.clone(), value.clone());
368                    }
369                }
370                // Forwarded options win over the target's own, as Nx's
371                // overrides do.
372                definition.options.extend(overrides);
373                (request.args, definition)
374            }
375        };
376        // As in Nx, creating a task validates its outputs.
377        if let Some(outputs) = &definition.outputs {
378            validate_outputs(outputs)?;
379        }
380        let task = Task {
381            id: id.clone(),
382            project: request.project,
383            target: request.target,
384            configuration,
385            args,
386            definition,
387            dependencies: BTreeSet::new(),
388            edges: BTreeMap::new(),
389        };
390        self.visiting.push(id.clone());
391        let mut dependencies = self
392            .tasks
393            .get(&id)
394            .map(|task| Dependencies {
395                ids: task.dependencies.clone(),
396                edges: task.edges.clone(),
397            })
398            .unwrap_or_default();
399        self.collect(
400            &task,
401            &task.project,
402            requested_configuration.as_deref(),
403            cli,
404            &mut dependencies,
405            &mut BTreeSet::new(),
406        )?;
407        self.visiting.pop();
408        self.visited.insert((id.clone(), requested_configuration));
409        self.tasks.insert(
410            id.clone(),
411            Task {
412                dependencies: dependencies.ids,
413                edges: dependencies.edges,
414                ..task
415            },
416        );
417        Ok(Some(id))
418    }
419
420    /// Resolves `dependsOn` with project dependencies taken from `derive_from`.
421    /// Like Nx, a dependency without the target stands in for the task: the
422    /// whole `dependsOn` list is applied again from there, so `^tsc` reaches
423    /// the nearest projects that have `tsc`.
424    fn collect(
425        &mut self,
426        task: &Task,
427        derive_from: &str,
428        requested_configuration: Option<&str>,
429        cli: &[String],
430        dependencies: &mut Dependencies,
431        seen: &mut BTreeSet<String>,
432    ) -> Result<()> {
433        if !seen.insert(derive_from.to_owned()) {
434            return Ok(());
435        }
436        for dependency in task.definition.depends_on.as_deref().unwrap_or_default() {
437            let Declared {
438                requests,
439                overrides,
440                derived,
441                edge,
442            } = self
443                .dependencies(task, derive_from, requested_configuration, cli, dependency)
444                .with_context(|| format!("dependsOn of {}", task.id))?;
445            for request in requests {
446                let has_target = self.workspace.projects[&request.project]
447                    .targets
448                    .contains_key(&request.target);
449                if derived && !has_target {
450                    let project = request.project;
451                    self.collect(
452                        task,
453                        &project,
454                        requested_configuration,
455                        cli,
456                        dependencies,
457                        seen,
458                    )?;
459                } else if request.project == task.project && request.target == task.target {
460                    // Reached back to itself through a dependency cycle.
461                    continue;
462                } else {
463                    let visit = Visit {
464                        request,
465                        overrides: overrides.clone(),
466                        cli,
467                    };
468                    if let Some(id) = self.visit(visit, false)? {
469                        dependencies.insert(id, edge);
470                    }
471                }
472            }
473        }
474        Ok(())
475    }
476
477    /// What one `dependsOn` entry declares, with project dependencies taken
478    /// from `derive_from`.
479    fn dependencies(
480        &self,
481        task: &Task,
482        derive_from: &str,
483        requested_configuration: Option<&str>,
484        cli: &[String],
485        value: &Value,
486    ) -> Result<Declared> {
487        let mut forward_options = false;
488        let mut edge = Edge::default();
489        let (target, projects, forward, derived) = match value {
490            Value::String(value) => {
491                if let Some(target) = value.strip_prefix('^') {
492                    (
493                        target.to_owned(),
494                        self.project_dependencies(derive_from),
495                        false,
496                        true,
497                    )
498                } else {
499                    let (project, target) = self.target_reference(value, &task.project);
500                    (target, vec![project], false, false)
501                }
502            }
503            Value::Object(object) => {
504                // As in Nx, fields it does not know are ignored, and any
505                // `params` or `options` but `forward` leaves them behind.
506                let target = object
507                    .get("target")
508                    .and_then(Value::as_str)
509                    .context("dependency target must be a string")?;
510                let forward = object.get("params").and_then(Value::as_str) == Some("forward");
511                forward_options = object.get("options").and_then(Value::as_str) == Some("forward");
512                if let Some(inputs) = object.get("inputs") {
513                    edge.inputs = inputs
514                        .as_bool()
515                        .context("dependency inputs must be boolean")?;
516                }
517                edge.wait_for = match object.get("waitFor").and_then(Value::as_str) {
518                    None if !object.contains_key("waitFor") => WaitFor::Started,
519                    Some("started") => WaitFor::Started,
520                    Some("ready") => WaitFor::Ready,
521                    _ => bail!("dependency waitFor must be started or ready"),
522                };
523                let dependencies = match object.get("dependencies") {
524                    Some(value) => value
525                        .as_bool()
526                        .context("dependency dependencies must be boolean")?,
527                    None => false,
528                };
529                // Named projects win over `dependencies: true`, as in Nx.
530                let projects = if let Some(selectors) = object.get("projects") {
531                    let selectors: Vec<String> = match selectors {
532                        // Nx 24 dropped the Nx 15 spellings: a task's own
533                        // project is no `projects`, its dependencies `^target`.
534                        Value::String(selector)
535                            if matches!(selector.as_str(), "self" | "dependencies")
536                                && !self.workspace.projects.contains_key(selector) =>
537                        {
538                            bail!(
539                                "projects: {selector:?} is no longer supported; leave projects \
540                                 out for the task's own project, or use dependencies: true"
541                            )
542                        }
543                        Value::String(selector) => vec![selector.clone()],
544                        value => serde_json::from_value(value.clone())
545                            .context("dependency projects must be a string or string array")?,
546                    };
547                    qk_config::find_matching_projects(
548                        &selectors,
549                        &self.workspace.projects,
550                        qk_config::Reader::Nx,
551                    )
552                } else if dependencies {
553                    self.project_dependencies(derive_from)
554                } else {
555                    vec![task.project.clone()]
556                };
557                let derived = dependencies && !object.contains_key("projects");
558                (target.to_owned(), projects, forward, derived)
559            }
560            _ => bail!("dependency must be a string or object"),
561        };
562        // As in Nx, a glob stands for every target name in the workspace it
563        // matches as minimatch matches it, each then depended on as if named.
564        let targets: Vec<String> = if qk_config::is_glob(&target) {
565            let matcher = qk_config::minimatch::Minimatch::new(&target, false);
566            self.workspace
567                .projects
568                .values()
569                .flat_map(|project| project.targets.keys())
570                .filter(|name| matcher.is_match(name))
571                .cloned()
572                .collect::<BTreeSet<_>>()
573                .into_iter()
574                .collect()
575        } else {
576            vec![target]
577        };
578        // As in Nx, `options: "forward"` passes the target's options, its
579        // configuration's included, as the dependency's own, and
580        // `params: "forward"` the run's arguments.
581        let mut overrides = Map::new();
582        if forward_options {
583            let source = &self.workspace.projects[&task.project].targets[&task.target];
584            overrides.extend(source.options.clone());
585            if let Some(options) = task
586                .configuration
587                .as_ref()
588                .and_then(|name| source.configurations.get(name))
589            {
590                overrides.extend(options.clone());
591            }
592        }
593        let args = if forward { cli.to_vec() } else { Vec::new() };
594        let requests = targets
595            .iter()
596            .flat_map(|target| {
597                projects.iter().map(|project| Request {
598                    project: project.clone(),
599                    target: target.clone(),
600                    configuration: requested_configuration.map(str::to_owned),
601                    requested_configuration: requested_configuration.map(str::to_owned),
602                    args: args.clone(),
603                })
604            })
605            .collect();
606        Ok(Declared {
607            requests,
608            overrides,
609            derived,
610            edge,
611        })
612    }
613
614    /// Nx's `readProjectAndTargetFromTargetString`: `project:target` when
615    /// the first part names a project, and otherwise a target of the task's
616    /// own project, colons and all.
617    fn target_reference(&self, text: &str, current: &str) -> (String, String) {
618        let parts = split_target(text, &self.workspace.projects, current);
619        match &parts[..] {
620            [target] => (current.to_owned(), target.clone()),
621            [project, rest @ ..] if self.workspace.projects.contains_key(project) => {
622                (project.clone(), rest.join(":"))
623            }
624            _ => (current.to_owned(), text.to_owned()),
625        }
626    }
627
628    fn project_dependencies(&self, project: &str) -> Vec<String> {
629        // A pair can have several edge kinds; each dependency counts once.
630        self.projects.dependencies[project]
631            .iter()
632            .map(|edge| edge.target.clone())
633            .collect::<BTreeSet<_>>()
634            .into_iter()
635            .collect()
636    }
637}
638
639/// Nx's `splitByColons`: parts between colons, a double-quoted run keeping
640/// its colons.
641fn split_by_colons(text: &str) -> Vec<String> {
642    let mut parts = vec![String::new()];
643    let mut chars = text.chars();
644    while let Some(c) = chars.next() {
645        match c {
646            ':' => parts.push(String::new()),
647            '"' => {
648                for c in chars.by_ref() {
649                    if c == '"' {
650                        break;
651                    }
652                    parts.last_mut().unwrap().push(c);
653                }
654            }
655            c => parts.last_mut().unwrap().push(c),
656        }
657    }
658    parts
659}
660
661/// Every `[project, target?, configuration?]` reading of the parts that
662/// names a project and, when given, one of its targets and configurations;
663/// read on the current project too, as a bare target.
664fn readings(
665    parts: &[String],
666    projects: &BTreeMap<String, qk_config::Project>,
667    current: &str,
668) -> Vec<Vec<String>> {
669    let mut readings = Vec::new();
670    let read = |project: &str, rest: &[String], readings: &mut Vec<Vec<String>>| {
671        let targets = &projects[project].targets;
672        for end in 1..=rest.len() {
673            let target = rest[..end].join(":");
674            let Some(definition) = targets.get(&target) else {
675                continue;
676            };
677            if end == rest.len() {
678                readings.push(vec![project.to_owned(), target]);
679            } else {
680                let configuration = rest[end..].join(":");
681                if definition.configurations.contains_key(&configuration) {
682                    readings.push(vec![project.to_owned(), target, configuration]);
683                }
684            }
685        }
686    };
687    if projects.contains_key(current) {
688        read(current, parts, &mut readings);
689    }
690    for end in 1..=parts.len() {
691        let project = parts[..end].join(":");
692        if !projects.contains_key(&project) {
693            continue;
694        }
695        if end == parts.len() {
696            readings.push(vec![project]);
697        } else {
698            read(&project, &parts[end..], &mut readings);
699        }
700    }
701    readings
702}
703
704/// The reading Nx prefers: on the current project, then the longest
705/// project, target and configuration names.
706fn best(readings: Vec<Vec<String>>, current: &str) -> Option<Vec<String>> {
707    let rank = |reading: &Vec<String>| {
708        let length = |index: usize| reading.get(index).map_or(0, String::len);
709        (reading[0] == current, length(0), length(1), length(2))
710    };
711    let mut best: Option<Vec<String>> = None;
712    for reading in readings {
713        if best.as_ref().is_none_or(|best| rank(&reading) > rank(best)) {
714            best = Some(reading);
715        }
716    }
717    best
718}
719
720/// Nx's `splitTargetFromNodes`.
721fn split_target(
722    text: &str,
723    projects: &BTreeMap<String, qk_config::Project>,
724    current: &str,
725) -> Vec<String> {
726    let parts = split_by_colons(text);
727    if let Some(best) = best(readings(&parts, projects, current), current) {
728        return best;
729    }
730    let colon = match text.find(':') {
731        Some(0) => text[1..].find(':').map(|index| index + 1),
732        colon => colon,
733    };
734    if colon.is_none_or(|colon| colon == 0) {
735        return vec![text.to_owned()];
736    }
737    let mut project = parts[0].clone();
738    let mut rest = parts[1..].to_vec();
739    // A leading colon starts the longest known project name it can.
740    if project.is_empty() && !rest.is_empty() {
741        let mut absorbed = 1;
742        for count in (1..rest.len()).rev() {
743            if projects.contains_key(&format!(":{}", rest[..count].join(":"))) {
744                absorbed = count;
745                break;
746            }
747        }
748        project = format!(":{}", rest[..absorbed].join(":"));
749        rest.drain(..absorbed);
750    }
751    let Some(configuration) = rest.last().cloned() else {
752        return vec![project];
753    };
754    // The project and target, with a configuration they do not have.
755    if let Some(head) = text.get(..text.len().saturating_sub(configuration.len() + 1))
756        && let Some(best) = best(readings(&split_by_colons(head), projects, current), current)
757        && best.len() == 2
758    {
759        return vec![best[0].clone(), best[1].clone(), configuration];
760    }
761    let mut result = vec![project.clone()];
762    let targets = projects.get(&project).map(|project| &project.targets);
763    for end in (1..=rest.len()).rev() {
764        let target = rest[..end].join(":");
765        if targets.is_some_and(|targets| targets.contains_key(&target)) {
766            result.push(target);
767            if end < rest.len() {
768                result.push(rest[end..].join(":"));
769            }
770            return result;
771        }
772    }
773    result.extend(rest);
774    result
775}
776
777/// Nx 24's `validateOutputs`: each output starts with a token, `!` before it
778/// if negated, and one from `{workspaceRoot}` names a directory before any
779/// pattern. The error lists every output that breaks either rule, in Nx's
780/// words.
781pub fn validate_outputs(outputs: &[String]) -> Result<()> {
782    let missing_prefix: Vec<&String> = outputs
783        .iter()
784        .filter(|output| {
785            // Nx's `^!?\{[\s\S]+\}`: a `{` and, after at least one more
786            // character, a `}`.
787            let mut chars = output.strip_prefix('!').unwrap_or(output).chars();
788            !(chars.next() == Some('{') && chars.next().is_some() && chars.any(|c| c == '}'))
789        })
790        .collect();
791    let workspace_globs: Vec<&String> = outputs
792        .iter()
793        .filter(|output| !missing_prefix.contains(output))
794        .filter(|output| {
795            ["!{workspaceRoot}", "{workspaceRoot}"]
796                .iter()
797                .any(|prefix| output.strip_prefix(prefix).is_some_and(has_no_directory))
798        })
799        .collect();
800    let mut message = String::new();
801    if !missing_prefix.is_empty() {
802        message.push_str(&format!(
803            "The following outputs are invalid: \n{}\n\nRun `nx repair` to fix this.",
804            missing_prefix
805                .iter()
806                .map(|output| format!(
807                    " - {output}\n   ** Reason: Outputs must start with either \"{{workspaceRoot}}/\" or \"{{projectRoot}}/\"."
808                ))
809                .collect::<Vec<_>>()
810                .join("\n")
811        ));
812    }
813    if !workspace_globs.is_empty() {
814        message.push_str(&format!(
815            "The following outputs are defined by a glob pattern from the workspace root: \n - {}\n\nThese can be slow, replace them with a more specific pattern.",
816            workspace_globs
817                .iter()
818                .map(|output| output.as_str())
819                .collect::<Vec<_>>()
820                .join("\n - ")
821        ));
822    }
823    if message.is_empty() {
824        Ok(())
825    } else {
826        bail!(message)
827    }
828}
829
830/// Whether `glob` has a pattern before any named directory, as Nx's
831/// `partition_glob` reads it: its leading segments with no glob syntax once
832/// their escapes are read join to nothing. `@` and `+` start no pattern
833/// without a group after them.
834fn has_no_directory(glob: &str) -> bool {
835    let mut directory = Vec::new();
836    for segment in glob.split('/') {
837        let mut syntax = false;
838        let mut chars = segment.chars();
839        while let Some(c) = chars.next() {
840            match c {
841                '\\' => {
842                    chars.next();
843                }
844                '*' | '?' | '[' | ']' | '{' | '}' | '(' | ')' | '!' => syntax = true,
845                _ => {}
846            }
847        }
848        if syntax {
849            return directory.join("/").is_empty();
850        }
851        directory.push(segment);
852    }
853    false
854}