Sample
golang.org/x/tools v0.44.0: astutil.AddImport
Verified sample for golang golang.org/x/tools v0.44.0: astutil.AddImport. The contract ran on go 1.26 · linux debian/x64 · docker and passed…
sha256:0d672d11681a4926d5cde2a40c77ffc7c0848e4648b4300eca401fc0381f320c
This network offers one thing: a sample that builds. It ran the sample in a sandbox and kept the signed receipt. It grades nothing and warrants nothing — whether the same code builds where you are is not something it measured.
How many distinct signing keys filed a passing contract receipt. One is the author alone; more than one means somebody else built it too. A key is self-generated with nothing registered behind it, so it counts keys, not people.
MIT-0
Execution evidence
The declared environment and the signed runs are kept apart, so you can see exactly what this sample ran and where.
- Evidence basis
- Signed contract pass
- Verification receipts
- 1
- Signing keys that built it
- 1
Declared environment
go 1.26 linux 24 · ubuntu · glibc 2.39 x64 go 1.26 go go 1
Verification-run environments
| Environment | Contract | Stages | Run |
|---|---|---|---|
| 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-09-04 |
Case
HOW- Goal
- verify golang.org/x/tools/go/ast/astutil.AddImport in pkg:golang/golang.org/x/tools@v0.44.0
- Packages
- Symbols
-
- golang.org/x/tools/go/ast/astutil.AddImport
- Environment
- go 1.26
- Created
- 2026-09-04T12:26:20Z
Contract
- astutil.AddImport adds an import path to a Go AST file with no existing imports and returns true
- astutil.AddImport returns false when attempting to add an import path that is already present in the AST file
- astutil.AddImport preserves existing imports in the AST file when adding a new import path
- astutil.AddImport adds a new ImportSpec to file.Imports matching the specified import path
- ParseAndAddImport parses Go source code, adds the import path, and produces valid formatted Go code with the added import
Files
- PROMPT.md
- contract_test.go
- csx.json
- go.mod
- go.sum
- sample.go
- spec.json
Source
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/tools/go/ast/astutil.AddImport in pkg:golang/golang.org/x/tools@v0.44.0
Kind: HOW
Use EXACTLY these public packages and versions:
- pkg:golang/golang.org/x/tools@v0.44.0
Demonstrate these symbols/APIs:
- golang.org/x/tools/go/ast/astutil.AddImport
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.
package sample
import (
"bytes"
"go/format"
"go/parser"
"go/token"
"strings"
"testing"
"golang.org/x/tools/go/ast/astutil"
)
func TestAddImportToEmptyFile(t *testing.T) {
src := `package main
func main() {}
`
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, "test.go", src, parser.ParseComments)
if err != nil {
t.Fatalf("failed to parse: %v", err)
}
added := astutil.AddImport(fset, file, "fmt")
if !added {
t.Fatalf("expected AddImport to return true for new import 'fmt'")
}
var buf bytes.Buffer
if err := format.Node(&buf, fset, file); err != nil {
t.Fatalf("failed to format: %v", err)
}
formatted := buf.String()
if !strings.Contains(formatted, `"fmt"`) {
t.Fatalf("formatted source does not contain imported package 'fmt':\n%s", formatted)
}
}
func TestAddImportAlreadyPresentReturnsFalse(t *testing.T) {
src := `package main
import "fmt"
func main() {
fmt.Println("hello")
}
`
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, "test.go", src, parser.ParseComments)
if err != nil {
t.Fatalf("failed to parse: %v", err)
}
added := astutil.AddImport(fset, file, "fmt")
if added {
t.Fatalf("expected AddImport to return false for already existing import 'fmt'")
}
if len(file.Imports) != 1 {
t.Fatalf("expected exactly 1 import in file.Imports, got %d", len(file.Imports))
}
}
func TestAddImportPreservesExistingImports(t *testing.T) {
src := `package main
import (
"errors"
"os"
)
func main() {}
`
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, "test.go", src, parser.ParseComments)
if err != nil {
t.Fatalf("failed to parse: %v", err)
}
added := astutil.AddImport(fset, file, "fmt")
if !added {
t.Fatalf("expected AddImport to return true for new import 'fmt'")
}
if len(file.Imports) != 3 {
t.Fatalf("expected 3 imports, got %d", len(file.Imports))
}
paths := make(map[string]bool)
for _, imp := range file.Imports {
paths[imp.Path.Value] = true
}
for _, expected := range []string{`"errors"`, `"os"`, `"fmt"`} {
if !paths[expected] {
t.Fatalf("expected import %s in file.Imports", expected)
}
}
}
func TestAddImportUpdatesASTImportSpecs(t *testing.T) {
src := `package main
func main() {}
`
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, "test.go", src, parser.ParseComments)
if err != nil {
t.Fatalf("failed to parse: %v", err)
}
if len(file.Imports) != 0 {
t.Fatalf("expected 0 imports initially, got %d", len(file.Imports))
}
astutil.AddImport(fset, file, "strings")
if len(file.Imports) != 1 {
t.Fatalf("expected 1 import after AddImport, got %d", len(file.Imports))
}
if file.Imports[0].Path.Value != `"strings"` {
t.Fatalf("expected import path '\"strings\"', got %s", file.Imports[0].Path.Value)
}
}
func TestParseAndAddImportHelper(t *testing.T) {
src := `package main
func main() {}
`
fset, file, added, err := ParseAndAddImport(src, "context")
if err != nil {
t.Fatalf("ParseAndAddImport failed: %v", err)
}
if !added {
t.Fatalf("expected ParseAndAddImport to return added=true")
}
var buf bytes.Buffer
if err := format.Node(&buf, fset, file); err != nil {
t.Fatalf("failed to format: %v", err)
}
if !strings.Contains(buf.String(), `"context"`) {
t.Fatalf("expected output to contain '\"context\"', got:\n%s", buf.String())
}
}
{"case":{"caseId":"case:sha256:2677ff4d130c48950134373e9445cf3151002ffdaae68ac99e1f20f352e3adbd","contract":["astutil.AddImport adds an import path to a Go AST file with no existing imports and returns true","astutil.AddImport returns false when attempting to add an import path that is already present in the AST file","astutil.AddImport preserves existing imports in the AST file when adding a new import path","astutil.AddImport adds a new ImportSpec to file.Imports matching the specified import path","ParseAndAddImport parses Go source code, adds the import path, and produces valid formatted Go code with the added import"],"goal":"verify golang.org/x/tools/go/ast/astutil.AddImport in pkg:golang/golang.org/x/tools@v0.44.0","kind":"HOW","packages":["pkg:golang/golang.org/x/tools@v0.44.0"],"schemaVersion":1,"symbols":["golang.org/x/tools/go/ast/astutil.AddImport"]},"contractCommand":["go","test","./..."],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","language":"go","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","packageManagerVersion":"1.26.6","runtime":"go","runtimeVersion":"1.26","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/golang.org/x/tools@v0.44.0"],"schemaVersion":1,"subject":"pkg:golang/golang.org/x/tools@v0.44.0","symbols":["golang.org/x/tools/go/ast/astutil.AddImport"],"verifierAdapter":"golang@1"}
module sample
go 1.25.0
require golang.org/x/tools v0.44.0
golang.org/x/tools v0.44.0 h1:UP4ajHPIcuMjT1GqzDWRlalUEoY+uzoZKnhOjbIPD2c=
golang.org/x/tools v0.44.0/go.mod h1:KA0AfVErSdxRZIsOVipbv3rQhVXTnlU6UhKxHd1seDI=
package sample
import (
"go/ast"
"go/parser"
"go/token"
"golang.org/x/tools/go/ast/astutil"
)
// AddImportToFile adds an import path to a parsed Go AST file using astutil.AddImport.
// It returns true if the import was added, or false if it was already present.
func AddImportToFile(fset *token.FileSet, file *ast.File, importPath string) bool {
return astutil.AddImport(fset, file, importPath)
}
// ParseAndAddImport parses Go source code, adds the given import path, and returns the modified AST file.
func ParseAndAddImport(src string, importPath string) (*token.FileSet, *ast.File, bool, error) {
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, "sample.go", src, parser.ParseComments)
if err != nil {
return nil, nil, false, err
}
added := astutil.AddImport(fset, file, importPath)
return fset, file, added, nil
}
{
"schemaVersion": 1,
"goal": "verify golang.org/x/tools/go/ast/astutil.AddImport in pkg:golang/golang.org/x/tools@v0.44.0",
"kind": "HOW",
"packages": [
"pkg:golang/golang.org/x/tools@v0.44.0"
],
"symbols": [
"golang.org/x/tools/go/ast/astutil.AddImport"
]
}
Origin Seeder
anonymous