fix(npm): handle legacy array-form engines field - #47
Merged
Conversation
Early npm packages (e.g. lodash 0.x) publish `engines` as a legacy
array (`["node","rhino"]`) instead of the modern object form
(`{"node":">=4"}`). The struct typed it as `map[string]string`, so
unmarshalling failed with "cannot unmarshal array into Go struct field
versionInfo.versions.engines of type map[string]string" and
FetchVersions returned an error for the entire package.
Relax the field to `interface{}` (matching Homepage, License, Keywords
and Funding, which already tolerate variable shapes). The value only
flows opaquely into Version.Metadata["engines"], so both the array and
object forms now round-trip without affecting any logic.
Adds TestFetchVersions_LegacyEnginesArray as a regression test covering
both the legacy array and modern object forms.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the npm registry client’s packument decoding to tolerate legacy npm metadata where versions[].engines is published as an array (e.g. early lodash 0.x), preventing FetchVersions / FetchPackage from failing with a JSON unmarshal error.
Changes:
- Relax
versionInfo.Enginesfrommap[string]stringtointerface{}to accept both object and array JSON shapes. - Add a regression test covering both legacy array-form engines and modern object-form engines, asserting both round-trip into
Version.Metadata["engines"].
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| internal/npm/npm.go | Makes the engines field decode tolerant of legacy array-form values by loosening the Go type. |
| internal/npm/npm_test.go | Adds coverage to ensure both legacy and modern engines shapes parse and are preserved in version metadata. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| Maintainers []maintainerInfo `json:"maintainers"` | ||
| NpmUser map[string]interface{} `json:"_npmUser"` | ||
| Engines map[string]string `json:"engines"` | ||
| Engines interface{} `json:"engines"` |
andrew
approved these changes
Jul 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
FetchVersions/FetchPackagefail with a JSON unmarshal error for packages whose versions publishenginesas a legacy array instead of the modern object form, making the whole package unusable through the client:lodashis the notable example — its0.xreleases publishenginesas an array.Reproducer
returns 38 entries like:
{"version":"0.1.0","engines":["node","rhino"]} {"version":"0.2.0","engines":["node","rhino"]} ...so
FetchVersions(ctx, "lodash")errors out instead of returning versions.Root cause
versionInfo.Enginesis typedmap[string]string, but the npm packument historically allowsenginesto be either an object ({"node": ">=4"}) or a legacy array (["node","rhino"]). The array form cannot unmarshal into a map, failing the entirepackageResponsedecode.Fix
Relax
Enginestointerface{}, matching the existing handling of other variable-shape npm fields (Homepage,License,Keywords,Funding). The value only flows opaquely intoVersion.Metadata["engines"]— no logic depends on the map shape — so both the array and object forms now round-trip without changing behavior.Testing
Added
TestFetchVersions_LegacyEnginesArray, which serves both forms (legacy array["node","rhino"]and modern object{"node": ">=10"}) from a test server and asserts they round-trip intoVersion.Metadata["engines"]as[]interface{}andmap[string]interface{}respectively, withFetchVersionsreturning no error.