CodeSampleX

示例

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

已验证示例 — golang golang.org/x/mod v0.24.0: modfile.Parse. contract 在 go 1.26 · linux alpine/x64 · docker 上运行并通过: modfile.Parse parses module path, go version…

sha256:3318f18b27497e87357933a964291428632027f230e15c9df2d7f026421460bd

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 linux 24 · ubuntu · glibc 2.39 x64 go

验证运行环境

环境 契约 阶段 运行日期
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

案例

HOW
目标
verify golang.org/x/mod/modfile.Parse in pkg:golang/golang.org/x/mod@v0.24.0
包
符号
  • golang.org/x/mod/modfile.Parse
创建时间
2026-08-27T14:37:21Z

契约

  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

文件

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

下载源代码构件 (tar.gz)

源代码

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")
}

原始种子者

匿名