@@ -151,11 +151,11 @@ export async function activate(ctx: vscode.ExtensionContext) {
151
151
152
152
const configGOROOT = getGoConfig ( ) [ 'goroot' ] ;
153
153
if ( configGOROOT ) {
154
- const goroot = resolvePath ( configGOROOT ) ;
155
- if ( dirExists ( goroot ) ) {
156
- logVerbose ( `setting GOROOT = ${ goroot } because "go.goroot": " ${ configGOROOT } "` ) ;
157
- process . env [ 'GOROOT' ] = goroot ;
158
- }
154
+ // We don't support unsetting go. goroot because we don't know whether
155
+ // !configGOROOT case indicates the user wants to unset process.env['GOROOT']
156
+ // or the user wants the extension to use the current process.env['GOROOT'] value.
157
+ // TODO(hyangah): consider utilizing an empty value to indicate unset?
158
+ await setGOROOTEnvVar ( configGOROOT ) ;
159
159
}
160
160
161
161
// Present a warning about the deprecation of the go.documentLink setting.
@@ -440,7 +440,7 @@ If you would like additional configuration for diagnostics from gopls, please se
440
440
) ;
441
441
442
442
ctx . subscriptions . push (
443
- vscode . workspace . onDidChangeConfiguration ( ( e : vscode . ConfigurationChangeEvent ) => {
443
+ vscode . workspace . onDidChangeConfiguration ( async ( e : vscode . ConfigurationChangeEvent ) => {
444
444
if ( ! e . affectsConfiguration ( 'go' ) ) {
445
445
return ;
446
446
}
@@ -449,12 +449,7 @@ If you would like additional configuration for diagnostics from gopls, please se
449
449
if ( e . affectsConfiguration ( 'go.goroot' ) ) {
450
450
const configGOROOT = updatedGoConfig [ 'goroot' ] ;
451
451
if ( configGOROOT ) {
452
- const goroot = resolvePath ( configGOROOT ) ;
453
- const oldGoroot = process . env [ 'GOROOT' ] ;
454
- if ( oldGoroot !== goroot && dirExists ( goroot ) ) {
455
- logVerbose ( `setting GOROOT = ${ goroot } because "go.goroot": "${ configGOROOT } "` ) ;
456
- process . env [ 'GOROOT' ] = goroot ;
457
- }
452
+ await setGOROOTEnvVar ( configGOROOT ) ;
458
453
}
459
454
}
460
455
if (
@@ -997,3 +992,42 @@ function lintDiagnosticCollectionName(lintToolName: string) {
997
992
}
998
993
return `go-${ lintToolName } ` ;
999
994
}
995
+
996
+ // set GOROOT env var. If necessary, shows a warning.
997
+ export async function setGOROOTEnvVar ( configGOROOT : string ) {
998
+ if ( ! configGOROOT ) {
999
+ return ;
1000
+ }
1001
+ const goroot = configGOROOT ? resolvePath ( configGOROOT ) : undefined ;
1002
+
1003
+ const currentGOROOT = process . env [ 'GOROOT' ] ;
1004
+ if ( goroot === currentGOROOT ) {
1005
+ return ;
1006
+ }
1007
+ if ( ! ( await dirExists ( goroot ) ) ) {
1008
+ vscode . window . showWarningMessage ( `go.goroot setting is ignored. ${ goroot } is not a valid GOROOT directory.` ) ;
1009
+ return ;
1010
+ }
1011
+ const neverAgain = { title : "Don't Show Again" } ;
1012
+ const ignoreGOROOTSettingWarningKey = 'ignoreGOROOTSettingWarning' ;
1013
+ const ignoreGOROOTSettingWarning = getFromGlobalState ( ignoreGOROOTSettingWarningKey ) ;
1014
+ if ( ! ignoreGOROOTSettingWarning ) {
1015
+ vscode . window
1016
+ . showInformationMessage (
1017
+ `"go.goroot" setting (${ goroot } ) will be applied and set the GOROOT environment variable.` ,
1018
+ neverAgain
1019
+ )
1020
+ . then ( ( result ) => {
1021
+ if ( result === neverAgain ) {
1022
+ updateGlobalState ( ignoreGOROOTSettingWarningKey , true ) ;
1023
+ }
1024
+ } ) ;
1025
+ }
1026
+
1027
+ logVerbose ( `setting GOROOT = ${ goroot } (old value: ${ currentGOROOT } ) because "go.goroot": "${ configGOROOT } "` ) ;
1028
+ if ( goroot ) {
1029
+ process . env [ 'GOROOT' ] = goroot ;
1030
+ } else {
1031
+ delete process . env . GOROOT ;
1032
+ }
1033
+ }
0 commit comments