Skip to content

Commit cceaf96

Browse files
findleyrgopherbot
authored andcommitted
internal/imports: carve out a Source interface for index integration
In preparation for integrating the newly implemented shared module cache index, carve out a minimal imports.Source interface to be used by FixImports. For now, this interface has a single implementation, wrapping the legacy ProcessEnv abstraction, but in the future we can replace it with an implementation that synthesizes the module cache index with gopls' own view of the workspace. This CL intentionally avoids any refactoring aside from extracting the ProcessEnv-specific logic into a ProcessEnvSource. For golang/go#36077 Change-Id: I189a908c917aba68868b08845880b1f0aa731180 Reviewed-on: https://go-review.googlesource.com/c/tools/+/623296 Reviewed-by: Peter Weinberger <[email protected]> Reviewed-by: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Robert Findley <[email protected]>
1 parent f4878ba commit cceaf96

File tree

7 files changed

+295
-166
lines changed

7 files changed

+295
-166
lines changed

gopls/internal/cache/view.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ type GoEnv struct {
6969
GOFLAGS string
7070
GO111MODULE string
7171
GOTOOLCHAIN string
72+
GOROOT string
7273

7374
// Go version output.
7475
GoVersion int // The X in Go 1.X
@@ -998,6 +999,7 @@ func FetchGoEnv(ctx context.Context, folder protocol.DocumentURI, opts *settings
998999
"GOFLAGS": &env.GOFLAGS,
9991000
"GO111MODULE": &env.GO111MODULE,
10001001
"GOTOOLCHAIN": &env.GOTOOLCHAIN,
1002+
"GOROOT": &env.GOROOT,
10011003
}
10021004
if err := loadGoEnv(ctx, dir, opts.EnvSlice(), runner, envvars); err != nil {
10031005
return nil, err

gopls/internal/golang/format.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func allImportsFixes(ctx context.Context, snapshot *cache.Snapshot, pgf *parsego
120120
defer done()
121121

122122
if err := snapshot.RunProcessEnvFunc(ctx, func(ctx context.Context, opts *imports.Options) error {
123-
allFixEdits, editsPerFix, err = computeImportEdits(ctx, pgf, opts)
123+
allFixEdits, editsPerFix, err = computeImportEdits(ctx, pgf, snapshot.View().Folder().Env.GOROOT, opts)
124124
return err
125125
}); err != nil {
126126
return nil, nil, fmt.Errorf("allImportsFixes: %v", err)
@@ -130,11 +130,12 @@ func allImportsFixes(ctx context.Context, snapshot *cache.Snapshot, pgf *parsego
130130

131131
// computeImportEdits computes a set of edits that perform one or all of the
132132
// necessary import fixes.
133-
func computeImportEdits(ctx context.Context, pgf *parsego.File, options *imports.Options) (allFixEdits []protocol.TextEdit, editsPerFix []*importFix, err error) {
133+
func computeImportEdits(ctx context.Context, pgf *parsego.File, goroot string, options *imports.Options) (allFixEdits []protocol.TextEdit, editsPerFix []*importFix, err error) {
134134
filename := pgf.URI.Path()
135135

136136
// Build up basic information about the original file.
137-
allFixes, err := imports.FixImports(ctx, filename, pgf.Src, options)
137+
isource, err := imports.NewProcessEnvSource(options.Env, filename, pgf.File.Name.Name)
138+
allFixes, err := imports.FixImports(ctx, filename, pgf.Src, goroot, options.Env.Logf, isource)
138139
if err != nil {
139140
return nil, nil, err
140141
}

0 commit comments

Comments
 (0)