Debian Codegraph @

internal/robustio/copyfiles.go

raw symbols SCIP
1 // Copyright 2022 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 //go:build ignore
6
7 // The copyfiles script copies the contents of the internal cmd/go robustio
8 // package to the current directory, with adjustments to make it build.
9 //
10 // NOTE: In retrospect this script got out of hand, as we have to perform
11 // various operations on the package to get it to build at old Go versions. If
12 // in the future it proves to be flaky, delete it and just copy code manually.
13 package main
14
15 import (
16 "bytes"
17 "go/build/constraint"
18 "go/scanner"
19 "go/token"
20 "log"
21 "os"
22 "path/filepath"
23 "runtime"
24 "strings"
25 )
26
27 func main() {
28 dir := filepath.Join(runtime.GOROOT(), "src", "cmd", "go", "internal", "robustio")
29
30 entries, err := os.ReadDir(dir)
31 if err != nil {
32 log.Fatalf("reading the robustio dir: %v", err)
33 }
34
35 // Collect file content so that we can validate before copying.
36 fileContent := make(map[string][]byte)
37 windowsImport := []byte("\t\"internal/syscall/windows\"\n")
38 foundWindowsImport := false
39 for _, entry := range entries {
40 if strings.HasSuffix(entry.Name(), ".go") {
41 pth := filepath.Join(dir, entry.Name())
42 content, err := os.ReadFile(pth)
43 if err != nil {
44 log.Fatalf("reading %q: %v", entry.Name(), err)
45 }
46
47 // Replace the use of internal/syscall/windows.ERROR_SHARING_VIOLATION
48 // with a local constant.
49 if entry.Name() == "robustio_windows.go" && bytes.Contains(content, windowsImport) {
50 foundWindowsImport = true
51 content = bytes.Replace(content, windowsImport, nil, 1)
52 content = bytes.Replace(content, []byte("windows.ERROR_SHARING_VIOLATION"), []byte("ERROR_SHARING_VIOLATION"), -1)
53 }
54
55 // Replace os.ReadFile with os.ReadFile (for 1.15 and older). We
56 // attempt to match calls (via the '('), to avoid matching mentions of
57 // os.ReadFile in comments.
58 //
59 // TODO(rfindley): once we (shortly!) no longer support 1.15, remove
60 // this and break the build.
61 if bytes.Contains(content, []byte("os.ReadFile(")) {
62 content = bytes.Replace(content, []byte("\"os\""), []byte("\"io/ioutil\"\n\t\"os\""), 1)
63 content = bytes.Replace(content, []byte("os.ReadFile("), []byte("os.ReadFile("), -1)
64 }
65
66 // Add +build constraints, for 1.16.
67 content = addPlusBuildConstraints(content)
68
69 fileContent[entry.Name()] = content
70 }
71 }
72
73 if !foundWindowsImport {
74 log.Fatal("missing expected import of internal/syscall/windows in robustio_windows.go")
75 }
76
77 for name, content := range fileContent {
78 if err := os.WriteFile(name, content, 0644); err != nil {
79 log.Fatalf("writing %q: %v", name, err)
80 }
81 }
82 }
83
84 // addPlusBuildConstraints splices in +build constraints for go:build
85 // constraints encountered in the source.
86 //
87 // Gopls still builds at Go 1.16, which requires +build constraints.
88 func addPlusBuildConstraints(src []byte) []byte {
89 var s scanner.Scanner
90 fset := token.NewFileSet()
91 file := fset.AddFile("", fset.Base(), len(src))
92 s.Init(file, src, nil /* no error handler */, scanner.ScanComments)
93
94 result := make([]byte, 0, len(src))
95 lastInsertion := 0
96 for {
97 pos, tok, lit := s.Scan()
98 if tok == token.EOF {
99 break
100 }
101 if tok == token.COMMENT {
102 if c, err := constraint.Parse(lit); err == nil {
103 plusBuild, err := constraint.PlusBuildLines(c)
104 if err != nil {
105 log.Fatalf("computing +build constraint for %q: %v", lit, err)
106 }
107 insertAt := file.Offset(pos) + len(lit)
108 result = append(result, src[lastInsertion:insertAt]...)
109 result = append(result, []byte("\n"+strings.Join(plusBuild, "\n"))...)
110 lastInsertion = insertAt
111 }
112 }
113 }
114 result = append(result, src[lastInsertion:]...)
115 return result
116 }