Skip to main content

fallow_config/workspace/
pnpm_overrides.rs

1//! Parser for the `overrides:` section of `pnpm-workspace.yaml` and the
2//! `pnpm.overrides` section of a root `package.json`.
3//!
4//! pnpm supports forcing transitive dependency versions through two equivalent
5//! locations:
6//!
7//! ```yaml
8//! # pnpm-workspace.yaml (pnpm 9+, canonical)
9//! overrides:
10//!   axios: ^1.6.0
11//!   "@types/react@<18": "18.0.0"
12//!   "react>react-dom": ^17
13//! ```
14//!
15//! ```json
16//! // package.json (legacy form, still supported)
17//! { "pnpm": { "overrides": { "axios": "^1.6.0" } } }
18//! ```
19//!
20//! For the unused-dependency-override and misconfigured-dependency-override
21//! detectors we need both the structured map of entries and the 1-based line
22//! number of each entry in the source so findings can point users to the exact
23//! line. `serde_yaml_ng` and `serde_json` give us the structural parse; a second
24//! targeted scan over the raw source recovers the line numbers.
25//!
26//! The detector treats the following key shapes as valid pnpm syntax:
27//! - `axios` (bare package)
28//! - `@scope/pkg` (scoped package)
29//! - `axios@>=1.0.0` (version selector on the overridden package)
30//! - `react>react-dom` (parent matcher; override `react-dom` only inside `react`'s subtree)
31//! - `react@1>zoo` (parent matcher with version selector on the parent)
32//! - `@scope/parent>@scope/child` (scoped packages on both sides)
33//!
34//! Special values that are valid pnpm syntax and must NOT be flagged as
35//! misconfigured: `-` (removal), `$ref` (self-reference to a workspace dep),
36//! `npm:alias@^1` (npm-protocol alias).
37
38use std::path::Path;
39
40use super::pnpm_catalog::{parse_key, strip_inline_comment};
41
42/// Where an override entry was declared.
43#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
44#[serde(rename_all = "snake_case")]
45pub enum OverrideSource {
46    /// Top-level `overrides:` in `pnpm-workspace.yaml`.
47    PnpmWorkspaceYaml,
48    /// `pnpm.overrides` in a root `package.json`.
49    PnpmPackageJson,
50}
51
52/// Structured override data extracted from one source.
53#[derive(Debug, Clone, Default)]
54pub struct PnpmOverrideData {
55    /// Entries declared in source order.
56    pub entries: Vec<PnpmOverrideEntry>,
57}
58
59/// A single override entry.
60#[derive(Debug, Clone)]
61pub struct PnpmOverrideEntry {
62    /// The full original key as written in the source (e.g.
63    /// `"react>react-dom"`, `"@types/react@<18"`). Preserved for round-trip
64    /// reporting so agents see the unmodified spelling.
65    pub raw_key: String,
66    /// Parsed structure of the key. `None` when the key cannot be parsed into
67    /// a pnpm-recognised shape; in that case the entry is reported as
68    /// misconfigured rather than checked for usage.
69    pub parsed_key: Option<ParsedOverrideKey>,
70    /// The right-hand side of the entry (the version pnpm should force).
71    /// `None` when the value is missing or unparsable.
72    pub raw_value: Option<String>,
73    /// 1-based line number of the entry within the source file.
74    pub line: u32,
75}
76
77/// Parsed structure of an override key.
78#[derive(Debug, Clone, PartialEq, Eq)]
79pub struct ParsedOverrideKey {
80    /// Optional parent package (left side of `>`). `None` for bare-target keys.
81    pub parent_package: Option<String>,
82    /// Optional version selector on the parent (e.g. `react@1>zoo` has
83    /// `parent_version_selector = Some("1")`).
84    pub parent_version_selector: Option<String>,
85    /// The target package name (the entry pnpm rewrites).
86    pub target_package: String,
87    /// Optional version selector on the target (e.g. `@types/react@<18` has
88    /// `target_version_selector = Some("<18")`).
89    pub target_version_selector: Option<String>,
90}
91
92/// Parse the `overrides:` section of `pnpm-workspace.yaml`. Returns an empty
93/// `PnpmOverrideData` when the file has no overrides or when the section is
94/// present but empty. Malformed YAML is an `Err` carrying the parse error text
95/// so callers can surface a workspace diagnostic instead of silently dropping
96/// every entry.
97pub fn parse_pnpm_workspace_overrides(source: &str) -> Result<PnpmOverrideData, String> {
98    let value: serde_yaml_ng::Value =
99        serde_yaml_ng::from_str(source).map_err(|error| error.to_string())?;
100    let Some(mapping) = value.as_mapping() else {
101        return Ok(PnpmOverrideData::default());
102    };
103    let Some(overrides_value) = mapping.get("overrides") else {
104        return Ok(PnpmOverrideData::default());
105    };
106    let Some(overrides_map) = overrides_value.as_mapping() else {
107        return Ok(PnpmOverrideData::default());
108    };
109
110    let line_index = build_yaml_line_index(source);
111    let entries = overrides_map
112        .iter()
113        .filter_map(|(k, v)| {
114            let raw_key = k.as_str()?.to_string();
115            let raw_value = match v {
116                serde_yaml_ng::Value::String(s) => Some(s.clone()),
117                serde_yaml_ng::Value::Null => None,
118                other => Some(yaml_value_to_string(other)),
119            };
120            let line = line_index.line_for(&raw_key)?;
121            let parsed_key = parse_override_key(&raw_key);
122            Some(PnpmOverrideEntry {
123                raw_key,
124                parsed_key,
125                raw_value,
126                line,
127            })
128        })
129        .collect();
130
131    Ok(PnpmOverrideData { entries })
132}
133
134/// Parse the `pnpm.overrides` section of a root `package.json`. Returns an
135/// empty `PnpmOverrideData` when the file has no overrides, when the JSON is
136/// malformed, or when the section is present but empty.
137#[must_use]
138pub fn parse_pnpm_package_json_overrides(source: &str) -> PnpmOverrideData {
139    let value: serde_json::Value = match serde_json::from_str(source) {
140        Ok(v) => v,
141        Err(_) => return PnpmOverrideData::default(),
142    };
143    let Some(overrides) = value.get("pnpm").and_then(|p| p.get("overrides")) else {
144        return PnpmOverrideData::default();
145    };
146    let Some(overrides_obj) = overrides.as_object() else {
147        return PnpmOverrideData::default();
148    };
149
150    let line_index = build_package_json_line_index(source);
151    let entries = overrides_obj
152        .iter()
153        .filter_map(|(raw_key, v)| {
154            let raw_value = match v {
155                serde_json::Value::String(s) => Some(s.clone()),
156                serde_json::Value::Null => None,
157                other => Some(other.to_string()),
158            };
159            let line = line_index.line_for(raw_key)?;
160            let parsed_key = parse_override_key(raw_key);
161            Some(PnpmOverrideEntry {
162                raw_key: raw_key.clone(),
163                parsed_key,
164                raw_value,
165                line,
166            })
167        })
168        .collect();
169
170    PnpmOverrideData { entries }
171}
172
173/// Parse an override key into `parent`, `target`, and optional version
174/// selectors. Returns `None` when the key cannot be split into a recognised
175/// shape (empty key, parent or target missing).
176#[must_use]
177pub fn parse_override_key(key: &str) -> Option<ParsedOverrideKey> {
178    let trimmed = key.trim();
179    if trimmed.is_empty() || trimmed.starts_with('>') {
180        return None;
181    }
182
183    let delimiter = trimmed
184        .as_bytes()
185        .iter()
186        .enumerate()
187        .skip(1)
188        .find_map(|(index, byte)| {
189            (*byte == b'>' && !matches!(trimmed.as_bytes()[index - 1], b' ' | b'|' | b'@'))
190                .then_some(index)
191        });
192    let (parent_part, target_part) = if let Some(idx) = delimiter {
193        (Some(trimmed[..idx].trim()), trimmed[idx + 1..].trim())
194    } else {
195        (None, trimmed)
196    };
197
198    let (target_package, target_version_selector) = split_pkg_and_selector(target_part)?;
199
200    let (parent_package, parent_version_selector) = match parent_part {
201        Some(parent) if !parent.is_empty() => {
202            let (pkg, selector) = split_pkg_and_selector(parent)?;
203            (Some(pkg), selector)
204        }
205        Some(_) => return None,
206        None => (None, None),
207    };
208
209    Some(ParsedOverrideKey {
210        parent_package,
211        parent_version_selector,
212        target_package,
213        target_version_selector,
214    })
215}
216
217/// Split a `pkg@selector` segment into `(package_name, Option<selector>)`.
218/// Handles scoped packages (`@scope/name@<2`) by skipping the leading `@`.
219/// Returns `None` when the package name is empty.
220pub fn split_pkg_and_selector(segment: &str) -> Option<(String, Option<String>)> {
221    let trimmed = segment.trim();
222    if trimmed.is_empty() {
223        return None;
224    }
225
226    let bytes = trimmed.as_bytes();
227    let scoped = bytes.first().copied() == Some(b'@');
228    let start = usize::from(scoped);
229    let at_pos = trimmed[start..].find('@').map(|i| i + start);
230
231    let (pkg, selector) = match at_pos {
232        Some(pos) => (
233            trimmed[..pos].to_string(),
234            Some(trimmed[pos + 1..].to_string()),
235        ),
236        None => (trimmed.to_string(), None),
237    };
238
239    if pkg.is_empty() {
240        return None;
241    }
242    Some((pkg, selector))
243}
244
245/// Check whether `value` is a valid pnpm override right-hand side, even if it
246/// is not a semver range. Returns `false` when the value is empty, contains a
247/// raw newline, or is otherwise garbage.
248#[must_use]
249pub fn is_valid_override_value(value: &str) -> bool {
250    let trimmed = value.trim();
251    if trimmed.is_empty() {
252        return false;
253    }
254    if trimmed.contains('\n') {
255        return false;
256    }
257    true
258}
259
260/// Convenience: is this entry effectively a misconfiguration the user should
261/// see as an error?
262#[must_use]
263pub fn override_misconfig_reason(entry: &PnpmOverrideEntry) -> Option<MisconfigReason> {
264    if entry.parsed_key.is_none() {
265        return Some(MisconfigReason::UnparsableKey);
266    }
267    match &entry.raw_value {
268        None => Some(MisconfigReason::EmptyValue),
269        Some(v) if !is_valid_override_value(v) => Some(MisconfigReason::EmptyValue),
270        _ => None,
271    }
272}
273
274/// Why an override entry is misconfigured.
275#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
276#[serde(rename_all = "kebab-case")]
277pub enum MisconfigReason {
278    /// The override key cannot be parsed into a recognised pnpm shape.
279    UnparsableKey,
280    /// The override value is missing or empty.
281    EmptyValue,
282}
283
284impl MisconfigReason {
285    /// Human-readable description.
286    #[must_use]
287    pub const fn describe(self) -> &'static str {
288        match self {
289            Self::UnparsableKey => "override key cannot be parsed",
290            Self::EmptyValue => "override value is missing or empty",
291        }
292    }
293}
294
295struct YamlLineIndex {
296    entries: Vec<(String, u32)>,
297}
298
299impl YamlLineIndex {
300    fn line_for(&self, key: &str) -> Option<u32> {
301        self.entries
302            .iter()
303            .find(|(k, _)| k == key)
304            .map(|(_, line)| *line)
305    }
306}
307
308/// Walk the raw YAML source to map each `overrides:` entry key to its 1-based
309/// line number. Mirrors the catalog parser's section-aware scanner.
310fn build_yaml_line_index(source: &str) -> YamlLineIndex {
311    let mut entries = Vec::new();
312    let mut in_overrides = false;
313
314    for (idx, raw_line) in source.lines().enumerate() {
315        let line_no = u32::try_from(idx).unwrap_or(u32::MAX).saturating_add(1);
316        let trimmed = strip_inline_comment(raw_line);
317        let trimmed_left = trimmed.trim_start();
318        let indent = trimmed.len() - trimmed_left.len();
319
320        if trimmed_left.is_empty() {
321            continue;
322        }
323
324        if indent == 0 {
325            in_overrides = trimmed_left.starts_with("overrides:");
326            continue;
327        }
328
329        if in_overrides && let Some(key) = parse_key(trimmed_left) {
330            entries.push((key, line_no));
331        }
332    }
333
334    YamlLineIndex { entries }
335}
336
337/// Walk a raw `package.json` source string to map each `pnpm.overrides` entry
338/// key to its 1-based line number. The scan tracks brace depth so nested
339/// objects under unrelated keys (e.g., `dependenciesMeta`) cannot be misread
340/// as override entries.
341/// Char-by-char brace-depth scanner state for the `pnpm.overrides` line index.
342#[derive(Default)]
343struct OverridesJsonScan {
344    entries: Vec<(String, u32)>,
345    depth: i32,
346    pnpm_depth: Option<i32>,
347    in_overrides_depth: Option<i32>,
348    in_string: bool,
349    escape: bool,
350    last_key: Option<String>,
351    key_buf: String,
352    collecting_key: bool,
353}
354
355impl OverridesJsonScan {
356    /// Handle one character while inside a quoted string, buffering key text and
357    /// closing the string on an unescaped quote.
358    fn consume_in_string_char(&mut self, ch: char) {
359        if self.escape {
360            if self.collecting_key {
361                self.key_buf.push(ch);
362            }
363            self.escape = false;
364            return;
365        }
366        if ch == '\\' {
367            self.escape = true;
368            if self.collecting_key {
369                self.key_buf.push(ch);
370            }
371            return;
372        }
373        if ch == '"' {
374            self.in_string = false;
375            if self.collecting_key {
376                self.last_key = Some(std::mem::take(&mut self.key_buf));
377                self.collecting_key = false;
378            }
379            return;
380        }
381        if self.collecting_key {
382            self.key_buf.push(ch);
383        }
384    }
385
386    /// Handle one structural character outside any string: brace depth, the
387    /// `pnpm`/`overrides` section transitions, and entry recording on `:`.
388    fn consume_structural_char(&mut self, ch: char, current_line: u32) {
389        match ch {
390            '"' => {
391                self.in_string = true;
392                self.collecting_key = true;
393                self.key_buf.clear();
394            }
395            '{' => self.depth += 1,
396            '}' => {
397                if Some(self.depth) == self.in_overrides_depth {
398                    self.in_overrides_depth = None;
399                }
400                if Some(self.depth) == self.pnpm_depth {
401                    self.pnpm_depth = None;
402                }
403                self.depth -= 1;
404            }
405            ':' => self.record_key_after_colon(current_line),
406            ',' => {
407                self.last_key = None;
408            }
409            _ => {}
410        }
411    }
412
413    /// On a `:`, enter the `pnpm` or `overrides` section, or record an override
414    /// entry when the depth places the key inside `pnpm.overrides`.
415    fn record_key_after_colon(&mut self, current_line: u32) {
416        let Some(key) = self.last_key.take() else {
417            return;
418        };
419        if self.pnpm_depth.is_none() && self.depth == 1 && key == "pnpm" {
420            self.pnpm_depth = Some(self.depth);
421        } else if self.in_overrides_depth.is_none()
422            && self.pnpm_depth.is_some()
423            && self.depth == self.pnpm_depth.unwrap_or(0) + 1
424            && key == "overrides"
425        {
426            self.in_overrides_depth = Some(self.depth);
427        } else if let Some(d) = self.in_overrides_depth
428            && self.depth == d + 1
429        {
430            self.entries.push((key, current_line));
431        }
432    }
433}
434
435fn build_package_json_line_index(source: &str) -> YamlLineIndex {
436    let mut scan = OverridesJsonScan::default();
437    let mut current_line = 1u32;
438
439    for ch in source.chars() {
440        if ch == '\n' {
441            current_line += 1;
442        }
443
444        if scan.in_string {
445            scan.consume_in_string_char(ch);
446        } else {
447            scan.consume_structural_char(ch, current_line);
448        }
449    }
450
451    YamlLineIndex {
452        entries: scan.entries,
453    }
454}
455
456fn yaml_value_to_string(value: &serde_yaml_ng::Value) -> String {
457    match value {
458        serde_yaml_ng::Value::String(s) => s.clone(),
459        serde_yaml_ng::Value::Number(n) => n.to_string(),
460        serde_yaml_ng::Value::Bool(b) => b.to_string(),
461        serde_yaml_ng::Value::Null => String::new(),
462        _ => serde_yaml_ng::to_string(value).unwrap_or_default(),
463    }
464}
465
466/// Source-name string for diagnostics.
467#[must_use]
468pub fn override_source_label(source: OverrideSource, path: &Path) -> String {
469    match source {
470        OverrideSource::PnpmWorkspaceYaml => "pnpm-workspace.yaml".to_string(),
471        OverrideSource::PnpmPackageJson => path.display().to_string(),
472    }
473}
474
475#[cfg(test)]
476mod tests {
477    use super::*;
478
479    #[test]
480    fn parse_bare_target() {
481        let parsed = parse_override_key("axios").unwrap();
482        assert_eq!(parsed.target_package, "axios");
483        assert!(parsed.parent_package.is_none());
484        assert!(parsed.target_version_selector.is_none());
485    }
486
487    #[test]
488    fn parse_scoped_target() {
489        let parsed = parse_override_key("@types/react").unwrap();
490        assert_eq!(parsed.target_package, "@types/react");
491        assert!(parsed.target_version_selector.is_none());
492    }
493
494    #[test]
495    fn parse_target_with_version_selector() {
496        let parsed = parse_override_key("@types/react@<18").unwrap();
497        assert_eq!(parsed.target_package, "@types/react");
498        assert_eq!(parsed.target_version_selector.as_deref(), Some("<18"));
499    }
500
501    #[test]
502    fn parse_target_with_greater_than_version_selector() {
503        let parsed = parse_override_key("a>b@>=1").unwrap();
504        assert_eq!(parsed.parent_package.as_deref(), Some("a"));
505        assert_eq!(parsed.target_package, "b");
506        assert_eq!(parsed.target_version_selector.as_deref(), Some(">=1"));
507    }
508
509    #[test]
510    fn parse_parent_chain() {
511        let parsed = parse_override_key("react>react-dom").unwrap();
512        assert_eq!(parsed.parent_package.as_deref(), Some("react"));
513        assert_eq!(parsed.target_package, "react-dom");
514    }
515
516    #[test]
517    fn parse_parent_chain_with_selectors() {
518        let parsed = parse_override_key("react@1>zoo").unwrap();
519        assert_eq!(parsed.parent_package.as_deref(), Some("react"));
520        assert_eq!(parsed.parent_version_selector.as_deref(), Some("1"));
521        assert_eq!(parsed.target_package, "zoo");
522    }
523
524    #[test]
525    fn parse_scoped_parent_and_target() {
526        let parsed = parse_override_key("@react-spring/web>@react-spring/core").unwrap();
527        assert_eq!(parsed.parent_package.as_deref(), Some("@react-spring/web"));
528        assert_eq!(parsed.target_package, "@react-spring/core");
529    }
530
531    #[test]
532    fn parse_empty_returns_none() {
533        assert!(parse_override_key("").is_none());
534        assert!(parse_override_key("   ").is_none());
535    }
536
537    #[test]
538    fn parse_dangling_separator_returns_none() {
539        assert!(parse_override_key("react>").is_none());
540        assert!(parse_override_key(">react-dom").is_none());
541    }
542
543    #[test]
544    fn is_valid_override_value_accepts_pnpm_idioms() {
545        assert!(is_valid_override_value("^1.6.0"));
546        assert!(is_valid_override_value("-"));
547        assert!(is_valid_override_value("$foo"));
548        assert!(is_valid_override_value("npm:@scope/alias@^1.0.0"));
549        assert!(is_valid_override_value("workspace:*"));
550    }
551
552    #[test]
553    fn is_valid_override_value_rejects_empty_and_newline() {
554        assert!(!is_valid_override_value(""));
555        assert!(!is_valid_override_value("   "));
556        assert!(!is_valid_override_value("^1\n^2"));
557    }
558
559    #[test]
560    fn parses_workspace_yaml_overrides() {
561        let yaml = "packages:\n  - 'packages/*'\n\noverrides:\n  axios: ^1.6.0\n  \"@types/react@<18\": '18.0.0'\n  \"react>react-dom\": ^17\n";
562        let data = parse_pnpm_workspace_overrides(yaml).expect("valid yaml");
563        assert_eq!(data.entries.len(), 3);
564        assert_eq!(data.entries[0].raw_key, "axios");
565        assert_eq!(data.entries[0].line, 5);
566        assert_eq!(data.entries[0].raw_value.as_deref(), Some("^1.6.0"));
567
568        assert_eq!(data.entries[1].raw_key, "@types/react@<18");
569        assert_eq!(data.entries[1].line, 6);
570        assert_eq!(data.entries[1].raw_value.as_deref(), Some("18.0.0"));
571        assert_eq!(
572            data.entries[1]
573                .parsed_key
574                .as_ref()
575                .and_then(|p| p.target_version_selector.as_deref()),
576            Some("<18")
577        );
578
579        assert_eq!(data.entries[2].raw_key, "react>react-dom");
580        assert_eq!(data.entries[2].line, 7);
581        assert_eq!(
582            data.entries[2]
583                .parsed_key
584                .as_ref()
585                .map(|p| p.target_package.as_str()),
586            Some("react-dom")
587        );
588    }
589
590    #[test]
591    fn parses_package_json_overrides() {
592        let json = r#"{
593  "name": "root",
594  "pnpm": {
595    "overrides": {
596      "axios": "^1.6.0",
597      "react>react-dom": "^17"
598    }
599  },
600  "dependenciesMeta": {
601    "shouldNotMatch": { "injected": true }
602  }
603}"#;
604        let data = parse_pnpm_package_json_overrides(json);
605        assert_eq!(data.entries.len(), 2);
606        assert_eq!(data.entries[0].raw_key, "axios");
607        assert_eq!(data.entries[0].raw_value.as_deref(), Some("^1.6.0"));
608        assert_eq!(data.entries[0].line, 5);
609        assert_eq!(data.entries[1].raw_key, "react>react-dom");
610        assert_eq!(data.entries[1].line, 6);
611    }
612
613    #[test]
614    fn empty_workspace_overrides_returns_no_entries() {
615        let data = parse_pnpm_workspace_overrides("overrides: {}\n").expect("valid yaml");
616        assert!(data.entries.is_empty());
617    }
618
619    #[test]
620    fn malformed_yaml_returns_the_parse_error() {
621        let error = parse_pnpm_workspace_overrides("{this is\nnot: valid: yaml")
622            .expect_err("malformed yaml surfaces the error");
623        assert!(!error.is_empty(), "error text names the syntax problem");
624    }
625
626    #[test]
627    fn package_json_without_pnpm_overrides_returns_no_entries() {
628        let data = parse_pnpm_package_json_overrides(r#"{"dependencies": {"axios": "^1"}}"#);
629        assert!(data.entries.is_empty());
630    }
631
632    #[test]
633    fn malformed_json_returns_no_entries() {
634        let data = parse_pnpm_package_json_overrides("{not valid json");
635        assert!(data.entries.is_empty());
636    }
637
638    #[test]
639    fn unparsable_key_carries_misconfig_signal() {
640        let yaml = "overrides:\n  \">@bad-key>\": ^1.0.0\n";
641        let data = parse_pnpm_workspace_overrides(yaml).expect("valid yaml");
642        assert_eq!(data.entries.len(), 1);
643        assert!(data.entries[0].parsed_key.is_none());
644        assert_eq!(
645            override_misconfig_reason(&data.entries[0]),
646            Some(MisconfigReason::UnparsableKey)
647        );
648    }
649}