diff --git a/README.md b/README.md index 54c29836f..dbc79dc23 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/extension.ts b/src/extension.ts index bc330f3ec..5bdd7f606 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -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'; @@ -69,8 +69,7 @@ export async function activate(context: vscode.ExtensionContext): Promise ], }, initializationOptions: initializationOptions, - initializationFailedHandler: (error) => { - reporter.sendTelemetryException(error); + initializationFailedHandler: () => { return false; }, errorHandler: errorHandler, @@ -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); } } diff --git a/src/utils/vscode.ts b/src/utils/vscode.ts index e77c3148a..fd2b1112c 100644 --- a/src/utils/vscode.ts +++ b/src/utils/vscode.ts @@ -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); @@ -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) { + 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('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); + } +}