Skip to content

Commit

Permalink
add in changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ellie-idb committed Jan 20, 2025
1 parent 66477c1 commit 4bd55a0
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 312 deletions.
22 changes: 18 additions & 4 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,22 @@ 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_net",
importpath = "golang.org/x/net",
sum = "h1:P4fl1q7dY2hnZFxEk4pPSkDHF+QqjitcnDjUQyMM+pM=",
version = "v0.31.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(
Expand All @@ -183,6 +190,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",
Expand Down
21 changes: 12 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
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
github.com/pmezard/go-difflib v1.0.0
golang.org/x/net v0.26.0
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013
google.golang.org/grpc v1.40.1
golang.org/x/net v0.31.0
golang.org/x/tools v0.27.0
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1
google.golang.org/grpc v1.56.3
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.3.0
google.golang.org/protobuf v1.31.0
google.golang.org/protobuf v1.33.0
)

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
)
127 changes: 18 additions & 109 deletions go.sum

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions go/tools/gopackagesdriver/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ go_library(
visibility = [
"//tests/integration/gopackagesdriver:__pkg__",
],
deps = ["@org_golang_x_tools//go/packages"],
)

go_binary(
Expand Down
10 changes: 6 additions & 4 deletions go/tools/gopackagesdriver/bazel_json_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"regexp"
"runtime"
"strings"

"golang.org/x/tools/go/packages"
)

type BazelJSONBuilder struct {
Expand Down Expand Up @@ -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 != "" {
Expand All @@ -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
Expand Down Expand Up @@ -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{
Expand Down
66 changes: 3 additions & 63 deletions go/tools/gopackagesdriver/driver_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
76 changes: 37 additions & 39 deletions go/tools/gopackagesdriver/flatpackage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -97,30 +99,30 @@ 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()
f, err := parser.ParseFile(fset, filename, nil, parser.PackageClauseOnly)
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)
Expand All @@ -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,
}
}

Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -213,12 +211,12 @@ func (fp *FlatPackage) ResolveImports(resolve ResolvePkgFunc, overlays map[strin
if imp == "C" {
continue
}
if _, ok := fp.Imports[imp]; ok {
if _, ok := pkg.Imports[imp]; ok {
continue
}

if pkgID := resolve(imp); pkgID != "" {
fp.Imports[imp] = pkgID
if impPkg := resolve(imp); impPkg != nil {
pkg.Imports[imp] = impPkg
}
}
}
Expand Down
Loading

0 comments on commit 4bd55a0

Please sign in to comment.