CodeSampleX

Exemple

golang.org/x/mod v0.4.0: module.Check, semver.Compare, semver.Canonical

Échantillon vérifié pour golang golang.org/x/mod v0.4.0: module.Check, semver.Compare, semver.Canonical. Le contrat s'est exécuté sur go 1.26 · linux…

sha256:fabed0b5a2c1ddf7171969dcae113d5956b3611b629627c80564dc95d1982e22

Ce réseau offre une seule chose : un échantillon qui compile. Il l'a exécuté dans un bac à sable et conservé le reçu signé. Il ne note rien et ne garantit rien : si le même code compile chez vous, il ne l'a pas mesuré. Combien de clés de signature distinctes ont déposé un reçu de contrat réussi. Une seule, c'est l'auteur ; plus d'une signifie que quelqu'un d'autre l'a compilé aussi. Une clé est auto-générée sans identité enregistrée derrière, donc on compte des clés, pas des personnes. MIT-0

Preuves d'exécution

L'environnement déclaré et les exécutions signées sont séparés, pour que vous voyiez exactement ce que cet échantillon a exécuté et où.

Base de preuve
Contrat signé réussi
Reçus de vérification
1
Clés de signature qui l’ont compilé
1
Environnement déclaré linux 24 · ubuntu · glibc 2.39 x64 go

Environnements des exécutions de vérification

Environnement Contrat Étapes Exécution
go 1.26 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1golang:1.26@sha256:e30143be198a…
2026-10-25

Cas

HOW
Objectif
verify pkg:golang/golang.org/x/mod@v0.4.0
Paquets
Symboles
  • module.Check
  • semver.Compare
  • semver.Canonical
  • modfile.Parse
Créé
2026-10-25T02:20:48Z

Contrat

  1. module.Check returns nil for a valid module path and semantic version
  2. module.Check returns nil for major version >= 2 when path includes the matching /vN suffix
  3. module.Check returns InvalidVersionError when major version >= 2 is used without a matching /vN path suffix
  4. module.Check returns an error describing malformed module path when module path has a leading dash
  5. semver.Compare orders semantic versions including pre-release tags according to semver precedence
  6. semver.Canonical strips build metadata and normalizes short versions to full vMAJOR.MINOR.PATCH form
  7. modfile.Parse parses go.mod syntax and AddRequire / Format produce updated canonical go.mod bytes

Fichiers

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • main.go
  • spec.json
  • test/contract.go

Télécharger l’artefact source (tar.gz)

Code source

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify pkg:golang/golang.org/x/mod@v0.4.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/golang.org/x/mod@v0.4.0

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:c09ab8e4a030f63eeafe9d2569196a283b205a6ec29cbfad87a15fb3f0e9a232","contract":["module.Check returns nil for a valid module path and semantic version","module.Check returns nil for major version \u003e= 2 when path includes the matching /vN suffix","module.Check returns InvalidVersionError when major version \u003e= 2 is used without a matching /vN path suffix","module.Check returns an error describing malformed module path when module path has a leading dash","semver.Compare orders semantic versions including pre-release tags according to semver precedence","semver.Canonical strips build metadata and normalizes short versions to full vMAJOR.MINOR.PATCH form","modfile.Parse parses go.mod syntax and AddRequire / Format produce updated canonical go.mod bytes"],"goal":"verify pkg:golang/golang.org/x/mod@v0.4.0","kind":"HOW","packages":["pkg:golang/golang.org/x/mod@v0.4.0"],"schemaVersion":1,"symbols":["module.Check","semver.Compare","semver.Canonical","modfile.Parse"]},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/golang.org/x/mod@v0.4.0"],"schemaVersion":1,"subject":"pkg:golang/golang.org/x/mod@v0.4.0","symbols":["module.Check","semver.Compare","semver.Canonical","modfile.Parse"],"verifierAdapter":"golang@1"}
go.mod
module example.com/sample

go 1.26.6

require golang.org/x/mod v0.4.0

require golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898 // indirect
go.sum
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/mod v0.4.0 h1:8pl+sMODzuvGJkmj2W4kZihvVb5mKm8pB/X44PIQHv8=
golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898 h1:/atklqdjdhuosWIl6AIbOeHJjicWYPqR9bpxqxYG2pA=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
main.go
package main

import (
	"fmt"
	"log"

	"golang.org/x/mod/modfile"
	"golang.org/x/mod/module"
	"golang.org/x/mod/semver"
)

func main() {
	modulePath := "example.com/pkg/demo"
	version := "v1.2.3"
	if err := module.Check(modulePath, version); err != nil {
		log.Fatalf("Unexpected check failure for %s@%s: %v", modulePath, version, err)
	}
	fmt.Printf("Validated module %s@%s (major %s).\n", modulePath, version, semver.Major(version))

	rawMod := []byte("module example.com/pkg/demo\n\ngo 1.20\n\nrequire example.com/dep v1.0.0\n")
	f, err := modfile.Parse("go.mod", rawMod, nil)
	if err != nil {
		log.Fatalf("Failed to parse go.mod: %v", err)
	}
	fmt.Printf("Parsed module %s with %d require(s).\n", f.Module.Mod.Path, len(f.Require))
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:golang/golang.org/x/mod@v0.4.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/golang.org/x/mod@v0.4.0"
  ]
}
test/contract.go
package main

import (
	"errors"
	"fmt"
	"os"
	"strings"

	"golang.org/x/mod/modfile"
	"golang.org/x/mod/module"
	"golang.org/x/mod/semver"
)

func fail(msg string, args ...any) {
	fmt.Fprintf(os.Stderr, msg+"\n", args...)
	os.Exit(1)
}

func main() {
	// 1. module.Check returns nil for a valid module path and semantic version
	if err := module.Check("example.com/pkg/foo", "v1.2.3"); err != nil {
		fail("Check(example.com/pkg/foo, v1.2.3) failed: %v", err)
	}

	// 2. module.Check returns nil for major version >= 2 when path includes the matching /vN suffix
	if err := module.Check("example.com/pkg/foo/v2", "v2.0.0"); err != nil {
		fail("Check(example.com/pkg/foo/v2, v2.0.0) failed: %v", err)
	}

	// 3. module.Check returns InvalidVersionError when major version >= 2 is used without a matching /vN path suffix
	errV2Mismatch := module.Check("example.com/pkg/foo", "v2.0.0")
	var verErr *module.InvalidVersionError
	if !errors.As(errV2Mismatch, &verErr) {
		fail("Check(example.com/pkg/foo, v2.0.0) expected InvalidVersionError, got %T (%v)", errV2Mismatch, errV2Mismatch)
	}

	// 4. module.Check returns an error describing malformed module path when module path has a leading dash
	errLeadingDash := module.Check("-invalid/path", "v1.0.0")
	if errLeadingDash == nil || !strings.Contains(errLeadingDash.Error(), "leading dash") {
		fail("Check(-invalid/path, v1.0.0) expected leading dash error, got %v", errLeadingDash)
	}

	// 5. semver.Compare orders semantic versions including pre-release tags according to semver precedence
	if c := semver.Compare("v1.2.0-alpha.1", "v1.2.0"); c != -1 {
		fail("semver.Compare(v1.2.0-alpha.1, v1.2.0) = %d, want -1", c)
	}
	if c := semver.Compare("v1.2.0+build1", "v1.2.0+build2"); c != 0 {
		fail("semver.Compare(v1.2.0+build1, v1.2.0+build2) = %d, want 0", c)
	}

	// 6. semver.Canonical strips build metadata and normalizes short versions to full vMAJOR.MINOR.PATCH form
	if got := semver.Canonical("v1.2"); got != "v1.2.0" {
		fail("semver.Canonical(v1.2) = %q, want v1.2.0", got)
	}
	if got := semver.Canonical("v1.2.0+meta"); got != "v1.2.0" {
		fail("semver.Canonical(v1.2.0+meta) = %q, want v1.2.0", got)
	}

	// 7. modfile.Parse parses go.mod syntax and AddRequire / Format produce updated canonical go.mod bytes
	raw := []byte("module example.com/app\n\ngo 1.21\n\nrequire example.com/lib v1.0.0\n")
	mf, err := modfile.Parse("go.mod", raw, nil)
	if err != nil {
		fail("modfile.Parse failed: %v", err)
	}
	if mf.Module == nil || mf.Module.Mod.Path != "example.com/app" {
		fail("unexpected module path: %+v", mf.Module)
	}
	if mf.Go == nil || mf.Go.Version != "1.21" {
		fail("unexpected go version: %+v", mf.Go)
	}
	if err := mf.AddRequire("example.com/lib", "v1.1.0"); err != nil {
		fail("AddRequire failed: %v", err)
	}
	if len(mf.Require) != 1 || mf.Require[0].Mod.Version != "v1.1.0" {
		fail("unexpected require list after AddRequire: %+v", mf.Require)
	}
	formatted, err := mf.Format()
	if err != nil || len(formatted) == 0 {
		fail("mf.Format() failed: %v", err)
	}

	fmt.Println("All contract assertions passed.")
}

Seeder d'origine

anonyme