...

Source file src/github.com/bazelbuild/rules_go/go/tools/gopackagesdriver/main.go

Documentation: github.com/bazelbuild/rules_go/go/tools/gopackagesdriver

     1  // Copyright 2021 The Bazel Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"context"
    19  	"encoding/json"
    20  	"fmt"
    21  	"os"
    22  	"runtime"
    23  	"strings"
    24  )
    25  
    26  type driverResponse struct {
    27  	// NotHandled is returned if the request can't be handled by the current
    28  	// driver. If an external driver returns a response with NotHandled, the
    29  	// rest of the driverResponse is ignored, and go/packages will fallback
    30  	// to the next driver. If go/packages is extended in the future to support
    31  	// lists of multiple drivers, go/packages will fall back to the next driver.
    32  	NotHandled bool
    33  
    34  	// Compiler and Arch are the arguments pass of types.SizesFor
    35  	// to get a types.Sizes to use when type checking.
    36  	Compiler string
    37  	Arch     string
    38  
    39  	// Roots is the set of package IDs that make up the root packages.
    40  	// We have to encode this separately because when we encode a single package
    41  	// we cannot know if it is one of the roots as that requires knowledge of the
    42  	// graph it is part of.
    43  	Roots []string `json:",omitempty"`
    44  
    45  	// Packages is the full set of packages in the graph.
    46  	// The packages are not connected into a graph.
    47  	// The Imports if populated will be stubs that only have their ID set.
    48  	// Imports will be connected and then type and syntax information added in a
    49  	// later pass (see refine).
    50  	Packages []*FlatPackage
    51  }
    52  
    53  var (
    54  	// Injected via x_defs.
    55  
    56  	rulesGoRepositoryName string
    57  	goDefaultAspect       = rulesGoRepositoryName + "//go/tools/gopackagesdriver:aspect.bzl%go_pkg_info_aspect"
    58  	bazelBin              = getenvDefault("GOPACKAGESDRIVER_BAZEL", "bazel")
    59  	bazelStartupFlags     = strings.Fields(os.Getenv("GOPACKAGESDRIVER_BAZEL_FLAGS"))
    60  	bazelQueryFlags       = strings.Fields(os.Getenv("GOPACKAGESDRIVER_BAZEL_QUERY_FLAGS"))
    61  	bazelQueryScope       = getenvDefault("GOPACKAGESDRIVER_BAZEL_QUERY_SCOPE", "")
    62  	bazelBuildFlags       = strings.Fields(os.Getenv("GOPACKAGESDRIVER_BAZEL_BUILD_FLAGS"))
    63  	workspaceRoot         = os.Getenv("BUILD_WORKSPACE_DIRECTORY")
    64  	additionalAspects     = strings.Fields(os.Getenv("GOPACKAGESDRIVER_BAZEL_ADDTL_ASPECTS"))
    65  	additionalKinds       = strings.Fields(os.Getenv("GOPACKAGESDRIVER_BAZEL_KINDS"))
    66  	emptyResponse         = &driverResponse{
    67  		NotHandled: true,
    68  		Compiler:   "gc",
    69  		Arch:       runtime.GOARCH,
    70  		Roots:      []string{},
    71  		Packages:   []*FlatPackage{},
    72  	}
    73  )
    74  
    75  func run() (*driverResponse, error) {
    76  	ctx, cancel := signalContext(context.Background(), os.Interrupt)
    77  	defer cancel()
    78  
    79  	queries := os.Args[1:]
    80  
    81  	request, err := ReadDriverRequest(os.Stdin)
    82  	if err != nil {
    83  		return emptyResponse, fmt.Errorf("unable to read request: %w", err)
    84  	}
    85  
    86  	bazel, err := NewBazel(ctx, bazelBin, workspaceRoot, bazelStartupFlags)
    87  	if err != nil {
    88  		return emptyResponse, fmt.Errorf("unable to create bazel instance: %w", err)
    89  	}
    90  
    91  	bazelJsonBuilder, err := NewBazelJSONBuilder(bazel, request.Tests)
    92  	if err != nil {
    93  		return emptyResponse, fmt.Errorf("unable to build JSON files: %w", err)
    94  	}
    95  
    96  	labels, err := bazelJsonBuilder.Labels(ctx, queries)
    97  	if err != nil {
    98  		return emptyResponse, fmt.Errorf("unable to lookup package: %w", err)
    99  	}
   100  
   101  	jsonFiles, err := bazelJsonBuilder.Build(ctx, labels, request.Mode)
   102  	if err != nil {
   103  		return emptyResponse, fmt.Errorf("unable to build JSON files: %w", err)
   104  	}
   105  
   106  	driver, err := NewJSONPackagesDriver(jsonFiles, bazelJsonBuilder.PathResolver(), bazel.version)
   107  	if err != nil {
   108  		return emptyResponse, fmt.Errorf("unable to load JSON files: %w", err)
   109  	}
   110  
   111  	// Note: we are returning all files required to build a specific package.
   112  	// For file queries (`file=`), this means that the CompiledGoFiles will
   113  	// include more than the only file being specified.
   114  	return driver.GetResponse(labels), nil
   115  }
   116  
   117  func main() {
   118  	response, err := run()
   119  	if err := json.NewEncoder(os.Stdout).Encode(response); err != nil {
   120  		fmt.Fprintf(os.Stderr, "unable to encode response: %v", err)
   121  	}
   122  	if err != nil {
   123  		fmt.Fprintf(os.Stderr, "error: %v", err)
   124  		// gopls will check the packages driver exit code, and if there is an
   125  		// error, it will fall back to go list. Obviously we don't want that,
   126  		// so force a 0 exit code.
   127  		os.Exit(0)
   128  	}
   129  }
   130  

View as plain text