CodeSampleX

Exemplo

golang.org/x/mod v0.24.0: modfile.Parse

Amostra verificada para golang golang.org/x/mod v0.24.0: modfile.Parse. O contrato rodou em go 1.26 · linux alpine/x64 · docker e passou.

sha256:3318f18b27497e87357933a964291428632027f230e15c9df2d7f026421460bd

Esta rede oferece uma coisa: uma amostra que compila. Ela a executou em um sandbox e guardou o recibo assinado. Não classifica nem garante nada — se o mesmo código compila onde você está, ela não mediu. Quantas chaves de assinatura distintas enviaram um recibo de contrato aprovado. Uma é só o autor; mais de uma significa que outra pessoa também o compilou. Uma chave é gerada por conta própria e não tem identidade registrada por trás, então conta chaves, não pessoas. MIT-0

Evidência de execução

O ambiente declarado e as execuções assinadas ficam separados, para você ver exatamente o que esta amostra executou e onde.

Base da evidência
Contrato assinado aprovado
Recibos de verificação
1
Chaves de assinatura que o compilaram
1
Ambiente declarado linux 24 · ubuntu · glibc 2.39 x64 go

Ambientes das execuções de verificação

Ambiente Contrato Etapas Execução
go 1.26 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1golang:1.26-alpine@sha256:28d89ee9cc0f…
2026-08-27

Caso

HOW
Objetivo
verify golang.org/x/mod/modfile.Parse in pkg:golang/golang.org/x/mod@v0.24.0
Pacotes
Símbolos
  • golang.org/x/mod/modfile.Parse
Criado
2026-08-27T14:37:21Z

Contrato

  1. modfile.Parse parses module path, go version, require and replace directives from valid go.mod bytes
  2. modfile.Parse distinguishes direct and indirect dependencies in require blocks
  3. modfile.Parse with VersionFixPrerelease callback allows modifying version strings during parsing
  4. modfile.Parse returns an error when parsing malformed or syntactically invalid go.mod content

Arquivos

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

Baixar o artefato de código-fonte (tar.gz)

Código-fonte

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 golang.org/x/mod/modfile.Parse in pkg:golang/golang.org/x/mod@v0.24.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/golang.org/x/mod@v0.24.0
Demonstrate these symbols/APIs:
  - golang.org/x/mod/modfile.Parse

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:68cf6620b6fcc11fefd836ab45dc4683d22a97369d4cffc5953edb77047e25af","contract":["modfile.Parse parses module path, go version, require and replace directives from valid go.mod bytes","modfile.Parse distinguishes direct and indirect dependencies in require blocks","modfile.Parse with VersionFixPrerelease callback allows modifying version strings during parsing","modfile.Parse returns an error when parsing malformed or syntactically invalid go.mod content"],"goal":"verify golang.org/x/mod/modfile.Parse in pkg:golang/golang.org/x/mod@v0.24.0","kind":"HOW","packages":["pkg:golang/golang.org/x/mod@v0.24.0"],"schemaVersion":1,"symbols":["golang.org/x/mod/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.24.0"],"schemaVersion":1,"subject":"pkg:golang/golang.org/x/mod@v0.24.0","symbols":["golang.org/x/mod/modfile.Parse"],"verifierAdapter":"golang@1"}
go.mod
module example.com/sample

go 1.26.6

require golang.org/x/mod v0.24.0
go.sum
golang.org/x/mod v0.24.0 h1:ZfthKaKaT4NrhGVZHO1/WDTwGES4De8KtWO0SIbNJMU=
golang.org/x/mod v0.24.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww=
main.go
package main

import (
	"fmt"
	"os"

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

// ParseGoMod parses a go.mod file content byte slice into a *modfile.File.
func ParseGoMod(filename string, data []byte) (*modfile.File, error) {
	return modfile.Parse(filename, data, nil)
}

func main() {
	sampleMod := []byte(`module example.com/my/project

go 1.22.0

require (
	golang.org/x/sync v0.6.0
	golang.org/x/text v0.14.0 // indirect
)

replace example.com/old/pkg => example.com/new/pkg v0.1.0
`)

	file, err := ParseGoMod("go.mod", sampleMod)
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to parse go.mod: %v\n", err)
		os.Exit(1)
	}

	if file.Module != nil {
		fmt.Printf("Module path: %s\n", file.Module.Mod.Path)
	}
	if file.Go != nil {
		fmt.Printf("Go version: %s\n", file.Go.Version)
	}
	fmt.Printf("Requires count: %d\n", len(file.Require))
	for _, req := range file.Require {
		fmt.Printf("  - %s %s (indirect: %v)\n", req.Mod.Path, req.Mod.Version, req.Indirect)
	}
	fmt.Printf("Replaces count: %d\n", len(file.Replace))
	for _, rep := range file.Replace {
		fmt.Printf("  - %s => %s %s\n", rep.Old.Path, rep.New.Path, rep.New.Version)
	}
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify golang.org/x/mod/modfile.Parse in pkg:golang/golang.org/x/mod@v0.24.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/golang.org/x/mod@v0.24.0"
  ],
  "symbols": [
    "golang.org/x/mod/modfile.Parse"
  ]
}
test/contract.go
package main

import (
	"fmt"
	"os"

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

func main() {
	// Assertion 1: modfile.Parse parses module path, go version, require and replace directives from valid go.mod bytes
	content := []byte(`module example.com/app

go 1.21.0

require github.com/stretchr/testify v1.8.4

replace example.com/old/pkg => example.com/new/pkg v0.2.0
`)
	f, err := modfile.Parse("go.mod", content, nil)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: Assertion 1 modfile.Parse failed: %v\n", err)
		os.Exit(1)
	}
	if f.Module == nil || f.Module.Mod.Path != "example.com/app" {
		fmt.Fprintf(os.Stderr, "FAIL: Assertion 1 module path mismatch: %v\n", f.Module)
		os.Exit(1)
	}
	if f.Go == nil || f.Go.Version != "1.21.0" {
		fmt.Fprintf(os.Stderr, "FAIL: Assertion 1 go version mismatch: %v\n", f.Go)
		os.Exit(1)
	}
	if len(f.Require) != 1 || f.Require[0].Mod.Path != "github.com/stretchr/testify" || f.Require[0].Mod.Version != "v1.8.4" {
		fmt.Fprintf(os.Stderr, "FAIL: Assertion 1 require mismatch: %v\n", f.Require)
		os.Exit(1)
	}
	if len(f.Replace) != 1 || f.Replace[0].Old.Path != "example.com/old/pkg" || f.Replace[0].New.Path != "example.com/new/pkg" {
		fmt.Fprintf(os.Stderr, "FAIL: Assertion 1 replace mismatch: %v\n", f.Replace)
		os.Exit(1)
	}

	// Assertion 2: modfile.Parse distinguishes direct and indirect dependencies in require blocks
	content2 := []byte(`module example.com/app

go 1.21.0

require (
	golang.org/x/sync v0.5.0
	golang.org/x/text v0.14.0 // indirect
)
`)
	f2, err := modfile.Parse("go.mod", content2, nil)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: Assertion 2 modfile.Parse failed: %v\n", err)
		os.Exit(1)
	}
	if len(f2.Require) != 2 {
		fmt.Fprintf(os.Stderr, "FAIL: Assertion 2 expected 2 requires, got %d\n", len(f2.Require))
		os.Exit(1)
	}
	var directReq, indirectReq *modfile.Require
	for _, req := range f2.Require {
		if req.Mod.Path == "golang.org/x/sync" {
			directReq = req
		} else if req.Mod.Path == "golang.org/x/text" {
			indirectReq = req
		}
	}
	if directReq == nil || directReq.Indirect {
		fmt.Fprintf(os.Stderr, "FAIL: Assertion 2 expected golang.org/x/sync to be direct (Indirect=false)\n")
		os.Exit(1)
	}
	if indirectReq == nil || !indirectReq.Indirect {
		fmt.Fprintf(os.Stderr, "FAIL: Assertion 2 expected golang.org/x/text to be indirect (Indirect=true)\n")
		os.Exit(1)
	}

	// Assertion 3: modfile.Parse with VersionFixPrerelease callback allows modifying version strings during parsing
	content3 := []byte(`module example.com/app

go 1.21.0

require example.com/pkg v1.0.0-draft
`)
	fixFunc := func(path, version string) (string, error) {
		if path == "example.com/pkg" && version == "v1.0.0-draft" {
			return "v1.0.0", nil
		}
		return version, nil
	}
	f3, err := modfile.Parse("go.mod", content3, fixFunc)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: Assertion 3 modfile.Parse with fix failed: %v\n", err)
		os.Exit(1)
	}
	if len(f3.Require) != 1 || f3.Require[0].Mod.Version != "v1.0.0" {
		fmt.Fprintf(os.Stderr, "FAIL: Assertion 3 expected version v1.0.0 after fix, got %s\n", f3.Require[0].Mod.Version)
		os.Exit(1)
	}

	// Assertion 4: modfile.Parse returns an error when parsing malformed or syntactically invalid go.mod content
	malformedContent := []byte(`module example.com/app
go 1.21.0
require (
	broken line without closing paren
`)
	_, err = modfile.Parse("go.mod", malformedContent, nil)
	if err == nil {
		fmt.Fprintf(os.Stderr, "FAIL: Assertion 4 expected error on malformed go.mod, got nil\n")
		os.Exit(1)
	}

	fmt.Println("PASS: golang.org/x/mod/modfile.Parse contract verified")
}

Seeder de origem

anônimo