Skip to content

Commit 33b0b78

Browse files
firelizzard18gopherbot
authored andcommitted
extension/src/goTest: disable when exp-vscode-go is installed
Adds 'experimental features' to preview versions. The first experimental feature: when exp-vscode-go is installed, disable goTest (the test explorer implementation). Adds a setting to disable this behavior Notifies the user the first time goTest is disabled for this reason. Modifies goTest to support being unloaded or reloaded based on configuration changes. Change-Id: I7a4b2188b5038f9f6b5841ed47a5b27307e24ef1 Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/613695 Reviewed-by: Hyang-Ah Hana Kim <[email protected]> kokoro-CI: kokoro <[email protected]> Commit-Queue: Hyang-Ah Hana Kim <[email protected]> Reviewed-by: Hongxiang Jiang <[email protected]> Auto-Submit: Hyang-Ah Hana Kim <[email protected]>
1 parent 866878e commit 33b0b78

File tree

9 files changed

+249
-43
lines changed

9 files changed

+249
-43
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ to use the go1.21 or newer when installing tools. ([Issue 3411](https://github.c
1919
* Extension build target is set to `es2022`. ([Issue 3540](https://github.com/golang/vscode-go/issues/3540))
2020
* The extension release workflow is migrated to the Go project's [Relui](https://pkg.go.dev/golang.org/x/build/cmd/relui#section-readme). ([Issue 3500](https://github.com/golang/vscode-go/issues/3500))
2121

22+
### Testing
23+
24+
A new extension, [Go Companion](https://marketplace.visualstudio.com/items?itemName=ethan-reesor.exp-vscode-go), has been released with experimental support for gopls-based test discovery. If Go Companion is installed, pre-release versions of this extension will automatically disable its test explorer in favor of Go Companion's implementation. See [experiments](./docs/experiments.md#test-explorer) for details on Go Companion's features and for disabling the automatic switchover.
25+
2226
## v0.42.1
2327

2428
Date: 9 Sep, 2024

docs/experiments.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Experiments
2+
3+
Pre-release versions of [vscode-go][vscode-go] include experimental features.
4+
These features may be individually enabled or disabled via the setting
5+
`go.experiments`.
6+
7+
[vscode-go]: https://github.com/golang/vscode-go/blob/master/README.md#pre-release-versions
8+
9+
## Test explorer
10+
11+
[Go Companion][exp-vscode-go] includes an experimental test explorer
12+
implementation based on `gopls`'s test discovery. This requires gopls v0.17.0 or
13+
newer. If Go Companion is present and vscode-go is a pre-release version,
14+
vscode-go will prefer Go Companion's test explorer, disabling its own, unless
15+
the experiment is set to `off`. The experimental test explorer provides more
16+
robust test discovery by using gopls, including static discovery of _some_
17+
subtests. It also implements:
18+
19+
- Ignore tests within files excluded by `files.exclude` or
20+
`goExp.testExplorer.exclude`.
21+
- Disable automatic discovery of tests by setting `goExp.testExplorer.discovery`
22+
to "off".
23+
- Control how tests are displayed with `goExp.testExplorer.showFiles`,
24+
`goExp.testExplorer.nestPackages`, and `goExp.testExplorer.nestSubtests`.
25+
- Debugging a test updates its status in the test explorer.
26+
- Support for continuous runs.
27+
- Support for code coverage.
28+
- Code lenses (hidden by default) that are integrated with the test explorer.
29+
- Integrated viewer for pprof profiles.
30+
31+
[exp-vscode-go]: https://marketplace.visualstudio.com/items?itemName=ethan-reesor.exp-vscode-go

docs/settings.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,19 @@ Default:
218218
"runtest" : true,
219219
}
220220
```
221+
### `go.experiments`
222+
223+
Disable experimental features. These features are only available in the pre-release version.
224+
| Properties | Description |
225+
| --- | --- |
226+
| `testExplorer` | Prefer the experimental test explorer <br/> Default: `true` |
227+
228+
Default:
229+
```
230+
{
231+
"testExplorer" : true,
232+
}
233+
```
221234
### `go.formatFlags`
222235

223236
Flags to pass to format tool (e.g. ["-s"]). Not applicable when using the language server.

extension/package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,6 +1477,20 @@
14771477
"description": "Open the test output terminal when a test run is started.",
14781478
"scope": "window"
14791479
},
1480+
"go.experiments": {
1481+
"type": "object",
1482+
"default": {
1483+
"testExplorer": true
1484+
},
1485+
"description": "Disable experimental features. These features are only available in the pre-release version.",
1486+
"properties": {
1487+
"testExplorer": {
1488+
"type": "boolean",
1489+
"default": true,
1490+
"description": "Prefer the experimental test explorer"
1491+
}
1492+
}
1493+
},
14801494
"go.generateTestsFlags": {
14811495
"type": "array",
14821496
"items": {

extension/src/experimental.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*---------------------------------------------------------
2+
* Copyright 2024 The Go Authors. All rights reserved.
3+
* Licensed under the MIT License. See LICENSE in the project root for license information.
4+
*--------------------------------------------------------*/
5+
6+
import { EventEmitter, ExtensionContext, ExtensionMode, extensions, workspace } from 'vscode';
7+
import { extensionInfo } from './config';
8+
9+
type Settings = {
10+
testExplorer: boolean;
11+
};
12+
13+
class Experiments {
14+
#didChange = new EventEmitter<Experiments>();
15+
16+
// Default to disabled
17+
#testExplorer = false;
18+
19+
activate(ctx: ExtensionContext) {
20+
// Cleanup the event emitter when the extension is unloaded
21+
ctx.subscriptions.push(this.#didChange);
22+
23+
// Don't enable any experiments in a production release
24+
if (ctx.extensionMode === ExtensionMode.Production && !extensionInfo.isPreview) {
25+
return;
26+
}
27+
28+
// Check on boot
29+
this.#maybeEnableExperiments();
30+
31+
// Check when an extension is installed or uninstalled
32+
ctx.subscriptions.push(extensions.onDidChange(() => this.#maybeEnableExperiments()));
33+
34+
// Check when the configuration changes
35+
ctx.subscriptions.push(
36+
workspace.onDidChangeConfiguration((e) => {
37+
if (e.affectsConfiguration('go.experiments')) {
38+
this.#maybeEnableExperiments();
39+
}
40+
})
41+
);
42+
}
43+
44+
/**
45+
* Checks whether experiments should be enabled or disabled. If the
46+
* enable/disable state of an experiment changes, an {@link onDidChange}
47+
* event is issued.
48+
*/
49+
#maybeEnableExperiments() {
50+
const settings = workspace.getConfiguration('go').get<Settings>('experiments');
51+
52+
// Check if the test explorer experiment should be activated
53+
const goExp = extensions.getExtension('ethan-reesor.exp-vscode-go');
54+
const testExplorer = settings?.testExplorer !== false && !!goExp;
55+
if (testExplorer !== this.#testExplorer) {
56+
this.#testExplorer = testExplorer;
57+
this.#didChange.fire(this);
58+
}
59+
}
60+
61+
/**
62+
* onDidChange issues an event whenever the enable/disable status of an
63+
* experiment changes. This can happen due to configuration changes or
64+
* companion extensions being loaded or unloaded.
65+
*/
66+
readonly onDidChange = this.#didChange.event;
67+
68+
/**
69+
* If true, this extension's test explorer is disabled in favor of Go
70+
* Companion's test explorer.
71+
*/
72+
get testExplorer() {
73+
return this.#testExplorer;
74+
}
75+
}
76+
77+
export const experiments = new Experiments();

extension/src/goMain.ts

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,15 @@ import { getFormatTool } from './language/legacy/goFormat';
6565
import { resetSurveyConfigs, showSurveyConfig } from './goSurvey';
6666
import { ExtensionAPI } from './export';
6767
import extensionAPI from './extensionAPI';
68-
import { GoTestExplorer, isVscodeTestingAPIAvailable } from './goTest/explore';
68+
import { GoTestExplorer } from './goTest/explore';
6969
import { killRunningPprof } from './goTest/profile';
7070
import { GoExplorerProvider } from './goExplorer';
7171
import { GoExtensionContext } from './context';
7272
import * as commands from './commands';
7373
import { toggleVulncheckCommandFactory } from './goVulncheck';
7474
import { GoTaskProvider } from './goTaskProvider';
7575
import { setTelemetryEnvVars, telemetryReporter } from './goTelemetry';
76+
import { experiments } from './experimental';
7677

7778
const goCtx: GoExtensionContext = {};
7879

@@ -147,6 +148,9 @@ export async function activate(ctx: vscode.ExtensionContext): Promise<ExtensionA
147148
GoRunTestCodeLensProvider.activate(ctx, goCtx);
148149
GoDebugConfigurationProvider.activate(ctx, goCtx);
149150
GoDebugFactory.activate(ctx, goCtx);
151+
experiments.activate(ctx);
152+
GoTestExplorer.setup(ctx, goCtx);
153+
GoExplorerProvider.setup(ctx);
150154

151155
goCtx.buildDiagnosticCollection = vscode.languages.createDiagnosticCollection('go');
152156
ctx.subscriptions.push(goCtx.buildDiagnosticCollection);
@@ -185,12 +189,6 @@ export async function activate(ctx: vscode.ExtensionContext): Promise<ExtensionA
185189
registerCommand('go.tools.install', commands.installTools);
186190
registerCommand('go.browse.packages', browsePackages);
187191

188-
if (isVscodeTestingAPIAvailable && cfg.get<boolean>('testExplorer.enable')) {
189-
GoTestExplorer.setup(ctx, goCtx);
190-
}
191-
192-
GoExplorerProvider.setup(ctx);
193-
194192
registerCommand('go.test.generate.package', goGenerateTests.generateTestCurrentPackage);
195193
registerCommand('go.test.generate.file', goGenerateTests.generateTestCurrentFile);
196194
registerCommand('go.test.generate.function', goGenerateTests.generateTestCurrentFunction);
@@ -332,15 +330,6 @@ function addOnDidChangeConfigListeners(ctx: vscode.ExtensionContext) {
332330
// TODO: actively maintain our own disposables instead of keeping pushing to ctx.subscription.
333331
}
334332
}
335-
if (e.affectsConfiguration('go.testExplorer.enable')) {
336-
const msg =
337-
'Go test explorer has been enabled or disabled. For this change to take effect, the window must be reloaded.';
338-
vscode.window.showInformationMessage(msg, 'Reload').then((selected) => {
339-
if (selected === 'Reload') {
340-
vscode.commands.executeCommand('workbench.action.reloadWindow');
341-
}
342-
});
343-
}
344333
})
345334
);
346335
}

0 commit comments

Comments
 (0)