Skip to content

Commit

Permalink
gopls/internal/settings: add a hidden option to disable zero config
Browse files Browse the repository at this point in the history
Fixes golang/go#65515

Change-Id: If9786c00eb7629ec8d488cf8483144514d28786b
Reviewed-on: https://go-review.googlesource.com/c/tools/+/562856
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
  • Loading branch information
findleyr committed Feb 9, 2024
1 parent 95f04f4 commit 730dc3c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion gopls/internal/cache/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ func selectViewDefs(ctx context.Context, fs file.Source, folders []*Folder, open
checkFiles:
for _, uri := range openFiles {
folder := folderForFile(uri)
if folder == nil {
if folder == nil || !folder.Options.ZeroConfig {
continue // only guess views for open files
}
fh, err := fs.ReadFile(ctx, uri)
Expand Down
1 change: 1 addition & 0 deletions gopls/internal/settings/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ func DefaultOptions(overrides ...func(*Options)) *Options {
TelemetryPrompt: false,
LinkifyShowMessage: false,
IncludeReplaceInWorkspace: true,
ZeroConfig: true,
},
Hooks: Hooks{
URLRegexp: urlRegexp(),
Expand Down
8 changes: 8 additions & 0 deletions gopls/internal/settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,11 @@ type InternalOptions struct {
// Or in other words, if a go.mod file with local replaces behaves like a
// go.work file.
IncludeReplaceInWorkspace bool

// ZeroConfig enables the zero-config algorithm for workspace layout,
// dynamically creating build configurations for different modules,
// directories, and GOOS/GOARCH combinations to cover open files.
ZeroConfig bool
}

type SubdirWatchPatterns string
Expand Down Expand Up @@ -1159,6 +1164,9 @@ func (o *Options) set(name string, value interface{}, seen map[string]struct{})
case "includeReplaceInWorkspace":
result.setBool(&o.IncludeReplaceInWorkspace)

case "zeroConfig":
result.setBool(&o.ZeroConfig)

// Replaced settings.
case "experimentalDisabledAnalyses":
result.deprecated("analyses")
Expand Down
34 changes: 34 additions & 0 deletions gopls/internal/test/integration/workspace/zero_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,37 @@ const B = 1
})
}
}

func TestDisableZeroConfig(t *testing.T) {
// This test checks that we treat locally replaced modules as workspace
// modules, according to the "includeReplaceInWorkspace" setting.
const files = `
-- moda/go.mod --
module golang.org/a
go 1.20
-- moda/a.go --
package a
-- modb/go.mod --
module golang.org/b
go 1.20
-- modb/b.go --
package b
`

WithOptions(
Settings{"zeroConfig": false},
).Run(t, files, func(t *testing.T, env *Env) {
env.OpenFile("moda/a.go")
env.OpenFile("modb/b.go")
env.AfterChange()
if got := env.Views(); len(got) != 1 || got[0].Type != cache.AdHocView.String() {
t.Errorf("Views: got %v, want one adhoc view", got)
}
})
}

0 comments on commit 730dc3c

Please # to comment.