6
6
package cache
7
7
8
8
import (
9
+ "bytes"
9
10
"context"
10
11
"encoding/json"
11
12
"fmt"
12
- "io"
13
13
"io/ioutil"
14
14
"os"
15
15
"path"
@@ -123,8 +123,10 @@ type workspaceInformation struct {
123
123
// The Go version in use: X in Go 1.X.
124
124
goversion int
125
125
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
128
130
129
131
// hasGopackagesDriver is true if the user has a value set for the
130
132
// GOPACKAGESDRIVER environment variable or a gopackagesdriver binary on
@@ -346,14 +348,28 @@ func (s *Session) SetViewOptions(ctx context.Context, v *View, options *source.O
346
348
return newView , err
347
349
}
348
350
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 )
354
370
355
371
fullEnv := make (map [string ]string )
356
- for k , v := range s . view .goEnv {
372
+ for k , v := range v .goEnv {
357
373
fullEnv [k ] = v
358
374
}
359
375
for _ , v := range env {
@@ -365,29 +381,11 @@ func (s *snapshot) WriteEnv(ctx context.Context, w io.Writer) error {
365
381
fullEnv [s [0 ]] = s [1 ]
366
382
}
367
383
}
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 )
387
384
for k , v := range fullEnv {
388
- fmt .Fprintf (w , "%s=%s\n " , k , v )
385
+ fmt .Fprintf (& buf , "%s=%s\n " , k , v )
389
386
}
390
- return nil
387
+
388
+ return buf .String ()
391
389
}
392
390
393
391
func (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,
816
814
if err != nil {
817
815
return nil , err
818
816
}
819
- goversionString , err := gocommand .GoVersionString (ctx , inv , s .gocmdRunner )
817
+ goversionOutput , err := gocommand .GoVersionOutput (ctx , inv , s .gocmdRunner )
820
818
if err != nil {
821
819
return nil , err
822
820
}
@@ -847,7 +845,7 @@ func (s *Session) getWorkspaceInformation(ctx context.Context, folder span.URI,
847
845
return & workspaceInformation {
848
846
hasGopackagesDriver : hasGopackagesDriver ,
849
847
goversion : goversion ,
850
- goversionString : goversionString ,
848
+ goversionOutput : goversionOutput ,
851
849
environmentVariables : envVars ,
852
850
goEnv : env ,
853
851
}, nil
@@ -1072,7 +1070,7 @@ func (v *View) GoVersion() int {
1072
1070
}
1073
1071
1074
1072
func (v * View ) GoVersionString () string {
1075
- return v .workspaceInformation .goversionString
1073
+ return gocommand . ParseGoVersionOutput ( v .workspaceInformation .goversionOutput )
1076
1074
}
1077
1075
1078
1076
// Copied from
0 commit comments