33 * Licensed under the MIT License. See LICENSE in the project root for license information.
44 *--------------------------------------------------------*/
55
6- import { ChildProcess , execFile , execSync , spawn , spawnSync } from 'child_process' ;
6+ import { ChildProcess , execFile , spawn } from 'child_process' ;
77import { EventEmitter } from 'events' ;
88import * as fs from 'fs' ;
99import { existsSync , lstatSync } from 'fs' ;
@@ -357,6 +357,7 @@ export class Delve {
357357 public dlvEnv : any ;
358358 public stackTraceDepth : number ;
359359 public isRemoteDebugging : boolean ;
360+ public goroot : string ;
360361 private localDebugeePath : string | undefined ;
361362 private debugProcess : ChildProcess ;
362363 private request : 'attach' | 'launch' ;
@@ -370,7 +371,7 @@ export class Delve {
370371 this . isApiV1 = launchArgs . apiVersion === 1 ;
371372 }
372373 this . stackTraceDepth = typeof launchArgs . stackTraceDepth === 'number' ? launchArgs . stackTraceDepth : 50 ;
373- this . connection = new Promise ( ( resolve , reject ) => {
374+ this . connection = new Promise ( async ( resolve , reject ) => {
374375 const mode = launchArgs . mode ;
375376 let dlvCwd = path . dirname ( program ) ;
376377 let serverRunning = false ;
@@ -391,6 +392,7 @@ export class Delve {
391392 log ( `Start remote debugging: connecting ${ launchArgs . host } :${ launchArgs . port } ` ) ;
392393 this . debugProcess = null ;
393394 this . isRemoteDebugging = true ;
395+ this . goroot = await queryGOROOT ( dlvCwd , process . env ) ;
394396 serverRunning = true ; // assume server is running when in remote mode
395397 connectClient ( launchArgs . port , launchArgs . host ) ;
396398 return ;
@@ -446,8 +448,11 @@ export class Delve {
446448 env [ 'GOPATH' ] = getInferredGopath ( dirname ) || env [ 'GOPATH' ] ;
447449 }
448450 this . dlvEnv = env ;
451+ this . goroot = await queryGOROOT ( dlvCwd , env ) ;
452+
449453 log ( `Using GOPATH: ${ env [ 'GOPATH' ] } ` ) ;
450- log ( `Using GOROOT: ${ env [ 'GOROOT' ] } ` ) ;
454+ log ( `Using GOROOT: ${ this . goroot } ` ) ;
455+ log ( `Using PATH: ${ env [ 'PATH' ] } ` ) ;
451456
452457 if ( ! ! launchArgs . noDebug ) {
453458 if ( mode === 'debug' ) {
@@ -1049,7 +1054,7 @@ export class GoDebugSession extends LoggingDebugSession {
10491054 protected inferLocalPathInGoRootFromRemoteGoPackage (
10501055 remotePathWithLocalSeparator : string , relativeRemotePath : string ) : string | undefined {
10511056 const srcIndex = remotePathWithLocalSeparator . indexOf ( `${ this . localPathSeparator } src${ this . localPathSeparator } ` ) ;
1052- const goroot = process . env [ 'GOROOT' ] || '' ;
1057+ const goroot = this . getGOROOT ( ) ;
10531058 const localGoRootImportPath = path . join (
10541059 goroot ,
10551060 srcIndex >= 0
@@ -1114,7 +1119,7 @@ export class GoDebugSession extends LoggingDebugSession {
11141119 if ( ! pathToConvert . startsWith ( this . delve . remotePath ) ) {
11151120 // Fix for https://github.com/Microsoft/vscode-go/issues/1178
11161121 const index = pathToConvert . indexOf ( `${ this . remotePathSeparator } src${ this . remotePathSeparator } ` ) ;
1117- const goroot = process . env [ 'GOROOT' ] ;
1122+ const goroot = this . getGOROOT ( ) ;
11181123 if ( goroot && index > 0 ) {
11191124 return path . join ( goroot , pathToConvert . substr ( index ) ) ;
11201125 }
@@ -1642,6 +1647,15 @@ export class GoDebugSession extends LoggingDebugSession {
16421647 } ) ;
16431648 }
16441649
1650+ private getGOROOT ( ) : string {
1651+ if ( this . delve && this . delve . goroot ) {
1652+ return this . delve . goroot ;
1653+ }
1654+ return process . env [ 'GOROOT' ] || '' ;
1655+ // this is a workaround to keep the tests in integration/goDebug.test.ts running.
1656+ // The tests synthesize a bogus Delve instance.
1657+ }
1658+
16451659 // contains common code for launch and attach debugging initialization
16461660 private initLaunchAttachRequest (
16471661 response : DebugProtocol . LaunchResponse ,
@@ -2317,4 +2331,20 @@ function killProcessTree(p: ChildProcess): Promise<void> {
23172331 } ) ;
23182332}
23192333
2334+ // queryGOROOT returns `go env GOROOT`.
2335+ function queryGOROOT ( cwd : any , env : any ) : Promise < string > {
2336+ return new Promise < string > ( ( resolve ) => {
2337+ execFile (
2338+ getBinPathWithPreferredGopath ( 'go' , [ ] ) ,
2339+ [ 'env' , 'GOROOT' ] ,
2340+ { cwd, env } ,
2341+ ( err , stdout , stderr ) => {
2342+ if ( err ) {
2343+ return resolve ( '' ) ;
2344+ }
2345+ return resolve ( stdout . trim ( ) ) ;
2346+ } ) ;
2347+ } ) ;
2348+ }
2349+
23202350DebugSession . run ( GoDebugSession ) ;
0 commit comments