-
Notifications
You must be signed in to change notification settings - Fork 758
/
util.ts
1140 lines (1025 loc) · 34.1 KB
/
util.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* eslint-disable @typescript-eslint/no-explicit-any */
/*---------------------------------------------------------
* 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 fs = require('fs');
import os = require('os');
import path = require('path');
import semver = require('semver');
import util = require('util');
import vscode = require('vscode');
import { NearestNeighborDict, Node } from './avlTree';
import { DefaultConfig, getGoConfig } from './config';
import { extensionId } from './const';
import { toolExecutionEnvironment } from './goEnv';
import { languageClient } from './goLanguageServer';
import { buildDiagnosticCollection, lintDiagnosticCollection, vetDiagnosticCollection } from './goMain';
import { getCurrentPackage } from './goModules';
import { outputChannel } from './goStatus';
import { getFromWorkspaceState } from './stateUtils';
import {
envPath,
fixDriveCasingInWindows,
getBinPathWithPreferredGopathGorootWithExplanation,
getCurrentGoRoot,
getInferredGopath,
resolveHomeDir
} from './utils/pathUtils';
import { killProcessTree } from './utils/processUtils';
let userNameHash = 0;
export const goKeywords: string[] = [
'break',
'case',
'chan',
'const',
'continue',
'default',
'defer',
'else',
'fallthrough',
'for',
'func',
'go',
'goto',
'if',
'import',
'interface',
'map',
'package',
'range',
'return',
'select',
'struct',
'switch',
'type',
'var'
];
export const goBuiltinTypes: Set<string> = new Set<string>([
'bool',
'byte',
'complex128',
'complex64',
'error',
'float32',
'float64',
'int',
'int16',
'int32',
'int64',
'int8',
'rune',
'string',
'uint',
'uint16',
'uint32',
'uint64',
'uint8',
'uintptr'
]);
export class GoVersion {
public sv?: semver.SemVer;
// Go version tags are not following the strict semver format
// so semver drops the prerelease tags used in Go version.
// If sv is valid, let's keep the original version string
// including the prerelease tag parts.
public svString?: string;
public isDevel?: boolean;
private commit?: string;
constructor(public binaryPath: string, public version: string) {
const matchesRelease = /^go version go(\d\.\d+\S*)\s+/.exec(version);
const matchesDevel = /go version devel \+(.[a-zA-Z0-9]+).*/.exec(version);
if (matchesRelease) {
// note: semver.parse does not work with Go version string like go1.14.
const sv = semver.coerce(matchesRelease[1]);
if (sv) {
this.sv = sv;
this.svString = matchesRelease[1];
}
} else if (matchesDevel) {
this.isDevel = true;
this.commit = matchesDevel[1];
}
}
public isValid(): boolean {
return !!this.sv || !!this.isDevel;
}
public format(includePrerelease?: boolean): string {
if (this.sv) {
if (includePrerelease && this.svString) {
return this.svString;
}
return this.sv.format();
}
if (this.isDevel) {
return `devel +${this.commit}`;
}
return 'unknown';
}
public lt(version: string): boolean {
// Assume a developer version is always above any released version.
// This is not necessarily true.
if (this.isDevel || !this.sv) {
return false;
}
const v = semver.coerce(version);
if (!v) {
return false;
}
return semver.lt(this.sv, v);
}
public gt(version: string): boolean {
// Assume a developer version is always above any released version.
// This is not necessarily true.
if (this.isDevel || !this.sv) {
return true;
}
const v = semver.coerce(version);
if (!v) {
return false;
}
return semver.gt(this.sv, v);
}
}
let cachedGoBinPath: string | undefined;
let cachedGoVersion: GoVersion | undefined;
let vendorSupport: boolean | undefined;
let toolsGopath: string;
// getCheckForToolsUpdatesConfig returns go.toolsManagement.checkForUpdates configuration.
export function getCheckForToolsUpdatesConfig(gocfg: vscode.WorkspaceConfiguration) {
// useGoProxyToCheckForToolUpdates deprecation
// TODO: Step 1. mark as deprecated in Dec 2020 release, and update dev containers.
// Step 2. prompt users to switch config. Jan 2020
// Step 3. delete useGoProxyToCheckForToolUpdates support. Feb 2020
const legacyCfg = gocfg.get('useGoProxyToCheckForToolUpdates');
if (legacyCfg === false) {
const cfg = gocfg.inspect('toolsManagement.checkForUpdates');
if (cfg.globalValue === undefined && cfg.workspaceValue === undefined) {
return 'local';
}
}
return gocfg.get('toolsManagement.checkForUpdates') as string;
}
export function byteOffsetAt(document: vscode.TextDocument, position: vscode.Position): number {
const offset = document.offsetAt(position);
const text = document.getText();
return Buffer.byteLength(text.substr(0, offset));
}
export interface Prelude {
imports: Array<{ kind: string; start: number; end: number; pkgs: string[] }>;
pkg: { start: number; end: number; name: string };
}
export function parseFilePrelude(text: string): Prelude {
const lines = text.split('\n');
const ret: Prelude = { imports: [], pkg: null };
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
const pkgMatch = line.match(/^(\s)*package(\s)+(\w+)/);
if (pkgMatch) {
ret.pkg = { start: i, end: i, name: pkgMatch[3] };
}
if (line.match(/^(\s)*import(\s)+\(/)) {
ret.imports.push({ kind: 'multi', start: i, end: -1, pkgs: [] });
} else if (line.match(/^\s*import\s+"C"/)) {
ret.imports.push({ kind: 'pseudo', start: i, end: i, pkgs: [] });
} else if (line.match(/^(\s)*import(\s)+[^(]/)) {
ret.imports.push({ kind: 'single', start: i, end: i, pkgs: [] });
}
if (line.match(/^(\s)*(\/\*.*\*\/)*\s*\)/)) {
// /* comments */
if (ret.imports[ret.imports.length - 1].end === -1) {
ret.imports[ret.imports.length - 1].end = i;
}
} else if (ret.imports.length) {
if (ret.imports[ret.imports.length - 1].end === -1) {
const importPkgMatch = line.match(/"([^"]+)"/);
if (importPkgMatch) {
ret.imports[ret.imports.length - 1].pkgs.push(importPkgMatch[1]);
}
}
}
if (line.match(/^(\s)*(func|const|type|var)\s/)) {
break;
}
}
return ret;
}
// Takes a Go function signature like:
// (foo, bar string, baz number) (string, string)
// and returns an array of parameter strings:
// ["foo", "bar string", "baz string"]
// Takes care of balancing parens so to not get confused by signatures like:
// (pattern string, handler func(ResponseWriter, *Request)) {
export function getParametersAndReturnType(signature: string): { params: string[]; returnType: string } {
const params: string[] = [];
let parenCount = 0;
let lastStart = 1;
for (let i = 1; i < signature.length; i++) {
switch (signature[i]) {
case '(':
parenCount++;
break;
case ')':
parenCount--;
if (parenCount < 0) {
if (i > lastStart) {
params.push(signature.substring(lastStart, i));
}
return {
params,
returnType: i < signature.length - 1 ? signature.substr(i + 1) : ''
};
}
break;
case ',':
if (parenCount === 0) {
params.push(signature.substring(lastStart, i));
lastStart = i + 2;
}
break;
}
}
return { params: [], returnType: '' };
}
export function canonicalizeGOPATHPrefix(filename: string): string {
const gopath: string = getCurrentGoPath();
if (!gopath) {
return filename;
}
const workspaces = gopath.split(path.delimiter);
const filenameLowercase = filename.toLowerCase();
// In case of multiple workspaces, find current workspace by checking if current file is
// under any of the workspaces in $GOPATH
let currentWorkspace: string = null;
for (const workspace of workspaces) {
// In case of nested workspaces, (example: both /Users/me and /Users/me/a/b/c are in $GOPATH)
// both parent & child workspace in the nested workspaces pair can make it inside the above if block
// Therefore, the below check will take longer (more specific to current file) of the two
if (
filenameLowercase.substring(0, workspace.length) === workspace.toLowerCase() &&
(!currentWorkspace || workspace.length > currentWorkspace.length)
) {
currentWorkspace = workspace;
}
}
if (!currentWorkspace) {
return filename;
}
return currentWorkspace + filename.slice(currentWorkspace.length);
}
/**
* Gets a numeric hash based on given string.
* Returns a number between 0 and 4294967295.
*/
export function getStringHash(value: string): number {
let hash = 5381;
let i = value.length;
while (i) {
hash = (hash * 33) ^ value.charCodeAt(--i);
}
/* JavaScript does bitwise operations (like XOR, above) on 32-bit signed
* integers. Since we want the results to be always positive, convert the
* signed int to an unsigned by doing an unsigned bitshift. */
return hash >>> 0;
}
export function getUserNameHash() {
if (userNameHash) {
return userNameHash;
}
try {
userNameHash = getStringHash(os.userInfo().username);
} catch (error) {
userNameHash = 1;
}
return userNameHash;
}
/**
* Gets version of Go based on the output of the command `go version`.
* Returns undefined if go version can't be determined because
* go is not available or `go version` fails.
*/
export async function getGoVersion(goBinPath?: string): Promise<GoVersion | undefined> {
// TODO(hyangah): limit the number of concurrent getGoVersion call.
// When the extension starts, at least 4 concurrent calls race
// and end up calling `go version`.
const goRuntimePath = goBinPath ?? getBinPath('go');
const warn = (msg: string) => {
outputChannel.appendLine(msg);
console.warn(msg);
};
if (!goRuntimePath) {
warn(`unable to locate "go" binary in GOROOT (${getCurrentGoRoot()}) or PATH (${envPath})`);
return;
}
if (cachedGoBinPath === goRuntimePath && cachedGoVersion) {
if (cachedGoVersion.isValid()) {
return Promise.resolve(cachedGoVersion);
}
warn(`cached Go version (${JSON.stringify(cachedGoVersion)}) is invalid, recomputing`);
}
const docUri = vscode.window.activeTextEditor?.document.uri;
const cwd = getWorkspaceFolderPath(docUri && docUri.fsPath.endsWith('.go') ? docUri : undefined);
let goVersion: GoVersion;
try {
const env = toolExecutionEnvironment();
const execFile = util.promisify(cp.execFile);
const { stdout, stderr } = await execFile(goRuntimePath, ['version'], { env, cwd });
if (stderr) {
warn(`failed to run "${goRuntimePath} version": stdout: ${stdout}, stderr: ${stderr}`);
return;
}
goVersion = new GoVersion(goRuntimePath, stdout);
} catch (err) {
warn(`failed to run "${goRuntimePath} version": ${err} cwd: ${cwd}`);
return;
}
if (!goBinPath) {
// if getGoVersion was called with a given goBinPath, don't cache the result.
cachedGoBinPath = goRuntimePath;
cachedGoVersion = goVersion;
if (!cachedGoVersion.isValid()) {
warn(`unable to determine version from the output of "${goRuntimePath} version": "${goVersion.svString}"`);
}
}
return goVersion;
}
/**
* Returns the output of `go env` from the specified directory.
* Throws an error if the command fails.
*/
export async function getGoEnv(cwd?: string): Promise<string> {
const goRuntime = getBinPath('go');
const execFile = util.promisify(cp.execFile);
const opts = { cwd, env: toolExecutionEnvironment() };
const { stdout, stderr } = await execFile(goRuntime, ['env'], opts);
if (stderr) {
throw new Error(`failed to run 'go env': ${stderr}`);
}
return stdout;
}
/**
* Returns boolean denoting if current version of Go supports vendoring
*/
export async function isVendorSupported(): Promise<boolean> {
if (vendorSupport !== null) {
return Promise.resolve(vendorSupport);
}
const goVersion = await getGoVersion();
if (!goVersion.sv) {
return process.env['GO15VENDOREXPERIMENT'] === '0' ? false : true;
}
switch (goVersion.sv.major) {
case 0:
vendorSupport = false;
break;
case 1:
vendorSupport =
goVersion.sv.minor > 6 ||
((goVersion.sv.minor === 5 || goVersion.sv.minor === 6) && process.env['GO15VENDOREXPERIMENT'] === '1')
? true
: false;
break;
default:
vendorSupport = true;
break;
}
return vendorSupport;
}
/**
* Returns boolean indicating if GOPATH is set or not
* If not set, then prompts user to do set GOPATH
*/
export function isGoPathSet(): boolean {
if (!getCurrentGoPath()) {
// TODO(hyangah): is it still possible after go1.8? (https://golang.org/doc/go1.8#gopath)
vscode.window
.showInformationMessage(
'Set GOPATH environment variable and restart VS Code or set GOPATH in Workspace settings',
'Set GOPATH in Workspace Settings'
)
.then((selected) => {
if (selected === 'Set GOPATH in Workspace Settings') {
vscode.commands.executeCommand('workbench.action.openWorkspaceSettings');
}
});
return false;
}
return true;
}
export function isPositionInString(document: vscode.TextDocument, position: vscode.Position): boolean {
const lineText = document.lineAt(position.line).text;
const lineTillCurrentPosition = lineText.substr(0, position.character);
// Count the number of double quotes in the line till current position. Ignore escaped double quotes
let doubleQuotesCnt = (lineTillCurrentPosition.match(/"/g) || []).length;
const escapedDoubleQuotesCnt = (lineTillCurrentPosition.match(/\\"/g) || []).length;
doubleQuotesCnt -= escapedDoubleQuotesCnt;
return doubleQuotesCnt % 2 === 1;
}
export function getToolsGopath(useCache = true): string {
if (!useCache || !toolsGopath) {
toolsGopath = resolveToolsGopath();
}
return toolsGopath;
}
function resolveToolsGopath(): string {
let toolsGopathForWorkspace = substituteEnv(getGoConfig()['toolsGopath'] || '');
// In case of single root
if (!vscode.workspace.workspaceFolders || vscode.workspace.workspaceFolders.length <= 1) {
return resolvePath(toolsGopathForWorkspace);
}
// In case of multi-root, resolve ~ and ${workspaceFolder}
if (toolsGopathForWorkspace.startsWith('~')) {
toolsGopathForWorkspace = path.join(os.homedir(), toolsGopathForWorkspace.substr(1));
}
if (
toolsGopathForWorkspace &&
toolsGopathForWorkspace.trim() &&
!/\${workspaceFolder}|\${workspaceRoot}/.test(toolsGopathForWorkspace)
) {
return toolsGopathForWorkspace;
}
if (DefaultConfig().workspaceIsTrusted() === false) {
return toolsGopathForWorkspace;
}
// If any of the folders in multi root have toolsGopath set and the workspace is trusted, use it.
for (const folder of vscode.workspace.workspaceFolders) {
let toolsGopathFromConfig = <string>getGoConfig(folder.uri).inspect('toolsGopath').workspaceFolderValue;
toolsGopathFromConfig = resolvePath(toolsGopathFromConfig, folder.uri.fsPath);
if (toolsGopathFromConfig) {
return toolsGopathFromConfig;
}
}
return toolsGopathForWorkspace;
}
// getBinPath returns the path to the tool.
export function getBinPath(tool: string, useCache = true): string {
const r = getBinPathWithExplanation(tool, useCache);
return r.binPath;
}
// getBinPathWithExplanation returns the path to the tool, and the explanation on why
// the path was chosen. See getBinPathWithPreferredGopathGorootWithExplanation for details.
export function getBinPathWithExplanation(tool: string, useCache = true): { binPath: string; why?: string } {
const cfg = getGoConfig();
const alternateTools: { [key: string]: string } = cfg.get('alternateTools');
const alternateToolPath: string = alternateTools[tool];
let selectedGoPath: string | undefined;
if (tool === 'go') {
selectedGoPath = getFromWorkspaceState('selectedGo')?.binpath;
}
return getBinPathWithPreferredGopathGorootWithExplanation(
tool,
tool === 'go' ? [] : [getToolsGopath(), getCurrentGoPath()],
tool === 'go' && cfg.get('goroot') ? resolvePath(cfg.get('goroot')) : undefined,
selectedGoPath ?? resolvePath(alternateToolPath),
useCache
);
}
export function getFileArchive(document: vscode.TextDocument): string {
const fileContents = document.getText();
return document.fileName + '\n' + Buffer.byteLength(fileContents, 'utf8') + '\n' + fileContents;
}
export function substituteEnv(input: string): string {
return input.replace(/\${env:([^}]+)}/g, (match, capture) => {
return process.env[capture.trim()] || '';
});
}
let currentGopath = '';
export function getCurrentGoPath(workspaceUri?: vscode.Uri): string {
const activeEditorUri = vscode.window.activeTextEditor && vscode.window.activeTextEditor.document.uri;
const currentFilePath = fixDriveCasingInWindows(activeEditorUri && activeEditorUri.fsPath);
const currentRoot = (workspaceUri && workspaceUri.fsPath) || getWorkspaceFolderPath(activeEditorUri);
const config = getGoConfig(workspaceUri || activeEditorUri);
// Infer the GOPATH from the current root or the path of the file opened in current editor
// Last resort: Check for the common case where GOPATH itself is opened directly in VS Code
let inferredGopath: string;
if (config['inferGopath'] === true) {
inferredGopath = getInferredGopath(currentRoot) || getInferredGopath(currentFilePath);
if (!inferredGopath) {
try {
if (fs.statSync(path.join(currentRoot, 'src')).isDirectory()) {
inferredGopath = currentRoot;
}
} catch (e) {
// No op
}
}
if (inferredGopath && process.env['GOPATH'] && inferredGopath !== process.env['GOPATH']) {
inferredGopath += path.delimiter + process.env['GOPATH'];
}
}
const configGopath = config['gopath'] ? resolvePath(substituteEnv(config['gopath']), currentRoot) : '';
currentGopath = inferredGopath ? inferredGopath : configGopath || process.env['GOPATH'];
return currentGopath;
}
export function getModuleCache(): string {
if (process.env['GOMODCACHE']) {
return process.env['GOMODCACHE'];
}
if (currentGopath) {
return path.join(currentGopath.split(path.delimiter)[0], 'pkg', 'mod');
}
}
export function getExtensionCommands(): any[] {
const pkgJSON = vscode.extensions.getExtension(extensionId).packageJSON;
if (!pkgJSON.contributes || !pkgJSON.contributes.commands) {
return;
}
const extensionCommands: any[] = vscode.extensions
.getExtension(extensionId)
.packageJSON.contributes.commands.filter((x: any) => x.command !== 'go.show.commands');
return extensionCommands;
}
export class LineBuffer {
private buf = '';
private lineListeners: { (line: string): void }[] = [];
private lastListeners: { (last: string): void }[] = [];
public append(chunk: string) {
this.buf += chunk;
for (;;) {
const idx = this.buf.indexOf('\n');
if (idx === -1) {
break;
}
this.fireLine(this.buf.substring(0, idx));
this.buf = this.buf.substring(idx + 1);
}
}
public done() {
this.fireDone(this.buf !== '' ? this.buf : null);
}
public onLine(listener: (line: string) => void) {
this.lineListeners.push(listener);
}
public onDone(listener: (last: string) => void) {
this.lastListeners.push(listener);
}
private fireLine(line: string) {
this.lineListeners.forEach((listener) => listener(line));
}
private fireDone(last: string) {
this.lastListeners.forEach((listener) => listener(last));
}
}
export function timeout(millis: number): Promise<void> {
return new Promise<void>((resolve) => {
setTimeout(() => resolve(), millis);
});
}
/**
* Expands ~ to homedir in non-Windows platform and resolves ${workspaceFolder} or ${workspaceRoot}
*/
export function resolvePath(inputPath: string, workspaceFolder?: string): string {
if (!inputPath || !inputPath.trim()) {
return inputPath;
}
if (!workspaceFolder && vscode.workspace.workspaceFolders) {
workspaceFolder = getWorkspaceFolderPath(
vscode.window.activeTextEditor && vscode.window.activeTextEditor.document.uri
);
}
if (workspaceFolder) {
inputPath = inputPath.replace(/\${workspaceFolder}|\${workspaceRoot}/g, workspaceFolder);
}
return resolveHomeDir(inputPath);
}
/**
* Returns the import path in a passed in string.
* @param text The string to search for an import path
*/
export function getImportPath(text: string): string {
// Catch cases like `import alias "importpath"` and `import "importpath"`
const singleLineImportMatches = text.match(/^\s*import\s+([a-z,A-Z,_,.]\w*\s+)?"([^"]+)"/);
if (singleLineImportMatches) {
return singleLineImportMatches[2];
}
// Catch cases like `alias "importpath"` and "importpath"
const groupImportMatches = text.match(/^\s*([a-z,A-Z,_,.]\w*\s+)?"([^"]+)"/);
if (groupImportMatches) {
return groupImportMatches[2];
}
return '';
}
// TODO: Add unit tests for the below
/**
* Guess the package name based on parent directory name of the given file
*
* Cases:
* - dir 'go-i18n' -> 'i18n'
* - dir 'go-spew' -> 'spew'
* - dir 'kingpin' -> 'kingpin'
* - dir 'go-expand-tilde' -> 'tilde'
* - dir 'gax-go' -> 'gax'
* - dir 'go-difflib' -> 'difflib'
* - dir 'jwt-go' -> 'jwt'
* - dir 'go-radix' -> 'radix'
*
* @param {string} filePath.
*/
export function guessPackageNameFromFile(filePath: string): Promise<string[]> {
return new Promise((resolve, reject) => {
const goFilename = path.basename(filePath);
if (goFilename === 'main.go') {
return resolve(['main']);
}
const directoryPath = path.dirname(filePath);
const dirName = path.basename(directoryPath);
let segments = dirName.split(/[.-]/);
segments = segments.filter((val) => val !== 'go');
if (segments.length === 0 || !/[a-zA-Z_]\w*/.test(segments[segments.length - 1])) {
return reject();
}
const proposedPkgName = segments[segments.length - 1];
fs.stat(path.join(directoryPath, 'main.go'), (err, stats) => {
if (stats && stats.isFile()) {
return resolve(['main']);
}
if (goFilename.endsWith('_test.go')) {
return resolve([proposedPkgName, proposedPkgName + '_test']);
}
return resolve([proposedPkgName]);
});
});
}
export interface ICheckResult {
file: string;
line: number;
col: number;
msg: string;
severity: string;
}
/**
* Runs given Go tool and returns errors/warnings that can be fed to the Problems Matcher
* @param args Arguments to be passed while running given tool
* @param cwd cwd that will passed in the env object while running given tool
* @param severity error or warning
* @param useStdErr If true, the stderr of the output of the given tool will be used, else stdout will be used
* @param toolName The name of the Go tool to run. If none is provided, the go runtime itself is used
* @param printUnexpectedOutput If true, then output that doesnt match expected format is printed to the output channel
*/
export function runTool(
args: string[],
cwd: string,
severity: string,
useStdErr: boolean,
toolName: string,
env: any,
printUnexpectedOutput: boolean,
token?: vscode.CancellationToken
): Promise<ICheckResult[]> {
let cmd: string;
if (toolName) {
cmd = getBinPath(toolName);
} else {
const goRuntimePath = getBinPath('go');
if (!goRuntimePath) {
return Promise.reject(new Error('Cannot find "go" binary. Update PATH or GOROOT appropriately'));
}
cmd = goRuntimePath;
}
let p: cp.ChildProcess;
if (token) {
token.onCancellationRequested(() => {
if (p) {
killProcessTree(p);
}
});
}
cwd = fixDriveCasingInWindows(cwd);
return new Promise((resolve, reject) => {
p = cp.execFile(cmd, args, { env, cwd }, (err, stdout, stderr) => {
try {
if (err && (<any>err).code === 'ENOENT') {
// Since the tool is run on save which can be frequent
// we avoid sending explicit notification if tool is missing
console.log(`Cannot find ${toolName ? toolName : 'go'}`);
return resolve([]);
}
if (err && stderr && !useStdErr) {
outputChannel.appendLine(['Error while running tool:', cmd, ...args].join(' '));
outputChannel.appendLine(stderr);
return resolve([]);
}
const lines = (useStdErr ? stderr : stdout).toString().split('\n');
outputChannel.appendLine([cwd + '>Finished running tool:', cmd, ...args].join(' '));
const ret: ICheckResult[] = [];
let unexpectedOutput = false;
let atLeastSingleMatch = false;
for (const l of lines) {
if (l[0] === '\t' && ret.length > 0) {
ret[ret.length - 1].msg += '\n' + l;
continue;
}
const match = /^([^:]*: )?((.:)?[^:]*):(\d+)(:(\d+)?)?:(?:\w+:)? (.*)$/.exec(l);
if (!match) {
if (printUnexpectedOutput && useStdErr && stderr) {
unexpectedOutput = true;
}
continue;
}
atLeastSingleMatch = true;
const [, , file, , lineStr, , colStr, msg] = match;
const line = +lineStr;
const col = colStr ? +colStr : undefined;
// Building skips vendor folders,
// But vet and lint take in directories and not import paths, so no way to skip them
// So prune out the results from vendor folders here.
if (
!path.isAbsolute(file) &&
(file.startsWith(`vendor${path.sep}`) || file.indexOf(`${path.sep}vendor${path.sep}`) > -1)
) {
continue;
}
const filePath = path.resolve(cwd, file);
ret.push({ file: filePath, line, col, msg, severity });
outputChannel.appendLine(`${filePath}:${line}:${col ?? ''} ${msg}`);
}
if (!atLeastSingleMatch && unexpectedOutput && vscode.window.activeTextEditor) {
outputChannel.appendLine(stderr);
if (err) {
ret.push({
file: vscode.window.activeTextEditor.document.fileName,
line: 1,
col: 1,
msg: stderr,
severity: 'error'
});
}
}
outputChannel.appendLine('');
resolve(ret);
} catch (e) {
reject(e);
}
});
});
}
export function handleDiagnosticErrors(
document: vscode.TextDocument,
errors: ICheckResult[],
diagnosticCollection: vscode.DiagnosticCollection,
diagnosticSource?: string
) {
diagnosticCollection.clear();
const diagnosticMap: Map<string, vscode.Diagnostic[]> = new Map();
const textDocumentMap: Map<string, vscode.TextDocument> = new Map();
if (document) {
textDocumentMap.set(document.uri.toString(), document);
}
// Also add other open .go files known to vscode for fast lookup.
vscode.workspace.textDocuments.forEach((t) => {
const fileName = t.uri.toString();
if (!fileName.endsWith('.go')) {
return;
}
textDocumentMap.set(fileName, t);
});
errors.forEach((error) => {
const canonicalFile = vscode.Uri.file(error.file).toString();
let startColumn = error.col ? error.col - 1 : 0;
let endColumn = startColumn + 1;
// Some tools output only the line number or the start position.
// If the file content is available, adjust the diagnostic range so
// the squiggly underline for the error message is more visible.
const doc = textDocumentMap.get(canonicalFile);
if (doc) {
const tempRange = new vscode.Range(
error.line - 1,
0,
error.line - 1,
doc.lineAt(error.line - 1).range.end.character + 1 // end of the line
);
const text = doc.getText(tempRange);
const [, leading, trailing] = /^(\s*).*(\s*)$/.exec(text);
if (!error.col) {
startColumn = leading.length; // beginning of the non-white space.
} else {
startColumn = error.col - 1; // range is 0-indexed
}
endColumn = text.length - trailing.length;
}
const range = new vscode.Range(error.line - 1, startColumn, error.line - 1, endColumn);
const severity = mapSeverityToVSCodeSeverity(error.severity);
const diagnostic = new vscode.Diagnostic(range, error.msg, severity);
// vscode uses source for deduping diagnostics.
diagnostic.source = diagnosticSource || diagnosticCollection.name;
let diagnostics = diagnosticMap.get(canonicalFile);
if (!diagnostics) {
diagnostics = [];
}
diagnostics.push(diagnostic);
diagnosticMap.set(canonicalFile, diagnostics);
});
diagnosticMap.forEach((newDiagnostics, file) => {
const fileUri = vscode.Uri.parse(file);
if (diagnosticCollection === buildDiagnosticCollection) {
// If there are lint/vet warnings on current file, remove the ones co-inciding with the new build errors
removeDuplicateDiagnostics(lintDiagnosticCollection, fileUri, newDiagnostics);
removeDuplicateDiagnostics(vetDiagnosticCollection, fileUri, newDiagnostics);
} else if (buildDiagnosticCollection && buildDiagnosticCollection.has(fileUri)) {
// If there are build errors on current file, ignore the new lint/vet warnings co-inciding with them
newDiagnostics = deDupeDiagnostics(buildDiagnosticCollection.get(fileUri).slice(), newDiagnostics);
}
// If there are errors from the language client that are on the current file, ignore the warnings co-inciding
// with them.
if (languageClient && languageClient.diagnostics?.has(fileUri)) {
newDiagnostics = deDupeDiagnostics(languageClient.diagnostics.get(fileUri).slice(), newDiagnostics);
}
diagnosticCollection.set(fileUri, newDiagnostics);
});
}
/**
* Removes any diagnostics in collection, where there is a diagnostic in
* newDiagnostics on the same line in fileUri.
*/
export function removeDuplicateDiagnostics(
collection: vscode.DiagnosticCollection,
fileUri: vscode.Uri,
newDiagnostics: vscode.Diagnostic[]
) {
if (collection && collection.has(fileUri)) {
collection.set(fileUri, deDupeDiagnostics(newDiagnostics, collection.get(fileUri).slice()));
}
}
/**
* Removes any diagnostics in otherDiagnostics, where there is a diagnostic in
* buildDiagnostics on the same line.
*/
function deDupeDiagnostics(
buildDiagnostics: vscode.Diagnostic[],
otherDiagnostics: vscode.Diagnostic[]
): vscode.Diagnostic[] {
const buildDiagnosticsLines = buildDiagnostics.map((x) => x.range.start.line);
return otherDiagnostics.filter((x) => buildDiagnosticsLines.indexOf(x.range.start.line) === -1);
}
function mapSeverityToVSCodeSeverity(sev: string): vscode.DiagnosticSeverity {
switch (sev) {
case 'error':
return vscode.DiagnosticSeverity.Error;
case 'warning':
return vscode.DiagnosticSeverity.Warning;
default:
return vscode.DiagnosticSeverity.Error;
}
}
export function getWorkspaceFolderPath(fileUri?: vscode.Uri): string | undefined {
if (fileUri) {
const workspace = vscode.workspace.getWorkspaceFolder(fileUri);
if (workspace) {
return fixDriveCasingInWindows(workspace.uri.fsPath);
}
}
// fall back to the first workspace
const folders = vscode.workspace.workspaceFolders;
if (folders && folders.length) {
return fixDriveCasingInWindows(folders[0].uri.fsPath);
}
return undefined;
}
export function makeMemoizedByteOffsetConverter(buffer: Buffer): (byteOffset: number) => number {
const defaultValue = new Node<number, number>(0, 0); // 0 bytes will always be 0 characters
const memo = new NearestNeighborDict(defaultValue, NearestNeighborDict.NUMERIC_DISTANCE_FUNCTION);
return (byteOffset: number) => {
const nearest = memo.getNearest(byteOffset);
const byteDelta = byteOffset - nearest.key;
if (byteDelta === 0) {
return nearest.value;
}
let charDelta: number;
if (byteDelta > 0) {
charDelta = buffer.toString('utf8', nearest.key, byteOffset).length;
} else {
charDelta = -buffer.toString('utf8', byteOffset, nearest.key).length;
}
memo.insert(byteOffset, nearest.value + charDelta);
return nearest.value + charDelta;
};
}
export function rmdirRecursive(dir: string) {
if (fs.existsSync(dir)) {
fs.readdirSync(dir).forEach((file) => {
const relPath = path.join(dir, file);