Skip to content

Commit 9caf3e3

Browse files
suzmuehyangah
authored andcommitted
[release] src/goTest: do not open text document on didCreateFile
The Test Explorer watches all _test.go files in the workspace in order to keep the list of tests updated in the test explorer ui. On branch switches, there may be many test files that are created, causing the test explorer to process these. Previously, part of the processing resulted in opening the file. This led to issues due to the interaction with the language server. This change updates the logic on file creation, to add the parent test item only, which will not require opening the document. Fixes #2570 Change-Id: I595f34daee257c85bafd3e5706176f73f891fdf1 Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/458999 Reviewed-by: Hyang-Ah Hana Kim <[email protected]> Run-TryBot: Suzy Mueller <[email protected]> TryBot-Result: kokoro <[email protected]> Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/462376 Run-TryBot: Hyang-Ah Hana Kim <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent 6368b28 commit 9caf3e3

File tree

4 files changed

+45
-26
lines changed

4 files changed

+45
-26
lines changed

src/goDocumentSymbols.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ export class GoplsDocumentSymbolProvider implements vscode.DocumentSymbolProvide
2525
constructor(private readonly goCtx: GoExtensionContext, private includeImports?: boolean) {}
2626

2727
public async provideDocumentSymbols(document: vscode.TextDocument): Promise<vscode.DocumentSymbol[]> {
28+
// TODO(suzmue): consider providing an interface for providing document symbols that only requires
29+
// the URI. Getting a TextDocument from a filename requires opening the file, which can lead to
30+
// files being opened that were not requested by the user in order to get information that we just
31+
// need the URI to access.
2832
if (typeof this.includeImports !== 'boolean') {
2933
const gotoSymbolConfig = getGoConfig(document.uri)['gotoSymbol'];
3034
this.includeImports = gotoSymbolConfig ? gotoSymbolConfig['includeImports'] : false;

src/goTest/explore.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,12 @@ export class GoTestExplorer {
258258
}
259259

260260
protected async didCreateFile(file: Uri) {
261-
await this.documentUpdate(await this.workspace.openTextDocument(file));
261+
// Do not use openTextDocument to get the TextDocument for file,
262+
// since this sends a didOpen text document notification to gopls,
263+
// leading to spurious diagnostics from gopls:
264+
// https://github.com/golang/vscode-go/issues/2570
265+
// Instead, get the test item for this file only.
266+
await this.resolver.getFile(file);
262267
}
263268

264269
protected async didDeleteFile(file: Uri) {

src/goTest/resolve.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,21 @@ export class GoTestResolver {
233233
vscode.commands.executeCommand('setContext', 'go.tests', items);
234234
}
235235

236+
// Retrieve or create an item for a Go file.
237+
public async getFile(uri: Uri): Promise<TestItem> {
238+
const dir = path.dirname(uri.path);
239+
const pkg = await this.getPackage(uri.with({ path: dir, query: '', fragment: '' }));
240+
const existing = this.getItem(pkg, uri, 'file');
241+
if (existing) {
242+
return existing;
243+
}
244+
245+
const label = path.basename(uri.path);
246+
const item = this.getOrCreateItem(pkg, label, uri, 'file');
247+
item.canResolveChildren = true;
248+
return item;
249+
}
250+
236251
/* ***** Private ***** */
237252

238253
private shouldSetRange(item: TestItem): boolean {
@@ -375,21 +390,6 @@ export class GoTestResolver {
375390
return item;
376391
}
377392

378-
// Retrieve or create an item for a Go file.
379-
private async getFile(uri: Uri): Promise<TestItem> {
380-
const dir = path.dirname(uri.path);
381-
const pkg = await this.getPackage(uri.with({ path: dir, query: '', fragment: '' }));
382-
const existing = this.getItem(pkg, uri, 'file');
383-
if (existing) {
384-
return existing;
385-
}
386-
387-
const label = path.basename(uri.path);
388-
const item = this.getOrCreateItem(pkg, label, uri, 'file');
389-
item.canResolveChildren = true;
390-
return item;
391-
}
392-
393393
private getTestSuite(type: string): TestSuite {
394394
if (this.testSuites.has(type)) {
395395
return this.testSuites.get(type) as TestSuite;

test/integration/goTest.explore.test.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,16 @@ suite('Go Test Explorer', () => {
6060
async _didOpen(doc: TextDocument) {
6161
await this.didOpenTextDocument(doc);
6262
}
63+
64+
async _didCreate(doc: TextDocument) {
65+
await this.didCreateFile(doc.uri);
66+
}
6367
}
6468

6569
interface TC extends TestCase {
66-
open: string;
67-
expect: string[];
70+
uri: string;
71+
expectCreate: string[];
72+
expectOpen: string[];
6873
}
6974

7075
const cases: Record<string, TC> = {
@@ -76,8 +81,9 @@ suite('Go Test Explorer', () => {
7681
'/src/proj/bar_test.go': 'package main\nfunc TestBar(*testing.T) {}',
7782
'/src/proj/baz/main_test.go': 'package main\nfunc TestBaz(*testing.T) {}'
7883
},
79-
open: 'file:///src/proj/foo_test.go',
80-
expect: [
84+
uri: 'file:///src/proj/foo_test.go',
85+
expectCreate: ['file:///src/proj?module', 'file:///src/proj/foo_test.go?file'],
86+
expectOpen: [
8187
'file:///src/proj?module',
8288
'file:///src/proj/foo_test.go?file',
8389
'file:///src/proj/foo_test.go?test#TestFoo'
@@ -89,8 +95,9 @@ suite('Go Test Explorer', () => {
8995
'/src/proj/go.mod': 'module test',
9096
'/src/proj/foo_test.go': 'package main\nfunc TestFoo(*testing.T) {}'
9197
},
92-
open: 'file:///src/proj/foo_test.go',
93-
expect: [
98+
uri: 'file:///src/proj/foo_test.go',
99+
expectCreate: ['file:///src/proj?module', 'file:///src/proj/foo_test.go?file'],
100+
expectOpen: [
94101
'file:///src/proj?module',
95102
'file:///src/proj/foo_test.go?file',
96103
'file:///src/proj/foo_test.go?test#TestFoo'
@@ -100,14 +107,17 @@ suite('Go Test Explorer', () => {
100107

101108
for (const name in cases) {
102109
test(name, async () => {
103-
const { workspace, files, open, expect } = cases[name];
110+
const { workspace, files, uri: uri, expectCreate: expectCreate, expectOpen: expectOpen } = cases[name];
104111
const { ctrl, expl, ws } = newExplorer(workspace, files, DUT);
105112

106-
const doc = ws.fs.files.get(open);
113+
const doc = ws.fs.files.get(uri);
107114
assert(doc);
108-
await expl._didOpen(doc);
109115

110-
assertTestItems(ctrl.items, expect);
116+
await expl._didCreate(doc);
117+
assertTestItems(ctrl.items, expectCreate);
118+
119+
await expl._didOpen(doc);
120+
assertTestItems(ctrl.items, expectOpen);
111121
});
112122
}
113123
});

0 commit comments

Comments
 (0)