From 955a93c1fc6845a88e40f73558b95699c5243ced Mon Sep 17 00:00:00 2001 From: Simon Chrzanowski Date: Thu, 12 Nov 2026 13:07:37 +0100 Subject: [PATCH 1/3] Add an Open VSX registry for vscode-extension Reads extensions from open-vsx.org or a self-hosted Open VSX server. Versions come from the v2 query API, page by page, with one artifact per target platform build carrying its signature and checksum URLs. Dependencies include bundled extension pack members. --- README.md | 4 + all/all.go | 3 +- internal/vscodeextension/vscodeextension.go | 412 ++++++++++++++++ .../vscodeextension/vscodeextension_test.go | 442 ++++++++++++++++++ registries_test.go | 4 +- 5 files changed, 863 insertions(+), 2 deletions(-) create mode 100644 internal/vscodeextension/vscodeextension.go create mode 100644 internal/vscodeextension/vscodeextension_test.go diff --git a/README.md b/README.md index d953bfe..55129ce 100644 --- a/README.md +++ b/README.md @@ -135,6 +135,7 @@ packages = registries.BulkFetchPackagesWithConcurrency(ctx, purls, nil, 5) | RubyGems | `pkg:gem/rails@7.1.0` | | Terraform | `pkg:terraform/hashicorp/consul/aws@0.11.0` | | GitHub Actions | `pkg:githubactions/actions/checkout@v4` | +| VS Code extension | `pkg:vscode-extension/redhat/vscode-yaml@1.15.0` | | Alpine | `pkg:apk/alpine/curl@8.22.0-r0?arch=x86_64&repository_url=https://dl-cdn.alpinelinux.org/alpine/v3.23/main/x86_64` | ## Direct Registry Usage @@ -197,12 +198,15 @@ import _ "github.com/git-pkgs/registries/all" | Swift | `swift` | URL required | | Alpine | `apk` | URL required | | GitHub Actions | `githubactions` | https://api.github.com | +| VS Code extension | `vscode-extension` | https://open-vsx.org | | APM | None | URL required | The Alpine URL is one repository architecture directory, such as `https://dl-cdn.alpinelinux.org/alpine/v3.23/main/x86_64`, or the `APKINDEX.tar.gz` inside it. The client doesn't pick a release, component or architecture from a mirror root. It reads v2 `APKINDEX.tar.gz` indexes. When a directory serves a v3 `Packages.adb`, the client returns an error rather than reading the v2 index. GitHub Actions are git repositories, so `FetchPackage` reads `/repos/{owner}/{repo}` and `FetchVersions` lists tags from the GitHub REST API. Unauthenticated requests are limited to 60 per hour. A subdirectory action such as `pkg:githubactions/github/codeql-action/init@v3` resolves to the `github/codeql-action` repository. `URLs().Download` returns the `/archive/{ref}.tar.gz` source tarball for any tag, branch or commit SHA. GitHub doesn't guarantee these tarballs are byte-stable, so versions carry no `Integrity`. Dependencies and maintainers return an error wrapping `errors.ErrUnsupported`. For GitHub Enterprise Server, pass the `/api/v3` API root as the base URL. +VS Code extensions are read from [Open VSX](https://open-vsx.org), or from a self-hosted Open VSX server given as the base URL. The purl spec makes the Visual Studio Marketplace the default for `vscode-extension`, but the client has no Marketplace support, so a PURL without `repository_url` also resolves to Open VSX. `URLs().PURL` always adds the registry as `repository_url`. Names are `publisher/name` or VS Code's `publisher.name`. An extension can publish a separate build for each target platform, such as `linux-x64` or `darwin-arm64`. Each version from `FetchVersions` and `FetchVersion` carries one artifact per build, with `target_platform`, `signature` (the `.sigzip`) and `sha256` (the URL of the checksum file) in its metadata. `URLs().Download` returns the universal build, which a version built only for specific platforms doesn't have. `FetchDependencies` lists both the extensions a version depends on and the ones it bundles as an extension pack. `FetchMaintainers` returns the user who published the latest version. + ### Swift registries Swift lookups require an explicit registry URL and a registry identity: diff --git a/all/all.go b/all/all.go index 897535a..6d17d87 100644 --- a/all/all.go +++ b/all/all.go @@ -9,7 +9,7 @@ // // // Now all ecosystems are available // ecosystems := registries.SupportedEcosystems() -// // ["apk", "apm", "brew", "cargo", "clojars", "cocoapods", "composer", "conda", "cpan", "cran", "deno", "dub", "elm", "gem", "githubactions", "golang", "hackage", "haxelib", "helm", "hex", "julia", "luarocks", "maven", "nimble", "npm", "nuget", "pub", "pypi", "swift", "terraform"] +// // ["apk", "apm", "brew", "cargo", "clojars", "cocoapods", "composer", "conda", "cpan", "cran", "deno", "dub", "elm", "gem", "githubactions", "golang", "hackage", "haxelib", "helm", "hex", "julia", "luarocks", "maven", "nimble", "npm", "nuget", "pub", "pypi", "swift", "terraform", "vscode-extension"] package all import ( @@ -43,4 +43,5 @@ import ( _ "github.com/git-pkgs/registries/internal/rubygems" _ "github.com/git-pkgs/registries/internal/swift" _ "github.com/git-pkgs/registries/internal/terraform" + _ "github.com/git-pkgs/registries/internal/vscodeextension" ) diff --git a/internal/vscodeextension/vscodeextension.go b/internal/vscodeextension/vscodeextension.go new file mode 100644 index 0000000..07dda16 --- /dev/null +++ b/internal/vscodeextension/vscodeextension.go @@ -0,0 +1,412 @@ +// Package vscodeextension provides a registry client for VS Code extensions +// served by Open VSX (https://open-vsx.org) or a self-hosted Open VSX server. +package vscodeextension + +import ( + "context" + "errors" + "fmt" + "net/url" + "path" + "sort" + "strconv" + "strings" + "time" + + "github.com/git-pkgs/purl" + "github.com/git-pkgs/registries/internal/core" + "github.com/git-pkgs/registries/internal/urlparser" +) + +const ( + DefaultURL = "https://open-vsx.org" + ecosystem = "vscode-extension" + + // pageSize is how many builds a query asks for at once. The server may + // answer fewer; the listing goes on from however many came back. + pageSize = 100 +) + +func init() { + core.Register(ecosystem, DefaultURL, func(baseURL string, client *core.Client) core.Registry { + return New(baseURL, client) + }) +} + +type Registry struct { + baseURL string + client *core.Client + urls *URLs +} + +func New(baseURL string, client *core.Client) *Registry { + if baseURL == "" { + baseURL = DefaultURL + } + r := &Registry{ + baseURL: strings.TrimSuffix(baseURL, "/"), + client: client, + } + r.urls = &URLs{baseURL: r.baseURL} + return r +} + +func (r *Registry) Ecosystem() string { + return ecosystem +} + +func (r *Registry) URLs() core.URLBuilder { //nolint:ireturn + return r.urls +} + +// splitName splits "publisher/name", the PURL's namespace and name, or +// "publisher.name", VS Code's extension identifier. +func splitName(name string) (namespace, extension string, err error) { + var ok bool + if strings.Contains(name, "/") { + namespace, extension, ok = strings.Cut(name, "/") + } else { + namespace, extension, ok = strings.Cut(name, ".") + } + if !ok || !validSegment(namespace) || !validSegment(extension) || strings.Contains(extension, "/") { + return "", "", fmt.Errorf("invalid VS Code extension name %q: expected publisher/name or publisher.name", name) + } + return namespace, extension, nil +} + +func validSegment(s string) bool { + return s != "" && s != "." && s != ".." +} + +func validVersion(version string) error { + if !validSegment(version) { + return fmt.Errorf("invalid VS Code extension version %q", version) + } + return nil +} + +type extension struct { + Namespace string `json:"namespace"` + Name string `json:"name"` + Version string `json:"version"` + Timestamp string `json:"timestamp"` + TargetPlatform string `json:"targetPlatform"` + DisplayName string `json:"displayName"` + NamespaceDisplayName string `json:"namespaceDisplayName"` + Description string `json:"description"` + License string `json:"license"` + Homepage string `json:"homepage"` + Repository string `json:"repository"` + Bugs string `json:"bugs"` + DownloadCount int64 `json:"downloadCount"` + Verified bool `json:"verified"` + Deprecated bool `json:"deprecated"` + PreRelease bool `json:"preRelease"` + Tags []string `json:"tags"` + Files map[string]string `json:"files"` + PublishedBy *user `json:"publishedBy"` + Dependencies []extensionRef `json:"dependencies"` + BundledExtensions []extensionRef `json:"bundledExtensions"` +} + +type user struct { + LoginName string `json:"loginName"` + FullName string `json:"fullName"` + Homepage string `json:"homepage"` +} + +type extensionRef struct { + Namespace string `json:"namespace"` + Extension string `json:"extension"` +} + +type queryResponse struct { + Offset int `json:"offset"` + TotalSize int `json:"totalSize"` + Extensions []extension `json:"extensions"` +} + +func (e *extension) is(namespace, name string) bool { + return strings.EqualFold(e.Namespace, namespace) && strings.EqualFold(e.Name, name) +} + +// get fetches one extension document, the latest version when version is +// empty. +func (r *Registry) get(ctx context.Context, name, version string) (*extension, error) { + namespace, ext, err := splitName(name) + if err != nil { + return nil, err + } + endpoint := r.baseURL + "/api/" + url.PathEscape(namespace) + "/" + url.PathEscape(ext) + if version != "" { + endpoint += "/" + url.PathEscape(version) + } + var resp extension + if err := r.client.GetJSON(ctx, endpoint, &resp); err != nil { + var httpErr *core.HTTPError + if errors.As(err, &httpErr) && httpErr.IsNotFound() { + return nil, &core.NotFoundError{Ecosystem: ecosystem, Name: name, Version: version} + } + return nil, err + } + if !resp.is(namespace, ext) { + return nil, fmt.Errorf("open VSX returned extension %s.%s for %s", resp.Namespace, resp.Name, name) + } + return &resp, nil +} + +func (r *Registry) FetchPackage(ctx context.Context, name string) (*core.Package, error) { + resp, err := r.get(ctx, name, "") + if err != nil { + return nil, err + } + var keywords []string + for _, tag := range resp.Tags { + // Tags such as __ext_yaml and __web_extension are VS Code's own + // markers, not keywords the publisher chose. + if !strings.HasPrefix(tag, "__") { + keywords = append(keywords, tag) + } + } + pkg := &core.Package{ + Name: resp.Namespace + "/" + resp.Name, + Description: resp.Description, + Homepage: resp.Homepage, + Repository: urlparser.Parse(resp.Repository), + Licenses: resp.License, + Keywords: keywords, + Namespace: resp.Namespace, + LatestVersion: resp.Version, + Maintainers: maintainers(resp), + Metadata: map[string]any{ + "display_name": resp.DisplayName, + "namespace_display_name": resp.NamespaceDisplayName, + "downloads": resp.DownloadCount, + "verified": resp.Verified, + "deprecated": resp.Deprecated, + "bugs": resp.Bugs, + }, + } + return pkg, nil +} + +// maintainers lists the user who published the latest version. Open VSX +// does not list a namespace's members. +func maintainers(e *extension) []core.Maintainer { + if e.PublishedBy == nil || e.PublishedBy.LoginName == "" { + return nil + } + return []core.Maintainer{{ + Login: e.PublishedBy.LoginName, + Name: e.PublishedBy.FullName, + URL: e.PublishedBy.Homepage, + }} +} + +// query lists every build of the extension, or of one version of it, page +// by page. Each build is one version for one target platform. +func (r *Registry) query(ctx context.Context, name, version string) ([]extension, error) { + namespace, ext, err := splitName(name) + if err != nil { + return nil, err + } + params := url.Values{ + "namespaceName": {namespace}, + "extensionName": {ext}, + "includeAllVersions": {"true"}, + "size": {strconv.Itoa(pageSize)}, + } + if version != "" { + params.Set("extensionVersion", version) + } + var builds []extension + for { + params.Set("offset", strconv.Itoa(len(builds))) + var page queryResponse + if err := r.client.GetJSON(ctx, r.baseURL+"/api/v2/-/query?"+params.Encode(), &page); err != nil { + return nil, err + } + for i := range page.Extensions { + if !page.Extensions[i].is(namespace, ext) { + return nil, fmt.Errorf("open VSX returned extension %s.%s for %s", + page.Extensions[i].Namespace, page.Extensions[i].Name, name) + } + } + builds = append(builds, page.Extensions...) + if len(page.Extensions) == 0 || len(builds) >= page.TotalSize { + break + } + } + if len(builds) == 0 { + return nil, &core.NotFoundError{Ecosystem: ecosystem, Name: name, Version: version} + } + return builds, nil +} + +// versions groups builds by version, newest first, each with one artifact +// per target platform. +func versions(name string, builds []extension) ([]core.Version, error) { + var out []core.Version + index := make(map[string]int) + seen := make(map[[2]string]bool) + for _, b := range builds { + if b.Version == "" { + return nil, fmt.Errorf("open VSX returned a build of %s without a version", name) + } + published, err := time.Parse(time.RFC3339Nano, b.Timestamp) + if err != nil { + return nil, fmt.Errorf("open VSX build %s of %s has invalid timestamp: %w", b.Version, name, err) + } + key := [2]string{b.Version, b.TargetPlatform} + if seen[key] { + return nil, fmt.Errorf("open VSX listed the %s build of %s %s twice", b.TargetPlatform, name, b.Version) + } + seen[key] = true + + i, ok := index[b.Version] + if !ok { + i = len(out) + index[b.Version] = i + out = append(out, core.Version{ + Number: b.Version, + PublishedAt: published, + Licenses: b.License, + Metadata: map[string]any{"pre_release": b.PreRelease}, + }) + } + v := &out[i] + if published.Before(v.PublishedAt) { + v.PublishedAt = published + } + if download := b.Files["download"]; download != "" { + v.Artifacts = append(v.Artifacts, core.Artifact{ + URL: download, + Filename: filename(download), + Metadata: map[string]any{ + "target_platform": b.TargetPlatform, + "signature": b.Files["signature"], + "sha256": b.Files["sha256"], + }, + }) + } + } + sort.SliceStable(out, func(i, j int) bool { + return out[i].PublishedAt.After(out[j].PublishedAt) + }) + return out, nil +} + +func filename(download string) string { + u, err := url.Parse(download) + if err != nil { + return "" + } + return path.Base(u.Path) +} + +func (r *Registry) FetchVersions(ctx context.Context, name string) ([]core.Version, error) { + builds, err := r.query(ctx, name, "") + if err != nil { + return nil, err + } + return versions(name, builds) +} + +// FetchVersion returns one version with the builds for all its target +// platforms. +func (r *Registry) FetchVersion(ctx context.Context, name, version string) (*core.Version, error) { + if err := validVersion(version); err != nil { + return nil, err + } + builds, err := r.query(ctx, name, version) + if err != nil { + return nil, err + } + all, err := versions(name, builds) + if err != nil { + return nil, err + } + for i := range all { + if all[i].Number == version { + return &all[i], nil + } + } + return nil, &core.NotFoundError{Ecosystem: ecosystem, Name: name, Version: version} +} + +// FetchDependencies lists the extensions a version depends on and the ones +// it bundles as an extension pack. VS Code installs both with it. +func (r *Registry) FetchDependencies(ctx context.Context, name, version string) ([]core.Dependency, error) { + if err := validVersion(version); err != nil { + return nil, err + } + resp, err := r.get(ctx, name, version) + if err != nil { + return nil, err + } + refs := make([]extensionRef, 0, len(resp.Dependencies)+len(resp.BundledExtensions)) + refs = append(refs, resp.Dependencies...) + refs = append(refs, resp.BundledExtensions...) + deps := make([]core.Dependency, 0, len(refs)) + for _, ref := range refs { + deps = append(deps, core.Dependency{ + Name: ref.Namespace + "/" + ref.Extension, + Scope: core.Runtime, + }) + } + sort.Slice(deps, func(i, j int) bool { + return deps[i].Name < deps[j].Name + }) + return deps, nil +} + +func (r *Registry) FetchMaintainers(ctx context.Context, name string) ([]core.Maintainer, error) { + resp, err := r.get(ctx, name, "") + if err != nil { + return nil, err + } + return maintainers(resp), nil +} + +type URLs struct { + baseURL string +} + +func (u *URLs) Registry(name, version string) string { + namespace, ext, err := splitName(name) + if err != nil { + return "" + } + page := u.baseURL + "/extension/" + url.PathEscape(namespace) + "/" + url.PathEscape(ext) + if version != "" { + page += "/" + url.PathEscape(version) + } + return page +} + +// Download returns the universal build. A version built only for specific +// target platforms has none; its builds are the artifacts FetchVersions and +// FetchVersion list. +func (u *URLs) Download(name, version string) string { + namespace, ext, err := splitName(name) + if err != nil || validVersion(version) != nil { + return "" + } + file := namespace + "." + ext + "-" + version + ".vsix" + return u.baseURL + "/api/" + url.PathEscape(namespace) + "/" + url.PathEscape(ext) + "/" + + url.PathEscape(version) + "/file/" + url.PathEscape(file) +} + +func (u *URLs) Documentation(name, version string) string { + return u.Registry(name, version) +} + +// PURL carries the registry as repository_url, since a vscode-extension PURL +// without one names the Visual Studio Marketplace. +func (u *URLs) PURL(name, version string) string { + namespace, ext, err := splitName(name) + if err != nil { + return "" + } + return purl.BuildPURLString(ecosystem, namespace+"."+ext, version, u.baseURL) +} diff --git a/internal/vscodeextension/vscodeextension_test.go b/internal/vscodeextension/vscodeextension_test.go new file mode 100644 index 0000000..015a877 --- /dev/null +++ b/internal/vscodeextension/vscodeextension_test.go @@ -0,0 +1,442 @@ +package vscodeextension + +import ( + "context" + "errors" + "fmt" + "net/http" + "net/http/httptest" + "reflect" + "strconv" + "strings" + "sync/atomic" + "testing" + "time" + + "github.com/git-pkgs/registries/internal/core" +) + +const yamlExtension = `{ + "namespace": "redhat", + "name": "vscode-yaml", + "version": "1.15.0", + "timestamp": "2024-06-03T10:00:00.5Z", + "targetPlatform": "universal", + "displayName": "YAML", + "namespaceDisplayName": "Red Hat", + "description": "YAML Language Support by Red Hat", + "license": "MIT", + "homepage": "https://github.com/redhat-developer/vscode-yaml#readme", + "repository": "https://github.com/redhat-developer/vscode-yaml.git", + "bugs": "https://github.com/redhat-developer/vscode-yaml/issues", + "downloadCount": 9255952, + "verified": true, + "deprecated": false, + "preRelease": false, + "tags": ["__ext_yaml", "__web_extension", "kubernetes", "yaml"], + "publishedBy": { + "loginName": "rhdevelopers-ci", + "fullName": "Red Hat Developers CI", + "homepage": "https://github.com/rhdevelopers-ci", + "provider": "github" + }, + "dependencies": [], + "bundledExtensions": [] +}` + +// csharpEntry is one entry of a query response: one version built for one +// target platform. +func csharpEntry(version, platform, timestamp string, preRelease bool) string { + file := "muhammad-sammy.csharp-" + version + dir := version + if platform != "universal" { + file += "@" + platform + dir = platform + "/" + version + } + base := "https://open-vsx.org/api/muhammad-sammy/csharp/" + dir + "/file/" + return fmt.Sprintf(`{ + "namespace": "muhammad-sammy", + "name": "csharp", + "version": %q, + "targetPlatform": %q, + "timestamp": %q, + "preRelease": %t, + "license": "MIT", + "files": { + "download": %q, + "signature": %q, + "sha256": %q, + "manifest": %q + } + }`, version, platform, timestamp, preRelease, base+file+".vsix", base+file+".sigzip", base+file+".sha256", base+"package.json") +} + +func queryPage(total, offset int, entries ...string) string { + return fmt.Sprintf(`{"offset": %d, "totalSize": %d, "extensions": [%s]}`, offset, total, strings.Join(entries, ",")) +} + +func newTestRegistry(t *testing.T, handler http.HandlerFunc) (*Registry, *atomic.Int32) { + t.Helper() + var requests atomic.Int32 + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + requests.Add(1) + handler(w, r) + })) + t.Cleanup(server.Close) + return New(server.URL, core.DefaultClient()), &requests +} + +func TestFetchPackage(t *testing.T) { + for _, name := range []string{"redhat/vscode-yaml", "redhat.vscode-yaml"} { + t.Run(name, func(t *testing.T) { + reg, _ := newTestRegistry(t, func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/api/redhat/vscode-yaml" { + t.Errorf("unexpected path %s", r.URL.Path) + http.NotFound(w, r) + return + } + _, _ = w.Write([]byte(yamlExtension)) + }) + + pkg, err := reg.FetchPackage(context.Background(), name) + if err != nil { + t.Fatalf("FetchPackage: %v", err) + } + want := &core.Package{ + Name: "redhat/vscode-yaml", + Description: "YAML Language Support by Red Hat", + Homepage: "https://github.com/redhat-developer/vscode-yaml#readme", + Repository: "https://github.com/redhat-developer/vscode-yaml", + Licenses: "MIT", + Keywords: []string{"kubernetes", "yaml"}, + Namespace: "redhat", + LatestVersion: "1.15.0", + Maintainers: []core.Maintainer{{ + Login: "rhdevelopers-ci", + Name: "Red Hat Developers CI", + URL: "https://github.com/rhdevelopers-ci", + }}, + Metadata: map[string]any{ + "display_name": "YAML", + "namespace_display_name": "Red Hat", + "downloads": int64(9255952), + "verified": true, + "deprecated": false, + "bugs": "https://github.com/redhat-developer/vscode-yaml/issues", + }, + } + if !reflect.DeepEqual(pkg, want) { + t.Errorf("FetchPackage =\n%#v\nwant\n%#v", pkg, want) + } + }) + } +} + +func TestFetchPackageNotFound(t *testing.T) { + reg, _ := newTestRegistry(t, func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusNotFound) + _, _ = w.Write([]byte(`{"error": "Extension not found: nope.missing"}`)) + }) + _, err := reg.FetchPackage(context.Background(), "nope/missing") + var notFound *core.NotFoundError + if !errors.As(err, ¬Found) { + t.Fatalf("FetchPackage error = %v, want NotFoundError", err) + } +} + +// A registry that answers with another extension's metadata, for example +// after a redirect, must not have it reported as the one asked for. +func TestFetchPackageRejectsAnotherExtension(t *testing.T) { + reg, _ := newTestRegistry(t, func(w http.ResponseWriter, _ *http.Request) { + _, _ = w.Write([]byte(yamlExtension)) + }) + if _, err := reg.FetchPackage(context.Background(), "redhat/java"); err == nil { + t.Fatal("FetchPackage accepted metadata for redhat/vscode-yaml as redhat/java") + } +} + +func TestFetchPackageMatchesNamesWithoutCase(t *testing.T) { + reg, _ := newTestRegistry(t, func(w http.ResponseWriter, _ *http.Request) { + _, _ = w.Write([]byte(yamlExtension)) + }) + pkg, err := reg.FetchPackage(context.Background(), "RedHat/VSCode-YAML") + if err != nil { + t.Fatalf("FetchPackage: %v", err) + } + if pkg.Name != "redhat/vscode-yaml" { + t.Errorf("Name = %q, want the registry's spelling redhat/vscode-yaml", pkg.Name) + } +} + +func TestFetchVersions(t *testing.T) { + entries := []string{ + csharpEntry("2.160.9", "linux-x64", "2026-10-09T16:23:47.834682Z", false), + csharpEntry("2.160.9", "alpine-x64", "2026-10-09T16:23:47.049825Z", false), + csharpEntry("2.170.0-rc1", "universal", "2026-11-01T08:00:00Z", true), + csharpEntry("2.110.4", "universal", "2025-01-02T03:04:05Z", false), + } + // The registry answers at most two entries a page, whatever size asks for. + const perPage = 2 + reg, requests := newTestRegistry(t, func(w http.ResponseWriter, r *http.Request) { + q := r.URL.Query() + if r.URL.Path != "/api/v2/-/query" || q.Get("namespaceName") != "muhammad-sammy" || + q.Get("extensionName") != "csharp" || q.Get("includeAllVersions") != "true" { + t.Errorf("unexpected request %s", r.URL) + http.NotFound(w, r) + return + } + offset, _ := strconv.Atoi(q.Get("offset")) + end := min(offset+perPage, len(entries)) + _, _ = w.Write([]byte(queryPage(len(entries), offset, entries[offset:end]...))) + }) + + versions, err := reg.FetchVersions(context.Background(), "muhammad-sammy/csharp") + if err != nil { + t.Fatalf("FetchVersions: %v", err) + } + if got := requests.Load(); got != 2 { + t.Errorf("made %d requests, want 2 pages", got) + } + + artifact := func(version, platform string) core.Artifact { + file := "muhammad-sammy.csharp-" + version + dir := version + if platform != "universal" { + file += "@" + platform + dir = platform + "/" + version + } + base := "https://open-vsx.org/api/muhammad-sammy/csharp/" + dir + "/file/" + return core.Artifact{ + URL: base + file + ".vsix", + Filename: file + ".vsix", + Metadata: map[string]any{ + "target_platform": platform, + "signature": base + file + ".sigzip", + "sha256": base + file + ".sha256", + }, + } + } + want := []core.Version{ + { + Number: "2.170.0-rc1", + PublishedAt: time.Date(2026, 11, 1, 8, 0, 0, 0, time.UTC), + Licenses: "MIT", + Artifacts: []core.Artifact{artifact("2.170.0-rc1", "universal")}, + Metadata: map[string]any{"pre_release": true}, + }, + { + Number: "2.160.9", + // The earliest of its builds. + PublishedAt: time.Date(2026, 10, 9, 16, 23, 47, 49825000, time.UTC), + Licenses: "MIT", + Artifacts: []core.Artifact{artifact("2.160.9", "linux-x64"), artifact("2.160.9", "alpine-x64")}, + Metadata: map[string]any{"pre_release": false}, + }, + { + Number: "2.110.4", + PublishedAt: time.Date(2025, 1, 2, 3, 4, 5, 0, time.UTC), + Licenses: "MIT", + Artifacts: []core.Artifact{artifact("2.110.4", "universal")}, + Metadata: map[string]any{"pre_release": false}, + }, + } + if !reflect.DeepEqual(versions, want) { + t.Errorf("FetchVersions =\n%#v\nwant\n%#v", versions, want) + } +} + +func TestFetchVersionsNotFound(t *testing.T) { + reg, _ := newTestRegistry(t, func(w http.ResponseWriter, _ *http.Request) { + _, _ = w.Write([]byte(queryPage(0, 0))) + }) + _, err := reg.FetchVersions(context.Background(), "nope/missing") + var notFound *core.NotFoundError + if !errors.As(err, ¬Found) { + t.Fatalf("FetchVersions error = %v, want NotFoundError", err) + } +} + +// A page that adds nothing ends the listing even if totalSize promises more, +// so a registry that miscounts cannot keep the client asking. +func TestFetchVersionsStopsAtAnEmptyPage(t *testing.T) { + reg, requests := newTestRegistry(t, func(w http.ResponseWriter, r *http.Request) { + if r.URL.Query().Get("offset") == "0" { + _, _ = w.Write([]byte(queryPage(50, 0, csharpEntry("2.110.4", "universal", "2025-01-02T03:04:05Z", false)))) + return + } + _, _ = w.Write([]byte(queryPage(50, 1))) + }) + versions, err := reg.FetchVersions(context.Background(), "muhammad-sammy/csharp") + if err != nil { + t.Fatalf("FetchVersions: %v", err) + } + if len(versions) != 1 || requests.Load() != 2 { + t.Errorf("got %d versions in %d requests, want 1 in 2", len(versions), requests.Load()) + } +} + +func TestFetchVersionsRejectsBadEntries(t *testing.T) { + for _, tt := range []struct { + name string + entries []string + }{ + {"another extension", []string{strings.Replace(csharpEntry("1.0.0", "universal", "2025-01-02T03:04:05Z", false), `"csharp"`, `"other"`, 1)}}, + {"no version", []string{csharpEntry("", "universal", "2025-01-02T03:04:05Z", false)}}, + {"bad timestamp", []string{csharpEntry("1.0.0", "universal", "yesterday", false)}}, + {"same build twice", []string{ + csharpEntry("1.0.0", "linux-x64", "2025-01-02T03:04:05Z", false), + csharpEntry("1.0.0", "linux-x64", "2025-01-02T03:04:05Z", false), + }}, + } { + t.Run(tt.name, func(t *testing.T) { + reg, _ := newTestRegistry(t, func(w http.ResponseWriter, _ *http.Request) { + _, _ = w.Write([]byte(queryPage(len(tt.entries), 0, tt.entries...))) + }) + if _, err := reg.FetchVersions(context.Background(), "muhammad-sammy/csharp"); err == nil { + t.Error("FetchVersions accepted the response") + } + }) + } +} + +func TestFetchVersion(t *testing.T) { + reg, _ := newTestRegistry(t, func(w http.ResponseWriter, r *http.Request) { + q := r.URL.Query() + if r.URL.Path != "/api/v2/-/query" || q.Get("extensionVersion") != "2.160.9" { + t.Errorf("unexpected request %s", r.URL) + } + _, _ = w.Write([]byte(queryPage(2, 0, + csharpEntry("2.160.9", "linux-x64", "2026-10-09T16:23:47Z", false), + csharpEntry("2.160.9", "win32-x64", "2026-10-09T16:23:48Z", false)))) + }) + version, err := reg.FetchVersion(context.Background(), "muhammad-sammy/csharp", "2.160.9") + if err != nil { + t.Fatalf("FetchVersion: %v", err) + } + var platforms []string + for _, a := range version.Artifacts { + platforms = append(platforms, a.Metadata["target_platform"].(string)) + } + if version.Number != "2.160.9" || !reflect.DeepEqual(platforms, []string{"linux-x64", "win32-x64"}) { + t.Errorf("FetchVersion = %s with %v, want 2.160.9 with linux-x64 and win32-x64", version.Number, platforms) + } +} + +func TestFetchVersionNotFound(t *testing.T) { + reg, _ := newTestRegistry(t, func(w http.ResponseWriter, _ *http.Request) { + _, _ = w.Write([]byte(queryPage(0, 0))) + }) + _, err := reg.FetchVersion(context.Background(), "muhammad-sammy/csharp", "9.9.9") + var notFound *core.NotFoundError + if !errors.As(err, ¬Found) || notFound.Version != "9.9.9" { + t.Fatalf("FetchVersion error = %v, want NotFoundError for 9.9.9", err) + } +} + +func TestFetchDependencies(t *testing.T) { + reg, _ := newTestRegistry(t, func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/api/ms-python/python/2026.4.0" { + t.Errorf("unexpected path %s", r.URL.Path) + http.NotFound(w, r) + return + } + _, _ = w.Write([]byte(`{ + "namespace": "ms-python", "name": "python", "version": "2026.4.0", + "dependencies": [{"namespace": "ms-toolsai", "extension": "jupyter"}], + "bundledExtensions": [ + {"namespace": "ms-python", "extension": "vscode-pylance"}, + {"namespace": "ms-python", "extension": "debugpy"} + ] + }`)) + }) + deps, err := reg.FetchDependencies(context.Background(), "ms-python/python", "2026.4.0") + if err != nil { + t.Fatalf("FetchDependencies: %v", err) + } + want := []core.Dependency{ + {Name: "ms-python/debugpy", Scope: core.Runtime}, + {Name: "ms-python/vscode-pylance", Scope: core.Runtime}, + {Name: "ms-toolsai/jupyter", Scope: core.Runtime}, + } + if !reflect.DeepEqual(deps, want) { + t.Errorf("FetchDependencies = %#v, want %#v", deps, want) + } +} + +func TestFetchDependenciesNotFound(t *testing.T) { + reg, _ := newTestRegistry(t, func(w http.ResponseWriter, r *http.Request) { + http.NotFound(w, r) + }) + _, err := reg.FetchDependencies(context.Background(), "ms-python/python", "0.0.1") + var notFound *core.NotFoundError + if !errors.As(err, ¬Found) || notFound.Version != "0.0.1" { + t.Fatalf("FetchDependencies error = %v, want NotFoundError for 0.0.1", err) + } +} + +func TestInvalidNamesMakeNoRequest(t *testing.T) { + reg, requests := newTestRegistry(t, func(w http.ResponseWriter, r *http.Request) { + http.NotFound(w, r) + }) + ctx := context.Background() + for _, name := range []string{"", "redhat", "redhat/", "/vscode-yaml", "a/b/c", "../x", "x/..", ".", "redhat."} { + if _, err := reg.FetchPackage(ctx, name); err == nil { + t.Errorf("FetchPackage(%q) succeeded", name) + } + if _, err := reg.FetchVersions(ctx, name); err == nil { + t.Errorf("FetchVersions(%q) succeeded", name) + } + if _, err := reg.FetchDependencies(ctx, name, "1.0.0"); err == nil { + t.Errorf("FetchDependencies(%q) succeeded", name) + } + } + for _, version := range []string{"", ".", ".."} { + if _, err := reg.FetchDependencies(ctx, "redhat/vscode-yaml", version); err == nil { + t.Errorf("FetchDependencies(version %q) succeeded", version) + } + } + if got := requests.Load(); got != 0 { + t.Errorf("made %d requests for invalid names", got) + } +} + +func TestEscapesPathSegments(t *testing.T) { + var path string + reg, _ := newTestRegistry(t, func(w http.ResponseWriter, r *http.Request) { + path = r.URL.EscapedPath() + http.NotFound(w, r) + }) + _, _ = reg.FetchDependencies(context.Background(), "a b/c", "1.0.0?x#y") + if want := "/api/a%20b/c/1.0.0%3Fx%23y"; path != want { + t.Errorf("requested %q, want %q", path, want) + } +} + +func TestURLs(t *testing.T) { + urls := New("", nil).URLs() + for _, tt := range []struct { + name, got, want string + }{ + {"registry", urls.Registry("redhat/vscode-yaml", ""), "https://open-vsx.org/extension/redhat/vscode-yaml"}, + {"registry version", urls.Registry("redhat.vscode-yaml", "1.15.0"), "https://open-vsx.org/extension/redhat/vscode-yaml/1.15.0"}, + {"download", urls.Download("redhat/vscode-yaml", "1.15.0"), "https://open-vsx.org/api/redhat/vscode-yaml/1.15.0/file/redhat.vscode-yaml-1.15.0.vsix"}, + {"download without version", urls.Download("redhat/vscode-yaml", ""), ""}, + {"documentation", urls.Documentation("redhat/vscode-yaml", "1.15.0"), "https://open-vsx.org/extension/redhat/vscode-yaml/1.15.0"}, + {"purl", urls.PURL("RedHat/VSCode-YAML", "1.15.0"), "pkg:vscode-extension/redhat/vscode-yaml@1.15.0?repository_url=https:%2F%2Fopen-vsx.org"}, + {"purl without version", urls.PURL("redhat/vscode-yaml", ""), "pkg:vscode-extension/redhat/vscode-yaml?repository_url=https:%2F%2Fopen-vsx.org"}, + {"invalid name", urls.Registry("vscode-yaml", ""), ""}, + {"invalid name purl", urls.PURL("vscode-yaml", "1.0.0"), ""}, + } { + if tt.got != tt.want { + t.Errorf("%s = %q, want %q", tt.name, tt.got, tt.want) + } + } + + self := New("https://vsx.example.com/", nil).URLs() + if got, want := self.Download("redhat/vscode-yaml", "1.15.0"), "https://vsx.example.com/api/redhat/vscode-yaml/1.15.0/file/redhat.vscode-yaml-1.15.0.vsix"; got != want { + t.Errorf("self-hosted download = %q, want %q", got, want) + } + if got, want := self.PURL("redhat/vscode-yaml", "1.15.0"), "pkg:vscode-extension/redhat/vscode-yaml@1.15.0?repository_url=https:%2F%2Fvsx.example.com"; got != want { + t.Errorf("self-hosted purl = %q, want %q", got, want) + } +} diff --git a/registries_test.go b/registries_test.go index 81027fa..cd9cf3d 100644 --- a/registries_test.go +++ b/registries_test.go @@ -20,7 +20,7 @@ import ( func TestSupportedEcosystems(t *testing.T) { ecosystems := registries.SupportedEcosystems() - expected := []string{"apk", "apm", "brew", "cargo", "clojars", "cocoapods", "composer", "conda", "cpan", "cran", "deno", "dub", "elm", "gem", "githubactions", "golang", "hackage", "haxelib", "helm", "hex", "julia", "luarocks", "maven", "nimble", "npm", "nuget", "pub", "pypi", "swift", "terraform"} + expected := []string{"apk", "apm", "brew", "cargo", "clojars", "cocoapods", "composer", "conda", "cpan", "cran", "deno", "dub", "elm", "gem", "githubactions", "golang", "hackage", "haxelib", "helm", "hex", "julia", "luarocks", "maven", "nimble", "npm", "nuget", "pub", "pypi", "swift", "terraform", "vscode-extension"} sort.Strings(ecosystems) if len(ecosystems) != len(expected) { @@ -65,6 +65,7 @@ func TestNew(t *testing.T) { {"deno", false}, {"terraform", false}, {"githubactions", false}, + {"vscode-extension", false}, {"helm", true}, {"swift", true}, {"apm", true}, @@ -113,6 +114,7 @@ func TestDefaultURL(t *testing.T) { {"deno", "https://apiland.deno.dev"}, {"terraform", "https://registry.terraform.io"}, {"githubactions", "https://api.github.com"}, + {"vscode-extension", "https://open-vsx.org"}, {"helm", ""}, {"swift", ""}, {"apm", ""}, From 92e2b79c6ea01a8dd2853457e0eb842233e6d389 Mon Sep 17 00:00:00 2001 From: Simon Chrzanowski Date: Sun, 15 Nov 2026 14:01:51 +0100 Subject: [PATCH 2/3] Cap Open VSX pagination and select a build by the platform qualifier The query loop now stops with ErrTooManyPages at core.MaxPages, so a server that ignores offset cannot keep the client requesting. The artifact metadata key is platform, and platform is a selection qualifier, so the resolver reads a version's artifacts when a PURL names a platform. A universal build matches any platform, after a build for the platform asked for. A registry that lists no artifacts keeps resolving to its built URL when a selection qualifier is given. --- README.md | 4 +- fetch/resolver.go | 58 ++++++-- fetch/vscode_resolver_test.go | 128 ++++++++++++++++++ internal/vscodeextension/vscodeextension.go | 11 +- .../vscodeextension/vscodeextension_test.go | 23 +++- 5 files changed, 206 insertions(+), 18 deletions(-) create mode 100644 fetch/vscode_resolver_test.go diff --git a/README.md b/README.md index 55129ce..c82e14f 100644 --- a/README.md +++ b/README.md @@ -205,7 +205,7 @@ The Alpine URL is one repository architecture directory, such as `https://dl-cdn GitHub Actions are git repositories, so `FetchPackage` reads `/repos/{owner}/{repo}` and `FetchVersions` lists tags from the GitHub REST API. Unauthenticated requests are limited to 60 per hour. A subdirectory action such as `pkg:githubactions/github/codeql-action/init@v3` resolves to the `github/codeql-action` repository. `URLs().Download` returns the `/archive/{ref}.tar.gz` source tarball for any tag, branch or commit SHA. GitHub doesn't guarantee these tarballs are byte-stable, so versions carry no `Integrity`. Dependencies and maintainers return an error wrapping `errors.ErrUnsupported`. For GitHub Enterprise Server, pass the `/api/v3` API root as the base URL. -VS Code extensions are read from [Open VSX](https://open-vsx.org), or from a self-hosted Open VSX server given as the base URL. The purl spec makes the Visual Studio Marketplace the default for `vscode-extension`, but the client has no Marketplace support, so a PURL without `repository_url` also resolves to Open VSX. `URLs().PURL` always adds the registry as `repository_url`. Names are `publisher/name` or VS Code's `publisher.name`. An extension can publish a separate build for each target platform, such as `linux-x64` or `darwin-arm64`. Each version from `FetchVersions` and `FetchVersion` carries one artifact per build, with `target_platform`, `signature` (the `.sigzip`) and `sha256` (the URL of the checksum file) in its metadata. `URLs().Download` returns the universal build, which a version built only for specific platforms doesn't have. `FetchDependencies` lists both the extensions a version depends on and the ones it bundles as an extension pack. `FetchMaintainers` returns the user who published the latest version. +VS Code extensions are read from [Open VSX](https://open-vsx.org), or from a self-hosted Open VSX server given as the base URL. The purl spec makes the Visual Studio Marketplace the default for `vscode-extension`, but the client has no Marketplace support, so a PURL without `repository_url` also resolves to Open VSX. `URLs().PURL` always adds the registry as `repository_url`. Names are `publisher/name` or VS Code's `publisher.name`. An extension can publish a separate build for each target platform, such as `linux-x64` or `darwin-arm64`. Each version from `FetchVersions` and `FetchVersion` carries one artifact per build, with `platform`, `signature` (the `.sigzip`) and `sha256` (the URL of the checksum file) in its metadata. `URLs().Download` returns the universal build, which a version built only for specific platforms doesn't have; the `platform` qualifier, as in `pkg:vscode-extension/muhammad-sammy/csharp@2.160.9?platform=linux-x64`, makes `fetch.Resolver` pick the build for that platform, or the universal one when there is none. `FetchDependencies` lists both the extensions a version depends on and the ones it bundles as an extension pack. `FetchMaintainers` returns the user who published the latest version. ### Swift registries @@ -570,7 +570,7 @@ info, err := resolver.Resolve(ctx, "cargo", "serde", "1.0.0") // info.Integrity may be populated for metadata-resolved URLs ``` -When a version has several files, `ResolveWithOptions` picks one by PURL qualifiers. `arch`, `distro`, `subdir` and `build` are matched against artifact metadata, and an artifact that doesn't record a qualifier isn't ruled out by it. `repository_url` and `channel` are ignored: register a registry for that URL instead. +When a version has several files, `ResolveWithOptions` picks one by PURL qualifiers. `arch`, `distro`, `subdir`, `build` and `platform` are matched against artifact metadata, and an artifact that doesn't record a qualifier isn't ruled out by it. An artifact whose `platform` is `universal` matches any requested platform, after one built for that platform. When a registry lists no artifacts for the version, the URL from `URLs().Download` is returned. `repository_url` and `channel` are ignored: register a registry for that URL instead. ```go info, err := resolver.ResolveWithOptions(ctx, "conda", "zlib", "1.3.1", fetch.ResolveOptions{ diff --git a/fetch/resolver.go b/fetch/resolver.go index dbe4469..ed6c070 100644 --- a/fetch/resolver.go +++ b/fetch/resolver.go @@ -113,14 +113,22 @@ type ResolveOptions struct { Integrity string // Qualifiers are PURL qualifiers. Those in selectionQualifiers pick among // the artifacts a registry lists for a version; an artifact that does not - // record a qualifier is not ruled out by it. repository_url and channel + // record a qualifier is not ruled out by it. An artifact whose platform is + // universal serves any platform, after one built for the platform asked + // for. repository_url and channel // choose the registry, which is up to the caller, and are ignored here. Qualifiers map[string]string } // selectionQualifiers are the PURL qualifiers matched against artifact // metadata of the same name. -var selectionQualifiers = []string{"arch", "distro", "subdir", "build"} +var selectionQualifiers = []string{"arch", "distro", "subdir", "build", qualifierPlatform} + +const ( + qualifierPlatform = "platform" + // universalPlatform is the platform of a build that runs on every one. + universalPlatform = "universal" +) func (o ResolveOptions) hasSelectionQualifier() bool { for _, key := range selectionQualifiers { @@ -166,7 +174,15 @@ func (r *Resolver) ResolveWithOptions( } // For ecosystems like PyPI, we need to fetch metadata to get the URL - return r.resolveFromMetadata(ctx, reg, name, version, options) + info, err := r.resolveFromMetadata(ctx, reg, name, version, options) + if errors.Is(err, ErrNoDownloadURL) && options.hasSelectionQualifier() { + // The registry lists no artifacts to pick among, so the built + // URL is the only one there is. + if url := reg.URLs().Download(name, version); url != "" { + return matchSingleArtifact(&ArtifactInfo{URL: url, Filename: filenameFromURL(url)}, options, nil) + } + } + return info, err } // resolveWithoutRegistry handles ecosystems with predictable URLs @@ -274,6 +290,27 @@ func (r *Resolver) resolveFromMetadata( } func (r *Resolver) selectArtifact(ctx context.Context, candidates []registries.Artifact, options ResolveOptions) (*ArtifactInfo, error) { + // A build for the requested platform wins over a universal one, so + // universal builds are only let in on a second pass. + universal := []bool{false} + if options.Qualifiers[qualifierPlatform] != "" { + universal = []bool{false, true} + } + for _, allowUniversal := range universal { + info, err := r.selectArtifactPass(ctx, candidates, options, allowUniversal) + if err != nil || info != nil { + return info, err + } + } + return nil, ErrNoMatchingArtifact +} + +func (r *Resolver) selectArtifactPass( + ctx context.Context, + candidates []registries.Artifact, + options ResolveOptions, + allowUniversal bool, +) (*ArtifactInfo, error) { for _, candidate := range candidates { filename := candidate.Filename if filename == "" { @@ -285,7 +322,7 @@ func (r *Resolver) selectArtifact(ctx context.Context, candidates []registries.A if options.Integrity != "" && !integrityMatches(candidate.Integrity, options.Integrity) { continue } - if !qualifiersMatch(candidate.Metadata, options.Qualifiers) { + if !qualifiersMatch(candidate.Metadata, options.Qualifiers, allowUniversal) { continue } if err := r.checkMetadataURL(ctx, candidate.URL); err != nil { @@ -298,18 +335,23 @@ func (r *Resolver) selectArtifact(ctx context.Context, candidates []registries.A Size: candidate.Size, }, nil } - return nil, ErrNoMatchingArtifact + return nil, nil } -func qualifiersMatch(metadata map[string]any, qualifiers map[string]string) bool { +func qualifiersMatch(metadata map[string]any, qualifiers map[string]string, allowUniversal bool) bool { for _, key := range selectionQualifiers { want := qualifiers[key] if want == "" { continue } - if got, ok := metadata[key].(string); ok && got != "" && got != want { - return false + got, ok := metadata[key].(string) + if !ok || got == "" || got == want { + continue + } + if key == qualifierPlatform && got == universalPlatform && allowUniversal { + continue } + return false } return true } diff --git a/fetch/vscode_resolver_test.go b/fetch/vscode_resolver_test.go new file mode 100644 index 0000000..f36cb29 --- /dev/null +++ b/fetch/vscode_resolver_test.go @@ -0,0 +1,128 @@ +package fetch + +import ( + "context" + "errors" + "fmt" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/git-pkgs/registries" + "github.com/git-pkgs/registries/client" + _ "github.com/git-pkgs/registries/internal/vscodeextension" +) + +func openVSXBuild(version, platform string) string { + file := "acme.tool-" + version + dir := version + if platform != "universal" { + file += "@" + platform + dir = platform + "/" + version + } + return fmt.Sprintf(`{"namespace": "acme", "name": "tool", "version": %q, "targetPlatform": %q, + "timestamp": "2026-01-02T03:04:05Z", + "files": {"download": "https://open-vsx.org/api/acme/tool/%s/file/%s.vsix"}}`, version, platform, dir, file) +} + +// The platform qualifier picks a build among those a version lists. A +// universal build serves any platform, and a build for the platform wins +// over it. +func TestResolveOpenVSXPlatform(t *testing.T) { + builds := []string{ + openVSXBuild("1.0.0", "linux-x64"), + openVSXBuild("1.0.0", "darwin-arm64"), + openVSXBuild("2.0.0", "universal"), + openVSXBuild("3.0.0", "universal"), + openVSXBuild("3.0.0", "linux-x64"), + } + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/api/v2/-/query" || r.URL.Query().Get("offset") != "0" { + _, _ = w.Write([]byte(`{"offset": 0, "totalSize": 0, "extensions": []}`)) + return + } + _, _ = fmt.Fprintf(w, `{"offset": 0, "totalSize": %d, "extensions": [%s]}`, len(builds), strings.Join(builds, ",")) + })) + defer server.Close() + + reg, err := registries.New("vscode-extension", server.URL, registries.DefaultClient()) + if err != nil { + t.Fatal(err) + } + r := NewResolver() + r.RegisterRegistry(reg) + + tests := []struct { + name string + version string + platform string + want string + }{ + {"platform-only version", "1.0.0", "linux-x64", "acme.tool-1.0.0@linux-x64.vsix"}, + {"platform-only version, second build", "1.0.0", "darwin-arm64", "acme.tool-1.0.0@darwin-arm64.vsix"}, + {"universal-only version", "2.0.0", "linux-x64", "acme.tool-2.0.0.vsix"}, + {"both, platform build wins", "3.0.0", "linux-x64", "acme.tool-3.0.0@linux-x64.vsix"}, + {"both, other platform gets universal", "3.0.0", "darwin-arm64", "acme.tool-3.0.0.vsix"}, + {"both, universal asked for", "3.0.0", "universal", "acme.tool-3.0.0.vsix"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + info, err := r.ResolveWithOptions(context.Background(), "vscode-extension", "acme/tool", tt.version, + ResolveOptions{Qualifiers: map[string]string{"platform": tt.platform}}) + if err != nil { + t.Fatalf("Resolve failed: %v", err) + } + if info.Filename != tt.want { + t.Errorf("Filename = %q, want %q", info.Filename, tt.want) + } + }) + } + + _, err = r.ResolveWithOptions(context.Background(), "vscode-extension", "acme/tool", "1.0.0", + ResolveOptions{Qualifiers: map[string]string{"platform": "win32-x64"}}) + if !errors.Is(err, ErrNoMatchingArtifact) { + t.Errorf("platform without a build: err = %v, want ErrNoMatchingArtifact", err) + } +} + +type urlOnlyRegistry struct{ fakeRegistry } + +func (f *urlOnlyRegistry) URLs() client.URLBuilder { //nolint:ireturn // satisfying Registry interface + return &client.BaseURLs{DownloadFn: func(name, version string) string { + return "https://example.com/downloads/" + name + "-" + version + ".gem" + }} +} + +// A registry that lists no artifacts, such as RubyGems, has nothing to match +// a selection qualifier against, so its built URL is still the answer. +func TestResolveSelectionQualifierWithoutArtifactsUsesTheBuiltURL(t *testing.T) { + r := NewResolver() + r.RegisterRegistry(&urlOnlyRegistry{fakeRegistry{versions: []registries.Version{{Number: "1.0.0"}}}}) + + info, err := r.ResolveWithOptions(context.Background(), "fake", "pkg", "1.0.0", + ResolveOptions{Qualifiers: map[string]string{"platform": "java"}}) + if err != nil { + t.Fatalf("Resolve failed: %v", err) + } + if want := "https://example.com/downloads/pkg-1.0.0.gem"; info.URL != want { + t.Errorf("URL = %q, want %q", info.URL, want) + } +} + +// Only the platform qualifier has a value that stands for every other. +func TestResolveUniversalOnlyStandsForPlatforms(t *testing.T) { + r := NewResolver() + r.RegisterRegistry(&fakeRegistry{versions: []registries.Version{{ + Number: "1.0.0", + Artifacts: []registries.Artifact{ + {URL: "https://example.com/pkg-1.0.0.bin", Metadata: map[string]any{"arch": "universal", "platform": "linux-x64"}}, + }, + }}}) + + _, err := r.ResolveWithOptions(context.Background(), "fake", "pkg", "1.0.0", + ResolveOptions{Qualifiers: map[string]string{"arch": "amd64", "platform": "linux-x64"}}) + if !errors.Is(err, ErrNoMatchingArtifact) { + t.Errorf("err = %v, want ErrNoMatchingArtifact", err) + } +} diff --git a/internal/vscodeextension/vscodeextension.go b/internal/vscodeextension/vscodeextension.go index 07dda16..0826d12 100644 --- a/internal/vscodeextension/vscodeextension.go +++ b/internal/vscodeextension/vscodeextension.go @@ -220,7 +220,10 @@ func (r *Registry) query(ctx context.Context, name, version string) ([]extension params.Set("extensionVersion", version) } var builds []extension - for { + for page := 1; ; page++ { + if page > core.MaxPages { + return nil, fmt.Errorf("%w: %s builds", core.ErrTooManyPages, name) + } params.Set("offset", strconv.Itoa(len(builds))) var page queryResponse if err := r.client.GetJSON(ctx, r.baseURL+"/api/v2/-/query?"+params.Encode(), &page); err != nil { @@ -283,9 +286,9 @@ func versions(name string, builds []extension) ([]core.Version, error) { URL: download, Filename: filename(download), Metadata: map[string]any{ - "target_platform": b.TargetPlatform, - "signature": b.Files["signature"], - "sha256": b.Files["sha256"], + "platform": b.TargetPlatform, + "signature": b.Files["signature"], + "sha256": b.Files["sha256"], }, }) } diff --git a/internal/vscodeextension/vscodeextension_test.go b/internal/vscodeextension/vscodeextension_test.go index 015a877..a50356b 100644 --- a/internal/vscodeextension/vscodeextension_test.go +++ b/internal/vscodeextension/vscodeextension_test.go @@ -210,9 +210,9 @@ func TestFetchVersions(t *testing.T) { URL: base + file + ".vsix", Filename: file + ".vsix", Metadata: map[string]any{ - "target_platform": platform, - "signature": base + file + ".sigzip", - "sha256": base + file + ".sha256", + "platform": platform, + "signature": base + file + ".sigzip", + "sha256": base + file + ".sha256", }, } } @@ -275,6 +275,21 @@ func TestFetchVersionsStopsAtAnEmptyPage(t *testing.T) { } } +// A server that ignores offset and reports a large totalSize answers the same +// page for ever, so the listing gives up at core.MaxPages. +func TestFetchVersionsStopsAtMaxPages(t *testing.T) { + reg, requests := newTestRegistry(t, func(w http.ResponseWriter, r *http.Request) { + _, _ = w.Write([]byte(queryPage(1<<30, 0, csharpEntry("2.110.4", "universal", "2025-01-02T03:04:05Z", false)))) + }) + _, err := reg.FetchVersions(context.Background(), "muhammad-sammy/csharp") + if !errors.Is(err, core.ErrTooManyPages) { + t.Errorf("err = %v, want ErrTooManyPages", err) + } + if got := requests.Load(); got != core.MaxPages { + t.Errorf("made %d requests, want %d", got, core.MaxPages) + } +} + func TestFetchVersionsRejectsBadEntries(t *testing.T) { for _, tt := range []struct { name string @@ -315,7 +330,7 @@ func TestFetchVersion(t *testing.T) { } var platforms []string for _, a := range version.Artifacts { - platforms = append(platforms, a.Metadata["target_platform"].(string)) + platforms = append(platforms, a.Metadata["platform"].(string)) } if version.Number != "2.160.9" || !reflect.DeepEqual(platforms, []string{"linux-x64", "win32-x64"}) { t.Errorf("FetchVersion = %s with %v, want 2.160.9 with linux-x64 and win32-x64", version.Number, platforms) From 8d5655833ec87d468185dcd123fe2b971be77cbe Mon Sep 17 00:00:00 2001 From: Simon Chrzanowski Date: Sun, 15 Nov 2026 14:25:31 +0100 Subject: [PATCH 3/3] Resolve to the built URL when a selection qualifier meets an unlisted version RubyGems lists a platform build as -, so a PURL with the plain number and a platform qualifier found no version and failed. It resolves to the built URL again, as before platform was a selection qualifier. --- README.md | 2 +- fetch/resolver.go | 7 ++++--- fetch/vscode_resolver_test.go | 39 +++++++++++++++++++++++++++-------- 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index c82e14f..dbda72f 100644 --- a/README.md +++ b/README.md @@ -570,7 +570,7 @@ info, err := resolver.Resolve(ctx, "cargo", "serde", "1.0.0") // info.Integrity may be populated for metadata-resolved URLs ``` -When a version has several files, `ResolveWithOptions` picks one by PURL qualifiers. `arch`, `distro`, `subdir`, `build` and `platform` are matched against artifact metadata, and an artifact that doesn't record a qualifier isn't ruled out by it. An artifact whose `platform` is `universal` matches any requested platform, after one built for that platform. When a registry lists no artifacts for the version, the URL from `URLs().Download` is returned. `repository_url` and `channel` are ignored: register a registry for that URL instead. +When a version has several files, `ResolveWithOptions` picks one by PURL qualifiers. `arch`, `distro`, `subdir`, `build` and `platform` are matched against artifact metadata, and an artifact that doesn't record a qualifier isn't ruled out by it. An artifact whose `platform` is `universal` matches any requested platform, after one built for that platform. When a registry lists no artifacts for the version, or doesn't list the version, the URL from `URLs().Download` is returned. `repository_url` and `channel` are ignored: register a registry for that URL instead. ```go info, err := resolver.ResolveWithOptions(ctx, "conda", "zlib", "1.3.1", fetch.ResolveOptions{ diff --git a/fetch/resolver.go b/fetch/resolver.go index ed6c070..3c3ab6c 100644 --- a/fetch/resolver.go +++ b/fetch/resolver.go @@ -175,9 +175,10 @@ func (r *Resolver) ResolveWithOptions( // For ecosystems like PyPI, we need to fetch metadata to get the URL info, err := r.resolveFromMetadata(ctx, reg, name, version, options) - if errors.Is(err, ErrNoDownloadURL) && options.hasSelectionQualifier() { - // The registry lists no artifacts to pick among, so the built - // URL is the only one there is. + if (errors.Is(err, ErrNoDownloadURL) || errors.Is(err, ErrNotFound)) && options.hasSelectionQualifier() { + // The registry lists no artifacts to pick among, or lists the + // version under another number as RubyGems does for a platform + // build, so the built URL is the only one there is. if url := reg.URLs().Download(name, version); url != "" { return matchSingleArtifact(&ArtifactInfo{URL: url, Filename: filenameFromURL(url)}, options, nil) } diff --git a/fetch/vscode_resolver_test.go b/fetch/vscode_resolver_test.go index f36cb29..6cc42a5 100644 --- a/fetch/vscode_resolver_test.go +++ b/fetch/vscode_resolver_test.go @@ -95,18 +95,39 @@ func (f *urlOnlyRegistry) URLs() client.URLBuilder { //nolint:ireturn // satisfy } // A registry that lists no artifacts, such as RubyGems, has nothing to match -// a selection qualifier against, so its built URL is still the answer. +// a selection qualifier against, so its built URL is still the answer. That +// also holds where it lists the version under another number: RubyGems +// lists a platform build as 1.0.0-java. func TestResolveSelectionQualifierWithoutArtifactsUsesTheBuiltURL(t *testing.T) { - r := NewResolver() - r.RegisterRegistry(&urlOnlyRegistry{fakeRegistry{versions: []registries.Version{{Number: "1.0.0"}}}}) + for name, listed := range map[string]string{"listed": "1.0.0", "listed with its platform": "1.0.0-java"} { + t.Run(name, func(t *testing.T) { + r := NewResolver() + r.RegisterRegistry(&urlOnlyRegistry{fakeRegistry{versions: []registries.Version{{Number: listed}}}}) - info, err := r.ResolveWithOptions(context.Background(), "fake", "pkg", "1.0.0", - ResolveOptions{Qualifiers: map[string]string{"platform": "java"}}) - if err != nil { - t.Fatalf("Resolve failed: %v", err) + info, err := r.ResolveWithOptions(context.Background(), "fake", "pkg", "1.0.0", + ResolveOptions{Qualifiers: map[string]string{"platform": "java"}}) + if err != nil { + t.Fatalf("Resolve failed: %v", err) + } + if want := "https://example.com/downloads/pkg-1.0.0.gem"; info.URL != want { + t.Errorf("URL = %q, want %q", info.URL, want) + } + }) } - if want := "https://example.com/downloads/pkg-1.0.0.gem"; info.URL != want { - t.Errorf("URL = %q, want %q", info.URL, want) +} + +// A version that lists artifacts and has none for the qualifier is an error, +// not the built URL. +func TestResolveSelectionQualifierWithoutMatchDoesNotUseTheBuiltURL(t *testing.T) { + r := NewResolver() + r.RegisterRegistry(&urlOnlyRegistry{fakeRegistry{versions: []registries.Version{{ + Number: "1.0.0", + Artifacts: []registries.Artifact{{URL: "https://example.com/pkg-1.0.0-linux.bin", Metadata: map[string]any{"platform": "linux-x64"}}}, + }}}}) + _, err := r.ResolveWithOptions(context.Background(), "fake", "pkg", "1.0.0", + ResolveOptions{Qualifiers: map[string]string{"platform": "win32-x64"}}) + if !errors.Is(err, ErrNoMatchingArtifact) { + t.Errorf("err = %v, want ErrNoMatchingArtifact", err) } }