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.ts36
1 files changed, 35 insertions, 1 deletions
diff --git a/editors/code/src/cargo.ts b/editors/code/src/cargo.ts
index 613aa82da..2a2c2e0e1 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;
@@ -63,7 +66,14 @@ export class Cargo {
63 onStderrString: (data: string) => void 66 onStderrString: (data: string) => void
64 ): Promise<number> { 67 ): Promise<number> {
65 return new Promise((resolve, reject) => { 68 return new Promise((resolve, reject) => {
66 const cargo = cp.spawn('cargo', cargoArgs, { 69 let cargoPath;
70 try {
71 cargoPath = getCargoPathOrFail();
72 } catch (err) {
73 return reject(err);
74 }
75
76 const cargo = cp.spawn(cargoPath, cargoArgs, {
67 stdio: ['ignore', 'pipe', 'pipe'], 77 stdio: ['ignore', 'pipe', 'pipe'],
68 cwd: this.rootFolder 78 cwd: this.rootFolder
69 }); 79 });
@@ -87,3 +97,27 @@ export class Cargo {
87 }); 97 });
88 } 98 }
89} 99}
100
101// Mirrors `ra_env::get_path_for_executable` implementation
102function getCargoPathOrFail(): string {
103 const envVar = process.env.CARGO;
104 const executableName = "cargo";
105
106 if (envVar) {
107 if (isValidExecutable(envVar)) return envVar;
108
109 throw new Error(`\`${envVar}\` environment variable points to something that's not a valid executable`);
110 }
111
112 if (isValidExecutable(executableName)) return executableName;
113
114 const standardLocation = path.join(os.homedir(), '.cargo', 'bin', executableName);
115
116 if (isValidExecutable(standardLocation)) return standardLocation;
117
118 throw new Error(
119 `Failed to find \`${executableName}\` executable. ` +
120 `Make sure \`${executableName}\` is in \`$PATH\`, ` +
121 `or set \`${envVar}\` to point to a valid executable.`
122 );
123}