Debian Codegraph @

tools/antithesis-go-toolexec/importcfg.go

raw
1 package main
2
3 import (
4 "bufio"
5 "fmt"
6 "os"
7 "path/filepath"
8 "strings"
9 )
10
11 // An importcfg tells the compiler and linker where each import path's compiled
12 // archive lives. The go command writes one per action, listing only the packages
13 // it computed as that action's dependencies. An import we inject is not in
14 // there, and the compiler cannot resolve it until we add a line.
15
16 // importCfg is a parsed importcfg file, preserving the original text so that
17 // anything we do not understand survives a rewrite.
18 type importCfg struct {
19 text string
20 // archives maps each resolvable import path to the compiled archive
21 // implementing it. Cataloguing type-checks against these.
22 archives map[string]string
23 }
24
25 func readImportCfg(path string) (*importCfg, error) {
26 body, err := os.ReadFile(path)
27 if err != nil {
28 return nil, fmt.Errorf("reading importcfg %s: %w", path, err)
29 }
30 cfg := &importCfg{text: string(body), archives: map[string]string{}}
31 scanner := bufio.NewScanner(strings.NewReader(cfg.text))
32 for scanner.Scan() {
33 rest, ok := strings.CutPrefix(strings.TrimSpace(scanner.Text()), "packagefile ")
34 if !ok {
35 continue
36 }
37 if importPath, archive, found := strings.Cut(rest, "="); found {
38 cfg.archives[importPath] = archive
39 }
40 }
41 return cfg, scanner.Err()
42 }
43
44 // Has reports whether the compiler or linker can already resolve importPath.
45 func (c *importCfg) Has(importPath string) bool { _, ok := c.archives[importPath]; return ok }
46
47 // withEntries writes a copy of the importcfg extended with entries for packages
48 // it does not already list. Existing entries always win; a package the go command
49 // built for this action must never be shadowed by one we resolved separately.
50 func (c *importCfg) withEntries(dir, name string, entries []packageEntry) (string, error) {
51 var added strings.Builder
52 for _, entry := range entries {
53 if c.Has(entry.ImportPath) {
54 continue
55 }
56 fmt.Fprintf(&added, "packagefile %s=%s\n", entry.ImportPath, entry.ArchiveFile)
57 }
58 if added.Len() == 0 {
59 return "", nil
60 }
61
62 text := c.text
63 if !strings.HasSuffix(text, "\n") {
64 text += "\n"
65 }
66 path := filepath.Join(dir, name)
67 if err := os.WriteFile(path, []byte(text+added.String()), 0o644); err != nil {
68 return "", fmt.Errorf("writing patched importcfg: %w", err)
69 }
70 return path, nil
71 }
72
73 // patchImportCfg replaces the importcfg at args[valueIndex] with a copy able to
74 // resolve entries, if any are missing. It returns args unchanged when the
75 // existing file already suffices.
76 func patchImportCfg(args []string, valueIndex int, entries []packageEntry) ([]string, error) {
77 original := args[valueIndex]
78 cfg, err := readImportCfg(original)
79 if err != nil {
80 return nil, err
81 }
82
83 work, err := os.MkdirTemp("", "antithesis-importcfg-")
84 if err != nil {
85 return nil, err
86 }
87 patched, err := cfg.withEntries(work, filepath.Base(original), entries)
88 if err != nil {
89 return nil, err
90 }
91 if patched == "" {
92 os.Remove(work)
93 return args, nil
94 }
95 args[valueIndex] = patched
96 return args, nil
97 }