Skip to content

Commit 646faec

Browse files
committed
package.json: use the updated workspace trust API
The workspace trust API has changed. Update to use the new API and utilize 'limited' + 'restrictedConfigurations' capability. If VSCode has a version that supports this new API and the user opted in to this experimental feature using security.workspace.trust.enable setting, we disable our hacky workaround using the Configuration in src/config.ts, and make our workspace toggle command trigger VSCode's trusted workspace configuration command. I wanted to add some of gopls.build.* configuration, but that doesn't seem to work as I expected. More experiments with it is necessary. Updates #1469 Change-Id: Ie192063bfaf151427688da747dd44758afdc2565 Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/317049 Trust: Hyang-Ah Hana Kim <[email protected]> Trust: Suzy Mueller <[email protected]> Run-TryBot: Hyang-Ah Hana Kim <[email protected]> TryBot-Result: kokoro <[email protected]> Reviewed-by: Suzy Mueller <[email protected]>
1 parent e8dc6c4 commit 646faec

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

package.json

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@
8989
"engines": {
9090
"vscode": "^1.52.0"
9191
},
92-
"requiresWorkspaceTrust": "onStart",
9392
"activationEvents": [
9493
"workspaceContains:**/*.go",
9594
"onLanguage:go",
@@ -103,6 +102,19 @@
103102
"onWebviewPanel:welcomeGo"
104103
],
105104
"main": "./dist/goMain.js",
105+
"capabilities": {
106+
"untrustedWorkspaces": {
107+
"supported": "limited",
108+
"restrictedConfigurations": [
109+
"go.alternateTools",
110+
"go.gopath",
111+
"go.goroot",
112+
"go.inferGopath",
113+
"go.toolsGopath",
114+
"go.toolsEnvVars"
115+
]
116+
}
117+
},
106118
"contributes": {
107119
"languages": [
108120
{

src/config.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,29 @@ const SECURITY_SENSITIVE_CONFIG: string[] = [
1717
'toolsEnvVars'
1818
];
1919

20+
// Set true only if the vscode is the recent version that has the workspace trust API AND
21+
// if the security.workspace.trust is enabled. Change of this configuration requires restart
22+
// of VSCode, so we don't need to set up the configuration change listener.
23+
// TODO(hyangah): remove this and Configuration & WrappedConfiguration when we update
24+
// our extension to require 2021 June VSCode engine.
25+
const isVscodeWorkspaceTrustAPIAvailable =
26+
'boolean' === typeof (vscode.workspace as any).isTrusted &&
27+
vscode.workspace.getConfiguration('security.workspace.trust')?.get('enabled') === true;
28+
2029
// Initialize the singleton defaultConfig and register related commands.
2130
// Prompt if workspace configuration was found but had to be ignored until
2231
// the user has to explicitly opt in to trust the workspace.
2332
export async function initConfig(ctx: vscode.ExtensionContext) {
33+
ctx.subscriptions.push(vscode.commands.registerCommand('go.workspace.isTrusted.toggle', toggleWorkspaceIsTrusted));
34+
35+
if (isVscodeWorkspaceTrustAPIAvailable) {
36+
return; // let vscode handle configuration management.
37+
}
38+
2439
const isTrusted = getFromWorkspaceState(WORKSPACE_IS_TRUSTED_KEY, false);
2540
if (isTrusted !== defaultConfig.workspaceIsTrusted()) {
2641
defaultConfig.toggleWorkspaceIsTrusted();
2742
}
28-
ctx.subscriptions.push(vscode.commands.registerCommand('go.workspace.isTrusted.toggle', toggleWorkspaceIsTrusted));
2943

3044
if (isTrusted) {
3145
return;
@@ -65,6 +79,10 @@ function ignoredWorkspaceConfig(cfg: vscode.WorkspaceConfiguration, keys: string
6579
}
6680

6781
async function toggleWorkspaceIsTrusted() {
82+
if (isVscodeWorkspaceTrustAPIAvailable) {
83+
vscode.commands.executeCommand('workbench.action.manageTrust');
84+
return;
85+
}
6886
const v = defaultConfig.toggleWorkspaceIsTrusted();
6987
await updateWorkspaceState(WORKSPACE_IS_TRUSTED_KEY, v);
7088
}
@@ -93,7 +111,19 @@ export class Configuration {
93111
}
94112
}
95113

96-
const defaultConfig = new Configuration();
114+
class vscodeConfiguration {
115+
public toggleWorkspaceIsTrusted() {
116+
/* no-op */
117+
}
118+
public get(section: string, uri?: vscode.Uri): vscode.WorkspaceConfiguration {
119+
return vscode.workspace.getConfiguration(section, uri);
120+
}
121+
public workspaceIsTrusted(): boolean {
122+
return !!(vscode.workspace as any).isTrusted;
123+
}
124+
}
125+
126+
const defaultConfig = isVscodeWorkspaceTrustAPIAvailable ? new vscodeConfiguration() : new Configuration();
97127

98128
// Returns the workspace Configuration used by the extension.
99129
export function DefaultConfig() {

0 commit comments

Comments
 (0)