aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/cargo.ts
diff options
context:
space:
mode:
Diffstat (limited to 'editors/code/src/cargo.ts')
-rw-r--r--editors/code/src/cargo.ts100
1 files changed, 67 insertions, 33 deletions
diff --git a/editors/code/src/cargo.ts b/editors/code/src/cargo.ts
index a328ba9bd..6a41873d0 100644
--- a/editors/code/src/cargo.ts
+++ b/editors/code/src/cargo.ts
@@ -1,6 +1,9 @@
1import * as cp from 'child_process'; 1import * as cp from 'child_process';
2import * as os from 'os';
3import * as path from 'path';
2import * as readline from 'readline'; 4import * as readline from 'readline';
3import { OutputChannel } from 'vscode'; 5import { OutputChannel } from 'vscode';
6import { isValidExecutable } from './util';
4 7
5interface CompilationArtifact { 8interface CompilationArtifact {
6 fileName: string; 9 fileName: string;
@@ -10,17 +13,9 @@ interface CompilationArtifact {
10} 13}
11 14
12export class Cargo { 15export class Cargo {
13 rootFolder: string; 16 constructor(readonly rootFolder: string, readonly output: OutputChannel) { }
14 env?: Record<string, string>;
15 output: OutputChannel;
16
17 public constructor(cargoTomlFolder: string, output: OutputChannel, env: Record<string, string> | undefined = undefined) {
18 this.rootFolder = cargoTomlFolder;
19 this.output = output;
20 this.env = env;
21 }
22 17
23 public async artifactsFromArgs(cargoArgs: string[]): Promise<CompilationArtifact[]> { 18 private async artifactsFromArgs(cargoArgs: string[]): Promise<CompilationArtifact[]> {
24 const artifacts: CompilationArtifact[] = []; 19 const artifacts: CompilationArtifact[] = [];
25 20
26 try { 21 try {
@@ -37,17 +32,13 @@ export class Cargo {
37 isTest: message.profile.test 32 isTest: message.profile.test
38 }); 33 });
39 } 34 }
40 } 35 } else if (message.reason === 'compiler-message') {
41 else if (message.reason === 'compiler-message') {
42 this.output.append(message.message.rendered); 36 this.output.append(message.message.rendered);
43 } 37 }
44 }, 38 },
45 stderr => { 39 stderr => this.output.append(stderr),
46 this.output.append(stderr);
47 }
48 ); 40 );
49 } 41 } catch (err) {
50 catch (err) {
51 this.output.show(true); 42 this.output.show(true);
52 throw new Error(`Cargo invocation has failed: ${err}`); 43 throw new Error(`Cargo invocation has failed: ${err}`);
53 } 44 }
@@ -55,11 +46,27 @@ export class Cargo {
55 return artifacts; 46 return artifacts;
56 } 47 }
57 48
58 public async executableFromArgs(args: string[]): Promise<string> { 49 async executableFromArgs(args: readonly string[]): Promise<string> {
59 const cargoArgs = [...args]; // to remain args unchanged 50 const cargoArgs = [...args, "--message-format=json"];
60 cargoArgs.push("--message-format=json");
61 51
62 const artifacts = await this.artifactsFromArgs(cargoArgs); 52 // arguments for a runnable from the quick pick should be updated.
53 // see crates\rust-analyzer\src\main_loop\handlers.rs, handle_code_lens
54 switch (cargoArgs[0]) {
55 case "run": cargoArgs[0] = "build"; break;
56 case "test": {
57 if (cargoArgs.indexOf("--no-run") === -1) {
58 cargoArgs.push("--no-run");
59 }
60 break;
61 }
62 }
63
64 let artifacts = await this.artifactsFromArgs(cargoArgs);
65 if (cargoArgs[0] === "test") {
66 // for instance, `crates\rust-analyzer\tests\heavy_tests\main.rs` tests
67 // produce 2 artifacts: {"kind": "bin"} and {"kind": "test"}
68 artifacts = artifacts.filter(a => a.isTest);
69 }
63 70
64 if (artifacts.length === 0) { 71 if (artifacts.length === 0) {
65 throw new Error('No compilation artifacts'); 72 throw new Error('No compilation artifacts');
@@ -70,24 +77,27 @@ export class Cargo {
70 return artifacts[0].fileName; 77 return artifacts[0].fileName;
71 } 78 }
72 79
73 runCargo( 80 private runCargo(
74 cargoArgs: string[], 81 cargoArgs: string[],
75 onStdoutJson: (obj: any) => void, 82 onStdoutJson: (obj: any) => void,
76 onStderrString: (data: string) => void 83 onStderrString: (data: string) => void
77 ): Promise<number> { 84 ): Promise<number> {
78 return new Promise<number>((resolve, reject) => { 85 return new Promise((resolve, reject) => {
79 const cargo = cp.spawn('cargo', cargoArgs, { 86 let cargoPath;
87 try {
88 cargoPath = getCargoPathOrFail();
89 } catch (err) {
90 return reject(err);
91 }
92
93 const cargo = cp.spawn(cargoPath, cargoArgs, {
80 stdio: ['ignore', 'pipe', 'pipe'], 94 stdio: ['ignore', 'pipe', 'pipe'],
81 cwd: this.rootFolder, 95 cwd: this.rootFolder
82 env: this.env,
83 }); 96 });
84 97
85 cargo.on('error', err => { 98 cargo.on('error', err => reject(new Error(`could not launch cargo: ${err}`)));
86 reject(new Error(`could not launch cargo: ${err}`)); 99
87 }); 100 cargo.stderr.on('data', chunk => onStderrString(chunk.toString()));
88 cargo.stderr.on('data', chunk => {
89 onStderrString(chunk.toString());
90 });
91 101
92 const rl = readline.createInterface({ input: cargo.stdout }); 102 const rl = readline.createInterface({ input: cargo.stdout });
93 rl.on('line', line => { 103 rl.on('line', line => {
@@ -103,4 +113,28 @@ export class Cargo {
103 }); 113 });
104 }); 114 });
105 } 115 }
106} \ No newline at end of file 116}
117
118// Mirrors `ra_env::get_path_for_executable` implementation
119function getCargoPathOrFail(): string {
120 const envVar = process.env.CARGO;
121 const executableName = "cargo";
122
123 if (envVar) {
124 if (isValidExecutable(envVar)) return envVar;
125
126 throw new Error(`\`${envVar}\` environment variable points to something that's not a valid executable`);
127 }
128
129 if (isValidExecutable(executableName)) return executableName;
130
131 const standardLocation = path.join(os.homedir(), '.cargo', 'bin', executableName);
132
133 if (isValidExecutable(standardLocation)) return standardLocation;
134
135 throw new Error(
136 `Failed to find \`${executableName}\` executable. ` +
137 `Make sure \`${executableName}\` is in \`$PATH\`, ` +
138 `or set \`${envVar}\` to point to a valid executable.`
139 );
140}