Skip to content

Commit

Permalink
Handle Invalid WSL Uri (#1219)
Browse files Browse the repository at this point in the history
This adds logic to handle UNC WSL Paths that will not work in terraform-ls. It prompts the user to switch to the Remote WSL extension or allows the user to ignore the prompt.
  • Loading branch information
jpogran authored Sep 7, 2022
1 parent 5a20ad4 commit b8615ae
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 11 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,14 @@ To provide the extension with an up-to-date schema for the Terraform providers u
1. Open any folder or VS Code workspace containing Terraform files.
1. Open the Command Palette and run `Terraform: init current folder` or perform a `terraform init` from the terminal.

### Remote Extension support

The Visual Studio Code [Remote - WSL extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-wsl) lets you use the Windows Subsystem for Linux (WSL) as your full-time development environment right from VS Code. You can author Terraform configuration files in a Linux-based environment, use Linux-specific toolchains and utilities from the comfort of Windows.

The Remote WSL extension runs the [HashiCorp Extension](https://marketplace.visualstudio.com/items?itemName=HashiCorp.terraform) and other extensions directly in WSL so you can edit files located in WSL or the mounted Windows filesystem (for example /mnt/c) without worrying about pathing issues, binary compatibility, or other cross-OS challenges.

For a detailed walkthrough for how to get started using WSL and VS Code, see https://code.visualstudio.com/docs/remote/wsl-tutorial.

## Configuration

The extension does not require any initial configuration and should work out of the box. To take advantage of additional VS Code features or experimental extension features you can configure settings to customize behavior.
Expand Down
14 changes: 3 additions & 11 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { GenerateBugReportCommand } from './commands/generateBugReport';
import { ModuleCallsDataProvider } from './providers/moduleCalls';
import { ModuleProvidersDataProvider } from './providers/moduleProviders';
import { ServerPath } from './utils/serverPath';
import { config } from './utils/vscode';
import { config, handleLanguageClientStart } from './utils/vscode';
import { TelemetryFeature } from './features/telemetry';
import { ShowReferencesFeature } from './features/showReferences';
import { CustomSemanticTokens } from './features/semanticTokens';
Expand Down Expand Up @@ -69,8 +69,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
],
},
initializationOptions: initializationOptions,
initializationFailedHandler: (error) => {
reporter.sendTelemetryException(error);
initializationFailedHandler: () => {
return false;
},
errorHandler: errorHandler,
Expand Down Expand Up @@ -124,14 +123,7 @@ async function startLanguageServer(ctx: vscode.ExtensionContext) {
console.log(`Multi-folder support: ${multiFoldersSupported}`);
}
} catch (error) {
console.log(error); // for test failure reporting
if (error instanceof Error) {
reporter.sendTelemetryException(error);
vscode.window.showErrorMessage(error.message);
} else if (typeof error === 'string') {
vscode.window.showErrorMessage(error);
reporter.sendTelemetryException(new Error(error));
}
await handleLanguageClientStart(error, ctx, reporter);
}
}

Expand Down
68 changes: 68 additions & 0 deletions src/utils/vscode.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import TelemetryReporter from '@vscode/extension-telemetry';
import * as vscode from 'vscode';
import { InitializeError, ResponseError } from 'vscode-languageclient';

export function config(section: string, scope?: vscode.ConfigurationScope): vscode.WorkspaceConfiguration {
return vscode.workspace.getConfiguration(section, scope);
Expand Down Expand Up @@ -160,3 +162,69 @@ export function isTerraformFile(document?: vscode.TextDocument): boolean {
// be safe and default to false
return false;
}

export async function handleLanguageClientStart(
error: unknown,
ctx: vscode.ExtensionContext,
reporter: TelemetryReporter,
) {
let message = 'Unknown Error';
if (error instanceof ResponseError<InitializeError>) {
message = error.data;
reporter.sendTelemetryException(error);
} else if (error instanceof Error) {
message = error.message;
reporter.sendTelemetryException(error);
} else if (typeof error === 'string') {
message = error;
reporter.sendTelemetryException(new Error(error));
}

if (message === 'INVALID_URI_WSL') {
// handle in startLanguageServer()
if (ctx.globalState.get<boolean>('terraform.disableWSLNotification') === true) {
return;
}

const messageText =
'It looks like you opened a WSL url using a Windows UNC path' +
' outside of the [Remote WSL extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-wsl).' +
' The HashiCorp Terraform Extension works seamlessly with the Remote WSL Extension, but cannot work with this URL. Would you like to reopen this folder' +
' in the WSL Extension?';

const choice = await vscode.window.showErrorMessage(
messageText,
{
detail: messageText,
modal: false,
},
{ title: 'Reopen Folder in WSL' },
{ title: 'More Info' },
{ title: 'Supress' },
);
if (choice === undefined) {
return;
}

switch (choice.title) {
case 'Suppress':
reporter.sendTelemetryEvent('disableWSLNotification');
ctx.globalState.update('terraform.disableWSLNotification', true);
break;
case 'Reopen Folder in WSL':
reporter.sendTelemetryEvent('reopenInWSL');
await vscode.commands.executeCommand('remote-wsl.reopenInWSL');
break;
case 'More Info':
reporter.sendTelemetryEvent('wslMoreInfo');
await vscode.commands.executeCommand(
'vscode.open',
vscode.Uri.parse(
'https://github.com/hashicorp/vscode-terraform/blob/v2.24.0/README.md#remote-extension-support',
),
);
}
} else {
vscode.window.showErrorMessage(message);
}
}

0 comments on commit b8615ae

Please sign in to comment.