-
-
Notifications
You must be signed in to change notification settings - Fork 758
go/tools: add gopackagesdriver #2858
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 26 commits
a67afe0
bdd0556
76e6753
5a53ae2
d2b527a
ce61882
b08da87
5dd1e0e
168e90d
331470a
29d0b78
529b5ab
a0eeeec
79f0cb8
8f76121
1203bea
2657063
859a3b1
2190ab6
d69fad2
4719e91
07ad711
b3af1b1
079bb2a
070b4c3
69fef40
fb2df68
0d01dc3
c36a5eb
2773277
5923382
b962df0
98338cc
bcd9713
b81ac13
ae8788f
5b80ccd
34ee2be
dfe668a
81166a8
32aba61
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,209 @@ | ||
| // Copyright 2018 The Bazel Authors. All rights reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/json" | ||
| "flag" | ||
| "go/build" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| ) | ||
|
|
||
| // Copy and pasted from golang.org/x/tools/go/packages | ||
| type flatPackagesError struct { | ||
| Pos string // "file:line:col" or "file:line" or "" or "-" | ||
| Msg string | ||
| Kind flatPackagesErrorKind | ||
| } | ||
|
|
||
| type flatPackagesErrorKind int | ||
|
|
||
| const ( | ||
| UnknownError flatPackagesErrorKind = iota | ||
| ListError | ||
| ParseError | ||
| TypeError | ||
| ) | ||
|
|
||
| func (err flatPackagesError) Error() string { | ||
| pos := err.Pos | ||
| if pos == "" { | ||
| pos = "-" // like token.Position{}.String() | ||
| } | ||
| return pos + ": " + err.Msg | ||
| } | ||
|
|
||
| // flatPackage is the JSON form of Package | ||
| // It drops all the type and syntax fields, and transforms the Imports | ||
| type flatPackage struct { | ||
| ID string | ||
| Name string `json:",omitempty"` | ||
| PkgPath string `json:",omitempty"` | ||
| Standard bool `json:",omitempty"` | ||
| Errors []flatPackagesError `json:",omitempty"` | ||
| GoFiles []string `json:",omitempty"` | ||
| CompiledGoFiles []string `json:",omitempty"` | ||
| OtherFiles []string `json:",omitempty"` | ||
| ExportFile string `json:",omitempty"` | ||
| Imports map[string]string `json:",omitempty"` | ||
| } | ||
|
|
||
| type goListPackage struct { | ||
| Dir string // directory containing package sources | ||
| ImportPath string // import path of package in dir | ||
| Name string // package name | ||
| Target string // install path | ||
| Goroot bool // is this package in the Go root? | ||
| Standard bool // is this package part of the standard Go library? | ||
| Root string // Go root or Go path dir containing this package | ||
| Export string // file containing export data (when using -export) | ||
| // Source files | ||
| GoFiles []string // .go source files (excluding CgoFiles, TestGoFiles, XTestGoFiles) | ||
| CgoFiles []string // .go source files that import "C" | ||
| CompiledGoFiles []string // .go files presented to compiler (when using -compiled) | ||
| IgnoredGoFiles []string // .go source files ignored due to build constraints | ||
| IgnoredOtherFiles []string // non-.go source files ignored due to build constraints | ||
| CFiles []string // .c source files | ||
| CXXFiles []string // .cc, .cxx and .cpp source files | ||
| MFiles []string // .m source files | ||
| HFiles []string // .h, .hh, .hpp and .hxx source files | ||
| FFiles []string // .f, .F, .for and .f90 Fortran source files | ||
| SFiles []string // .s source files | ||
| SwigFiles []string // .swig files | ||
| SwigCXXFiles []string // .swigcxx files | ||
| SysoFiles []string // .syso object files to add to archive | ||
| TestGoFiles []string // _test.go files in package | ||
| XTestGoFiles []string // _test.go files outside package | ||
| // Embedded files | ||
| EmbedPatterns []string // //go:embed patterns | ||
| EmbedFiles []string // files matched by EmbedPatterns | ||
| TestEmbedPatterns []string // //go:embed patterns in TestGoFiles | ||
| TestEmbedFiles []string // files matched by TestEmbedPatterns | ||
| XTestEmbedPatterns []string // //go:embed patterns in XTestGoFiles | ||
| XTestEmbedFiles []string // files matched by XTestEmbedPatterns | ||
| // Dependency information | ||
| Imports []string // import paths used by this package | ||
| ImportMap map[string]string // map from source import to ImportPath (identity entries omitted) | ||
| // Error information | ||
| Incomplete bool // this package or a dependency has an error | ||
| Error *flatPackagesError // error loading package | ||
| DepsErrors []*flatPackagesError // errors loading dependencies | ||
| } | ||
|
|
||
| func stdlibPackageID(importPath string) string { | ||
| return "@io_bazel_rules_go//stdlib:" + importPath | ||
| } | ||
|
|
||
| func execRootPath(execRoot, p string) string { | ||
| dir, _ := filepath.Rel(execRoot, p) | ||
| return filepath.Join("__BAZEL_EXECROOT__", dir) | ||
| } | ||
|
|
||
| func absoluteSourcesPaths(execRoot, pkgDir string, srcs []string) []string { | ||
| ret := []string{} | ||
|
steeve marked this conversation as resolved.
Outdated
|
||
| pkgDir = execRootPath(execRoot, pkgDir) | ||
| for _, src := range srcs { | ||
| ret = append(ret, filepath.Join(pkgDir, src)) | ||
| } | ||
| return ret | ||
| } | ||
|
|
||
| func packageToPackage(execRoot string, pkg *goListPackage) *flatPackage { | ||
|
steeve marked this conversation as resolved.
Outdated
|
||
| // Don't use generated files from the stdlib | ||
| goFiles := absoluteSourcesPaths(execRoot, pkg.Dir, pkg.GoFiles) | ||
|
|
||
| newPkg := &flatPackage{ | ||
| ID: stdlibPackageID(pkg.ImportPath), | ||
| Name: pkg.Name, | ||
| PkgPath: pkg.ImportPath, | ||
| ExportFile: execRootPath(execRoot, pkg.Target), | ||
| Imports: map[string]string{}, | ||
| Standard: pkg.Standard, | ||
| GoFiles: goFiles, | ||
| CompiledGoFiles: goFiles, | ||
| } | ||
| for _, imp := range pkg.Imports { | ||
| newPkg.Imports[imp] = stdlibPackageID(imp) | ||
| } | ||
| // We don't support CGo for now | ||
| delete(newPkg.Imports, "C") | ||
| return newPkg | ||
| } | ||
|
|
||
| // stdlib builds the standard library in the appropriate mode into a new goroot. | ||
|
steeve marked this conversation as resolved.
Outdated
|
||
| func stdliblist(args []string) error { | ||
| // process the args | ||
| flags := flag.NewFlagSet("stdliblist", flag.ExitOnError) | ||
| goenv := envFlags(flags) | ||
| out := flags.String("out", "", "Path to output go list json") | ||
| if err := flags.Parse(args); err != nil { | ||
| return err | ||
| } | ||
| if err := goenv.checkFlags(); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Ensure paths are absolute. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are there paths in particular that must be relative coming in? If so, let's document that here. If not, let's drop relative paths from We had a security issue in cmd/go with That may not really be relevant for this action, and it may not be possible since analysis might only have relative paths, but it's taught me to handle relative paths in
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I actually copied it from the stdlib builder |
||
| absPaths := []string{} | ||
| for _, path := range filepath.SplitList(os.Getenv("PATH")) { | ||
| absPaths = append(absPaths, abs(path)) | ||
| } | ||
| os.Setenv("PATH", strings.Join(absPaths, string(os.PathListSeparator))) | ||
| os.Setenv("GOROOT", abs(os.Getenv("GOROOT"))) | ||
| // Make sure we have an absolute path to the C compiler. | ||
| // TODO(#1357): also take absolute paths of includes and other paths in flags. | ||
| os.Setenv("CC", abs(os.Getenv("CC"))) | ||
|
|
||
| execRoot := abs(".") | ||
|
|
||
| cachePath := abs(*out + ".gocache") | ||
| defer os.RemoveAll(cachePath) | ||
| os.Setenv("GOCACHE", cachePath) | ||
| os.Setenv("GOPATH", cachePath) | ||
|
steeve marked this conversation as resolved.
|
||
|
|
||
| listArgs := goenv.goCmd("list") | ||
| if len(build.Default.BuildTags) > 0 { | ||
| listArgs = append(listArgs, "-tags", strings.Join(build.Default.BuildTags, " ")) | ||
|
steeve marked this conversation as resolved.
Outdated
|
||
| } | ||
| listArgs = append(listArgs, "-json", "builtin", "std", "runtime/cgo") | ||
|
|
||
| jsonFile, err := os.Create(*out) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer jsonFile.Close() | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check for an error when closing writable files:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean, yes, but is there a cleaner way to do this ? |
||
|
|
||
| jsonData := &bytes.Buffer{} | ||
| if err := goenv.runCommandToFile(jsonData, listArgs); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| encoder := json.NewEncoder(jsonFile) | ||
| decoder := json.NewDecoder(jsonData) | ||
| for decoder.More() { | ||
| var pkg *goListPackage | ||
| if err := decoder.Decode(&pkg); err != nil { | ||
| return err | ||
| } | ||
| if err := encoder.Encode(packageToPackage(execRoot, pkg)); err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library") | ||
|
|
||
| go_library( | ||
| name = "go_default_library", | ||
|
steeve marked this conversation as resolved.
Outdated
|
||
| srcs = [ | ||
| "bazel.go", | ||
| "bazel_json_builder.go", | ||
| "driver_request.go", | ||
| "flatpackage.go", | ||
| "json_packages_driver.go", | ||
| "main.go", | ||
| "packageregistry.go", | ||
| ], | ||
| importpath = "github.com/bazelbuild/rules_go/go/tools/gopackagesdriver", | ||
| visibility = ["//visibility:private"], | ||
| ) | ||
|
|
||
| go_binary( | ||
| name = "gopackagesdriver", | ||
| embed = [":go_default_library"], | ||
| visibility = ["//visibility:public"], | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| load( | ||
|
steeve marked this conversation as resolved.
|
||
| "//go/private:providers.bzl", | ||
| "GoArchive", | ||
| "GoStdLib", | ||
| ) | ||
| load( | ||
| "//go/private:context.bzl", | ||
| "go_context", | ||
| ) | ||
| load( | ||
| "@bazel_skylib//lib:paths.bzl", | ||
| "paths", | ||
| ) | ||
| load( | ||
| "@bazel_skylib//lib:collections.bzl", | ||
| "collections", | ||
| ) | ||
|
|
||
| GoPkgInfo = provider() | ||
|
|
||
| def _is_file_external(f): | ||
| return f.owner.workspace_root != "" | ||
|
|
||
| def _file_path(f): | ||
| if f.is_source and not _is_file_external(f): | ||
| return paths.join("__BAZEL_WORKSPACE__", f.path) | ||
| return paths.join("__BAZEL_EXECROOT__", f.path) | ||
|
|
||
| def _go_pkg_info_aspect_impl(target, ctx): | ||
| # Fetch the stdlib JSON file from the inner most target | ||
| stdlib_json = None | ||
|
|
||
| deps_transitive_json = [] | ||
| deps_transitive_x = [] | ||
| if hasattr(ctx.rule.attr, "deps"): | ||
|
steeve marked this conversation as resolved.
Outdated
|
||
| for dep in ctx.rule.attr.deps: | ||
| if GoPkgInfo in dep: | ||
| pkg_info = dep[GoPkgInfo] | ||
| deps_transitive_json.append(pkg_info.transitive_json) | ||
| deps_transitive_x.append(pkg_info.transitive_x) | ||
| # Fetch the stdlib json from the first dependency | ||
|
steeve marked this conversation as resolved.
Outdated
|
||
| if not stdlib_json: | ||
| stdlib_json = pkg_info.stdlib_json | ||
|
|
||
| # If deps are embedded, do not gather their json or x since they are | ||
| # included in the current target, but do gather their deps'. | ||
| if hasattr(ctx.rule.attr, "embed"): | ||
| for dep in ctx.rule.attr.embed: | ||
| if GoPkgInfo in dep: | ||
| pkg_info = dep[GoPkgInfo] | ||
| deps_transitive_json.append(pkg_info.deps_transitive_json) | ||
| deps_transitive_x.append(pkg_info.deps_transitive_x) | ||
|
|
||
| pkg_json = None | ||
| x = None | ||
| if GoArchive in target: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI, we'll eventually need an action that writes JSON files with more data (AST, type info). I believe gopls only requests the minimum from go/packages, then loads the rest on its own. Other clients will want a more complete response. Definitely no need to do that now, but this might be a good spot for a TODO.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, the type flatPackage struct {
ID string
Name string `json:",omitempty"`
PkgPath string `json:",omitempty"`
Errors []Error `json:",omitempty"`
GoFiles []string `json:",omitempty"`
CompiledGoFiles []string `json:",omitempty"`
OtherFiles []string `json:",omitempty"`
IgnoredFiles []string `json:",omitempty"`
ExportFile string `json:",omitempty"`
Imports map[string]string `json:",omitempty"`
} |
||
| archive = target[GoArchive] | ||
| x = archive.data.export_file | ||
| pkg = struct( | ||
| ID = str(archive.data.label), | ||
| PkgPath = archive.data.importpath, | ||
| ExportFile = _file_path(archive.data.export_file), | ||
| GoFiles = [ | ||
| _file_path(src) | ||
| for src in archive.data.orig_srcs | ||
| ], | ||
| CompiledGoFiles = [ | ||
| _file_path(src) | ||
| for src in archive.data.srcs | ||
| ], | ||
| ) | ||
| pkg_json = ctx.actions.declare_file(archive.data.name + ".pkg.json") | ||
| ctx.actions.write(pkg_json, content = pkg.to_json()) | ||
| # If there was no stdlib json in any dependencies, fetch it from the | ||
| # current go_ node. | ||
| if not stdlib_json: | ||
| stdlib_json = ctx.attr._go_stdlib[GoStdLib].list_json | ||
|
|
||
| pkg_info = GoPkgInfo( | ||
| json = pkg_json, | ||
| stdlib_json = stdlib_json, | ||
| transitive_json = depset( | ||
| direct = [pkg_json] if pkg_json else [], | ||
| transitive = deps_transitive_json, | ||
| ), | ||
| deps_transitive_json = depset( | ||
| transitive = deps_transitive_json, | ||
| ), | ||
| x = x, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is 'x'?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. export file, an archive with only the
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's rename to |
||
| transitive_x = depset( | ||
| direct = [x] if x else None, | ||
| transitive = deps_transitive_x, | ||
| ), | ||
| deps_transitive_x = depset( | ||
| transitive = deps_transitive_x, | ||
| ), | ||
| ) | ||
|
|
||
| return [ | ||
| pkg_info, | ||
| OutputGroupInfo( | ||
| go_pkg_driver_json = pkg_info.transitive_json, | ||
| go_pkg_driver_x = pkg_info.transitive_x, | ||
| go_pkg_driver_stdlib_json = depset([pkg_info.stdlib_json] if pkg_info.stdlib_json else []) | ||
| ), | ||
| ] | ||
|
|
||
| go_pkg_info_aspect = aspect( | ||
| implementation = _go_pkg_info_aspect_impl, | ||
| attr_aspects = ["embed", "deps"], | ||
| attrs = { | ||
| "_go_stdlib": attr.label( | ||
| default = "@io_bazel_rules_go//:stdlib", | ||
| ), | ||
| }, | ||
| ) | ||
Uh oh!
There was an error while loading. Please reload this page.