Skip to content

Commit 7782270

Browse files
committed
add in changes
1 parent 6505cf2 commit 7782270

11 files changed

+176
-313
lines changed

WORKSPACE

+18-4
Original file line numberDiff line numberDiff line change
@@ -165,15 +165,22 @@ go_repository(
165165
go_repository(
166166
name = "org_golang_x_mod",
167167
importpath = "golang.org/x/mod",
168-
sum = "h1:KENHtAZL2y3NLMYZeHY9DW8HW8V+kQyJsY/V9JlKvCs=",
169-
version = "v0.9.0",
168+
sum = "h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4=",
169+
version = "v0.22.0",
170+
)
171+
172+
go_repository(
173+
name = "org_golang_x_net",
174+
importpath = "golang.org/x/net",
175+
sum = "h1:P4fl1q7dY2hnZFxEk4pPSkDHF+QqjitcnDjUQyMM+pM=",
176+
version = "v0.31.0",
170177
)
171178

172179
go_repository(
173180
name = "org_golang_x_sync",
174181
importpath = "golang.org/x/sync",
175-
sum = "h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=",
176-
version = "v0.1.0",
182+
sum = "h1:fEo0HyrW1GIgZdpbhCRO0PkJajUS5H9IFUztCgEo2jQ=",
183+
version = "v0.9.0",
177184
)
178185

179186
go_repository(
@@ -183,6 +190,13 @@ go_repository(
183190
version = "v0.6.0",
184191
)
185192

193+
go_repository(
194+
name = "org_golang_x_tools",
195+
importpath = "golang.org/x/tools",
196+
sum = "h1:qEKojBykQkQ4EynWy4S8Weg69NumxKdn40Fce3uc/8o=",
197+
version = "v0.27.0",
198+
)
199+
186200
http_archive(
187201
name = "googleapis",
188202
sha256 = "9d1a930e767c93c825398b8f8692eca3fe353b9aaadedfbcf1fca2282c85df88",

go.mod

+12-9
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
module github.com/bazelbuild/rules_go
22

3-
go 1.21.1
3+
go 1.22.0
4+
5+
toolchain go1.22.2
46

57
require (
68
github.com/gogo/protobuf v1.3.2
79
github.com/golang/mock v1.7.0-rc.1
810
github.com/golang/protobuf v1.5.3
9-
golang.org/x/net v0.26.0
10-
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d
11-
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013
12-
google.golang.org/grpc v1.40.1
11+
golang.org/x/net v0.31.0
12+
golang.org/x/tools v0.27.0
13+
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1
14+
google.golang.org/grpc v1.56.3
1315
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.3.0
14-
google.golang.org/protobuf v1.31.0
16+
google.golang.org/protobuf v1.33.0
1517
)
1618

1719
require (
18-
golang.org/x/mod v0.17.0 // indirect
19-
golang.org/x/sys v0.21.0 // indirect
20-
golang.org/x/text v0.16.0 // indirect
20+
golang.org/x/mod v0.22.0 // indirect
21+
golang.org/x/sync v0.9.0 // indirect
22+
golang.org/x/sys v0.27.0 // indirect
23+
golang.org/x/text v0.20.0 // indirect
2124
)

go.sum

+18-110
Large diffs are not rendered by default.

go/tools/gopackagesdriver/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ go_library(
1919
visibility = [
2020
"//tests/integration/gopackagesdriver:__pkg__",
2121
],
22+
deps = ["@org_golang_x_tools//go/packages"],
2223
)
2324

2425
go_binary(

go/tools/gopackagesdriver/bazel_json_builder.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import (
2525
"regexp"
2626
"runtime"
2727
"strings"
28+
29+
"golang.org/x/tools/go/packages"
2830
)
2931

3032
type BazelJSONBuilder struct {
@@ -136,7 +138,7 @@ func (b *BazelJSONBuilder) queryFromRequests(requests ...string) string {
136138
} else if isLocalPattern(request) {
137139
result = b.localQuery(request)
138140
} else if request == "builtin" || request == "std" {
139-
result = fmt.Sprintf(RulesGoStdlibLabel)
141+
result = fmt.Sprintf("%s", RulesGoStdlibLabel)
140142
}
141143

142144
if result != "" {
@@ -156,9 +158,9 @@ func NewBazelJSONBuilder(bazel *Bazel, includeTests bool) (*BazelJSONBuilder, er
156158
}, nil
157159
}
158160

159-
func (b *BazelJSONBuilder) outputGroupsForMode(mode LoadMode) string {
161+
func (b *BazelJSONBuilder) outputGroupsForMode(mode packages.LoadMode) string {
160162
og := "go_pkg_driver_json_file,go_pkg_driver_stdlib_json_file,go_pkg_driver_srcs"
161-
if mode&NeedExportsFile != 0 {
163+
if mode&packages.NeedExportsFile != 0 {
162164
og += ",go_pkg_driver_export_file"
163165
}
164166
return og
@@ -200,7 +202,7 @@ func (b *BazelJSONBuilder) Labels(ctx context.Context, requests []string) ([]str
200202
return labels, nil
201203
}
202204

203-
func (b *BazelJSONBuilder) Build(ctx context.Context, labels []string, mode LoadMode) ([]string, error) {
205+
func (b *BazelJSONBuilder) Build(ctx context.Context, labels []string, mode packages.LoadMode) ([]string, error) {
204206
aspects := append(additionalAspects, goDefaultAspect)
205207

206208
buildArgs := concatStringsArrays([]string{

go/tools/gopackagesdriver/driver_request.go

+3-63
Original file line numberDiff line numberDiff line change
@@ -18,72 +18,12 @@ import (
1818
"encoding/json"
1919
"fmt"
2020
"io"
21-
)
22-
23-
// From https://pkg.go.dev/golang.org/x/tools/go/packages#LoadMode
24-
type LoadMode int
25-
26-
// Only NeedExportsFile is needed in our case
27-
const (
28-
// NeedName adds Name and PkgPath.
29-
NeedName LoadMode = 1 << iota
30-
31-
// NeedFiles adds GoFiles and OtherFiles.
32-
NeedFiles
33-
34-
// NeedCompiledGoFiles adds CompiledGoFiles.
35-
NeedCompiledGoFiles
36-
37-
// NeedImports adds Imports. If NeedDeps is not set, the Imports field will contain
38-
// "placeholder" Packages with only the ID set.
39-
NeedImports
40-
41-
// NeedDeps adds the fields requested by the LoadMode in the packages in Imports.
42-
NeedDeps
43-
44-
// NeedExportsFile adds ExportFile.
45-
NeedExportFile
4621

47-
// NeedTypes adds Types, Fset, and IllTyped.
48-
NeedTypes
49-
50-
// NeedSyntax adds Syntax.
51-
NeedSyntax
52-
53-
// NeedTypesInfo adds TypesInfo.
54-
NeedTypesInfo
55-
56-
// NeedTypesSizes adds TypesSizes.
57-
NeedTypesSizes
58-
59-
// typecheckCgo enables full support for type checking cgo. Requires Go 1.15+.
60-
// Modifies CompiledGoFiles and Types, and has no effect on its own.
61-
typecheckCgo
62-
63-
// NeedModule adds Module.
64-
NeedModule
22+
"golang.org/x/tools/go/packages"
6523
)
6624

67-
// Deprecated: NeedExportsFile is a historical misspelling of NeedExportFile.
68-
const NeedExportsFile = NeedExportFile
69-
70-
// From https://github.com/golang/tools/blob/v0.1.0/go/packages/external.go#L32
71-
// Most fields are disabled since there is no need for them
72-
type DriverRequest struct {
73-
Mode LoadMode `json:"mode"`
74-
// Env specifies the environment the underlying build system should be run in.
75-
// Env []string `json:"env"`
76-
// BuildFlags are flags that should be passed to the underlying build system.
77-
// BuildFlags []string `json:"build_flags"`
78-
// Tests specifies whether the patterns should also return test packages.
79-
Tests bool `json:"tests"`
80-
// Overlay maps file paths (relative to the driver's working directory) to the byte contents
81-
// of overlay files.
82-
Overlay map[string][]byte `json:"overlay"`
83-
}
84-
85-
func ReadDriverRequest(r io.Reader) (*DriverRequest, error) {
86-
req := &DriverRequest{}
25+
func ReadDriverRequest(r io.Reader) (*packages.DriverRequest, error) {
26+
req := &packages.DriverRequest{}
8727
if err := json.NewDecoder(r).Decode(&req); err != nil {
8828
return nil, fmt.Errorf("unable to decode driver request: %w", err)
8929
}

go/tools/gopackagesdriver/flatpackage.go

+37-39
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ import (
2424
"os"
2525
"strconv"
2626
"strings"
27+
28+
"golang.org/x/tools/go/packages"
2729
)
2830

29-
type ResolvePkgFunc func(importPath string) string
31+
type ResolvePkgFunc func(importPath string) *packages.Package
3032

3133
// Copy and pasted from golang.org/x/tools/go/packages
3234
type FlatPackagesError struct {
@@ -97,30 +99,30 @@ func WalkFlatPackagesFromJSON(jsonFile string, onPkg PackageFunc) error {
9799
return nil
98100
}
99101

100-
func (fp *FlatPackage) ResolvePaths(prf PathResolverFunc) error {
101-
resolvePathsInPlace(prf, fp.CompiledGoFiles)
102-
resolvePathsInPlace(prf, fp.GoFiles)
103-
resolvePathsInPlace(prf, fp.OtherFiles)
104-
fp.ExportFile = prf(fp.ExportFile)
102+
func ResolvePaths(pak *packages.Package, prf PathResolverFunc) error {
103+
resolvePathsInPlace(prf, pak.CompiledGoFiles)
104+
resolvePathsInPlace(prf, pak.GoFiles)
105+
resolvePathsInPlace(prf, pak.OtherFiles)
106+
pak.ExportFile = prf(pak.ExportFile)
105107
return nil
106108
}
107109

108110
// FilterFilesForBuildTags filters the source files given the current build
109111
// tags.
110-
func (fp *FlatPackage) FilterFilesForBuildTags() {
111-
fp.GoFiles = filterSourceFilesForTags(fp.GoFiles)
112-
fp.CompiledGoFiles = filterSourceFilesForTags(fp.CompiledGoFiles)
112+
func FilterFilesForBuildTags(pak *packages.Package) {
113+
pak.GoFiles = filterSourceFilesForTags(pak.GoFiles)
114+
pak.CompiledGoFiles = filterSourceFilesForTags(pak.CompiledGoFiles)
113115
}
114116

115-
func (fp *FlatPackage) filterTestSuffix(files []string) (err error, testFiles []string, xTestFiles, nonTestFiles []string) {
117+
func filterTestSuffix(pkg *packages.Package, files []string) (err error, testFiles []string, xTestFiles, nonTestFiles []string) {
116118
for _, filename := range files {
117119
if strings.HasSuffix(filename, "_test.go") {
118120
fset := token.NewFileSet()
119121
f, err := parser.ParseFile(fset, filename, nil, parser.PackageClauseOnly)
120122
if err != nil {
121123
return err, nil, nil, nil
122124
}
123-
if f.Name.Name == fp.Name {
125+
if f.Name.Name == pkg.Name {
124126
testFiles = append(testFiles, filename)
125127
} else {
126128
xTestFiles = append(xTestFiles, filename)
@@ -132,44 +134,45 @@ func (fp *FlatPackage) filterTestSuffix(files []string) (err error, testFiles []
132134
return
133135
}
134136

135-
func (fp *FlatPackage) MoveTestFiles() *FlatPackage {
136-
err, tgf, xtgf, gf := fp.filterTestSuffix(fp.GoFiles)
137+
func MoveTestFiles(pkg *packages.Package) *packages.Package {
138+
err, tgf, xtgf, gf := filterTestSuffix(pkg, pkg.GoFiles)
137139
if err != nil {
138140
return nil
139141
}
140142

141-
fp.GoFiles = append(gf, tgf...)
143+
pkg.GoFiles = append(gf, tgf...)
142144

143-
err, ctgf, cxtgf, cgf := fp.filterTestSuffix(fp.CompiledGoFiles)
145+
err, ctgf, cxtgf, cgf := filterTestSuffix(pkg, pkg.CompiledGoFiles)
144146
if err != nil {
145147
return nil
146148
}
147149

148-
fp.CompiledGoFiles = append(cgf, ctgf...)
150+
pkg.CompiledGoFiles = append(cgf, ctgf...)
149151

150152
if len(xtgf) == 0 && len(cxtgf) == 0 {
151153
return nil
152154
}
153155

154-
newImports := make(map[string]string, len(fp.Imports))
155-
for k, v := range fp.Imports {
156+
newImports := make(map[string]*packages.Package, len(pkg.Imports))
157+
for k, v := range pkg.Imports {
156158
newImports[k] = v
157159
}
158160

159-
newImports[fp.PkgPath] = fp.ID
161+
newImports[pkg.PkgPath] = &packages.Package{
162+
ID: pkg.ID,
163+
}
160164

161165
// Clone package, only xtgf files
162-
return &FlatPackage{
163-
ID: fp.ID + "_xtest",
164-
Name: fp.Name + "_test",
165-
PkgPath: fp.PkgPath + "_test",
166+
return &packages.Package{
167+
ID: pkg.ID + "_xtest",
168+
Name: pkg.Name + "_test",
169+
PkgPath: pkg.PkgPath + "_test",
166170
Imports: newImports,
167-
Errors: fp.Errors,
171+
Errors: pkg.Errors,
168172
GoFiles: append([]string{}, xtgf...),
169173
CompiledGoFiles: append([]string{}, cxtgf...),
170-
OtherFiles: fp.OtherFiles,
171-
ExportFile: fp.ExportFile,
172-
Standard: fp.Standard,
174+
OtherFiles: pkg.OtherFiles,
175+
ExportFile: pkg.ExportFile,
173176
}
174177
}
175178

@@ -179,15 +182,10 @@ func (fp *FlatPackage) IsStdlib() bool {
179182

180183
// ResolveImports resolves imports for non-stdlib packages and integrates file overlays
181184
// to allow modification of package imports without modifying disk files.
182-
func (fp *FlatPackage) ResolveImports(resolve ResolvePkgFunc, overlays map[string][]byte) error {
183-
// Stdlib packages are already complete import wise
184-
if fp.IsStdlib() {
185-
return nil
186-
}
187-
185+
func ResolveImports(pkg *packages.Package, resolve ResolvePkgFunc, overlays map[string][]byte) error {
188186
fset := token.NewFileSet()
189187

190-
for _, file := range fp.CompiledGoFiles {
188+
for _, file := range pkg.CompiledGoFiles {
191189
// Only assign overlayContent when an overlay for the file exists, since ParseFile checks by type.
192190
// If overlay is assigned directly from the map, it will have []byte as type
193191
// Empty []byte types are parsed into io.EOF
@@ -200,8 +198,8 @@ func (fp *FlatPackage) ResolveImports(resolve ResolvePkgFunc, overlays map[strin
200198
return err
201199
}
202200
// If the name is not provided, fetch it from the sources
203-
if fp.Name == "" {
204-
fp.Name = f.Name.Name
201+
if pkg.Name == "" {
202+
pkg.Name = f.Name.Name
205203
}
206204

207205
for _, rawImport := range f.Imports {
@@ -213,12 +211,12 @@ func (fp *FlatPackage) ResolveImports(resolve ResolvePkgFunc, overlays map[strin
213211
if imp == "C" {
214212
continue
215213
}
216-
if _, ok := fp.Imports[imp]; ok {
214+
if _, ok := pkg.Imports[imp]; ok {
217215
continue
218216
}
219217

220-
if pkgID := resolve(imp); pkgID != "" {
221-
fp.Imports[imp] = pkgID
218+
if impPkg := resolve(imp); impPkg != nil {
219+
pkg.Imports[imp] = impPkg
222220
}
223221
}
224222
}

0 commit comments

Comments
 (0)