Skip to content

Commit

Permalink
bugfix: Run task in the correct directory
Browse files Browse the repository at this point in the history
Also fixed two places where we used the first folder, though this shouldn't be an issue.
  • Loading branch information
tgodzik committed Jun 16, 2023
1 parent e43d90c commit de88aba
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
7 changes: 4 additions & 3 deletions packages/metals-vscode/src/debugger/scalaDebugger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
WorkspaceFolder,
DebugAdapterDescriptor,
DebugConfigurationProviderTriggerKind,
workspace,
tasks,
Task,
ShellExecution,
Expand All @@ -18,6 +17,7 @@ import {
} from "metals-languageclient";
import { ExtendedScalaRunMain, ScalaCodeLensesParams } from "./types";
import { platform } from "os";
import { currentWorkspace } from "../util";

const configurationType = "scala";

Expand Down Expand Up @@ -67,7 +67,8 @@ function platformSpecificOptions(): vscode.ShellExecutionOptions {

async function runMain(main: ExtendedScalaRunMain): Promise<boolean> {
const { environmentVariables, shellCommand } = main.data;
if (workspace.workspaceFolders) {
const workspaceFolder = currentWorkspace();
if (workspaceFolder) {
const env = environmentVariables.reduce<Record<string, string>>(
(acc, envKeyValue) => {
const [key, value] = envKeyValue.split("=");
Expand All @@ -79,7 +80,7 @@ async function runMain(main: ExtendedScalaRunMain): Promise<boolean> {
const shellOptions = { ...platformSpecificOptions(), env };
const task = new Task(
{ type: "scala", task: "run" },
workspace.workspaceFolders[0],
workspaceFolder,
"Scala run",
"Metals",
new ShellExecution(shellCommand, shellOptions)
Expand Down
3 changes: 2 additions & 1 deletion packages/metals-vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ import {
import * as ext from "./hoverExtension";
import { decodeAndShowFile, MetalsFileProvider } from "./metalsContentProvider";
import {
currentWorkspace,
getJavaHomeFromConfig,
getTextDocumentPositionParams,
getValueFromConfig,
Expand Down Expand Up @@ -959,7 +960,7 @@ function launchMetals(
});
});

const workspaceUri = workspace.workspaceFolders?.[0]?.uri;
const workspaceUri = currentWorkspace()?.uri;
// NOTE: we offer a custom symbol search command to work around the limitations of the built-in one, see https://github.com/microsoft/vscode/issues/98125 for more details.
registerCommand(`metals.symbol-search`, () =>
openSymbolSearch(client, metalsFileProvider, workspaceUri)
Expand Down
23 changes: 20 additions & 3 deletions packages/metals-vscode/src/util.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import * as path from "path";
import os from "os";
import {
workspace,
TextEditor,
WorkspaceConfiguration,
ConfigurationTarget,
window,
workspace,
WorkspaceFolder,
} from "vscode";
import {
ExecuteCommandRequest,
Expand Down Expand Up @@ -48,14 +50,29 @@ export function getConfigValue<A>(
}

export function metalsDir(target: ConfigurationTarget): string {
if (target == ConfigurationTarget.Workspace && workspace.workspaceFolders) {
const wsDir = workspace.workspaceFolders[0]?.uri.fsPath;
const workspaceFolder = currentWorkspace();
if (target == ConfigurationTarget.Workspace && workspaceFolder) {
const wsDir = workspaceFolder.uri.fsPath;
return path.join(wsDir, ".metals");
} else {
return path.join(os.homedir(), ".metals");
}
}

export function currentWorkspace(): WorkspaceFolder | undefined {
const activeEditor = window.activeTextEditor;
let workspaceIndex = 0;
if (activeEditor) {
const workspaceFolder = workspace.getWorkspaceFolder(
activeEditor.document.uri
);
if (workspaceFolder) {
workspaceIndex = workspaceFolder.index;
}
}
return workspace.workspaceFolders?.[workspaceIndex];
}

export function getTextDocumentPositionParams(
editor: TextEditor
): TextDocumentPositionParams {
Expand Down

0 comments on commit de88aba

Please sign in to comment.