@@ -141,11 +141,11 @@ function installTools(goVersion: SemVersion, missing?: string[]) {
141141 }
142142
143143 // If the go.toolsGopath is set, use
144- // its value as the GOPATH for the "go get" child process.
144+ // its value as the GOPATH for the "go get" child process.
145145 let toolsGopath = getToolsGopath ( ) ;
146146 let envWithSeparateGoPathForTools = null ;
147147 if ( toolsGopath ) {
148- envWithSeparateGoPathForTools = Object . assign ( { } , envForTools , { GOPATH : toolsGopath } ) ;
148+ envWithSeparateGoPathForTools = Object . assign ( { } , envForTools , { GOPATH : toolsGopath } ) ;
149149 }
150150
151151 missing . reduce ( ( res : Promise < string [ ] > , tool : string ) => {
@@ -199,7 +199,7 @@ function installTools(goVersion: SemVersion, missing?: string[]) {
199199 } ) ;
200200}
201201
202- export function updateGoPathGoRootFromConfig ( ) {
202+ export function updateGoPathGoRootFromConfig ( ) : Promise < void > {
203203 let goroot = vscode . workspace . getConfiguration ( 'go' ) [ 'goroot' ] ;
204204 if ( goroot ) {
205205 process . env [ 'GOROOT' ] = goroot ;
@@ -220,10 +220,28 @@ export function updateGoPathGoRootFromConfig() {
220220 process . env [ 'GOPATH' ] = vscode . workspace . rootPath . substr ( 0 , dirs . slice ( 0 , srcIdx ) . join ( path . sep ) . length ) ;
221221 }
222222 }
223+
224+ if ( process . env [ 'GOPATH' ] ) {
225+ return Promise . resolve ( ) ;
226+ }
227+
228+ // From Go 1.8 onwards, when there is no GOPATH set, there is default GOPATH used which can be got from running `go env`
229+ let goRuntimePath = getGoRuntimePath ( ) ;
230+ return new Promise < void > ( ( resolve , reject ) => {
231+ cp . execFile ( goRuntimePath , [ 'env' ] , ( err , stdout , stderr ) => {
232+ if ( err ) {
233+ return reject ( ) ;
234+ }
235+ let gopathOutput = stdout . split ( '\n' ) . find ( ( value , index ) => { return value . startsWith ( 'GOPATH="' ) && value . trim ( ) . endsWith ( '"' ) ; } ) ;
236+ if ( gopathOutput ) {
237+ process . env [ 'GOPATH' ] = gopathOutput . trim ( ) . substring ( 'GOPATH="' . length , gopathOutput . length - 1 ) ;
238+ }
239+ return resolve ( ) ;
240+ } ) ;
241+ } ) ;
223242}
224243
225- export function setupGoPathAndOfferToInstallTools ( ) {
226- updateGoPathGoRootFromConfig ( ) ;
244+ export function offerToInstallTools ( ) {
227245 isVendorSupported ( ) ;
228246
229247 getGoVersion ( ) . then ( goVersion => {
0 commit comments