Repository navigation
Return an empty range for unsatisfiable npm comparators #63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
8d3ffde
cb8b86f
b48a9ff
14a0d59
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -210,6 +210,10 @@ func (p *Parser) ToVersString(r *Range, scheme string) string { | |
| } | ||
| // Check if empty but has raw constraints (preserve them for output) | ||
| if r.isEmptyFor(scheme) && len(r.RawConstraints) == 0 { | ||
| if scheme == schemeNPM { | ||
| // VERS has no empty range; this bound is below the lowest semver version. | ||
| return "vers:npm/<0.0.0-0" | ||
| } | ||
| return fmt.Sprintf("vers:%s/", scheme) | ||
| } | ||
|
|
||
|
|
@@ -439,6 +443,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 +454,9 @@ func (p *Parser) parseNpmRange(s string) (*Range, error) { | |
| allRaw = append(allRaw, r.Intervals...) | ||
| } | ||
| } | ||
| if len(allIntervals) == 0 { | ||
| return &Range{}, nil | ||
| } | ||
| return &Range{ | ||
| Intervals: mergeIntervals(allIntervals, CompareVersions), | ||
| Exclusions: allExclusions, | ||
|
|
@@ -455,96 +465,30 @@ 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)) | ||
| if parts := tokenizeNpmConstraints(s); len(parts) > 1 { | ||
| var result *Range | ||
| for i, part := range parts { | ||
| 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) | ||
| if result.IsEmpty() { | ||
| return nil, npmUnsatisfiableError(parts, ranges, i) | ||
| } | ||
| } | ||
| if len(ranges) > 1 { | ||
| result.RawConstraints = npmIntersectedRaw(ranges) | ||
| if result.IsEmpty() { | ||
| return &Range{}, nil | ||
| } | ||
| // Per-comparator constraints would serialize as a union. | ||
| result.RawConstraints = nil | ||
| return result, nil | ||
| } | ||
|
|
||
| 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) | ||
| } | ||
|
|
||
| // 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 { | ||
|
|
@@ -558,6 +502,11 @@ func tokenizeNpmConstraints(s string) []string { | |
| i := 0 | ||
| for i < len(tokens) { | ||
| token := tokens[i] | ||
| if i+2 < len(tokens) && tokens[i+1] == "-" { | ||
| result = append(result, token+" - "+tokens[i+2]) | ||
| i += 3 | ||
| continue | ||
|
Comment on lines
+505
to
+508
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keep npm hyphen grouping out of the Conan tokenizer path. For Use npm-specific hyphen grouping, or retain the previous tokenizer behavior for Conan. Preserve npm hyphen parsing and ordinary Conan comparator parsing.
|
||
| } | ||
| // Check if this token is just an operator | ||
| if isOperatorOnly(token) && i+1 < len(tokens) { | ||
| // Merge with next token | ||
|
|
@@ -593,6 +542,9 @@ func extractOperator(s string) (string, string) { | |
|
|
||
| func (p *Parser) parseNpmSingleRange(s string) (*Range, error) { | ||
| s = strings.TrimSpace(s) | ||
| if s == "-" { | ||
| return nil, fmt.Errorf("invalid npm range: hyphen without a version on both sides") | ||
| } | ||
| // Caret range: ^1.2.3 | ||
| if strings.HasPrefix(s, "^") { | ||
| return p.parseCaretRange(s[1:]) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Parsing the empty-npm sentinel returns a range that
IsEmpty()reports as non-empty.ToVersStringwrites an empty npm range asvers:npm/<0.0.0-0.Parseturns that string into one maximum-only interval,{Max: "0.0.0-0", MaxInclusive: false}.isEmptyCmpreturns false whenMinis empty, soRange.IsEmpty()returns false for the reparsed range. Membership is still empty under npm ordering, because no valid version is below0.0.0-0. Callers that useIsEmpty()to detect an unsatisfiable constraint therefore see the round trip as non-empty.Keep the empty state through
Parse, so the reparsed range also reportsIsEmpty() == true. The fix can special-case the sentinel during npm parsing or makeIsEmptyrecognize it. Add an assertion onIsEmpty()for the round trip. The existing containment assertions do not check this state.