Skip to content

Commit 7d904ca

Browse files
authored
feat(extension): Update untrusted workspace support from 'false' to 'limited' (#1695)
The workspace trust guide for extension authors can be found here: microsoft/vscode#120251 Importantly the sectino for evaluating whether the extension should work in an untrusted workspace asks "Does my extension treat any contents of the workspace as code?". The answer to this is "yes" for the extension because we execute `ngcc` from the `node_modules` folder. However, this isn't always necessary and becomes less so with time. As library authors publish their libraries with Ivy instructions, we will not need to run `ngcc`. This commit updates the workspace trust to indicate that it's 'limited' support due to `ngcc`. In addition the command to run `ngcc` is disabled on the server and removed from the command palette.
1 parent f8b0db8 commit 7d904ca

File tree

5 files changed

+21
-6
lines changed

5 files changed

+21
-6
lines changed

client/src/client.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,8 @@ function getProbeLocations(bundled: string): string[] {
395395
* Construct the arguments that's used to spawn the server process.
396396
* @param ctx vscode extension context
397397
*/
398-
function constructArgs(ctx: vscode.ExtensionContext, viewEngine: boolean): string[] {
398+
function constructArgs(
399+
ctx: vscode.ExtensionContext, viewEngine: boolean, isTrustedWorkspace: boolean): string[] {
399400
const config = vscode.workspace.getConfiguration();
400401
const args: string[] = ['--logToConsole'];
401402

@@ -440,6 +441,10 @@ function constructArgs(ctx: vscode.ExtensionContext, viewEngine: boolean): strin
440441
args.push('--forceStrictTemplates');
441442
}
442443

444+
if (!isTrustedWorkspace) {
445+
args.push('--untrustedWorkspace');
446+
}
447+
443448
const tsdk: string|null = config.get('typescript.tsdk', null);
444449
const tsProbeLocations = [tsdk, ...getProbeLocations(ctx.extensionPath)];
445450
args.push('--tsProbeLocations', tsProbeLocations.join(','));
@@ -475,7 +480,7 @@ function getServerOptions(ctx: vscode.ExtensionContext, debug: boolean): lsp.Nod
475480
}
476481

477482
// Node module for the language server
478-
const args = constructArgs(ctx, viewEngine);
483+
const args = constructArgs(ctx, viewEngine, vscode.workspace.isTrusted);
479484
const prodBundle = ctx.asAbsolutePath('server');
480485
const devBundle = ctx.asAbsolutePath(path.join('dist', 'server', 'server.js'));
481486
// VS Code Insider launches extensions in debug mode by default but users

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
},
1616
"capabilities": {
1717
"untrustedWorkspaces": {
18-
"supported": false,
19-
"description": "This extension requires workspace trust because it needs to execute ngcc from the node_modules in the workspace."
18+
"supported": "limited",
19+
"description": "This extension requires workspace trust because it needs to execute ngcc from the node_modules in the workspace. Projects do not require ngcc if all library dependencies are published with partial-Ivy or Full-Ivy."
2020
},
2121
"virtualWorkspaces": {
2222
"supported": "limited",
@@ -71,7 +71,7 @@
7171
},
7272
{
7373
"command": "angular.runNgcc",
74-
"when": "!virtualWorkspace"
74+
"when": "!virtualWorkspace && isWorkspaceTrusted"
7575
},
7676
{
7777
"command": "angular.getTemplateTcb",

server/src/cmdline_utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ interface CommandLineOptions {
4444
includeAutomaticOptionalChainCompletions: boolean;
4545
includeCompletionsWithSnippetText: boolean;
4646
forceStrictTemplates: boolean;
47+
untrustedWorkspace: boolean;
4748
}
4849

4950
export function parseCommandLine(argv: string[]): CommandLineOptions {
@@ -60,6 +61,7 @@ export function parseCommandLine(argv: string[]): CommandLineOptions {
6061
hasArgument(argv, '--includeAutomaticOptionalChainCompletions'),
6162
includeCompletionsWithSnippetText: hasArgument(argv, '--includeCompletionsWithSnippetText'),
6263
forceStrictTemplates: hasArgument(argv, '--forceStrictTemplates'),
64+
untrustedWorkspace: hasArgument(argv, '--untrustedWorkspace'),
6365
};
6466
}
6567

server/src/server.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,12 @@ function main() {
4343
ngPlugin: '@angular/language-service',
4444
resolvedNgLsPath: ng.resolvedPath,
4545
ivy: isG3 ? true : options.ivy,
46-
disableAutomaticNgcc: options.disableAutomaticNgcc,
46+
disableAutomaticNgcc: options.disableAutomaticNgcc || options.untrustedWorkspace,
4747
logToConsole: options.logToConsole,
4848
includeAutomaticOptionalChainCompletions: options.includeAutomaticOptionalChainCompletions,
4949
includeCompletionsWithSnippetText: options.includeCompletionsWithSnippetText,
5050
forceStrictTemplates: isG3 || options.forceStrictTemplates,
51+
untrustedWorkspace: options.untrustedWorkspace,
5152
});
5253

5354
// Log initialization info

server/src/session.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export interface SessionOptions {
3232
includeAutomaticOptionalChainCompletions: boolean;
3333
includeCompletionsWithSnippetText: boolean;
3434
forceStrictTemplates: boolean;
35+
untrustedWorkspace: boolean;
3536
}
3637

3738
enum LanguageId {
@@ -61,6 +62,7 @@ export class Session {
6162
private readonly openFiles = new MruTracker();
6263
private readonly includeAutomaticOptionalChainCompletions: boolean;
6364
private readonly includeCompletionsWithSnippetText: boolean;
65+
private readonly untrustedWorkspace: boolean;
6466
private snippetSupport: boolean|undefined;
6567
// Tracks the spawn order and status of the `ngcc` processes. This allows us to ensure we enable
6668
// the LS in the same order the projects were created in.
@@ -84,6 +86,7 @@ export class Session {
8486
this.ivy = options.ivy;
8587
this.disableAutomaticNgcc = options.disableAutomaticNgcc;
8688
this.logToConsole = options.logToConsole;
89+
this.untrustedWorkspace = options.untrustedWorkspace;
8790
// Create a connection for the server. The connection uses Node's IPC as a transport.
8891
this.connection = lsp.createConnection({
8992
// cancelUndispatched is a "middleware" to handle all cancellation requests.
@@ -1133,6 +1136,10 @@ export class Session {
11331136
* Disable the language service, run ngcc, then re-enable language service.
11341137
*/
11351138
private async runNgcc(project: ts.server.Project): Promise<void> {
1139+
if (this.untrustedWorkspace) {
1140+
this.error('Cannot run ngcc in an untrusted workspace.');
1141+
return;
1142+
}
11361143
if (!isConfiguredProject(project) || this.projectNgccQueue.some(p => p.project === project)) {
11371144
return;
11381145
}

0 commit comments

Comments
 (0)