Skip to content

Commit

Permalink
extension/src/goTest: disable when exp-vscode-go is installed
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
firelizzard18 authored and gopherbot committed Nov 1, 2024
1 parent 866878e commit 33b0b78
Show file tree
Hide file tree
Showing 9 changed files with 249 additions and 43 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ to use the go1.21 or newer when installing tools. ([Issue 3411](https://github.c
* Extension build target is set to `es2022`. ([Issue 3540](https://github.com/golang/vscode-go/issues/3540))
* 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))

### Testing

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.

## v0.42.1

Date: 9 Sep, 2024
Expand Down
31 changes: 31 additions & 0 deletions docs/experiments.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Experiments

Pre-release versions of [vscode-go][vscode-go] include experimental features.
These features may be individually enabled or disabled via the setting
`go.experiments`.

[vscode-go]: https://github.com/golang/vscode-go/blob/master/README.md#pre-release-versions

## Test explorer

[Go Companion][exp-vscode-go] includes an experimental test explorer
implementation based on `gopls`'s test discovery. This requires gopls v0.17.0 or
newer. If Go Companion is present and vscode-go is a pre-release version,
vscode-go will prefer Go Companion's test explorer, disabling its own, unless
the experiment is set to `off`. The experimental test explorer provides more
robust test discovery by using gopls, including static discovery of _some_
subtests. It also implements:

- Ignore tests within files excluded by `files.exclude` or
`goExp.testExplorer.exclude`.
- Disable automatic discovery of tests by setting `goExp.testExplorer.discovery`
to "off".
- Control how tests are displayed with `goExp.testExplorer.showFiles`,
`goExp.testExplorer.nestPackages`, and `goExp.testExplorer.nestSubtests`.
- Debugging a test updates its status in the test explorer.
- Support for continuous runs.
- Support for code coverage.
- Code lenses (hidden by default) that are integrated with the test explorer.
- Integrated viewer for pprof profiles.

[exp-vscode-go]: https://marketplace.visualstudio.com/items?itemName=ethan-reesor.exp-vscode-go
13 changes: 13 additions & 0 deletions docs/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,19 @@ Default:
"runtest" : true,
}
```
### `go.experiments`

Disable experimental features. These features are only available in the pre-release version.
| Properties | Description |
| --- | --- |
| `testExplorer` | Prefer the experimental test explorer <br/> Default: `true` |

Default:
```
{
"testExplorer" : true,
}
```
### `go.formatFlags`

Flags to pass to format tool (e.g. ["-s"]). Not applicable when using the language server.
Expand Down
14 changes: 14 additions & 0 deletions extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1477,6 +1477,20 @@
"description": "Open the test output terminal when a test run is started.",
"scope": "window"
},
"go.experiments": {
"type": "object",
"default": {
"testExplorer": true
},
"description": "Disable experimental features. These features are only available in the pre-release version.",
"properties": {
"testExplorer": {
"type": "boolean",
"default": true,
"description": "Prefer the experimental test explorer"
}
}
},
"go.generateTestsFlags": {
"type": "array",
"items": {
Expand Down
77 changes: 77 additions & 0 deletions extension/src/experimental.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*---------------------------------------------------------
* Copyright 2024 The Go Authors. All rights reserved.
* Licensed under the MIT License. See LICENSE in the project root for license information.
*--------------------------------------------------------*/

import { EventEmitter, ExtensionContext, ExtensionMode, extensions, workspace } from 'vscode';
import { extensionInfo } from './config';

type Settings = {
testExplorer: boolean;
};

class Experiments {
#didChange = new EventEmitter<Experiments>();

// Default to disabled
#testExplorer = false;

activate(ctx: ExtensionContext) {
// Cleanup the event emitter when the extension is unloaded
ctx.subscriptions.push(this.#didChange);

// Don't enable any experiments in a production release
if (ctx.extensionMode === ExtensionMode.Production && !extensionInfo.isPreview) {
return;
}

// Check on boot
this.#maybeEnableExperiments();

// Check when an extension is installed or uninstalled
ctx.subscriptions.push(extensions.onDidChange(() => this.#maybeEnableExperiments()));

// Check when the configuration changes
ctx.subscriptions.push(
workspace.onDidChangeConfiguration((e) => {
if (e.affectsConfiguration('go.experiments')) {
this.#maybeEnableExperiments();
}
})
);
}

/**
* Checks whether experiments should be enabled or disabled. If the
* enable/disable state of an experiment changes, an {@link onDidChange}
* event is issued.
*/
#maybeEnableExperiments() {
const settings = workspace.getConfiguration('go').get<Settings>('experiments');

// Check if the test explorer experiment should be activated
const goExp = extensions.getExtension('ethan-reesor.exp-vscode-go');
const testExplorer = settings?.testExplorer !== false && !!goExp;
if (testExplorer !== this.#testExplorer) {
this.#testExplorer = testExplorer;
this.#didChange.fire(this);
}
}

/**
* onDidChange issues an event whenever the enable/disable status of an
* experiment changes. This can happen due to configuration changes or
* companion extensions being loaded or unloaded.
*/
readonly onDidChange = this.#didChange.event;

/**
* If true, this extension's test explorer is disabled in favor of Go
* Companion's test explorer.
*/
get testExplorer() {
return this.#testExplorer;
}
}

export const experiments = new Experiments();
21 changes: 5 additions & 16 deletions extension/src/goMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,15 @@ import { getFormatTool } from './language/legacy/goFormat';
import { resetSurveyConfigs, showSurveyConfig } from './goSurvey';
import { ExtensionAPI } from './export';
import extensionAPI from './extensionAPI';
import { GoTestExplorer, isVscodeTestingAPIAvailable } from './goTest/explore';
import { GoTestExplorer } from './goTest/explore';
import { killRunningPprof } from './goTest/profile';
import { GoExplorerProvider } from './goExplorer';
import { GoExtensionContext } from './context';
import * as commands from './commands';
import { toggleVulncheckCommandFactory } from './goVulncheck';
import { GoTaskProvider } from './goTaskProvider';
import { setTelemetryEnvVars, telemetryReporter } from './goTelemetry';
import { experiments } from './experimental';

const goCtx: GoExtensionContext = {};

Expand Down Expand Up @@ -147,6 +148,9 @@ export async function activate(ctx: vscode.ExtensionContext): Promise<ExtensionA
GoRunTestCodeLensProvider.activate(ctx, goCtx);
GoDebugConfigurationProvider.activate(ctx, goCtx);
GoDebugFactory.activate(ctx, goCtx);
experiments.activate(ctx);
GoTestExplorer.setup(ctx, goCtx);
GoExplorerProvider.setup(ctx);

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

if (isVscodeTestingAPIAvailable && cfg.get<boolean>('testExplorer.enable')) {
GoTestExplorer.setup(ctx, goCtx);
}

GoExplorerProvider.setup(ctx);

registerCommand('go.test.generate.package', goGenerateTests.generateTestCurrentPackage);
registerCommand('go.test.generate.file', goGenerateTests.generateTestCurrentFile);
registerCommand('go.test.generate.function', goGenerateTests.generateTestCurrentFunction);
Expand Down Expand Up @@ -332,15 +330,6 @@ function addOnDidChangeConfigListeners(ctx: vscode.ExtensionContext) {
// TODO: actively maintain our own disposables instead of keeping pushing to ctx.subscription.
}
}
if (e.affectsConfiguration('go.testExplorer.enable')) {
const msg =
'Go test explorer has been enabled or disabled. For this change to take effect, the window must be reloaded.';
vscode.window.showInformationMessage(msg, 'Reload').then((selected) => {
if (selected === 'Reload') {
vscode.commands.executeCommand('workbench.action.reloadWindow');
}
});
}
})
);
}
Expand Down
Loading

0 comments on commit 33b0b78

Please sign in to comment.