@@ -10,12 +10,17 @@ import { promptForMissingTool, promptForUpdatingTool } from './goInstallTools';
10
10
import { envPath , fixDriveCasingInWindows , getCurrentGoWorkspaceFromGOPATH } from './goPath' ;
11
11
import { getBinPath , getCurrentGoPath , getGoVersion , getToolsEnvVars , isVendorSupported , sendTelemetryEvent } from './util' ;
12
12
13
- type GopkgsDone = ( res : Map < string , string > ) => void ;
13
+ type GopkgsDone = ( res : Map < string , PackageInfo > ) => void ;
14
14
interface Cache {
15
- entry : Map < string , string > ;
15
+ entry : Map < string , PackageInfo > ;
16
16
lastHit : number ;
17
17
}
18
18
19
+ export interface PackageInfo {
20
+ name : string ;
21
+ isStd : boolean ;
22
+ }
23
+
19
24
let gopkgsNotified : boolean = false ;
20
25
let cacheTimeout : number = 5000 ;
21
26
@@ -26,16 +31,16 @@ const allPkgsCache: Map<string, Cache> = new Map<string, Cache>();
26
31
27
32
const pkgRootDirs = new Map < string , string > ( ) ;
28
33
29
- function gopkgs ( workDir ?: string ) : Promise < Map < string , string > > {
34
+ function gopkgs ( workDir ?: string ) : Promise < Map < string , PackageInfo > > {
30
35
const gopkgsBinPath = getBinPath ( 'gopkgs' ) ;
31
36
if ( ! path . isAbsolute ( gopkgsBinPath ) ) {
32
37
promptForMissingTool ( 'gopkgs' ) ;
33
- return Promise . resolve ( new Map < string , string > ( ) ) ;
38
+ return Promise . resolve ( new Map < string , PackageInfo > ( ) ) ;
34
39
}
35
40
36
41
const t0 = Date . now ( ) ;
37
- return new Promise < Map < string , string > > ( ( resolve , reject ) => {
38
- const args = [ '-format' , '{{.Name}};{{.ImportPath}}' ] ;
42
+ return new Promise < Map < string , PackageInfo > > ( ( resolve , reject ) => {
43
+ const args = [ '-format' , '{{.Name}};{{.ImportPath}};{{.Dir}} ' ] ;
39
44
if ( workDir ) {
40
45
args . push ( '-workDir' , workDir ) ;
41
46
}
@@ -48,7 +53,7 @@ function gopkgs(workDir?: string): Promise<Map<string, string>> {
48
53
cmd . stderr . on ( 'data' , d => errchunks . push ( d ) ) ;
49
54
cmd . on ( 'error' , e => err = e ) ;
50
55
cmd . on ( 'close' , ( ) => {
51
- const pkgs = new Map < string , string > ( ) ;
56
+ const pkgs = new Map < string , PackageInfo > ( ) ;
52
57
if ( err && err . code === 'ENOENT' ) {
53
58
return promptForMissingTool ( 'gopkgs' ) ;
54
59
}
@@ -64,7 +69,7 @@ function gopkgs(workDir?: string): Promise<Map<string, string>> {
64
69
console . log ( `Running gopkgs failed with "${ errorMsg } "\nCheck if you can run \`gopkgs -format {{.Name}};{{.ImportPath}}\` in a terminal successfully.` ) ;
65
70
return resolve ( pkgs ) ;
66
71
}
67
-
72
+ const goroot = process . env [ 'GOROOT' ] ;
68
73
const output = chunks . join ( '' ) ;
69
74
if ( output . indexOf ( ';' ) === - 1 ) {
70
75
// User might be using the old gopkgs tool, prompt to update
@@ -75,19 +80,23 @@ function gopkgs(workDir?: string): Promise<Map<string, string>> {
75
80
}
76
81
const index = pkgPath . lastIndexOf ( '/' ) ;
77
82
const pkgName = index === - 1 ? pkgPath : pkgPath . substr ( index + 1 ) ;
78
- pkgs . set ( pkgPath , pkgName ) ;
83
+ pkgs . set ( pkgPath , {
84
+ name : pkgName ,
85
+ isStd : ! pkgPath . includes ( '.' )
86
+ } ) ;
79
87
} ) ;
80
88
return resolve ( pkgs ) ;
81
89
}
82
-
83
90
output . split ( '\n' ) . forEach ( ( pkgDetail ) => {
84
91
if ( ! pkgDetail || ! pkgDetail . trim ( ) || pkgDetail . indexOf ( ';' ) === - 1 ) {
85
92
return ;
86
93
}
87
- const [ pkgName , pkgPath ] = pkgDetail . trim ( ) . split ( ';' ) ;
88
- pkgs . set ( pkgPath , pkgName ) ;
94
+ const [ pkgName , pkgPath , pkgDir ] = pkgDetail . trim ( ) . split ( ';' ) ;
95
+ pkgs . set ( pkgPath , {
96
+ name : pkgName ,
97
+ isStd : goroot === null ? false : pkgDir . startsWith ( goroot )
98
+ } ) ;
89
99
} ) ;
90
-
91
100
const timeTaken = Date . now ( ) - t0 ;
92
101
/* __GDPR__
93
102
"gopkgs" : {
@@ -102,10 +111,10 @@ function gopkgs(workDir?: string): Promise<Map<string, string>> {
102
111
} ) ;
103
112
}
104
113
105
- function getAllPackagesNoCache ( workDir : string ) : Promise < Map < string , string > > {
106
- return new Promise < Map < string , string > > ( ( resolve , reject ) => {
114
+ function getAllPackagesNoCache ( workDir : string ) : Promise < Map < string , PackageInfo > > {
115
+ return new Promise < Map < string , PackageInfo > > ( ( resolve , reject ) => {
107
116
// Use subscription style to guard costly/long running invocation
108
- const callback = function ( pkgMap : Map < string , string > ) {
117
+ const callback = function ( pkgMap : Map < string , PackageInfo > ) {
109
118
resolve ( pkgMap ) ;
110
119
} ;
111
120
@@ -134,7 +143,7 @@ function getAllPackagesNoCache(workDir: string): Promise<Map<string, string>> {
134
143
* @argument workDir. The workspace directory of the project.
135
144
* @returns Map<string, string> mapping between package import path and package name
136
145
*/
137
- export async function getAllPackages ( workDir : string ) : Promise < Map < string , string > > {
146
+ export async function getAllPackages ( workDir : string ) : Promise < Map < string , PackageInfo > > {
138
147
const cache = allPkgsCache . get ( workDir ) ;
139
148
const useCache = cache && ( new Date ( ) . getTime ( ) - cache . lastHit ) < cacheTimeout ;
140
149
if ( useCache ) {
@@ -163,32 +172,32 @@ export async function getAllPackages(workDir: string): Promise<Map<string, strin
163
172
* @param useCache. Force to use cache
164
173
* @returns Map<string, string> mapping between package import path and package name
165
174
*/
166
- export function getImportablePackages ( filePath : string , useCache : boolean = false ) : Promise < Map < string , string > > {
175
+ export function getImportablePackages ( filePath : string , useCache : boolean = false ) : Promise < Map < string , PackageInfo > > {
167
176
filePath = fixDriveCasingInWindows ( filePath ) ;
168
177
const fileDirPath = path . dirname ( filePath ) ;
169
178
170
179
let foundPkgRootDir = pkgRootDirs . get ( fileDirPath ) ;
171
180
const workDir = foundPkgRootDir || fileDirPath ;
172
181
const cache = allPkgsCache . get ( workDir ) ;
173
182
174
- const getAllPackagesPromise : Promise < Map < string , string > > = useCache && cache
183
+ const getAllPackagesPromise : Promise < Map < string , PackageInfo > > = useCache && cache
175
184
? Promise . race ( [ getAllPackages ( workDir ) , cache . entry ] )
176
185
: getAllPackages ( workDir ) ;
177
186
178
187
return Promise . all ( [ isVendorSupported ( ) , getAllPackagesPromise ] ) . then ( ( [ vendorSupported , pkgs ] ) => {
179
- const pkgMap = new Map < string , string > ( ) ;
188
+ const pkgMap = new Map < string , PackageInfo > ( ) ;
180
189
if ( ! pkgs ) {
181
190
return pkgMap ;
182
191
}
183
192
184
193
const currentWorkspace = getCurrentGoWorkspaceFromGOPATH ( getCurrentGoPath ( ) , fileDirPath ) ;
185
- pkgs . forEach ( ( pkgName , pkgPath ) => {
186
- if ( pkgName === 'main' ) {
194
+ pkgs . forEach ( ( info , pkgPath ) => {
195
+ if ( info . name === 'main' ) {
187
196
return ;
188
197
}
189
198
190
199
if ( ! vendorSupported || ! currentWorkspace ) {
191
- pkgMap . set ( pkgPath , pkgName ) ;
200
+ pkgMap . set ( pkgPath , info ) ;
192
201
return ;
193
202
}
194
203
@@ -208,7 +217,7 @@ export function getImportablePackages(filePath: string, useCache: boolean = fals
208
217
209
218
const allowToImport = isAllowToImportPackage ( fileDirPath , currentWorkspace , relativePkgPath ) ;
210
219
if ( allowToImport ) {
211
- pkgMap . set ( relativePkgPath , pkgName ) ;
220
+ pkgMap . set ( relativePkgPath , info ) ;
212
221
}
213
222
} ) ;
214
223
return pkgMap ;
0 commit comments