3
3
* Licensed under the MIT License. See LICENSE in the project root for license information.
4
4
*--------------------------------------------------------*/
5
5
6
- import { ChildProcess , execFile , execSync , spawn , spawnSync } from 'child_process' ;
6
+ import { ChildProcess , execFile , spawn } from 'child_process' ;
7
7
import { EventEmitter } from 'events' ;
8
8
import * as fs from 'fs' ;
9
9
import { existsSync , lstatSync } from 'fs' ;
@@ -357,6 +357,7 @@ export class Delve {
357
357
public dlvEnv : any ;
358
358
public stackTraceDepth : number ;
359
359
public isRemoteDebugging : boolean ;
360
+ public goroot : string ;
360
361
private localDebugeePath : string | undefined ;
361
362
private debugProcess : ChildProcess ;
362
363
private request : 'attach' | 'launch' ;
@@ -370,7 +371,7 @@ export class Delve {
370
371
this . isApiV1 = launchArgs . apiVersion === 1 ;
371
372
}
372
373
this . stackTraceDepth = typeof launchArgs . stackTraceDepth === 'number' ? launchArgs . stackTraceDepth : 50 ;
373
- this . connection = new Promise ( ( resolve , reject ) => {
374
+ this . connection = new Promise ( async ( resolve , reject ) => {
374
375
const mode = launchArgs . mode ;
375
376
let dlvCwd = path . dirname ( program ) ;
376
377
let serverRunning = false ;
@@ -391,6 +392,7 @@ export class Delve {
391
392
log ( `Start remote debugging: connecting ${ launchArgs . host } :${ launchArgs . port } ` ) ;
392
393
this . debugProcess = null ;
393
394
this . isRemoteDebugging = true ;
395
+ this . goroot = await queryGOROOT ( dlvCwd , process . env ) ;
394
396
serverRunning = true ; // assume server is running when in remote mode
395
397
connectClient ( launchArgs . port , launchArgs . host ) ;
396
398
return ;
@@ -446,8 +448,11 @@ export class Delve {
446
448
env [ 'GOPATH' ] = getInferredGopath ( dirname ) || env [ 'GOPATH' ] ;
447
449
}
448
450
this . dlvEnv = env ;
451
+ this . goroot = await queryGOROOT ( dlvCwd , env ) ;
452
+
449
453
log ( `Using GOPATH: ${ env [ 'GOPATH' ] } ` ) ;
450
- log ( `Using GOROOT: ${ env [ 'GOROOT' ] } ` ) ;
454
+ log ( `Using GOROOT: ${ this . goroot } ` ) ;
455
+ log ( `Using PATH: ${ env [ 'PATH' ] } ` ) ;
451
456
452
457
if ( ! ! launchArgs . noDebug ) {
453
458
if ( mode === 'debug' ) {
@@ -1049,7 +1054,7 @@ export class GoDebugSession extends LoggingDebugSession {
1049
1054
protected inferLocalPathInGoRootFromRemoteGoPackage (
1050
1055
remotePathWithLocalSeparator : string , relativeRemotePath : string ) : string | undefined {
1051
1056
const srcIndex = remotePathWithLocalSeparator . indexOf ( `${ this . localPathSeparator } src${ this . localPathSeparator } ` ) ;
1052
- const goroot = process . env [ 'GOROOT' ] || '' ;
1057
+ const goroot = this . getGOROOT ( ) ;
1053
1058
const localGoRootImportPath = path . join (
1054
1059
goroot ,
1055
1060
srcIndex >= 0
@@ -1114,7 +1119,7 @@ export class GoDebugSession extends LoggingDebugSession {
1114
1119
if ( ! pathToConvert . startsWith ( this . delve . remotePath ) ) {
1115
1120
// Fix for https://github.com/Microsoft/vscode-go/issues/1178
1116
1121
const index = pathToConvert . indexOf ( `${ this . remotePathSeparator } src${ this . remotePathSeparator } ` ) ;
1117
- const goroot = process . env [ 'GOROOT' ] ;
1122
+ const goroot = this . getGOROOT ( ) ;
1118
1123
if ( goroot && index > 0 ) {
1119
1124
return path . join ( goroot , pathToConvert . substr ( index ) ) ;
1120
1125
}
@@ -1642,6 +1647,15 @@ export class GoDebugSession extends LoggingDebugSession {
1642
1647
} ) ;
1643
1648
}
1644
1649
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
+
1645
1659
// contains common code for launch and attach debugging initialization
1646
1660
private initLaunchAttachRequest (
1647
1661
response : DebugProtocol . LaunchResponse ,
@@ -2317,4 +2331,20 @@ function killProcessTree(p: ChildProcess): Promise<void> {
2317
2331
} ) ;
2318
2332
}
2319
2333
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
+
2320
2350
DebugSession . run ( GoDebugSession ) ;
0 commit comments