github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/internal/lsp/command/interface.go (about)

     1  // Copyright 2021 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package command defines the interface provided by gopls for the
     6  // workspace/executeCommand LSP request.
     7  //
     8  // This interface is fully specified by the Interface type, provided it
     9  // conforms to the restrictions outlined in its doc string.
    10  //
    11  // Bindings for server-side command dispatch and client-side serialization are
    12  // also provided by this package, via code generation.
    13  package command
    14  
    15  //go:generate go run -tags=generate generate.go
    16  
    17  import (
    18  	"context"
    19  
    20  	"github.com/powerman/golang-tools/internal/lsp/protocol"
    21  )
    22  
    23  // Interface defines the interface gopls exposes for the
    24  // workspace/executeCommand request.
    25  //
    26  // This interface is used to generate marshaling/unmarshaling code, dispatch,
    27  // and documentation, and so has some additional restrictions:
    28  //  1. All method arguments must be JSON serializable.
    29  //  2. Methods must return either error or (T, error), where T is a
    30  //     JSON serializable type.
    31  //  3. The first line of the doc string is special. Everything after the colon
    32  //     is considered the command 'Title'.
    33  //     TODO(rFindley): reconsider this -- Title may be unnecessary.
    34  type Interface interface {
    35  	// ApplyFix: Apply a fix
    36  	//
    37  	// Applies a fix to a region of source code.
    38  	ApplyFix(context.Context, ApplyFixArgs) error
    39  	// Test: Run test(s) (legacy)
    40  	//
    41  	// Runs `go test` for a specific set of test or benchmark functions.
    42  	Test(context.Context, protocol.DocumentURI, []string, []string) error
    43  
    44  	// TODO: deprecate Test in favor of RunTests below.
    45  
    46  	// Test: Run test(s)
    47  	//
    48  	// Runs `go test` for a specific set of test or benchmark functions.
    49  	RunTests(context.Context, RunTestsArgs) error
    50  
    51  	// Generate: Run go generate
    52  	//
    53  	// Runs `go generate` for a given directory.
    54  	Generate(context.Context, GenerateArgs) error
    55  
    56  	// RegenerateCgo: Regenerate cgo
    57  	//
    58  	// Regenerates cgo definitions.
    59  	RegenerateCgo(context.Context, URIArg) error
    60  
    61  	// Tidy: Run go mod tidy
    62  	//
    63  	// Runs `go mod tidy` for a module.
    64  	Tidy(context.Context, URIArgs) error
    65  
    66  	// Vendor: Run go mod vendor
    67  	//
    68  	// Runs `go mod vendor` for a module.
    69  	Vendor(context.Context, URIArg) error
    70  
    71  	// EditGoDirective: Run go mod edit -go=version
    72  	//
    73  	// Runs `go mod edit -go=version` for a module.
    74  	EditGoDirective(context.Context, EditGoDirectiveArgs) error
    75  
    76  	// UpdateGoSum: Update go.sum
    77  	//
    78  	// Updates the go.sum file for a module.
    79  	UpdateGoSum(context.Context, URIArgs) error
    80  
    81  	// CheckUpgrades: Check for upgrades
    82  	//
    83  	// Checks for module upgrades.
    84  	CheckUpgrades(context.Context, CheckUpgradesArgs) error
    85  
    86  	// AddDependency: Add a dependency
    87  	//
    88  	// Adds a dependency to the go.mod file for a module.
    89  	AddDependency(context.Context, DependencyArgs) error
    90  
    91  	// UpgradeDependency: Upgrade a dependency
    92  	//
    93  	// Upgrades a dependency in the go.mod file for a module.
    94  	UpgradeDependency(context.Context, DependencyArgs) error
    95  
    96  	// RemoveDependency: Remove a dependency
    97  	//
    98  	// Removes a dependency from the go.mod file of a module.
    99  	RemoveDependency(context.Context, RemoveDependencyArgs) error
   100  
   101  	// GoGetPackage: go get a package
   102  	//
   103  	// Runs `go get` to fetch a package.
   104  	GoGetPackage(context.Context, GoGetPackageArgs) error
   105  
   106  	// GCDetails: Toggle gc_details
   107  	//
   108  	// Toggle the calculation of gc annotations.
   109  	GCDetails(context.Context, protocol.DocumentURI) error
   110  
   111  	// TODO: deprecate GCDetails in favor of ToggleGCDetails below.
   112  
   113  	// ToggleGCDetails: Toggle gc_details
   114  	//
   115  	// Toggle the calculation of gc annotations.
   116  	ToggleGCDetails(context.Context, URIArg) error
   117  
   118  	// GenerateGoplsMod: Generate gopls.mod
   119  	//
   120  	// (Re)generate the gopls.mod file for a workspace.
   121  	GenerateGoplsMod(context.Context, URIArg) error
   122  
   123  	// ListKnownPackages: List known packages
   124  	//
   125  	// Retrieve a list of packages that are importable from the given URI.
   126  	ListKnownPackages(context.Context, URIArg) (ListKnownPackagesResult, error)
   127  
   128  	// ListImports: List imports of a file and its package
   129  	//
   130  	// Retrieve a list of imports in the given Go file, and the package it
   131  	// belongs to.
   132  	ListImports(context.Context, URIArg) (ListImportsResult, error)
   133  
   134  	// AddImport: Add an import
   135  	//
   136  	// Ask the server to add an import path to a given Go file.  The method will
   137  	// call applyEdit on the client so that clients don't have to apply the edit
   138  	// themselves.
   139  	AddImport(context.Context, AddImportArgs) error
   140  
   141  	// StartDebugging: Start the gopls debug server
   142  	//
   143  	// Start the gopls debug server if it isn't running, and return the debug
   144  	// address.
   145  	StartDebugging(context.Context, DebuggingArgs) (DebuggingResult, error)
   146  
   147  	// RunVulncheckExp: Run vulncheck (experimental)
   148  	//
   149  	// Run vulnerability check (`govulncheck`).
   150  	RunVulncheckExp(context.Context, VulncheckArgs) (VulncheckResult, error)
   151  }
   152  
   153  type RunTestsArgs struct {
   154  	// The test file containing the tests to run.
   155  	URI protocol.DocumentURI
   156  
   157  	// Specific test names to run, e.g. TestFoo.
   158  	Tests []string
   159  
   160  	// Specific benchmarks to run, e.g. BenchmarkFoo.
   161  	Benchmarks []string
   162  }
   163  
   164  type GenerateArgs struct {
   165  	// URI for the directory to generate.
   166  	Dir protocol.DocumentURI
   167  
   168  	// Whether to generate recursively (go generate ./...)
   169  	Recursive bool
   170  }
   171  
   172  // TODO(rFindley): document the rest of these once the docgen is fleshed out.
   173  
   174  type ApplyFixArgs struct {
   175  	// The fix to apply.
   176  	Fix string
   177  	// The file URI for the document to fix.
   178  	URI protocol.DocumentURI
   179  	// The document range to scan for fixes.
   180  	Range protocol.Range
   181  }
   182  
   183  type URIArg struct {
   184  	// The file URI.
   185  	URI protocol.DocumentURI
   186  }
   187  
   188  type URIArgs struct {
   189  	// The file URIs.
   190  	URIs []protocol.DocumentURI
   191  }
   192  
   193  type CheckUpgradesArgs struct {
   194  	// The go.mod file URI.
   195  	URI protocol.DocumentURI
   196  	// The modules to check.
   197  	Modules []string
   198  }
   199  
   200  type DependencyArgs struct {
   201  	// The go.mod file URI.
   202  	URI protocol.DocumentURI
   203  	// Additional args to pass to the go command.
   204  	GoCmdArgs []string
   205  	// Whether to add a require directive.
   206  	AddRequire bool
   207  }
   208  
   209  type RemoveDependencyArgs struct {
   210  	// The go.mod file URI.
   211  	URI protocol.DocumentURI
   212  	// The module path to remove.
   213  	ModulePath     string
   214  	OnlyDiagnostic bool
   215  }
   216  
   217  type EditGoDirectiveArgs struct {
   218  	// Any document URI within the relevant module.
   219  	URI protocol.DocumentURI
   220  	// The version to pass to `go mod edit -go`.
   221  	Version string
   222  }
   223  
   224  type GoGetPackageArgs struct {
   225  	// Any document URI within the relevant module.
   226  	URI protocol.DocumentURI
   227  	// The package to go get.
   228  	Pkg        string
   229  	AddRequire bool
   230  }
   231  
   232  type AddImportArgs struct {
   233  	// ImportPath is the target import path that should
   234  	// be added to the URI file
   235  	ImportPath string
   236  	// URI is the file that the ImportPath should be
   237  	// added to
   238  	URI protocol.DocumentURI
   239  }
   240  
   241  type ListKnownPackagesResult struct {
   242  	// Packages is a list of packages relative
   243  	// to the URIArg passed by the command request.
   244  	// In other words, it omits paths that are already
   245  	// imported or cannot be imported due to compiler
   246  	// restrictions.
   247  	Packages []string
   248  }
   249  
   250  type ListImportsResult struct {
   251  	// Imports is a list of imports in the requested file.
   252  	Imports []FileImport
   253  
   254  	// PackageImports is a list of all imports in the requested file's package.
   255  	PackageImports []PackageImport
   256  }
   257  
   258  type FileImport struct {
   259  	// Path is the import path of the import.
   260  	Path string
   261  	// Name is the name of the import, e.g. `foo` in `import foo "strings"`.
   262  	Name string
   263  }
   264  
   265  type PackageImport struct {
   266  	// Path is the import path of the import.
   267  	Path string
   268  }
   269  
   270  type WorkspaceMetadataArgs struct {
   271  }
   272  
   273  type WorkspaceMetadataResult struct {
   274  	// All workspaces for this session.
   275  	Workspaces []Workspace
   276  }
   277  
   278  type Workspace struct {
   279  	// The workspace name.
   280  	Name string
   281  	// The workspace module directory.
   282  	ModuleDir string
   283  }
   284  
   285  type DebuggingArgs struct {
   286  	// Optional: the address (including port) for the debug server to listen on.
   287  	// If not provided, the debug server will bind to "localhost:0", and the
   288  	// full debug URL will be contained in the result.
   289  	//
   290  	// If there is more than one gopls instance along the serving path (i.e. you
   291  	// are using a daemon), each gopls instance will attempt to start debugging.
   292  	// If Addr specifies a port, only the daemon will be able to bind to that
   293  	// port, and each intermediate gopls instance will fail to start debugging.
   294  	// For this reason it is recommended not to specify a port (or equivalently,
   295  	// to specify ":0").
   296  	//
   297  	// If the server was already debugging this field has no effect, and the
   298  	// result will contain the previously configured debug URL(s).
   299  	Addr string
   300  }
   301  
   302  type DebuggingResult struct {
   303  	// The URLs to use to access the debug servers, for all gopls instances in
   304  	// the serving path. For the common case of a single gopls instance (i.e. no
   305  	// daemon), this will be exactly one address.
   306  	//
   307  	// In the case of one or more gopls instances forwarding the LSP to a daemon,
   308  	// URLs will contain debug addresses for each server in the serving path, in
   309  	// serving order. The daemon debug address will be the last entry in the
   310  	// slice. If any intermediate gopls instance fails to start debugging, no
   311  	// error will be returned but the debug URL for that server in the URLs slice
   312  	// will be empty.
   313  	URLs []string
   314  }
   315  
   316  type VulncheckArgs struct {
   317  	// Dir is the directory from which vulncheck will run from.
   318  	Dir protocol.DocumentURI
   319  
   320  	// Package pattern. E.g. "", ".", "./...".
   321  	Pattern string
   322  
   323  	// TODO: Flag []string (flags accepted by govulncheck, e.g., -tests)
   324  	// TODO: Format string (json, text)
   325  }
   326  
   327  type VulncheckResult struct {
   328  	Vuln []Vuln
   329  
   330  	// TODO: Text string format output?
   331  }
   332  
   333  // CallStack models a trace of function calls starting
   334  // with a client function or method and ending with a
   335  // call to a vulnerable symbol.
   336  type CallStack []StackEntry
   337  
   338  // StackEntry models an element of a call stack.
   339  type StackEntry struct {
   340  	// See golang.org/x/exp/vulncheck.StackEntry.
   341  
   342  	// User-friendly representation of function/method names.
   343  	// e.g. package.funcName, package.(recvType).methodName, ...
   344  	Name string
   345  	URI  protocol.DocumentURI
   346  	Pos  protocol.Position // Start position. (0-based. Column is always 0)
   347  }
   348  
   349  // Vuln models an osv.Entry and representative call stacks.
   350  type Vuln struct {
   351  	// ID is the vulnerability ID (osv.Entry.ID).
   352  	// https://ossf.github.io/osv-schema/#id-modified-fields
   353  	ID string
   354  	// Details is the description of the vulnerability (osv.Entry.Details).
   355  	// https://ossf.github.io/osv-schema/#summary-details-fields
   356  	Details string `json:",omitempty"`
   357  	// Aliases are alternative IDs of the vulnerability.
   358  	// https://ossf.github.io/osv-schema/#aliases-field
   359  	Aliases []string `json:",omitempty"`
   360  
   361  	// Symbol is the name of the detected vulnerable function or method.
   362  	Symbol string `json:",omitempty"`
   363  	// PkgPath is the package path of the detected Symbol.
   364  	PkgPath string `json:",omitempty"`
   365  	// ModPath is the module path corresponding to PkgPath.
   366  	// TODO: how do we specify standard library's vulnerability?
   367  	ModPath string `json:",omitempty"`
   368  
   369  	// URL is the URL for more info about the information.
   370  	// Either the database specific URL or the one of the URLs
   371  	// included in osv.Entry.References.
   372  	URL string `json:",omitempty"`
   373  
   374  	// Current is the current module version.
   375  	CurrentVersion string `json:",omitempty"`
   376  
   377  	// Fixed is the minimum module version that contains the fix.
   378  	FixedVersion string `json:",omitempty"`
   379  
   380  	// Example call stacks.
   381  	CallStacks []CallStack `json:",omitempty"`
   382  
   383  	// TODO: import graph & module graph.
   384  }