Skip to content

Commit

Permalink
Auto merge of #15993 - meowtec:fix/workspaces-debug-cwd, r=Veykril
Browse files Browse the repository at this point in the history
Debug use cargo workspace root as `cwd`

fixes #13022
  • Loading branch information
bors committed Nov 30, 2023
2 parents 1c51e25 + 4ca86ed commit c9d189d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
27 changes: 18 additions & 9 deletions editors/code/src/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as vscode from "vscode";
import * as path from "path";
import type * as ra from "./lsp_ext";

import { Cargo, getRustcId, getSysroot } from "./toolchain";
import { Cargo, type ExecutableInfo, getRustcId, getSysroot } from "./toolchain";
import type { Ctx } from "./ctx";
import { prepareEnv } from "./run";
import { unwrapUndefinable } from "./undefinable";
Expand All @@ -12,6 +12,7 @@ const debugOutput = vscode.window.createOutputChannel("Debug");
type DebugConfigProvider = (
config: ra.Runnable,
executable: string,
cargoWorkspace: string,
env: Record<string, string>,
sourceFileMap?: Record<string, string>,
) => vscode.DebugConfiguration;
Expand Down Expand Up @@ -130,7 +131,7 @@ async function getDebugConfiguration(
}

const env = prepareEnv(runnable, ctx.config.runnablesExtraEnv);
const executable = await getDebugExecutable(runnable, env);
const { executable, workspace: cargoWorkspace } = await getDebugExecutableInfo(runnable, env);
let sourceFileMap = debugOptions.sourceFileMap;
if (sourceFileMap === "auto") {
// let's try to use the default toolchain
Expand All @@ -142,7 +143,13 @@ async function getDebugConfiguration(
}

const provider = unwrapUndefinable(knownEngines[debugEngine.id]);
const debugConfig = provider(runnable, simplifyPath(executable), env, sourceFileMap);
const debugConfig = provider(
runnable,
simplifyPath(executable),
cargoWorkspace,
env,
sourceFileMap,
);
if (debugConfig.type in debugOptions.engineSettings) {
const settingsMap = (debugOptions.engineSettings as any)[debugConfig.type];
for (var key in settingsMap) {
Expand All @@ -164,20 +171,21 @@ async function getDebugConfiguration(
return debugConfig;
}

async function getDebugExecutable(
async function getDebugExecutableInfo(
runnable: ra.Runnable,
env: Record<string, string>,
): Promise<string> {
): Promise<ExecutableInfo> {
const cargo = new Cargo(runnable.args.workspaceRoot || ".", debugOutput, env);
const executable = await cargo.executableFromArgs(runnable.args.cargoArgs);
const executableInfo = await cargo.executableInfoFromArgs(runnable.args.cargoArgs);

// if we are here, there were no compilation errors.
return executable;
return executableInfo;
}

function getLldbDebugConfig(
runnable: ra.Runnable,
executable: string,
cargoWorkspace: string,
env: Record<string, string>,
sourceFileMap?: Record<string, string>,
): vscode.DebugConfiguration {
Expand All @@ -187,7 +195,7 @@ function getLldbDebugConfig(
name: runnable.label,
program: executable,
args: runnable.args.executableArgs,
cwd: runnable.args.workspaceRoot,
cwd: cargoWorkspace || runnable.args.workspaceRoot,
sourceMap: sourceFileMap,
sourceLanguages: ["rust"],
env,
Expand All @@ -197,6 +205,7 @@ function getLldbDebugConfig(
function getCppvsDebugConfig(
runnable: ra.Runnable,
executable: string,
cargoWorkspace: string,
env: Record<string, string>,
sourceFileMap?: Record<string, string>,
): vscode.DebugConfiguration {
Expand All @@ -206,7 +215,7 @@ function getCppvsDebugConfig(
name: runnable.label,
program: executable,
args: runnable.args.executableArgs,
cwd: runnable.args.workspaceRoot,
cwd: cargoWorkspace || runnable.args.workspaceRoot,
sourceFileMap,
env,
};
Expand Down
14 changes: 12 additions & 2 deletions editors/code/src/toolchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@ import { unwrapUndefinable } from "./undefinable";

interface CompilationArtifact {
fileName: string;
workspace: string;
name: string;
kind: string;
isTest: boolean;
}

export interface ExecutableInfo {
executable: string;
workspace: string;
}

export interface ArtifactSpec {
cargoArgs: string[];
filter?: (artifacts: CompilationArtifact[]) => CompilationArtifact[];
Expand Down Expand Up @@ -68,6 +74,7 @@ export class Cargo {
artifacts.push({
fileName: message.executable,
name: message.target.name,
workspace: message.manifest_path.replace(/\/Cargo\.toml$/, ""),
kind: message.target.kind[0],
isTest: message.profile.test,
});
Expand All @@ -86,7 +93,7 @@ export class Cargo {
return spec.filter?.(artifacts) ?? artifacts;
}

async executableFromArgs(args: readonly string[]): Promise<string> {
async executableInfoFromArgs(args: readonly string[]): Promise<ExecutableInfo> {
const artifacts = await this.getArtifacts(Cargo.artifactSpec(args));

if (artifacts.length === 0) {
Expand All @@ -96,7 +103,10 @@ export class Cargo {
}

const artifact = unwrapUndefinable(artifacts[0]);
return artifact.fileName;
return {
executable: artifact.fileName,
workspace: artifact.workspace,
};
}

private async runCargo(
Expand Down

0 comments on commit c9d189d

Please sign in to comment.