2020-04-28 14:30:49 +00:00
|
|
|
import * as cp from "child_process";
|
2020-05-05 22:42:04 +00:00
|
|
|
import * as os from "os";
|
|
|
|
import * as path from "path";
|
2020-04-28 14:30:49 +00:00
|
|
|
import * as readline from "readline";
|
2021-05-24 02:37:10 +00:00
|
|
|
import * as vscode from "vscode";
|
2024-07-16 11:35:14 +00:00
|
|
|
import { log, memoizeAsync, unwrapUndefinable } from "./util";
|
2024-07-06 15:01:40 +00:00
|
|
|
import type { CargoRunnableArgs } from "./lsp_ext";
|
2020-04-28 14:30:49 +00:00
|
|
|
|
|
|
|
interface CompilationArtifact {
|
|
|
|
fileName: string;
|
|
|
|
name: string;
|
|
|
|
kind: string;
|
|
|
|
isTest: boolean;
|
|
|
|
}
|
|
|
|
|
2020-05-20 18:03:49 +00:00
|
|
|
export interface ArtifactSpec {
|
|
|
|
cargoArgs: string[];
|
|
|
|
filter?: (artifacts: CompilationArtifact[]) => CompilationArtifact[];
|
|
|
|
}
|
|
|
|
|
2020-05-31 02:13:08 +00:00
|
|
|
export class Cargo {
|
2023-03-30 06:22:20 +00:00
|
|
|
constructor(
|
|
|
|
readonly rootFolder: string,
|
|
|
|
readonly output: vscode.OutputChannel,
|
2023-07-11 13:35:10 +00:00
|
|
|
readonly env: Record<string, string>,
|
2023-03-30 06:22:20 +00:00
|
|
|
) {}
|
2020-05-20 18:03:49 +00:00
|
|
|
|
2020-05-31 02:13:08 +00:00
|
|
|
// Made public for testing purposes
|
2024-07-06 15:01:40 +00:00
|
|
|
static artifactSpec(cargoArgs: string[], executableArgs?: string[]): ArtifactSpec {
|
|
|
|
cargoArgs = [...cargoArgs, "--message-format=json"];
|
2020-05-31 02:13:08 +00:00
|
|
|
// arguments for a runnable from the quick pick should be updated.
|
|
|
|
// see crates\rust-analyzer\src\main_loop\handlers.rs, handle_code_lens
|
|
|
|
switch (cargoArgs[0]) {
|
|
|
|
case "run":
|
|
|
|
cargoArgs[0] = "build";
|
|
|
|
break;
|
|
|
|
case "test": {
|
|
|
|
if (!cargoArgs.includes("--no-run")) {
|
|
|
|
cargoArgs.push("--no-run");
|
|
|
|
}
|
|
|
|
break;
|
2020-05-20 18:03:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-31 02:13:08 +00:00
|
|
|
const result: ArtifactSpec = { cargoArgs: cargoArgs };
|
2022-06-29 11:16:55 +00:00
|
|
|
if (cargoArgs[0] === "test" || cargoArgs[0] === "bench") {
|
2020-05-31 02:13:08 +00:00
|
|
|
// for instance, `crates\rust-analyzer\tests\heavy_tests\main.rs` tests
|
|
|
|
// produce 2 artifacts: {"kind": "bin"} and {"kind": "test"}
|
|
|
|
result.filter = (artifacts) => artifacts.filter((it) => it.isTest);
|
|
|
|
}
|
2024-07-06 15:01:40 +00:00
|
|
|
if (executableArgs) {
|
|
|
|
cargoArgs.push("--", ...executableArgs);
|
|
|
|
}
|
2020-05-20 18:03:49 +00:00
|
|
|
|
2020-05-31 02:13:08 +00:00
|
|
|
return result;
|
|
|
|
}
|
2020-04-28 14:30:49 +00:00
|
|
|
|
2024-07-16 11:35:14 +00:00
|
|
|
private async getArtifacts(
|
|
|
|
spec: ArtifactSpec,
|
|
|
|
env?: Record<string, string>,
|
|
|
|
): Promise<CompilationArtifact[]> {
|
2020-05-21 08:34:34 +00:00
|
|
|
const artifacts: CompilationArtifact[] = [];
|
2020-04-28 14:30:49 +00:00
|
|
|
|
|
|
|
try {
|
2020-05-20 18:03:49 +00:00
|
|
|
await this.runCargo(
|
|
|
|
spec.cargoArgs,
|
2020-04-28 14:30:49 +00:00
|
|
|
(message) => {
|
2020-04-30 15:41:48 +00:00
|
|
|
if (message.reason === "compiler-artifact" && message.executable) {
|
|
|
|
const isBinary = message.target.crate_types.includes("bin");
|
|
|
|
const isBuildScript = message.target.kind.includes("custom-build");
|
2020-04-28 14:30:49 +00:00
|
|
|
if ((isBinary && !isBuildScript) || message.profile.test) {
|
|
|
|
artifacts.push({
|
|
|
|
fileName: message.executable,
|
|
|
|
name: message.target.name,
|
|
|
|
kind: message.target.kind[0],
|
|
|
|
isTest: message.profile.test,
|
2020-04-30 15:41:48 +00:00
|
|
|
});
|
2020-04-28 14:30:49 +00:00
|
|
|
}
|
2020-05-05 22:22:02 +00:00
|
|
|
} else if (message.reason === "compiler-message") {
|
2020-04-30 12:25:04 +00:00
|
|
|
this.output.append(message.message.rendered);
|
|
|
|
}
|
2020-04-28 14:30:49 +00:00
|
|
|
},
|
2023-07-11 13:35:10 +00:00
|
|
|
(stderr) => this.output.append(stderr),
|
2024-07-16 11:35:14 +00:00
|
|
|
env,
|
2020-04-28 14:30:49 +00:00
|
|
|
);
|
2020-05-05 22:22:02 +00:00
|
|
|
} catch (err) {
|
2020-04-30 12:25:04 +00:00
|
|
|
this.output.show(true);
|
2020-04-28 14:30:49 +00:00
|
|
|
throw new Error(`Cargo invocation has failed: ${err}`);
|
|
|
|
}
|
|
|
|
|
2020-05-21 08:34:34 +00:00
|
|
|
return spec.filter?.(artifacts) ?? artifacts;
|
2020-04-28 14:30:49 +00:00
|
|
|
}
|
|
|
|
|
2024-07-06 15:01:40 +00:00
|
|
|
async executableFromArgs(runnableArgs: CargoRunnableArgs): Promise<string> {
|
|
|
|
const artifacts = await this.getArtifacts(
|
|
|
|
Cargo.artifactSpec(runnableArgs.cargoArgs, runnableArgs.executableArgs),
|
2024-07-16 11:35:14 +00:00
|
|
|
runnableArgs.environment,
|
2024-07-06 15:01:40 +00:00
|
|
|
);
|
2020-04-28 14:30:49 +00:00
|
|
|
|
2020-04-30 15:41:48 +00:00
|
|
|
if (artifacts.length === 0) {
|
2020-04-28 14:30:49 +00:00
|
|
|
throw new Error("No compilation artifacts");
|
|
|
|
} else if (artifacts.length > 1) {
|
|
|
|
throw new Error("Multiple compilation artifacts are not supported.");
|
|
|
|
}
|
|
|
|
|
2023-06-27 19:03:53 +00:00
|
|
|
const artifact = unwrapUndefinable(artifacts[0]);
|
2024-05-22 06:27:06 +00:00
|
|
|
return artifact.fileName;
|
2020-04-28 14:30:49 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 16:19:45 +00:00
|
|
|
private async runCargo(
|
2020-04-28 14:30:49 +00:00
|
|
|
cargoArgs: string[],
|
|
|
|
onStdoutJson: (obj: any) => void,
|
2023-07-11 13:35:10 +00:00
|
|
|
onStderrString: (data: string) => void,
|
2024-07-16 11:35:14 +00:00
|
|
|
env?: Record<string, string>,
|
2020-04-28 14:30:49 +00:00
|
|
|
): Promise<number> {
|
2024-07-16 11:35:14 +00:00
|
|
|
const path = await cargoPath(env);
|
2021-08-15 16:19:45 +00:00
|
|
|
return await new Promise((resolve, reject) => {
|
|
|
|
const cargo = cp.spawn(path, cargoArgs, {
|
2020-04-28 14:30:49 +00:00
|
|
|
stdio: ["ignore", "pipe", "pipe"],
|
2020-05-05 22:22:02 +00:00
|
|
|
cwd: this.rootFolder,
|
2023-03-30 06:22:20 +00:00
|
|
|
env: this.env,
|
2020-04-28 14:30:49 +00:00
|
|
|
});
|
|
|
|
|
2020-05-05 22:22:02 +00:00
|
|
|
cargo.on("error", (err) => reject(new Error(`could not launch cargo: ${err}`)));
|
|
|
|
|
|
|
|
cargo.stderr.on("data", (chunk) => onStderrString(chunk.toString()));
|
2020-04-28 14:30:49 +00:00
|
|
|
|
2020-04-30 15:41:48 +00:00
|
|
|
const rl = readline.createInterface({ input: cargo.stdout });
|
2020-04-28 14:30:49 +00:00
|
|
|
rl.on("line", (line) => {
|
2020-04-30 15:41:48 +00:00
|
|
|
const message = JSON.parse(line);
|
2020-04-28 14:30:49 +00:00
|
|
|
onStdoutJson(message);
|
|
|
|
});
|
|
|
|
|
|
|
|
cargo.on("exit", (exitCode, _) => {
|
2020-04-30 15:41:48 +00:00
|
|
|
if (exitCode === 0) resolve(exitCode);
|
2020-04-28 14:30:49 +00:00
|
|
|
else reject(new Error(`exit code: ${exitCode}.`));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2020-05-05 22:22:02 +00:00
|
|
|
}
|
2020-05-05 22:42:04 +00:00
|
|
|
|
2020-08-12 14:52:28 +00:00
|
|
|
/** Mirrors `toolchain::cargo()` implementation */
|
2024-06-17 12:06:01 +00:00
|
|
|
// FIXME: The server should provide this
|
2024-07-16 11:35:14 +00:00
|
|
|
export function cargoPath(env?: Record<string, string>): Promise<string> {
|
|
|
|
if (env?.["RUSTC_TOOLCHAIN"]) {
|
|
|
|
return Promise.resolve("cargo");
|
|
|
|
}
|
2020-05-31 02:13:08 +00:00
|
|
|
return getPathForExecutable("cargo");
|
|
|
|
}
|
|
|
|
|
2020-08-12 14:52:28 +00:00
|
|
|
/** Mirrors `toolchain::get_path_for_executable()` implementation */
|
2024-06-19 07:36:22 +00:00
|
|
|
const getPathForExecutable = memoizeAsync(
|
2020-05-31 02:13:08 +00:00
|
|
|
// We apply caching to decrease file-system interactions
|
2021-08-15 16:19:45 +00:00
|
|
|
async (executableName: "cargo" | "rustc" | "rustup"): Promise<string> => {
|
2020-05-31 02:13:08 +00:00
|
|
|
{
|
|
|
|
const envVar = process.env[executableName.toUpperCase()];
|
|
|
|
if (envVar) return envVar;
|
|
|
|
}
|
|
|
|
|
2021-08-15 16:19:45 +00:00
|
|
|
if (await lookupInPath(executableName)) return executableName;
|
2020-05-05 22:42:04 +00:00
|
|
|
|
2023-02-24 06:26:25 +00:00
|
|
|
const cargoHome = getCargoHome();
|
|
|
|
if (cargoHome) {
|
|
|
|
const standardPath = vscode.Uri.joinPath(cargoHome, "bin", executableName);
|
2021-08-17 07:24:13 +00:00
|
|
|
if (await isFileAtUri(standardPath)) return standardPath.fsPath;
|
2020-05-31 02:13:08 +00:00
|
|
|
}
|
|
|
|
return executableName;
|
2023-07-11 13:35:10 +00:00
|
|
|
},
|
2020-05-31 02:13:08 +00:00
|
|
|
);
|
2020-05-05 22:42:04 +00:00
|
|
|
|
2021-08-15 16:19:45 +00:00
|
|
|
async function lookupInPath(exec: string): Promise<boolean> {
|
2023-06-28 09:15:30 +00:00
|
|
|
const paths = process.env["PATH"] ?? "";
|
2020-05-05 22:42:04 +00:00
|
|
|
|
2020-05-31 02:13:08 +00:00
|
|
|
const candidates = paths.split(path.delimiter).flatMap((dirInPath) => {
|
|
|
|
const candidate = path.join(dirInPath, exec);
|
|
|
|
return os.type() === "Windows_NT" ? [candidate, `${candidate}.exe`] : [candidate];
|
|
|
|
});
|
2020-05-05 22:42:04 +00:00
|
|
|
|
2021-08-15 16:19:45 +00:00
|
|
|
for await (const isFile of candidates.map(isFileAtPath)) {
|
|
|
|
if (isFile) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2020-05-31 02:13:08 +00:00
|
|
|
}
|
2020-05-05 22:42:04 +00:00
|
|
|
|
2023-02-24 06:26:25 +00:00
|
|
|
function getCargoHome(): vscode.Uri | null {
|
|
|
|
const envVar = process.env["CARGO_HOME"];
|
|
|
|
if (envVar) return vscode.Uri.file(envVar);
|
|
|
|
|
|
|
|
try {
|
|
|
|
// hmm, `os.homedir()` seems to be infallible
|
|
|
|
// it is not mentioned in docs and cannot be inferred by the type signature...
|
|
|
|
return vscode.Uri.joinPath(vscode.Uri.file(os.homedir()), ".cargo");
|
|
|
|
} catch (err) {
|
|
|
|
log.error("Failed to read the fs info", err);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-06-15 17:29:02 +00:00
|
|
|
async function isFileAtPath(path: string): Promise<boolean> {
|
|
|
|
return isFileAtUri(vscode.Uri.file(path));
|
|
|
|
}
|
|
|
|
|
|
|
|
async function isFileAtUri(uri: vscode.Uri): Promise<boolean> {
|
|
|
|
try {
|
|
|
|
return ((await vscode.workspace.fs.stat(uri)).type & vscode.FileType.File) !== 0;
|
|
|
|
} catch {
|
|
|
|
return false;
|
|
|
|
}
|
2020-05-05 22:42:04 +00:00
|
|
|
}
|