diff --git a/internal/lsp/cache/view.go b/internal/lsp/cache/view.go index 2aef2237..e18db6bc 100644 --- a/internal/lsp/cache/view.go +++ b/internal/lsp/cache/view.go @@ -48,6 +48,9 @@ type view struct { // env is the environment to use when invoking underlying tools. env []string + // buildFlags is the build flags to use when invoking underlying tools. + buildFlags []string + // keep track of files by uri and by basename, a single file may be mapped // to multiple uris, and the same basename may map to multiple files filesByURI map[span.URI]viewFile @@ -119,9 +122,10 @@ func (v *view) buildConfig() *packages.Config { folderPath = "" } return &packages.Config{ - Context: v.backgroundCtx, - Dir: folderPath, - Env: v.env, + Context: v.backgroundCtx, + Dir: folderPath, + Env: v.env, + BuildFlags: v.buildFlags, Mode: packages.NeedName | packages.NeedFiles | packages.NeedCompiledGoFiles | @@ -148,6 +152,12 @@ func (v *view) SetEnv(env []string) { v.env = env } +func (v *view) SetBuildFlags(buildFlags []string) { + v.mu.Lock() + defer v.mu.Unlock() + v.buildFlags = buildFlags +} + func (v *view) Shutdown(ctx context.Context) { v.session.removeView(ctx, v) } diff --git a/internal/lsp/general.go b/internal/lsp/general.go index 8ddbe595..0964de70 100644 --- a/internal/lsp/general.go +++ b/internal/lsp/general.go @@ -167,6 +167,18 @@ func (s *Server) processConfig(view source.View, config interface{}) error { } view.SetEnv(env) } + // Get the build flags for the go/packages config. + if buildFlags := c["buildFlags"]; buildFlags != nil { + iflags, ok := buildFlags.([]interface{}) + if !ok { + return fmt.Errorf("invalid config gopls.buildFlags type %T", buildFlags) + } + flags := make([]string, 0, len(iflags)) + for _, flag := range iflags { + flags = append(flags, fmt.Sprintf("%s", flag)) + } + view.SetBuildFlags(flags) + } // Check if placeholders are enabled. if usePlaceholders, ok := c["usePlaceholders"].(bool); ok { s.usePlaceholders = usePlaceholders diff --git a/internal/lsp/source/view.go b/internal/lsp/source/view.go index 90916462..8f7ce8cf 100644 --- a/internal/lsp/source/view.go +++ b/internal/lsp/source/view.go @@ -129,6 +129,9 @@ type View interface { // SetEnv is used to adjust the environment applied to the view. SetEnv([]string) + // SetBuildFlags is used to adjust the build flags applied to the view. + SetBuildFlags([]string) + // Shutdown closes this view, and detaches it from it's session. Shutdown(ctx context.Context)