66package cache
77
88import (
9+ "bytes"
910 "context"
1011 "encoding/json"
1112 "fmt"
12- "io"
1313 "io/ioutil"
1414 "os"
1515 "path"
@@ -123,8 +123,10 @@ type workspaceInformation struct {
123123 // The Go version in use: X in Go 1.X.
124124 goversion int
125125
126- // The Go version reported by go version command. (e.g. go1.19.1, go1.20-rc.1, go1.21-abcdef01)
127- goversionString string
126+ // The complete output of the go version command.
127+ // (Call gocommand.ParseGoVersionOutput to extract a version
128+ // substring such as go1.19.1 or go1.20-rc.1, go1.21-abcdef01.)
129+ goversionOutput string
128130
129131 // hasGopackagesDriver is true if the user has a value set for the
130132 // GOPACKAGESDRIVER environment variable or a gopackagesdriver binary on
@@ -346,14 +348,28 @@ func (s *Session) SetViewOptions(ctx context.Context, v *View, options *source.O
346348 return newView , err
347349}
348350
349- func (s * snapshot ) WriteEnv (ctx context.Context , w io.Writer ) error {
350- s .view .optionsMu .Lock ()
351- env := s .view .options .EnvSlice ()
352- buildFlags := append ([]string {}, s .view .options .BuildFlags ... )
353- s .view .optionsMu .Unlock ()
351+ // viewEnv returns a string describing the environment of a newly created view.
352+ func viewEnv (v * View ) string {
353+ v .optionsMu .Lock ()
354+ env := v .options .EnvSlice ()
355+ buildFlags := append ([]string {}, v .options .BuildFlags ... )
356+ v .optionsMu .Unlock ()
357+
358+ var buf bytes.Buffer
359+ fmt .Fprintf (& buf , `go env for %v
360+ (root %s)
361+ (go version %s)
362+ (valid build configuration = %v)
363+ (build flags: %v)
364+ ` ,
365+ v .folder .Filename (),
366+ v .rootURI .Filename (),
367+ strings .TrimRight (v .workspaceInformation .goversionOutput , "\n " ),
368+ v .snapshot .ValidBuildConfiguration (),
369+ buildFlags )
354370
355371 fullEnv := make (map [string ]string )
356- for k , v := range s . view .goEnv {
372+ for k , v := range v .goEnv {
357373 fullEnv [k ] = v
358374 }
359375 for _ , v := range env {
@@ -365,29 +381,11 @@ func (s *snapshot) WriteEnv(ctx context.Context, w io.Writer) error {
365381 fullEnv [s [0 ]] = s [1 ]
366382 }
367383 }
368- goVersion , err := s .view .gocmdRunner .Run (ctx , gocommand.Invocation {
369- Verb : "version" ,
370- Env : env ,
371- WorkingDir : s .view .rootURI .Filename (),
372- })
373- if err != nil {
374- return err
375- }
376- fmt .Fprintf (w , `go env for %v
377- (root %s)
378- (go version %s)
379- (valid build configuration = %v)
380- (build flags: %v)
381- ` ,
382- s .view .folder .Filename (),
383- s .view .rootURI .Filename (),
384- strings .TrimRight (goVersion .String (), "\n " ),
385- s .ValidBuildConfiguration (),
386- buildFlags )
387384 for k , v := range fullEnv {
388- fmt .Fprintf (w , "%s=%s\n " , k , v )
385+ fmt .Fprintf (& buf , "%s=%s\n " , k , v )
389386 }
390- return nil
387+
388+ return buf .String ()
391389}
392390
393391func (s * snapshot ) RunProcessEnvFunc (ctx context.Context , fn func (* imports.Options ) error ) error {
@@ -816,7 +814,7 @@ func (s *Session) getWorkspaceInformation(ctx context.Context, folder span.URI,
816814 if err != nil {
817815 return nil , err
818816 }
819- goversionString , err := gocommand .GoVersionString (ctx , inv , s .gocmdRunner )
817+ goversionOutput , err := gocommand .GoVersionOutput (ctx , inv , s .gocmdRunner )
820818 if err != nil {
821819 return nil , err
822820 }
@@ -847,7 +845,7 @@ func (s *Session) getWorkspaceInformation(ctx context.Context, folder span.URI,
847845 return & workspaceInformation {
848846 hasGopackagesDriver : hasGopackagesDriver ,
849847 goversion : goversion ,
850- goversionString : goversionString ,
848+ goversionOutput : goversionOutput ,
851849 environmentVariables : envVars ,
852850 goEnv : env ,
853851 }, nil
@@ -1072,7 +1070,7 @@ func (v *View) GoVersion() int {
10721070}
10731071
10741072func (v * View ) GoVersionString () string {
1075- return v .workspaceInformation .goversionString
1073+ return gocommand . ParseGoVersionOutput ( v .workspaceInformation .goversionOutput )
10761074}
10771075
10781076// Copied from
0 commit comments