Skip to main content

npm_utils/package_json/
spec.rs

1//! The npm "package spec" — the dependency-specifier grammar, per
2//! <https://docs.npmjs.com/cli/v8/using-npm/package-spec>.
3//!
4//! A `package.json` `dependencies` *value* is one of these forms. [`Spec::parse`] classifies
5//! a value by *form*; [`Spec::is_registry`] reports whether it resolves to a fetchable
6//! registry tarball — the only form `npm-utils` installs (git / remote-tarball / local-path /
7//! alias-to-non-registry are not). Range *parsing* is deferred to [`version_req`]: classifying
8//! never fails, so an npm range we can't fully parse (spaces, `||`) is still a registry spec.
9
10use semver::{Version, VersionReq};
11
12/// A classified npm dependency specifier.
13#[derive(Debug, Clone, PartialEq, Eq)]
14pub enum Spec {
15    /// A registry spec — an exact version, a semver range, or a dist-tag (e.g. `latest`) —
16    /// held raw. Resolve it with [`version_req`] (the Rust `semver` subset of npm's grammar).
17    Registry(String),
18    /// An `npm:<name>@<spec>` alias — install `name` (per the inner spec) under the
19    /// dependency's own key.
20    Alias { name: String, spec: Box<Spec> },
21    /// A git source — a full git URL or a `host:owner/repo` / bare `owner/repo` shorthand —
22    /// with an optional `#<committish>` (branch, tag, commit, or `semver:<range>`).
23    Git {
24        source: String,
25        committish: Option<String>,
26    },
27    /// A remote tarball fetched over http(s).
28    Tarball(String),
29    /// A local path (`file:…`, `./`, `../`, `/abs`, `~/…`), linked or copied in place.
30    Path(String),
31}
32
33impl Spec {
34    /// Classify a `dependencies` value by form. Never fails — an unparseable-but-registry
35    /// range is still [`Spec::Registry`]; turning it into a [`VersionReq`] is a later step.
36    pub fn parse(spec: &str) -> Spec {
37        let s = spec.trim();
38
39        if let Some(rest) = s.strip_prefix("npm:") {
40            let (name, inner) = split_alias(rest);
41            return Spec::Alias {
42                name: name.to_string(),
43                spec: Box::new(Spec::parse(inner)),
44            };
45        }
46        if is_git_url(s) {
47            return git_spec(s);
48        }
49        if s.starts_with("http://") || s.starts_with("https://") {
50            return Spec::Tarball(s.to_string());
51        }
52        if is_path(s) {
53            return Spec::Path(s.to_string());
54        }
55        // After ruling out paths, a bare `owner/repo` is a GitHub shorthand.
56        if is_git_shorthand(s) {
57            return git_spec(s);
58        }
59        Spec::Registry(s.to_string())
60    }
61
62    /// Whether this spec resolves to a registry tarball (the only form `npm-utils` fetches).
63    pub fn is_registry(&self) -> bool {
64        match self {
65            Spec::Registry(_) => true,
66            Spec::Alias { spec, .. } => spec.is_registry(),
67            Spec::Git { .. } | Spec::Tarball(_) | Spec::Path(_) => false,
68        }
69    }
70}
71
72/// npm-faithful version → [`VersionReq`]: a bare full version (`1.2.3`) is an **exact** pin
73/// (`=1.2.3`); `*`, empty, `x`, and `latest` mean any; range syntax (`^`, `~`, `>=`, …) parses
74/// as written, within what the Rust `semver` crate accepts (comma-separated comparators; npm's
75/// space-separated and `||` ranges are not supported).
76pub fn version_req(spec: &str) -> Result<VersionReq, semver::Error> {
77    let spec = spec.trim();
78    if spec.is_empty() || spec == "*" || spec == "x" || spec == "latest" {
79        return Ok(VersionReq::STAR);
80    }
81    if Version::parse(spec).is_ok() {
82        return VersionReq::parse(&format!("={spec}"));
83    }
84    if let Some(xrange) = bare_partial_xrange(spec) {
85        return VersionReq::parse(&xrange);
86    }
87    VersionReq::parse(spec)
88}
89
90/// An npm version **range**: `||`-separated alternatives, each a (possibly space-separated) set
91/// of comparators. Rust's [`VersionReq`] handles only comma-separated comparators and has no
92/// `||`, yet `||` ranges are pervasive in published packages' dependencies (e.g.
93/// `@lit/reactive-element`'s `^1.6.2 || ^2.1.0`). A [`Range`] parses npm's grammar into a set of
94/// [`VersionReq`]s and is satisfied when **any** alternative is — so transitive resolution works
95/// on real-world trees. ([`version_req`] stays for the single-comparator-set case.)
96#[derive(Debug, Clone)]
97pub struct Range {
98    alternatives: Vec<VersionReq>,
99}
100
101impl Range {
102    /// A range matching any version (`*`).
103    pub fn any() -> Range {
104        Range {
105            alternatives: vec![VersionReq::STAR],
106        }
107    }
108
109    /// Parse an npm range. `||` separates alternatives; within one, npm's space-separated
110    /// comparators are joined with commas for `semver`. A bare full version is an exact pin;
111    /// `*`/`x`/empty/`latest` match anything.
112    pub fn parse(spec: &str) -> Result<Range, Box<dyn std::error::Error + Send + Sync>> {
113        let spec = spec.trim();
114        if spec.is_empty() || spec == "*" || spec == "x" || spec == "latest" {
115            return Ok(Range::any());
116        }
117        let alternatives = spec
118            .split("||")
119            .map(|alt| parse_alternative(alt.trim()))
120            .collect::<Result<Vec<_>, _>>()?;
121        Ok(Range { alternatives })
122    }
123
124    /// Whether `version` satisfies any alternative.
125    pub fn matches(&self, version: &Version) -> bool {
126        self.alternatives.iter().any(|req| req.matches(version))
127    }
128}
129
130impl From<VersionReq> for Range {
131    fn from(req: VersionReq) -> Range {
132        Range {
133            alternatives: vec![req],
134        }
135    }
136}
137
138impl std::str::FromStr for Range {
139    type Err = Box<dyn std::error::Error + Send + Sync>;
140    fn from_str(s: &str) -> Result<Range, Self::Err> {
141        Range::parse(s)
142    }
143}
144
145impl std::fmt::Display for Range {
146    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
147        for (i, req) in self.alternatives.iter().enumerate() {
148            if i > 0 {
149                write!(f, " || ")?;
150            }
151            write!(f, "{req}")?;
152        }
153        Ok(())
154    }
155}
156
157/// Parse one `||`-free alternative: a bare full version → an exact pin; otherwise npm's
158/// space-separated comparators joined with commas (what `semver` expects). A bare alphabetic word
159/// is reported as an unsupported npm dist-tag rather than leaking a cryptic semver error.
160fn parse_alternative(alt: &str) -> Result<VersionReq, Box<dyn std::error::Error + Send + Sync>> {
161    if alt.is_empty() || alt == "*" || alt == "x" {
162        return Ok(VersionReq::STAR);
163    }
164    if Version::parse(alt).is_ok() {
165        return Ok(VersionReq::parse(&format!("={alt}"))?);
166    }
167    // A bare partial numeric version is an npm x-range: `1` = `1.x`, `1.2` = `1.2.x`. The semver
168    // crate would read `1.2` as a caret (`^1.2`, `< 2.0.0`); normalize to the wildcard form so it
169    // means `>=1.2.0, <1.3.0` as npm intends.
170    if let Some(xrange) = bare_partial_xrange(alt) {
171        return Ok(VersionReq::parse(&xrange)?);
172    }
173    // A bare alphabetic word (`next`, `beta`, …) is an npm dist-tag, not a semver range. We don't
174    // resolve dist-tags (that needs a `dist-tags` lookup), so say so clearly. (`latest` is mapped
175    // to `*` earlier, in `Range::parse`.)
176    if looks_like_dist_tag(alt) {
177        return Err(format!(
178            "version {alt:?} looks like an npm dist-tag, which npm-utils doesn't resolve — pin a \
179             semver version or range (e.g. `^1.2.3`), or install from a package-lock.json"
180        )
181        .into());
182    }
183    Ok(VersionReq::parse(
184        &alt.split_whitespace().collect::<Vec<_>>().join(", "),
185    )?)
186}
187
188/// Whether `s` has the shape of an npm dist-tag — a bare word `[A-Za-z][A-Za-z0-9-]*` — as opposed
189/// to a semver range (which begins with a digit or a comparator like `^`/`~`/`>`/`<`/`=`). Used
190/// only to turn an unsupported tag into a clear error.
191fn looks_like_dist_tag(s: &str) -> bool {
192    matches!(s.chars().next(), Some(c) if c.is_ascii_alphabetic())
193        && s.chars().all(|c| c.is_ascii_alphanumeric() || c == '-')
194}
195
196/// A bare partial numeric version (`1`, `1.2`) rendered as npm's x-range wildcard (`1.*`, `1.2.*`).
197/// npm reads such a partial as `major.minor.x`, but the `semver` crate reads it as a caret, so this
198/// normalizes it. Returns `None` for anything that isn't one or two all-numeric components.
199fn bare_partial_xrange(spec: &str) -> Option<String> {
200    let parts: Vec<&str> = spec.split('.').collect();
201    let numeric = |p: &&str| !p.is_empty() && p.bytes().all(|b| b.is_ascii_digit());
202    ((1..=2).contains(&parts.len()) && parts.iter().all(numeric)).then(|| format!("{spec}.*"))
203}
204
205/// Build a [`Spec::Git`], splitting off a `#committish` if present.
206fn git_spec(s: &str) -> Spec {
207    match s.split_once('#') {
208        Some((source, c)) => Spec::Git {
209            source: source.to_string(),
210            committish: Some(c.to_string()),
211        },
212        None => Spec::Git {
213            source: s.to_string(),
214            committish: None,
215        },
216    }
217}
218
219/// Whether a spec value starts with an explicit git scheme or host shorthand.
220fn is_git_url(s: &str) -> bool {
221    const GIT_PREFIXES: &[&str] = &[
222        "git+",
223        "git://",
224        "git@",
225        "ssh://",
226        "github:",
227        "gitlab:",
228        "bitbucket:",
229        "gist:",
230    ];
231    GIT_PREFIXES.iter().any(|p| s.starts_with(p))
232}
233
234/// Whether a spec value is a bare `owner/repo` GitHub shorthand. Checked only *after* paths
235/// are ruled out: a slash, not scoped (`@`), and no URL scheme. A registry range never
236/// contains '/', so this is unambiguous here.
237fn is_git_shorthand(s: &str) -> bool {
238    let head = s.split('#').next().unwrap_or(s);
239    head.contains('/') && !head.starts_with('@') && !head.contains("://")
240}
241
242/// Whether a spec value names a local path. `~1.2.3` (a tilde range) is *not* a path — only
243/// `~/…` (a home path) is.
244fn is_path(s: &str) -> bool {
245    s.starts_with("file:")
246        || s.starts_with("./")
247        || s.starts_with("../")
248        || s.starts_with('/')
249        || s.starts_with("~/")
250}
251
252/// Split an `npm:` alias body into `(name, inner-spec)`, honoring scoped names: the version
253/// separator is the *last* `@` (a leading `@` is the scope, not a version marker).
254fn split_alias(rest: &str) -> (&str, &str) {
255    match rest.rfind('@') {
256        Some(at) if at > 0 => (&rest[..at], &rest[at + 1..]),
257        _ => (rest, ""),
258    }
259}
260
261#[cfg(test)]
262mod tests {
263    use super::*;
264
265    #[test]
266    fn version_req_pins_bare_versions_and_parses_ranges() {
267        assert_eq!(version_req("1.2.3").unwrap(), "=1.2.3".parse().unwrap());
268        assert_eq!(version_req("^3.0.0").unwrap(), "^3.0.0".parse().unwrap());
269        assert_eq!(version_req("*").unwrap(), VersionReq::STAR);
270        assert_eq!(version_req("").unwrap(), VersionReq::STAR);
271        assert_eq!(version_req("latest").unwrap(), VersionReq::STAR);
272        // A bare version matches ONLY itself — npm's exact-pin semantics.
273        let exact = version_req("1.2.3").unwrap();
274        assert!(exact.matches(&Version::parse("1.2.3").unwrap()));
275        assert!(!exact.matches(&Version::parse("1.2.4").unwrap()));
276    }
277
278    #[test]
279    fn range_handles_or_and_space_separated_alternatives() {
280        let v = |s: &str| Version::parse(s).unwrap();
281
282        // The `||` OR-range that broke transitive resolution (e.g. @lit/reactive-element).
283        let r = Range::parse("^1.6.2 || ^2.1.0").unwrap();
284        assert!(r.matches(&v("1.6.2")));
285        assert!(r.matches(&v("1.9.0")));
286        assert!(r.matches(&v("2.1.0")));
287        assert!(
288            !r.matches(&v("2.0.0")),
289            "below the ^2.1.0 alternative's floor"
290        );
291        assert!(!r.matches(&v("3.0.0")));
292
293        // Space-separated comparators (npm AND) are joined with commas for semver.
294        let and = Range::parse(">=1.6.2 <2.0.0").unwrap();
295        assert!(and.matches(&v("1.9.0")));
296        assert!(!and.matches(&v("2.0.0")));
297
298        // A bare version is an exact pin; `*`/empty/`Range::any` match anything.
299        assert!(Range::parse("1.2.3").unwrap().matches(&v("1.2.3")));
300        assert!(!Range::parse("1.2.3").unwrap().matches(&v("1.2.4")));
301        assert!(Range::any().matches(&v("9.9.9")));
302        assert!(Range::parse("*").unwrap().matches(&v("9.9.9")));
303    }
304
305    #[test]
306    fn rejects_dist_tags_with_a_clear_message() {
307        // `latest` resolves (≈ any); other dist-tags aren't supported, and must say so clearly
308        // rather than leak a raw semver parse error.
309        assert!(Range::parse("latest").is_ok());
310        for tag in ["next", "beta", "canary"] {
311            let err = Range::parse(tag).unwrap_err().to_string();
312            assert!(
313                err.contains("dist-tag"),
314                "{tag:?} should give a dist-tag error, got: {err}"
315            );
316        }
317        // A real range still parses.
318        assert!(Range::parse("^1.2.3").is_ok());
319    }
320
321    #[test]
322    fn classifies_registry_versions_ranges_and_tags() {
323        for s in [
324            "^1.2.3", "1.2.3", ">=1 <2", "~1.2.3", "*", "", "latest", "next",
325        ] {
326            assert!(matches!(Spec::parse(s), Spec::Registry(_)), "{s:?}");
327            assert!(Spec::parse(s).is_registry(), "{s:?}");
328        }
329        // The raw spec is preserved (incl. npm space-ranges we don't fully parse).
330        assert_eq!(Spec::parse(">=1 <2"), Spec::Registry(">=1 <2".into()));
331        assert_eq!(Spec::parse("latest"), Spec::Registry("latest".into()));
332    }
333
334    #[test]
335    fn classifies_npm_alias_to_its_inner_spec() {
336        match Spec::parse("npm:@scope/pkg@^1.2.3") {
337            Spec::Alias { name, spec } => {
338                assert_eq!(name, "@scope/pkg");
339                assert_eq!(*spec, Spec::Registry("^1.2.3".into()));
340            }
341            other => panic!("expected alias, got {other:?}"),
342        }
343        // An alias to a registry range is itself a fetchable registry install.
344        assert!(Spec::parse("npm:left-pad@1.0.0").is_registry());
345    }
346
347    #[test]
348    fn classifies_git_sources_with_committish() {
349        for s in [
350            "git+https://github.com/npm/cli.git",
351            "git+ssh://git@github.com/npm/cli.git",
352            "git://github.com/npm/cli.git",
353            "github:npm/cli",
354            "gitlab:owner/repo",
355            "bitbucket:owner/repo",
356            "npm/cli", // bare owner/repo shorthand
357        ] {
358            assert!(matches!(Spec::parse(s), Spec::Git { .. }), "{s}");
359            assert!(!Spec::parse(s).is_registry(), "{s}");
360        }
361        match Spec::parse("npm/cli#v6.0.0") {
362            Spec::Git { source, committish } => {
363                assert_eq!(source, "npm/cli");
364                assert_eq!(committish.as_deref(), Some("v6.0.0"));
365            }
366            other => panic!("expected git, got {other:?}"),
367        }
368    }
369
370    #[test]
371    fn classifies_remote_tarballs_and_local_paths() {
372        assert!(matches!(
373            Spec::parse("https://registry.npmjs.org/semver/-/semver-1.0.0.tgz"),
374            Spec::Tarball(_)
375        ));
376        for p in ["file:../local", "./pkg", "../pkg", "/abs/pkg", "~/pkg"] {
377            assert!(matches!(Spec::parse(p), Spec::Path(_)), "{p}");
378            assert!(!Spec::parse(p).is_registry(), "{p}");
379        }
380    }
381
382    // ---- node-semver conformance ----
383    // Lock in that Range::parse + matches behave like node-semver for the forms npm-utils
384    // resolves, so a future change to the parsing layer can't silently drift.
385
386    fn m(range: &str, ver: &str) -> bool {
387        Range::parse(range)
388            .unwrap_or_else(|e| panic!("range {range:?} failed to parse: {e}"))
389            .matches(&Version::parse(ver).unwrap())
390    }
391
392    #[test]
393    fn conformance_caret() {
394        assert!(m("^1.2.3", "1.2.3"));
395        assert!(m("^1.2.3", "1.9.0"));
396        assert!(!m("^1.2.3", "2.0.0"));
397        assert!(!m("^1.2.3", "1.2.2"));
398        // Caret on 0.x pins the minor.
399        assert!(m("^0.2.3", "0.2.9"));
400        assert!(!m("^0.2.3", "0.3.0"));
401        // Caret on 0.0.x pins the patch.
402        assert!(m("^0.0.3", "0.0.3"));
403        assert!(!m("^0.0.3", "0.0.4"));
404    }
405
406    #[test]
407    fn conformance_tilde() {
408        assert!(m("~1.2.3", "1.2.9"));
409        assert!(!m("~1.2.3", "1.3.0"));
410        assert!(m("~1.2", "1.2.0"));
411        assert!(!m("~1.2", "1.3.0"));
412        assert!(m("~1", "1.9.9"));
413        assert!(!m("~1", "2.0.0"));
414    }
415
416    #[test]
417    fn conformance_x_ranges() {
418        // npm treats x / X / * as wildcards; a bare partial version is equivalent.
419        for r in ["1.x", "1.X", "1.*", "1"] {
420            assert!(m(r, "1.0.0"), "{r}");
421            assert!(m(r, "1.9.9"), "{r}");
422            assert!(!m(r, "2.0.0"), "{r}");
423        }
424        for r in ["1.2.x", "1.2.X", "1.2.*", "1.2"] {
425            assert!(m(r, "1.2.9"), "{r}");
426            assert!(!m(r, "1.3.0"), "{r}");
427        }
428    }
429
430    #[test]
431    fn conformance_exact_and_wildcard() {
432        assert!(m("1.2.3", "1.2.3"));
433        assert!(!m("1.2.3", "1.2.4"));
434        for star in ["*", "x", "", "latest"] {
435            assert!(m(star, "9.9.9"), "{star:?}");
436        }
437    }
438
439    #[test]
440    fn conformance_prerelease() {
441        // A caret range does not match a pre-release of a different (major, minor, patch) tuple.
442        assert!(!m("^1.2.3", "1.3.0-rc.1"));
443        assert!(!m("^1.2.3", "2.0.0-rc.1"));
444        // A pre-release range matches pre-releases of the same tuple, and the release.
445        assert!(m("^1.2.3-rc.1", "1.2.3-rc.2"));
446        assert!(m("^1.2.3-rc.1", "1.2.3"));
447    }
448
449    #[test]
450    fn hyphen_ranges_are_not_yet_supported() {
451        // node-semver's `a - b` (== `>=a <=b`) isn't parsed by the underlying semver crate. Record
452        // the current limitation so that adding support later updates this test deliberately;
453        // tracked with the resolution-fidelity follow-up.
454        assert!(Range::parse("1.2.3 - 2.3.4").is_err());
455    }
456}