Validate suggested edits and dependencies
Validate suggested edits and dependencies
A suggested fix is a small proposed source edit, not permission to modify a file. Each x/tools analysis.TextEdit describes a position range and replacement bytes; edits in one fix must not overlap. Apply edits against the exact source snapshot used for analysis, verify boundaries, then parse, type-check and test. Use package metadata rather than recursively guessing imports from filenames.
package main
import ( "fmt" "go/ast" "go/format" "go/parser" "go/token")
func replace(src []byte, start, end int, old, new string) ([]byte, error) { if start < 0 || end < start || end > len(src) || string(src[start:end]) != old { return nil, fmt.Errorf("stale or invalid edit") } out := append([]byte{}, src[:start]...) out = append(out, new...) return append(out, src[end:]...), nil}func main() { src := []byte("package p; const N=10") fs := token.NewFileSet() f, e := parser.ParseFile(fs, "p.go", src, 0) if e != nil { panic(e) } lit := f.Decls[0].(*ast.GenDecl).Specs[0].(*ast.ValueSpec).Values[0] file := fs.File(lit.Pos()) start, end := file.Offset(lit.Pos()), file.Offset(lit.End()) out, e := replace(src, start, end, "10", "20") if e != nil { panic(e) } out, e = format.Source(out) if e != nil { panic(e) } fmt.Print(string(out)) _, e = replace(src, start, end, "99", "20") fmt.Println(e != nil)}The old-text guard catches a mismatch in this single-edit model. A real editor must also validate the whole document version or content hash: unchanged text at one range does not prove that the rest of the file is unchanged. For multiple edits, validate all ranges against the original snapshot, sort in descending offset order, and reject overlaps before writing.
Expected output:
package p
const N = 20trueRead a dependency list without walking directories
package main
import ( "encoding/json" "fmt" "sort")
func main() { const metadata = `{"ImportPath":"example/p","Imports":["fmt","example/p/store"]}` var p struct { ImportPath string Imports []string } if e := json.Unmarshal([]byte(metadata), &p); e != nil { panic(e) } sort.Strings(p.Imports) for _, imp := range p.Imports { fmt.Println(imp) }}This fixture models fields in go list -json output, not a live graph. Use go list -deps -json ./... for build-selected dependency metadata; decode successive JSON objects until EOF. Check command status, package errors and build configuration. In x/tools, packages.Load with a suitable mode and pattern provides package metadata; use analysis.Requires for dependencies between analyzers and facts for information exchanged across imported packages. Those are distinct graphs.
Expected output:
example/p/storefmtTry it locally
Apply two non-overlapping edits to const A=1; const B=2 using original byte offsets, descending order. Replace 1 with 100 and 2 with 30; print the result.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Developer tooling 07: Validate suggested edits and dependencies
Check the lesson objective before moving on.
Why apply validated byte edits from right to left?
What does analysis.Requires describe?