diff --git a/WORKSPACE b/WORKSPACE index 862aad8419..893c997023 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -165,15 +165,15 @@ go_repository( go_repository( name = "org_golang_x_mod", importpath = "golang.org/x/mod", - sum = "h1:KENHtAZL2y3NLMYZeHY9DW8HW8V+kQyJsY/V9JlKvCs=", - version = "v0.9.0", + sum = "h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4=", + version = "v0.22.0", ) go_repository( name = "org_golang_x_sync", importpath = "golang.org/x/sync", - sum = "h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=", - version = "v0.1.0", + sum = "h1:fEo0HyrW1GIgZdpbhCRO0PkJajUS5H9IFUztCgEo2jQ=", + version = "v0.9.0", ) go_repository( @@ -183,6 +183,13 @@ go_repository( version = "v0.6.0", ) +go_repository( + name = "org_golang_x_tools", + importpath = "golang.org/x/tools", + sum = "h1:qEKojBykQkQ4EynWy4S8Weg69NumxKdn40Fce3uc/8o=", + version = "v0.27.0", +) + http_archive( name = "googleapis", sha256 = "9d1a930e767c93c825398b8f8692eca3fe353b9aaadedfbcf1fca2282c85df88", diff --git a/go.mod b/go.mod index 4a29f87973..e3ca60d441 100644 --- a/go.mod +++ b/go.mod @@ -1,13 +1,15 @@ module github.com/bazelbuild/rules_go -go 1.21.1 +go 1.22.0 + +toolchain go1.22.2 require ( github.com/gogo/protobuf v1.3.2 github.com/golang/mock v1.7.0-rc.1 github.com/golang/protobuf v1.5.3 - golang.org/x/net v0.26.0 - golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d + golang.org/x/net v0.31.0 + golang.org/x/tools v0.27.0 google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 google.golang.org/grpc v1.40.1 google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.3.0 @@ -15,7 +17,8 @@ require ( ) require ( - golang.org/x/mod v0.17.0 // indirect - golang.org/x/sys v0.21.0 // indirect - golang.org/x/text v0.16.0 // indirect + golang.org/x/mod v0.22.0 // indirect + golang.org/x/sync v0.9.0 // indirect + golang.org/x/sys v0.27.0 // indirect + golang.org/x/text v0.20.0 // indirect ) diff --git a/go.sum b/go.sum index 46841e9c80..a1e471c686 100644 --- a/go.sum +++ b/go.sum @@ -71,6 +71,8 @@ golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro= golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA= golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= +golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4= +golang.org/x/mod v0.22.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -84,6 +86,7 @@ golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwY golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ= golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE= +golang.org/x/net v0.31.0/go.mod h1:P4fl1q7dY2hnZFxEk4pPSkDHF+QqjitcnDjUQyMM+pM= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -95,6 +98,8 @@ golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sync v0.9.0 h1:fEo0HyrW1GIgZdpbhCRO0PkJajUS5H9IFUztCgEo2jQ= +golang.org/x/sync v0.9.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -105,6 +110,7 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws= golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= @@ -112,6 +118,7 @@ golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= +golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= @@ -123,6 +130,8 @@ golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4f golang.org/x/tools v0.1.8/go.mod h1:nABZi5QlRsZVlzPpHl034qft6wpY4eDcsTt5AaioBiU= golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d h1:vU5i/LfpvrRCpgM/VPfJLg5KjxD3E+hfT1SH+d9zLwg= golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk= +golang.org/x/tools v0.27.0 h1:qEKojBykQkQ4EynWy4S8Weg69NumxKdn40Fce3uc/8o= +golang.org/x/tools v0.27.0/go.mod h1:sUi0ZgbwW9ZPAq26Ekut+weQPR5eIM6GQLQ1Yjm1H0Q= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/go/runfiles/BUILD.bazel b/go/runfiles/BUILD.bazel index 0ed371cf10..58e3b9bf63 100644 --- a/go/runfiles/BUILD.bazel +++ b/go/runfiles/BUILD.bazel @@ -49,3 +49,13 @@ filegroup( srcs = glob(["**"]), visibility = ["//visibility:public"], ) + +go_test( + name = "runfiles_test", + srcs = [ + "caller_repository_example_test.go", + "example_test.go", + "rlocationpath_xdefs_example_test.go", + ], + deps = [":runfiles"], +) diff --git a/go/tools/bzltestutil/chdir/BUILD.bazel b/go/tools/bzltestutil/chdir/BUILD.bazel index 7f88d9df07..aa2dd3cb49 100644 --- a/go/tools/bzltestutil/chdir/BUILD.bazel +++ b/go/tools/bzltestutil/chdir/BUILD.bazel @@ -19,3 +19,9 @@ filegroup( ), visibility = ["//visibility:public"], ) + +alias( + name = "go_default_library", + actual = ":chdir", + visibility = ["//visibility:public"], +) diff --git a/go/tools/gopackagesdriver/BUILD.bazel b/go/tools/gopackagesdriver/BUILD.bazel index 0351845f5e..5d2adc967c 100644 --- a/go/tools/gopackagesdriver/BUILD.bazel +++ b/go/tools/gopackagesdriver/BUILD.bazel @@ -19,6 +19,7 @@ go_library( visibility = [ "//tests/integration/gopackagesdriver:__pkg__", ], + deps = ["@org_golang_x_tools//go/packages"], ) go_binary( diff --git a/go/tools/gopackagesdriver/bazel_json_builder.go b/go/tools/gopackagesdriver/bazel_json_builder.go index a86c396011..763863320c 100644 --- a/go/tools/gopackagesdriver/bazel_json_builder.go +++ b/go/tools/gopackagesdriver/bazel_json_builder.go @@ -25,6 +25,8 @@ import ( "regexp" "runtime" "strings" + + "golang.org/x/tools/go/packages" ) type BazelJSONBuilder struct { @@ -136,7 +138,7 @@ func (b *BazelJSONBuilder) queryFromRequests(requests ...string) string { } else if isLocalPattern(request) { result = b.localQuery(request) } else if request == "builtin" || request == "std" { - result = fmt.Sprintf(RulesGoStdlibLabel) + result = fmt.Sprintf("%s", RulesGoStdlibLabel) } if result != "" { @@ -156,9 +158,9 @@ func NewBazelJSONBuilder(bazel *Bazel, includeTests bool) (*BazelJSONBuilder, er }, nil } -func (b *BazelJSONBuilder) outputGroupsForMode(mode LoadMode) string { +func (b *BazelJSONBuilder) outputGroupsForMode(mode packages.LoadMode) string { og := "go_pkg_driver_json_file,go_pkg_driver_stdlib_json_file,go_pkg_driver_srcs" - if mode&NeedExportsFile != 0 { + if mode&packages.NeedExportsFile != 0 { og += ",go_pkg_driver_export_file" } return og @@ -200,7 +202,7 @@ func (b *BazelJSONBuilder) Labels(ctx context.Context, requests []string) ([]str return labels, nil } -func (b *BazelJSONBuilder) Build(ctx context.Context, labels []string, mode LoadMode) ([]string, error) { +func (b *BazelJSONBuilder) Build(ctx context.Context, labels []string, mode packages.LoadMode) ([]string, error) { aspects := append(additionalAspects, goDefaultAspect) buildArgs := concatStringsArrays([]string{ diff --git a/go/tools/gopackagesdriver/driver_request.go b/go/tools/gopackagesdriver/driver_request.go index 3d7dea1d98..5e1c2e18c2 100644 --- a/go/tools/gopackagesdriver/driver_request.go +++ b/go/tools/gopackagesdriver/driver_request.go @@ -18,72 +18,12 @@ import ( "encoding/json" "fmt" "io" -) - -// From https://pkg.go.dev/golang.org/x/tools/go/packages#LoadMode -type LoadMode int - -// Only NeedExportsFile is needed in our case -const ( - // NeedName adds Name and PkgPath. - NeedName LoadMode = 1 << iota - - // NeedFiles adds GoFiles and OtherFiles. - NeedFiles - - // NeedCompiledGoFiles adds CompiledGoFiles. - NeedCompiledGoFiles - - // NeedImports adds Imports. If NeedDeps is not set, the Imports field will contain - // "placeholder" Packages with only the ID set. - NeedImports - - // NeedDeps adds the fields requested by the LoadMode in the packages in Imports. - NeedDeps - - // NeedExportsFile adds ExportFile. - NeedExportFile - // NeedTypes adds Types, Fset, and IllTyped. - NeedTypes - - // NeedSyntax adds Syntax. - NeedSyntax - - // NeedTypesInfo adds TypesInfo. - NeedTypesInfo - - // NeedTypesSizes adds TypesSizes. - NeedTypesSizes - - // typecheckCgo enables full support for type checking cgo. Requires Go 1.15+. - // Modifies CompiledGoFiles and Types, and has no effect on its own. - typecheckCgo - - // NeedModule adds Module. - NeedModule + "golang.org/x/tools/go/packages" ) -// Deprecated: NeedExportsFile is a historical misspelling of NeedExportFile. -const NeedExportsFile = NeedExportFile - -// From https://github.com/golang/tools/blob/v0.1.0/go/packages/external.go#L32 -// Most fields are disabled since there is no need for them -type DriverRequest struct { - Mode LoadMode `json:"mode"` - // Env specifies the environment the underlying build system should be run in. - // Env []string `json:"env"` - // BuildFlags are flags that should be passed to the underlying build system. - // BuildFlags []string `json:"build_flags"` - // Tests specifies whether the patterns should also return test packages. - Tests bool `json:"tests"` - // Overlay maps file paths (relative to the driver's working directory) to the byte contents - // of overlay files. - Overlay map[string][]byte `json:"overlay"` -} - -func ReadDriverRequest(r io.Reader) (*DriverRequest, error) { - req := &DriverRequest{} +func ReadDriverRequest(r io.Reader) (*packages.DriverRequest, error) { + req := &packages.DriverRequest{} if err := json.NewDecoder(r).Decode(&req); err != nil { return nil, fmt.Errorf("unable to decode driver request: %w", err) } diff --git a/go/tools/gopackagesdriver/flatpackage.go b/go/tools/gopackagesdriver/flatpackage.go index c4170b9fab..203b2fd4e0 100644 --- a/go/tools/gopackagesdriver/flatpackage.go +++ b/go/tools/gopackagesdriver/flatpackage.go @@ -24,9 +24,11 @@ import ( "os" "strconv" "strings" + + "golang.org/x/tools/go/packages" ) -type ResolvePkgFunc func(importPath string) string +type ResolvePkgFunc func(importPath string) *packages.Package // Copy and pasted from golang.org/x/tools/go/packages type FlatPackagesError struct { @@ -97,22 +99,22 @@ func WalkFlatPackagesFromJSON(jsonFile string, onPkg PackageFunc) error { return nil } -func (fp *FlatPackage) ResolvePaths(prf PathResolverFunc) error { - resolvePathsInPlace(prf, fp.CompiledGoFiles) - resolvePathsInPlace(prf, fp.GoFiles) - resolvePathsInPlace(prf, fp.OtherFiles) - fp.ExportFile = prf(fp.ExportFile) +func ResolvePaths(pak *packages.Package, prf PathResolverFunc) error { + resolvePathsInPlace(prf, pak.CompiledGoFiles) + resolvePathsInPlace(prf, pak.GoFiles) + resolvePathsInPlace(prf, pak.OtherFiles) + pak.ExportFile = prf(pak.ExportFile) return nil } // FilterFilesForBuildTags filters the source files given the current build // tags. -func (fp *FlatPackage) FilterFilesForBuildTags() { - fp.GoFiles = filterSourceFilesForTags(fp.GoFiles) - fp.CompiledGoFiles = filterSourceFilesForTags(fp.CompiledGoFiles) +func FilterFilesForBuildTags(pak *packages.Package) { + pak.GoFiles = filterSourceFilesForTags(pak.GoFiles) + pak.CompiledGoFiles = filterSourceFilesForTags(pak.CompiledGoFiles) } -func (fp *FlatPackage) filterTestSuffix(files []string) (err error, testFiles []string, xTestFiles, nonTestFiles []string) { +func filterTestSuffix(pkg *packages.Package, files []string) (err error, testFiles []string, xTestFiles, nonTestFiles []string) { for _, filename := range files { if strings.HasSuffix(filename, "_test.go") { fset := token.NewFileSet() @@ -120,7 +122,7 @@ func (fp *FlatPackage) filterTestSuffix(files []string) (err error, testFiles [] if err != nil { return err, nil, nil, nil } - if f.Name.Name == fp.Name { + if f.Name.Name == pkg.Name { testFiles = append(testFiles, filename) } else { xTestFiles = append(xTestFiles, filename) @@ -132,44 +134,45 @@ func (fp *FlatPackage) filterTestSuffix(files []string) (err error, testFiles [] return } -func (fp *FlatPackage) MoveTestFiles() *FlatPackage { - err, tgf, xtgf, gf := fp.filterTestSuffix(fp.GoFiles) +func MoveTestFiles(pkg *packages.Package) *packages.Package { + err, tgf, xtgf, gf := filterTestSuffix(pkg, pkg.GoFiles) if err != nil { return nil } - fp.GoFiles = append(gf, tgf...) + pkg.GoFiles = append(gf, tgf...) - err, ctgf, cxtgf, cgf := fp.filterTestSuffix(fp.CompiledGoFiles) + err, ctgf, cxtgf, cgf := filterTestSuffix(pkg, pkg.CompiledGoFiles) if err != nil { return nil } - fp.CompiledGoFiles = append(cgf, ctgf...) + pkg.CompiledGoFiles = append(cgf, ctgf...) if len(xtgf) == 0 && len(cxtgf) == 0 { return nil } - newImports := make(map[string]string, len(fp.Imports)) - for k, v := range fp.Imports { + newImports := make(map[string]*packages.Package, len(pkg.Imports)) + for k, v := range pkg.Imports { newImports[k] = v } - newImports[fp.PkgPath] = fp.ID + newImports[pkg.PkgPath] = &packages.Package{ + ID: pkg.ID, + } // Clone package, only xtgf files - return &FlatPackage{ - ID: fp.ID + "_xtest", - Name: fp.Name + "_test", - PkgPath: fp.PkgPath + "_test", + return &packages.Package{ + ID: pkg.ID + "_xtest", + Name: pkg.Name + "_test", + PkgPath: pkg.PkgPath + "_test", Imports: newImports, - Errors: fp.Errors, + Errors: pkg.Errors, GoFiles: append([]string{}, xtgf...), CompiledGoFiles: append([]string{}, cxtgf...), - OtherFiles: fp.OtherFiles, - ExportFile: fp.ExportFile, - Standard: fp.Standard, + OtherFiles: pkg.OtherFiles, + ExportFile: pkg.ExportFile, } } @@ -179,15 +182,10 @@ func (fp *FlatPackage) IsStdlib() bool { // ResolveImports resolves imports for non-stdlib packages and integrates file overlays // to allow modification of package imports without modifying disk files. -func (fp *FlatPackage) ResolveImports(resolve ResolvePkgFunc, overlays map[string][]byte) error { - // Stdlib packages are already complete import wise - if fp.IsStdlib() { - return nil - } - +func ResolveImports(pkg *packages.Package, resolve ResolvePkgFunc, overlays map[string][]byte) error { fset := token.NewFileSet() - for _, file := range fp.CompiledGoFiles { + for _, file := range pkg.CompiledGoFiles { // Only assign overlayContent when an overlay for the file exists, since ParseFile checks by type. // If overlay is assigned directly from the map, it will have []byte as type // Empty []byte types are parsed into io.EOF @@ -200,8 +198,8 @@ func (fp *FlatPackage) ResolveImports(resolve ResolvePkgFunc, overlays map[strin return err } // If the name is not provided, fetch it from the sources - if fp.Name == "" { - fp.Name = f.Name.Name + if pkg.Name == "" { + pkg.Name = f.Name.Name } for _, rawImport := range f.Imports { @@ -213,12 +211,12 @@ func (fp *FlatPackage) ResolveImports(resolve ResolvePkgFunc, overlays map[strin if imp == "C" { continue } - if _, ok := fp.Imports[imp]; ok { + if pkg := pkg.Imports[imp]; pkg != nil { continue } - if pkgID := resolve(imp); pkgID != "" { - fp.Imports[imp] = pkgID + if impPkg := resolve(imp); impPkg != nil { + pkg.Imports[imp] = impPkg } } } diff --git a/go/tools/gopackagesdriver/gopackagesdriver_test.go b/go/tools/gopackagesdriver/gopackagesdriver_test.go index 751470adea..232ee55a30 100644 --- a/go/tools/gopackagesdriver/gopackagesdriver_test.go +++ b/go/tools/gopackagesdriver/gopackagesdriver_test.go @@ -11,6 +11,7 @@ import ( "testing" "github.com/bazelbuild/rules_go/go/tools/bazel_testing" + "golang.org/x/tools/go/packages" ) func TestMain(m *testing.M) { @@ -86,7 +87,7 @@ const ( ) func TestStdlib(t *testing.T) { - resp := runForTest(t, DriverRequest{}, ".", "std") + resp := runForTest(t, packages.DriverRequest{}, ".", "std") if len(resp.Packages) == 0 { t.Fatal("Expected stdlib packages") @@ -113,7 +114,7 @@ func TestStdlib(t *testing.T) { } func TestBaseFileLookup(t *testing.T) { - resp := runForTest(t, DriverRequest{}, ".", "file=hello.go") + resp := runForTest(t, packages.DriverRequest{}, ".", "file=hello.go") t.Run("roots", func(t *testing.T) { if len(resp.Roots) != 1 { @@ -178,7 +179,7 @@ func TestBaseFileLookup(t *testing.T) { } func TestRelativeFileLookup(t *testing.T) { - resp := runForTest(t, DriverRequest{}, "subhello", "file=./subhello.go") + resp := runForTest(t, packages.DriverRequest{}, "subhello", "file=./subhello.go") t.Run("roots", func(t *testing.T) { if len(resp.Roots) != 1 { @@ -209,7 +210,7 @@ func TestRelativeFileLookup(t *testing.T) { } func TestRelativePatternWildcardLookup(t *testing.T) { - resp := runForTest(t, DriverRequest{}, "subhello", "./...") + resp := runForTest(t, packages.DriverRequest{}, "subhello", "./...") t.Run("roots", func(t *testing.T) { if len(resp.Roots) != 1 { @@ -240,7 +241,7 @@ func TestRelativePatternWildcardLookup(t *testing.T) { } func TestExternalTests(t *testing.T) { - resp := runForTest(t, DriverRequest{}, ".", "file=hello_external_test.go") + resp := runForTest(t, packages.DriverRequest{}, ".", "file=hello_external_test.go") if len(resp.Roots) != 2 { t.Errorf("Expected exactly two roots for package: %+v", resp.Roots) } @@ -278,11 +279,11 @@ func TestOverlay(t *testing.T) { subhelloPath := path.Join(wd, "subhello/subhello.go") expectedImportsPerFile := map[string][]string{ - helloPath: []string{"fmt"}, - subhelloPath: []string{"os", "encoding/json"}, + helloPath: {"fmt"}, + subhelloPath: {"os", "encoding/json"}, } - overlayDriverRequest := DriverRequest{ + overlayDriverRequest := packages.DriverRequest{ Overlay: map[string][]byte{ helloPath: []byte(` package hello @@ -324,7 +325,7 @@ func TestOverlay(t *testing.T) { expectSetEquality(t, expectedImportsPerFile[subhelloPath], subhelloPkgImportPaths, "subhello imports") } -func runForTest(t *testing.T, driverRequest DriverRequest, relativeWorkingDir string, args ...string) driverResponse { +func runForTest(t *testing.T, driverRequest packages.DriverRequest, relativeWorkingDir string, args ...string) driverResponse { t.Helper() // Remove most environment variables, other than those on an allowlist. diff --git a/go/tools/gopackagesdriver/json_packages_driver.go b/go/tools/gopackagesdriver/json_packages_driver.go index 3ad7360479..14589f7c2f 100644 --- a/go/tools/gopackagesdriver/json_packages_driver.go +++ b/go/tools/gopackagesdriver/json_packages_driver.go @@ -16,7 +16,8 @@ package main import ( "fmt" - "runtime" + + "golang.org/x/tools/go/packages" ) type JSONPackagesDriver struct { @@ -47,14 +48,12 @@ func NewJSONPackagesDriver(jsonFiles []string, prf PathResolverFunc, bazelVersio return jpd, nil } -func (b *JSONPackagesDriver) GetResponse(labels []string) *driverResponse { - rootPkgs, packages := b.registry.Match(labels) +func (b *JSONPackagesDriver) GetResponse(labels []string) *packages.DriverResponse { + rootPkgs, paks := b.registry.Match(labels) - return &driverResponse{ + return &packages.DriverResponse{ NotHandled: false, - Compiler: "gc", - Arch: runtime.GOARCH, Roots: rootPkgs, - Packages: packages, + Packages: paks, } } diff --git a/go/tools/gopackagesdriver/packageregistry.go b/go/tools/gopackagesdriver/packageregistry.go index ff67c9f304..44f88e82b9 100644 --- a/go/tools/gopackagesdriver/packageregistry.go +++ b/go/tools/gopackagesdriver/packageregistry.go @@ -18,18 +18,20 @@ import ( "fmt" "os" "strings" + + "golang.org/x/tools/go/packages" ) type PackageRegistry struct { - packagesByID map[string]*FlatPackage - stdlib map[string]string + packagesByID map[string]*packages.Package + stdlib map[string]*packages.Package bazelVersion bazelVersion } func NewPackageRegistry(bazelVersion bazelVersion, pkgs ...*FlatPackage) *PackageRegistry { pr := &PackageRegistry{ - packagesByID: map[string]*FlatPackage{}, - stdlib: map[string]string{}, + packagesByID: map[string]*packages.Package{}, + stdlib: map[string]*packages.Package{}, bazelVersion: bazelVersion, } pr.Add(pkgs...) @@ -37,11 +39,34 @@ func NewPackageRegistry(bazelVersion bazelVersion, pkgs ...*FlatPackage) *Packag } func (pr *PackageRegistry) Add(pkgs ...*FlatPackage) *PackageRegistry { - for _, pkg := range pkgs { + for _, flatPkg := range pkgs { + imports := make(map[string]*packages.Package) + for impKey, imp := range flatPkg.Imports { + imports[impKey] = &packages.Package{ + ID: imp, + } + } + + pkg := &packages.Package{ + ID: flatPkg.ID, + Name: flatPkg.Name, + PkgPath: flatPkg.PkgPath, + GoFiles: flatPkg.GoFiles, + CompiledGoFiles: flatPkg.CompiledGoFiles, + OtherFiles: flatPkg.OtherFiles, + ExportFile: flatPkg.ExportFile, + Imports: imports, + } + + // TODO(ellie): wtf why does this fix things + if flatPkg.IsStdlib() { + pkg.CompiledGoFiles = pkg.GoFiles + } + pr.packagesByID[pkg.ID] = pkg - if pkg.IsStdlib() { - pr.stdlib[pkg.PkgPath] = pkg.ID + if flatPkg.IsStdlib() { + pr.stdlib[pkg.PkgPath] = pkg } } return pr @@ -49,8 +74,8 @@ func (pr *PackageRegistry) Add(pkgs ...*FlatPackage) *PackageRegistry { func (pr *PackageRegistry) ResolvePaths(prf PathResolverFunc) error { for _, pkg := range pr.packagesByID { - pkg.ResolvePaths(prf) - pkg.FilterFilesForBuildTags() + ResolvePaths(pkg, prf) + FilterFilesForBuildTags(pkg) } return nil } @@ -59,28 +84,29 @@ func (pr *PackageRegistry) ResolvePaths(prf PathResolverFunc) error { // stdlib packages are not part of the JSON file exports as bazel is unaware of // them. func (pr *PackageRegistry) ResolveImports(overlays map[string][]byte) error { - resolve := func(importPath string) string { - if pkgID, ok := pr.stdlib[importPath]; ok { - return pkgID + resolve := func(importPath string) *packages.Package { + if pkg, ok := pr.stdlib[importPath]; ok { + return pkg } - return "" + return nil } for _, pkg := range pr.packagesByID { - if err := pkg.ResolveImports(resolve, overlays); err != nil { + if err := ResolveImports(pkg, resolve, overlays); err != nil { return err } - testFp := pkg.MoveTestFiles() - if testFp != nil { - pr.packagesByID[testFp.ID] = testFp + + testPkg := MoveTestFiles(pkg) + if testPkg != nil { + pr.packagesByID[testPkg.ID] = testPkg } } return nil } -func (pr *PackageRegistry) walk(acc map[string]*FlatPackage, root string) { +func (pr *PackageRegistry) walk(acc map[string]*packages.Package, root string) { pkg := pr.packagesByID[root] if pkg == nil { @@ -89,14 +115,21 @@ func (pr *PackageRegistry) walk(acc map[string]*FlatPackage, root string) { } acc[pkg.ID] = pkg - for _, pkgID := range pkg.Imports { - if _, ok := acc[pkgID]; !ok { - pr.walk(acc, pkgID) + for imp, pkgI := range pkg.Imports { + if pkgI == nil { + fmt.Fprintf(os.Stderr, "Error: import %s for package %v not resolved\n", imp, root) + if _, ok := pr.stdlib[imp]; ok { + fmt.Fprintf(os.Stderr, "import was stdlib\n") + } + continue + } + if _, ok := acc[pkgI.ID]; !ok { + pr.walk(acc, pkgI.ID) } } } -func (pr *PackageRegistry) Match(labels []string) ([]string, []*FlatPackage) { +func (pr *PackageRegistry) Match(labels []string) ([]string, []*packages.Package) { roots := map[string]struct{}{} for _, label := range labels { @@ -110,10 +143,8 @@ func (pr *PackageRegistry) Match(labels []string) ([]string, []*FlatPackage) { if label == RulesGoStdlibLabel { // For stdlib, we need to append all the subpackages as roots // since RulesGoStdLibLabel doesn't actually show up in the stdlib pkg.json - for _, pkg := range pr.packagesByID { - if pkg.Standard { - roots[pkg.ID] = struct{}{} - } + for _, pkg := range pr.stdlib { + roots[pkg.ID] = struct{}{} } } else { roots[label] = struct{}{} @@ -124,14 +155,14 @@ func (pr *PackageRegistry) Match(labels []string) ([]string, []*FlatPackage) { } } - walkedPackages := map[string]*FlatPackage{} + walkedPackages := map[string]*packages.Package{} retRoots := make([]string, 0, len(roots)) for rootPkg := range roots { retRoots = append(retRoots, rootPkg) pr.walk(walkedPackages, rootPkg) } - retPkgs := make([]*FlatPackage, 0, len(walkedPackages)) + retPkgs := make([]*packages.Package, 0, len(walkedPackages)) for _, pkg := range walkedPackages { retPkgs = append(retPkgs, pkg) }