forked from golang/vscode-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goPackages.ts
327 lines (289 loc) · 10.9 KB
/
goPackages.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See LICENSE in the project root for license information.
*--------------------------------------------------------*/
import cp = require('child_process');
import path = require('path');
import vscode = require('vscode');
import { toolExecutionEnvironment } from './goEnv';
import { promptForMissingTool, promptForUpdatingTool } from './goInstallTools';
import { getBinPath, getCurrentGoPath, getGoVersion, isVendorSupported } from './util';
import { envPath, fixDriveCasingInWindows, getCurrentGoRoot, getCurrentGoWorkspaceFromGOPATH } from './utils/pathUtils';
type GopkgsDone = (res: Map<string, PackageInfo>) => void;
interface Cache {
entry: Map<string, PackageInfo>;
lastHit: number;
}
export interface PackageInfo {
name: string;
isStd: boolean;
}
let gopkgsNotified: boolean = false;
let cacheTimeout: number = 5000;
const gopkgsSubscriptions: Map<string, GopkgsDone[]> = new Map<string, GopkgsDone[]>();
const gopkgsRunning: Set<string> = new Set<string>();
const allPkgsCache: Map<string, Cache> = new Map<string, Cache>();
const pkgRootDirs = new Map<string, string>();
function gopkgs(workDir?: string): Promise<Map<string, PackageInfo>> {
const gopkgsBinPath = getBinPath('gopkgs');
if (!path.isAbsolute(gopkgsBinPath)) {
promptForMissingTool('gopkgs');
return Promise.resolve(new Map<string, PackageInfo>());
}
const t0 = Date.now();
return new Promise<Map<string, PackageInfo>>((resolve, reject) => {
const args = ['-format', '{{.Name}};{{.ImportPath}};{{.Dir}}'];
if (workDir) {
args.push('-workDir', workDir);
}
const env = toolExecutionEnvironment();
env['GOROOT'] = getCurrentGoRoot(); // https://github.com/golang/vscode-go/issues/294
const cmd = cp.spawn(gopkgsBinPath, args, { env, cwd: workDir });
const chunks: any[] = [];
const errchunks: any[] = [];
let err: any;
cmd.stdout.on('data', (d) => chunks.push(d));
cmd.stderr.on('data', (d) => errchunks.push(d));
cmd.on('error', (e) => (err = e));
cmd.on('close', () => {
const pkgs = new Map<string, PackageInfo>();
if (err && err.code === 'ENOENT') {
return promptForMissingTool('gopkgs');
}
const errorMsg = errchunks.join('').trim() || (err && err.message);
if (errorMsg) {
if (errorMsg.startsWith('flag provided but not defined: -workDir')) {
promptForUpdatingTool('gopkgs');
// fallback to gopkgs without -workDir
return gopkgs().then((result) => resolve(result));
}
console.log(
`Running gopkgs failed with "${errorMsg}"\nCheck if you can run \`gopkgs -format {{.Name}};{{.ImportPath}}\` in a terminal successfully.`
);
return resolve(pkgs);
}
const goroot = getCurrentGoRoot();
const output = chunks.join('');
if (output.indexOf(';') === -1) {
// User might be using the old gopkgs tool, prompt to update
promptForUpdatingTool('gopkgs');
output.split('\n').forEach((pkgPath) => {
if (!pkgPath || !pkgPath.trim()) {
return;
}
const index = pkgPath.lastIndexOf('/');
const pkgName = index === -1 ? pkgPath : pkgPath.substr(index + 1);
pkgs.set(pkgPath, {
name: pkgName,
isStd: !pkgPath.includes('.')
});
});
return resolve(pkgs);
}
output.split('\n').forEach((pkgDetail) => {
if (!pkgDetail || !pkgDetail.trim() || pkgDetail.indexOf(';') === -1) {
return;
}
const [pkgName, pkgPath, pkgDir] = pkgDetail.trim().split(';');
pkgs.set(pkgPath, {
name: pkgName,
isStd: goroot === null ? false : pkgDir.startsWith(goroot)
});
});
const timeTaken = Date.now() - t0;
cacheTimeout = timeTaken > 5000 ? timeTaken : 5000;
return resolve(pkgs);
});
});
}
function getAllPackagesNoCache(workDir: string): Promise<Map<string, PackageInfo>> {
return new Promise<Map<string, PackageInfo>>((resolve, reject) => {
// Use subscription style to guard costly/long running invocation
const callback = (pkgMap: Map<string, PackageInfo>) => {
resolve(pkgMap);
};
let subs = gopkgsSubscriptions.get(workDir);
if (!subs) {
subs = [];
gopkgsSubscriptions.set(workDir, subs);
}
subs.push(callback);
// Ensure only single gokpgs running
if (!gopkgsRunning.has(workDir)) {
gopkgsRunning.add(workDir);
gopkgs(workDir).then((pkgMap) => {
gopkgsRunning.delete(workDir);
gopkgsSubscriptions.delete(workDir);
subs.forEach((cb) => cb(pkgMap));
});
}
});
}
/**
* Runs gopkgs
* @argument workDir. The workspace directory of the project.
* @returns Map<string, string> mapping between package import path and package name
*/
export async function getAllPackages(workDir: string): Promise<Map<string, PackageInfo>> {
const cache = allPkgsCache.get(workDir);
const useCache = cache && new Date().getTime() - cache.lastHit < cacheTimeout;
if (useCache) {
cache.lastHit = new Date().getTime();
return Promise.resolve(cache.entry);
}
const pkgs = await getAllPackagesNoCache(workDir);
if (!pkgs || pkgs.size === 0) {
if (!gopkgsNotified) {
vscode.window.showInformationMessage(
'Could not find packages. Ensure `gopkgs -format {{.Name}};{{.ImportPath}}` runs successfully.'
);
gopkgsNotified = true;
}
}
allPkgsCache.set(workDir, {
entry: pkgs,
lastHit: new Date().getTime()
});
return pkgs;
}
/**
* Returns mapping of import path and package name for packages that can be imported
* Possible to return empty if useCache options is used.
* @param filePath. Used to determine the right relative path for vendor pkgs
* @param useCache. Force to use cache
* @returns Map<string, string> mapping between package import path and package name
*/
export function getImportablePackages(filePath: string, useCache: boolean = false): Promise<Map<string, PackageInfo>> {
filePath = fixDriveCasingInWindows(filePath);
const fileDirPath = path.dirname(filePath);
let foundPkgRootDir = pkgRootDirs.get(fileDirPath);
const workDir = foundPkgRootDir || fileDirPath;
const cache = allPkgsCache.get(workDir);
const getAllPackagesPromise: Promise<Map<string, PackageInfo>> =
useCache && cache ? Promise.race([getAllPackages(workDir), cache.entry]) : getAllPackages(workDir);
return Promise.all([isVendorSupported(), getAllPackagesPromise]).then(([vendorSupported, pkgs]) => {
const pkgMap = new Map<string, PackageInfo>();
if (!pkgs) {
return pkgMap;
}
const currentWorkspace = getCurrentGoWorkspaceFromGOPATH(getCurrentGoPath(), fileDirPath);
pkgs.forEach((info, pkgPath) => {
if (info.name === 'main') {
return;
}
if (!vendorSupported || !currentWorkspace) {
pkgMap.set(pkgPath, info);
return;
}
if (!foundPkgRootDir) {
// try to guess package root dir
const vendorIndex = pkgPath.indexOf('/vendor/');
if (vendorIndex !== -1) {
foundPkgRootDir = path.join(
currentWorkspace,
pkgPath.substring(0, vendorIndex).replace('/', path.sep)
);
pkgRootDirs.set(fileDirPath, foundPkgRootDir);
}
}
const relativePkgPath = getRelativePackagePath(fileDirPath, currentWorkspace, pkgPath);
if (!relativePkgPath) {
return;
}
const allowToImport = isAllowToImportPackage(fileDirPath, currentWorkspace, relativePkgPath);
if (allowToImport) {
pkgMap.set(relativePkgPath, info);
}
});
return pkgMap;
});
}
/**
* If given pkgPath is not vendor pkg, then the same pkgPath is returned
* Else, the import path for the vendor pkg relative to given filePath is returned.
*/
function getRelativePackagePath(currentFileDirPath: string, currentWorkspace: string, pkgPath: string): string {
let magicVendorString = '/vendor/';
let vendorIndex = pkgPath.indexOf(magicVendorString);
if (vendorIndex === -1) {
magicVendorString = 'vendor/';
if (pkgPath.startsWith(magicVendorString)) {
vendorIndex = 0;
}
}
// Check if current file and the vendor pkg belong to the same root project and not sub vendor
// If yes, then vendor pkg can be replaced with its relative path to the "vendor" folder
// If not, then the vendor pkg should not be allowed to be imported.
if (vendorIndex > -1) {
const rootProjectForVendorPkg = path.join(currentWorkspace, pkgPath.substr(0, vendorIndex));
const relativePathForVendorPkg = pkgPath.substring(vendorIndex + magicVendorString.length);
const subVendor = relativePathForVendorPkg.indexOf('/vendor/') !== -1;
if (relativePathForVendorPkg && currentFileDirPath.startsWith(rootProjectForVendorPkg) && !subVendor) {
return relativePathForVendorPkg;
}
return '';
}
return pkgPath;
}
const pkgToFolderMappingRegex = /ImportPath: (.*) FolderPath: (.*)/;
/**
* Returns mapping between import paths and folder paths for all packages under given folder (vendor will be excluded)
*/
export function getNonVendorPackages(
currentFolderPath: string, recursive: boolean = true): Promise<Map<string, string>> {
const target = recursive ? './...' : '.'; // go list ./... excludes vendor dirs since 1.9
return getImportPathToFolder([target], currentFolderPath);
}
export function getImportPathToFolder(targets: string[], cwd?: string): Promise<Map<string, string>> {
const goRuntimePath = getBinPath('go');
if (!goRuntimePath) {
console.warn(
`Failed to run "go list" to find packages as the "go" binary cannot be found in either GOROOT(${getCurrentGoRoot()}) PATH(${envPath})`
);
return;
}
return new Promise<Map<string, string>>((resolve, reject) => {
const childProcess = cp.spawn(
goRuntimePath,
['list', '-f', 'ImportPath: {{.ImportPath}} FolderPath: {{.Dir}}', ...targets],
{ cwd, env: toolExecutionEnvironment() }
);
const chunks: any[] = [];
childProcess.stdout.on('data', (stdout) => {
chunks.push(stdout);
});
childProcess.on('close', async (status) => {
const lines = chunks.join('').toString().split('\n');
const result = new Map<string, string>();
lines.forEach((line) => {
const matches = line.match(pkgToFolderMappingRegex);
if (!matches || matches.length !== 3) {
return;
}
const [_, pkgPath, folderPath] = matches;
if (!pkgPath) {
return;
}
result.set(pkgPath, folderPath);
});
resolve(result);
});
});
}
// This will check whether it's regular package or internal package
// Regular package will always allowed
// Internal package only allowed if the package doing the import is within the
// tree rooted at the parent of "internal" directory
// see: https://golang.org/doc/go1.4#internalpackages
// see: https://golang.org/s/go14internal
function isAllowToImportPackage(toDirPath: string, currentWorkspace: string, pkgPath: string) {
if (pkgPath.startsWith('internal/')) {
return false;
}
const internalPkgFound = pkgPath.match(/\/internal\/|\/internal$/);
if (internalPkgFound) {
const rootProjectForInternalPkg = path.join(currentWorkspace, pkgPath.substr(0, internalPkgFound.index));
return toDirPath.startsWith(rootProjectForInternalPkg + path.sep) || toDirPath === rootProjectForInternalPkg;
}
return true;
}