golang.org/x/tools/gopls@v0.15.3/internal/lsprpc/goenv.go (about)

     1  // Copyright 2021 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package lsprpc
     6  
     7  import (
     8  	"context"
     9  	"encoding/json"
    10  	"fmt"
    11  
    12  	"golang.org/x/tools/internal/gocommand"
    13  )
    14  
    15  func getGoEnv(ctx context.Context, env map[string]interface{}) (map[string]string, error) {
    16  	var runEnv []string
    17  	for k, v := range env {
    18  		runEnv = append(runEnv, fmt.Sprintf("%s=%s", k, v))
    19  	}
    20  	runner := gocommand.Runner{}
    21  	output, err := runner.Run(ctx, gocommand.Invocation{
    22  		Verb: "env",
    23  		Args: []string{"-json"},
    24  		Env:  runEnv,
    25  	})
    26  	if err != nil {
    27  		return nil, err
    28  	}
    29  	envmap := make(map[string]string)
    30  	if err := json.Unmarshal(output.Bytes(), &envmap); err != nil {
    31  		return nil, err
    32  	}
    33  	return envmap, nil
    34  }