CodeSampleX

샘플

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

검증된 샘플 — golang golang.org/x/mod v0.24.0: modfile.Parse. go 1.26 · linux alpine/x64 · docker에서 contract를 실행해 통과했습니다: modfile.Parse parses module path, go…

sha256:3318f18b27497e87357933a964291428632027f230e15c9df2d7f026421460bd

이 네트워크가 제공하는 것은 하나입니다. 빌드되는 샘플. 샌드박스에서 돌리고 서명된 영수증을 보관합니다. 등급을 매기지 않고 무엇도 보증하지 않습니다 — 같은 코드가 당신 환경에서 빌드되는지는 측정한 적이 없습니다. 통과한 계약 영수증을 낸 서로 다른 서명 키의 수입니다. 하나면 작성자 혼자이고, 둘 이상이면 다른 사람도 빌드했다는 뜻입니다. 키는 스스로 만드는 것이고 뒤에 등록된 신원이 없으므로, 세는 것은 사람이 아니라 키입니다. 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")
}

오리진 시더

익명