From 8d3ffdee81ed8551cac38740cb05d7a9def6f0cd Mon Sep 17 00:00:00 2001 From: Andrew Nesbitt Date: Fri, 20 Nov 2026 14:21:16 +0000 Subject: [PATCH 1/2] Return an empty range for unsatisfiable npm comparators ParseNative returned an error for these, which broke callers that expect an empty range. Skip the upstream conformance case that expects a failure. --- advisory_test.go | 14 +------------- parser.go | 31 +++++-------------------------- parser_test.go | 32 +++++++++++++++++--------------- testdata/local/skip.json | 9 +++++++++ 4 files changed, 32 insertions(+), 54 deletions(-) diff --git a/advisory_test.go b/advisory_test.go index dc81986..2a0069a 100644 --- a/advisory_test.go +++ b/advisory_test.go @@ -13,11 +13,6 @@ type npmAdvisoryRange struct { Native string `json:"npm_native"` } -// Advisory ranges that list exact versions with spaces, which npm intersects. -var unsatisfiableNPMAdvisoryRanges = map[string]bool{ - "1.1.2 1.2.2": true, -} - func TestNPMAdvisoryRangesParse(t *testing.T) { filename := filepath.Join("testdata", "local", "data", "npm_advisory.json") data, err := os.ReadFile(filename) @@ -33,14 +28,7 @@ func TestNPMAdvisoryRangesParse(t *testing.T) { } for _, advisoryRange := range ranges { t.Run(fmt.Sprintf("%04d", advisoryRange.TestIndex), func(t *testing.T) { - _, err := ParseNative(advisoryRange.Native, schemeNPM) - if unsatisfiableNPMAdvisoryRanges[advisoryRange.Native] { - if err == nil { - t.Errorf("ParseNative(%q, npm) succeeded, want unsatisfiable range error", advisoryRange.Native) - } - return - } - if err != nil { + if _, err := ParseNative(advisoryRange.Native, schemeNPM); err != nil { t.Errorf("ParseNative(%q, npm): %v", advisoryRange.Native, err) } }) diff --git a/parser.go b/parser.go index adf45e0..d489410 100644 --- a/parser.go +++ b/parser.go @@ -459,7 +459,7 @@ func (p *Parser) parseNpmRange(s string) (*Range, error) { parts := tokenizeNpmConstraints(s) ranges := make([]*Range, 0, len(parts)) var result *Range - for i, part := range parts { + for _, part := range parts { r, err := p.parseNpmSingleRange(part) if err != nil { return nil, err @@ -470,11 +470,11 @@ func (p *Parser) parseNpmRange(s string) (*Range, error) { continue } result = result.Intersect(r) - if result.IsEmpty() { - return nil, npmUnsatisfiableError(parts, ranges, i) - } } - if len(ranges) > 1 { + switch { + case result.IsEmpty(): + result.RawConstraints = nil + case len(ranges) > 1: result.RawConstraints = npmIntersectedRaw(ranges) } return result, nil @@ -524,27 +524,6 @@ func tighterUpper(a, b Interval) bool { return c < 0 || (c == 0 && !a.MaxInclusive && b.MaxInclusive) } -// npmUnsatisfiableError names the comparator at index last and an earlier one -// it cannot be satisfied together with. -func npmUnsatisfiableError(parts []string, ranges []*Range, last int) error { - conflict := 0 - for i := range last { - if ranges[i].Intersect(ranges[last]).IsEmpty() { - conflict = i - break - } - } - return fmt.Errorf("space-separated npm comparators are intersected: %s and %s match no version", - npmComparatorString(parts[conflict]), npmComparatorString(parts[last])) -} - -func npmComparatorString(part string) string { - if strings.ContainsAny(part[:1], "<>=!~^") { - return part - } - return "=" + part -} - // tokenizeNpmConstraints splits an npm constraint string into individual constraints, // properly handling operators followed by spaces (e.g., ">= 1.0.0" stays as one token). func tokenizeNpmConstraints(s string) []string { diff --git a/parser_test.go b/parser_test.go index 81fc2c2..1555c98 100644 --- a/parser_test.go +++ b/parser_test.go @@ -227,25 +227,27 @@ func TestParseNpmIntersectionToVers(t *testing.T) { } func TestParseNpmUnsatisfiableIntersection(t *testing.T) { - tests := []struct { - input string - want string - }{ - {"1.1.2 1.2.2", "space-separated npm comparators are intersected: =1.1.2 and =1.2.2 match no version"}, - {">=2.0.0 <1.0.0", "space-separated npm comparators are intersected: >=2.0.0 and <1.0.0 match no version"}, - {">=1.0.0 ^1.2.3 <1.1.0", "space-separated npm comparators are intersected: ^1.2.3 and <1.1.0 match no version"}, - {"1.0.0 || >=2.0.0 <1.0.0", "space-separated npm comparators are intersected: >=2.0.0 and <1.0.0 match no version"}, - } - for _, tt := range tests { - _, err := ParseNative(tt.input, schemeNPM) - if err == nil { - t.Errorf("ParseNative(%q, npm) succeeded", tt.input) + for _, input := range []string{"1.1.2 1.2.2", ">=2.0.0 <1.0.0", ">=1.0.0 ^1.2.3 <1.1.0"} { + r, err := ParseNative(input, schemeNPM) + if err != nil { + t.Errorf("ParseNative(%q, npm): %v", input, err) continue } - if err.Error() != tt.want { - t.Errorf("ParseNative(%q, npm) error = %q, want %q", tt.input, err, tt.want) + if !r.IsEmpty() { + t.Errorf("ParseNative(%q, npm) is not empty", input) + } + if got := ToVersString(r, schemeNPM); got != "vers:npm/" { + t.Errorf("ParseNative(%q, npm) = %q, want %q", input, got, "vers:npm/") } } + + r, err := ParseNative("1.0.0 || >=2.0.0 <1.0.0", schemeNPM) + if err != nil { + t.Fatalf("ParseNative: %v", err) + } + if got := ToVersString(r, schemeNPM); got != "vers:npm/1.0.0" { + t.Errorf("unsatisfiable alternative: got %q, want %q", got, "vers:npm/1.0.0") + } } func TestParseGemRange(t *testing.T) { diff --git a/testdata/local/skip.json b/testdata/local/skip.json index 3dbaea1..cb26a3a 100644 --- a/testdata/local/skip.json +++ b/testdata/local/skip.json @@ -9,6 +9,15 @@ }, "expected_output": false, "reason": "VERS constraints cannot encode Conan's per-branch prerelease opt-in." + }, + { + "file": "vers-spec/tests/npm_range_from_native_test.json", + "test_type": "from_native", + "input": { + "native_range": "1.1.2 1.2.2", + "scheme": "npm" + }, + "reason": "ParseNative returns an empty range for unsatisfiable npm comparators instead of an error." } ] } From cb8b86f7ca3e230918d130e9f349b857cc697941 Mon Sep 17 00:00:00 2001 From: Andrew Nesbitt Date: Fri, 20 Nov 2026 14:36:42 +0000 Subject: [PATCH 2/2] Serialize intersected npm comparators from their intervals Picking raw bounds per comparator gave VERS strings that disagreed with the range for partial versions. An empty range now serializes as <0.0.0-0 so it does not reparse as unbounded. --- parser.go | 60 ++++++++++------------------------------ parser_test.go | 60 ++++++++++++++++++++++++++-------------- testdata/local/skip.json | 10 +++++++ 3 files changed, 64 insertions(+), 66 deletions(-) diff --git a/parser.go b/parser.go index d489410..cfd744c 100644 --- a/parser.go +++ b/parser.go @@ -439,6 +439,9 @@ func (p *Parser) parseNpmRange(s string) (*Range, error) { if err != nil { return nil, err } + if r.IsEmpty() { + continue + } allIntervals = append(allIntervals, r.Intervals...) allExclusions = append(allExclusions, r.Exclusions...) if len(r.RawConstraints) > 0 { @@ -447,6 +450,9 @@ func (p *Parser) parseNpmRange(s string) (*Range, error) { allRaw = append(allRaw, r.Intervals...) } } + if len(allIntervals) == 0 { + return emptyNpmRange(), nil + } return &Range{ Intervals: mergeIntervals(allIntervals, CompareVersions), Exclusions: allExclusions, @@ -457,25 +463,24 @@ func (p *Parser) parseNpmRange(s string) (*Range, error) { // Handle space-separated AND constraints if strings.ContainsAny(s, " \t\r\n") && !strings.Contains(s, " - ") { parts := tokenizeNpmConstraints(s) - ranges := make([]*Range, 0, len(parts)) var result *Range for _, part := range parts { r, err := p.parseNpmSingleRange(part) if err != nil { return nil, err } - ranges = append(ranges, r) if result == nil { result = r continue } result = result.Intersect(r) } - switch { - case result.IsEmpty(): + if result.IsEmpty() { + return emptyNpmRange(), nil + } + if len(parts) > 1 { + // Per-comparator constraints would serialize as a union. result.RawConstraints = nil - case len(ranges) > 1: - result.RawConstraints = npmIntersectedRaw(ranges) } return result, nil } @@ -483,45 +488,10 @@ func (p *Parser) parseNpmRange(s string) (*Range, error) { return p.parseNpmSingleRange(s) } -// npmIntersectedRaw returns the raw constraints of the comparators that set -// the tightest lower and upper bound, since listing every comparator would -// serialize as a union. It returns nil when the merged intervals should be -// serialized instead. -func npmIntersectedRaw(ranges []*Range) []Interval { - var out Interval - lower, upper := -1, -1 - for i, r := range ranges { - raw := r.RawConstraints - if len(raw) == 0 { - raw = r.Intervals - } - if len(r.Intervals) != 1 || len(raw) != 1 { - return nil - } - bound := r.Intervals[0] - if bound.Min != "" && (lower < 0 || tighterLower(bound, ranges[lower].Intervals[0])) { - lower = i - out.Min, out.MinInclusive = raw[0].Min, raw[0].MinInclusive - } - if bound.Max != "" && (upper < 0 || tighterUpper(bound, ranges[upper].Intervals[0])) { - upper = i - out.Max, out.MaxInclusive = raw[0].Max, raw[0].MaxInclusive - } - } - if lower < 0 && upper < 0 { - return nil - } - return []Interval{out} -} - -func tighterLower(a, b Interval) bool { - c := CompareVersions(a.Min, b.Min) - return c > 0 || (c == 0 && !a.MinInclusive && b.MinInclusive) -} - -func tighterUpper(a, b Interval) bool { - c := CompareVersions(a.Max, b.Max) - return c < 0 || (c == 0 && !a.MaxInclusive && b.MaxInclusive) +// emptyNpmRange matches no version. VERS has no empty range, so it serializes +// as a bound below the lowest semver version. +func emptyNpmRange() *Range { + return &Range{RawConstraints: []Interval{NewInterval("", "0.0.0-0", false, false)}} } // tokenizeNpmConstraints splits an npm constraint string into individual constraints, diff --git a/parser_test.go b/parser_test.go index 1555c98..0e74764 100644 --- a/parser_test.go +++ b/parser_test.go @@ -203,16 +203,26 @@ func TestParseNpmRangeErrors(t *testing.T) { func TestParseNpmIntersectionToVers(t *testing.T) { tests := []struct { - input string - want string + input string + want string + contains []string + excludes []string }{ - {">=5.0.3 >=4.2.1", "vers:npm/>=5.0.3"}, - {">=4.2.1 >=5.0.3 <6.0.0 <7.0.0", "vers:npm/>=5.0.3|<6.0.0"}, - {">1.0.0 >=1.0.0", "vers:npm/>1.0.0"}, - {"<=2.1 >=1.1 >=1.0", "vers:npm/>=1.1.0|<=2.1.0"}, - {"<=2.1 <2.1.5", "vers:npm/<2.1.5"}, - {"^1.2.3 >=1.5.0", "vers:npm/>=1.5.0|<2.0.0"}, - {">=1.0.0 <2.0.0 || >=3.0.0 >=3.1.0", "vers:npm/>=1.0.0|<2.0.0|>=3.1.0"}, + {">=5.0.3 >=4.2.1", "vers:npm/>=5.0.3", []string{"5.0.3"}, []string{"4.2.1"}}, + {">=4.2.1 >=5.0.3 <6.0.0 <7.0.0", "vers:npm/>=5.0.3|<6.0.0", []string{"5.5.0"}, []string{"6.0.0"}}, + {">1.0.0 >=1.0.0", "vers:npm/>1.0.0", []string{"1.0.1"}, []string{"1.0.0"}}, + {"<=2.1 >=1.1 >=1.0", "vers:npm/>=1.1.0|<2.2.0", []string{"2.1.9"}, []string{"1.0.5", "2.2.0"}}, + {"<=2.1 <2.2.0", "vers:npm/<2.2.0", []string{"2.1.9"}, []string{"2.2.0"}}, + {"<2.2.0 <=2.1", "vers:npm/<2.2.0", []string{"2.1.9"}, []string{"2.2.0"}}, + {">1.2 <2.0.0", "vers:npm/>=1.3.0|<2.0.0", []string{"1.3.0"}, []string{"1.2.8"}}, + {"1.2 >=1.2.3", "vers:npm/>=1.2.3|<1.3.0", []string{"1.2.5"}, []string{"1.2.0", "9.0.0"}}, + {"<2.x >=1.0.0", "vers:npm/>=1.0.0|<2.0.0", []string{"1.9.0"}, []string{"2.5.0"}}, + {"^1.2.3 >=1.5.0", "vers:npm/>=1.5.0|<2.0.0", []string{"1.5.0"}, []string{"1.4.0", "2.0.0"}}, + {">=1.0.0 <2.0.0 || >=3.0.0 >=3.1.0", "vers:npm/>=1.0.0|<2.0.0|>=3.1.0", []string{"1.5.0", "3.1.0"}, []string{"3.0.0"}}, + {"1.0.0 || >=2.0.0 <1.0.0", "vers:npm/1.0.0", []string{"1.0.0"}, []string{"2.0.0"}}, + {"1.1.2 1.2.2", "vers:npm/<0.0.0-0", nil, []string{"1.1.2", "1.2.2", "999.0.0"}}, + {">=1.0.0 ^1.2.3 <1.1.0", "vers:npm/<0.0.0-0", nil, []string{"1.0.5", "1.2.3"}}, + {">=2.0.0 <1.0.0 || 1.1.2 1.2.2", "vers:npm/<0.0.0-0", nil, []string{"1.1.2", "2.0.0"}}, } for _, tt := range tests { r, err := ParseNative(tt.input, schemeNPM) @@ -220,14 +230,30 @@ func TestParseNpmIntersectionToVers(t *testing.T) { t.Errorf("ParseNative(%q, npm): %v", tt.input, err) continue } - if got := ToVersString(r, schemeNPM); got != tt.want { + got := ToVersString(r, schemeNPM) + if got != tt.want { t.Errorf("ParseNative(%q, npm) = %q, want %q", tt.input, got, tt.want) } + reparsed, err := Parse(got) + if err != nil { + t.Errorf("Parse(%q): %v", got, err) + continue + } + for _, v := range tt.contains { + if !r.Contains(v) || !reparsed.Contains(v) { + t.Errorf("%q: Contains(%q) = %v, reparsed %v, want true", tt.input, v, r.Contains(v), reparsed.Contains(v)) + } + } + for _, v := range tt.excludes { + if r.Contains(v) || reparsed.Contains(v) { + t.Errorf("%q: Contains(%q) = %v, reparsed %v, want false", tt.input, v, r.Contains(v), reparsed.Contains(v)) + } + } } } func TestParseNpmUnsatisfiableIntersection(t *testing.T) { - for _, input := range []string{"1.1.2 1.2.2", ">=2.0.0 <1.0.0", ">=1.0.0 ^1.2.3 <1.1.0"} { + for _, input := range []string{"1.1.2 1.2.2", ">=2.0.0 <1.0.0", ">=2.0.0 <1.0.0 || 1.1.2 1.2.2"} { r, err := ParseNative(input, schemeNPM) if err != nil { t.Errorf("ParseNative(%q, npm): %v", input, err) @@ -236,17 +262,9 @@ func TestParseNpmUnsatisfiableIntersection(t *testing.T) { if !r.IsEmpty() { t.Errorf("ParseNative(%q, npm) is not empty", input) } - if got := ToVersString(r, schemeNPM); got != "vers:npm/" { - t.Errorf("ParseNative(%q, npm) = %q, want %q", input, got, "vers:npm/") - } - } - - r, err := ParseNative("1.0.0 || >=2.0.0 <1.0.0", schemeNPM) - if err != nil { - t.Fatalf("ParseNative: %v", err) } - if got := ToVersString(r, schemeNPM); got != "vers:npm/1.0.0" { - t.Errorf("unsatisfiable alternative: got %q, want %q", got, "vers:npm/1.0.0") + if _, err := ParseNative("1.1.2 1.2.2 1.x.2", schemeNPM); err == nil { + t.Error("malformed comparator after a contradiction parsed without error") } } diff --git a/testdata/local/skip.json b/testdata/local/skip.json index cb26a3a..105977a 100644 --- a/testdata/local/skip.json +++ b/testdata/local/skip.json @@ -18,6 +18,16 @@ "scheme": "npm" }, "reason": "ParseNative returns an empty range for unsatisfiable npm comparators instead of an error." + }, + { + "file": "vers-spec/tests/npm_range_from_native_test.json", + "test_type": "from_native", + "input": { + "native_range": "<=2.1 >=1.1", + "scheme": "npm" + }, + "expected_output": "vers:npm/>=1.1.0|<=2.1.0", + "reason": "npm <=2.1 admits every 2.1.x, so the upper bound is <2.2.0." } ] }