Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
This repository was archived by the owner on Dec 8, 2020. It is now read-only.

Commit 767bdaf

Browse filesBrowse files
authored
Make cargo use rustup (#276)
1 parent 9f30221 commit 767bdaf
Copy full SHA for 767bdaf

9 files changed

+85
-19
lines changed

‎doc/common_configuration_parameters.md

Copy file name to clipboardExpand all lines: doc/common_configuration_parameters.md
+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ Users should adjust properties of this configuration parameter to customize rust
77
### toolchain
88

99
This configuration parameter specifies which toolchain the extension will invoke rustup with.
10-
It is used for getting sysroot, installing components.
10+
It is used for getting sysroot, installing components, invoking Cargo
1111

1212
However there are few exceptions. Currently RLS is available for nightly hence RLS and rust-analysis are installed for the nightly toolchain.

‎src/CargoInvocationManager.ts

Copy file name to clipboard
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { Configuration } from './components/configuration/Configuration';
2+
import { Rustup } from './components/configuration/Rustup';
3+
4+
/**
5+
* The class defines functions which can be used to get data required to invoke Cargo
6+
*/
7+
export class CargoInvocationManager {
8+
private _configuration: Configuration;
9+
private _rustup: Rustup | undefined;
10+
11+
public constructor(configuration: Configuration, rustup: Rustup | undefined) {
12+
this._configuration = configuration;
13+
this._rustup = rustup;
14+
}
15+
16+
/**
17+
* Cargo can be accessible from multiple places, but the only one is correct.
18+
* This function determines the path to the executable which either Cargo itself or proxy to
19+
* Cargo. If the executable is a proxy to Cargo, then the proxy may require some arguments to
20+
* understand that Cargo is requested. An example is running Cargo using rustup.
21+
*/
22+
public getExecutableAndArgs(): { executable: string, args: string[] } {
23+
const userCargoPath = this._configuration.getCargoPath();
24+
if (userCargoPath) {
25+
return { executable: userCargoPath, args: [] };
26+
}
27+
const userToolchain = this._rustup ? this._rustup.getUserToolchain() : undefined;
28+
if (!userToolchain) {
29+
return { executable: 'cargo', args: [] };
30+
}
31+
const args = ['run', userToolchain.toString(true, false), 'cargo'];
32+
return { executable: Rustup.getRustupExecutable(), args };
33+
}
34+
}

‎src/components/cargo/CargoManager.ts

Copy file name to clipboardExpand all lines: src/components/cargo/CargoManager.ts
+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as tmp from 'tmp';
22
import { Disposable, ExtensionContext, Uri, commands, window, workspace } from 'vscode';
3+
import { CargoInvocationManager } from '../../CargoInvocationManager';
34
import { Configuration } from '../configuration/Configuration';
45
import { CurrentWorkingDirectoryManager }
56
from '../configuration/current_working_directory_manager';
@@ -17,13 +18,15 @@ export class CargoManager {
1718
public constructor(
1819
context: ExtensionContext,
1920
configuration: Configuration,
21+
cargoInvocationManager: CargoInvocationManager,
2022
currentWorkingDirectoryManager: CurrentWorkingDirectoryManager,
2123
logger: ChildLogger
2224
) {
2325
const stopCommandName = 'rust.cargo.terminate';
2426
this._cargoTaskManager = new CargoTaskManager(
2527
context,
2628
configuration,
29+
cargoInvocationManager,
2730
currentWorkingDirectoryManager,
2831
logger.createChildLogger('CargoTaskManager: '),
2932
stopCommandName

‎src/components/cargo/CargoTaskManager.ts

Copy file name to clipboardExpand all lines: src/components/cargo/CargoTaskManager.ts
+16-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { join } from 'path';
22
import { ExtensionContext, window } from 'vscode';
3+
import { CargoInvocationManager } from '../../CargoInvocationManager';
34
import { Configuration } from '../configuration/Configuration';
45
import { CurrentWorkingDirectoryManager }
56
from '../configuration/current_working_directory_manager';
@@ -13,6 +14,7 @@ import { UserDefinedArgs } from './UserDefinedArgs';
1314

1415
export class CargoTaskManager {
1516
private _configuration: Configuration;
17+
private _cargoInvocationManager: CargoInvocationManager;
1618
private _currentWorkingDirectoryManager: CurrentWorkingDirectoryManager;
1719
private _logger: ChildLogger;
1820
private _outputChannelTaskManager: OutputChannelTaskManager;
@@ -21,11 +23,13 @@ export class CargoTaskManager {
2123
public constructor(
2224
context: ExtensionContext,
2325
configuration: Configuration,
26+
cargoInvocationManager: CargoInvocationManager,
2427
currentWorkingDirectoryManager: CurrentWorkingDirectoryManager,
2528
logger: ChildLogger,
2629
stopCommandName: string
2730
) {
2831
this._configuration = configuration;
32+
this._cargoInvocationManager = cargoInvocationManager;
2933
this._currentWorkingDirectoryManager = currentWorkingDirectoryManager;
3034
this._logger = logger;
3135
this._outputChannelTaskManager = new OutputChannelTaskManager(
@@ -155,9 +159,10 @@ export class CargoTaskManager {
155159
workingDirectory
156160
));
157161
}
158-
const cargoPath = this._configuration.getCargoPath();
162+
const { executable, args: preCommandArgs } = this._cargoInvocationManager.getExecutableAndArgs();
159163
this.startTask(
160-
cargoPath,
164+
executable,
165+
preCommandArgs,
161166
command,
162167
args,
163168
workingDirectory,
@@ -241,6 +246,7 @@ export class CargoTaskManager {
241246

242247
private async startTask(
243248
executable: string,
249+
preCommandArgs: string[],
244250
command: string,
245251
args: string[],
246252
cwd: string,
@@ -249,7 +255,13 @@ export class CargoTaskManager {
249255
shouldParseOutput: boolean
250256
): Promise<void> {
251257
if (shouldExecuteCargoCommandInTerminal) {
252-
await this._terminalTaskManager.startTask(executable, command, args, cwd);
258+
await this._terminalTaskManager.startTask(
259+
executable,
260+
preCommandArgs,
261+
command,
262+
args,
263+
cwd
264+
);
253265
} else {
254266
// The output channel should be shown only if the user wants that.
255267
// The only exception is checking invoked on saving the active document - in that case the output channel shouldn't be shown.
@@ -258,6 +270,7 @@ export class CargoTaskManager {
258270
!(command === 'check' && reason === CommandInvocationReason.ActionOnSave);
259271
await this._outputChannelTaskManager.startTask(
260272
executable,
273+
preCommandArgs,
261274
command,
262275
args,
263276
cwd,

‎src/components/cargo/output_channel_task_manager.ts

Copy file name to clipboardExpand all lines: src/components/cargo/output_channel_task_manager.ts
+3-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export class OutputChannelTaskManager {
3131

3232
public async startTask(
3333
executable: string,
34+
preCommandArgs: string[],
3435
command: string,
3536
args: string[],
3637
cwd: string,
@@ -54,7 +55,7 @@ export class OutputChannelTaskManager {
5455
}
5556
}
5657
prependArgsWithMessageFormatIfRequired();
57-
args = [command].concat(args);
58+
args = preCommandArgs.concat(command, ...args);
5859
this.runningTask = new Task(
5960
this.configuration,
6061
this.logger.createChildLogger('Task: '),
@@ -65,7 +66,7 @@ export class OutputChannelTaskManager {
6566
this.runningTask.setStarted(() => {
6667
this.channel.clear();
6768
this.channel.append(`Working directory: ${cwd}\n`);
68-
this.channel.append(`Started cargo ${args.join(' ')}\n\n`);
69+
this.channel.append(`Started ${executable} ${args.join(' ')}\n\n`);
6970
this.diagnostics.clear();
7071
});
7172
this.runningTask.setLineReceivedInStdout(line => {

‎src/components/cargo/terminal_task_manager.ts

Copy file name to clipboardExpand all lines: src/components/cargo/terminal_task_manager.ts
+8-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,14 @@ export class TerminalTaskManager {
3131
}
3232
}
3333

34-
public async startTask(executable: string, command: string, args: string[], cwd: string): Promise<void> {
35-
args = [command].concat(args);
34+
public async startTask(
35+
executable: string,
36+
preCommandArgs: string[],
37+
command: string,
38+
args: string[],
39+
cwd: string
40+
): Promise<void> {
41+
args = preCommandArgs.concat(command, ...args);
3642
const terminal = window.createTerminal('Cargo Task');
3743
this._runningTerminal = terminal;
3844
const shell = parseShell(workspace.getConfiguration('terminal')['integrated']['shell']['windows']);

‎src/components/configuration/Configuration.ts

Copy file name to clipboardExpand all lines: src/components/configuration/Configuration.ts
+2-2
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,8 @@ export class Configuration {
206206
return Configuration.getPathConfigParameter('cargoCwd');
207207
}
208208

209-
public getCargoPath(): string {
210-
return Configuration.getPathConfigParameterOrDefault('cargoPath', 'cargo');
209+
public getCargoPath(): string | undefined {
210+
return Configuration.getPathConfigParameter('cargoPath');
211211
}
212212

213213
public getCargoHomePath(): string | undefined {

‎src/components/configuration/Rustup.ts

Copy file name to clipboardExpand all lines: src/components/configuration/Rustup.ts
+8-7
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ export class Rustup {
4848
*/
4949
private _userToolchain: Toolchain | undefined;
5050

51+
52+
/**
53+
* Returns the executable of Rustup
54+
*/
55+
public static getRustupExecutable(): string {
56+
return 'rustup';
57+
}
58+
5159
/**
5260
* Creates a new instance of the class.
5361
* The method is asynchronous because it tries to find Rust's source code
@@ -373,13 +381,6 @@ export class Rustup {
373381
return !componentInstalled;
374382
}
375383

376-
/**
377-
* Returns the executable of Rustup
378-
*/
379-
private static getRustupExecutable(): string {
380-
return 'rustup';
381-
}
382-
383384
/**
384385
* Returns the name of the component rust-analysis
385386
*/

‎src/extension.ts

Copy file name to clipboardExpand all lines: src/extension.ts
+10-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { Manager as LanguageClientManager } from './components/language_client/m
1414
import { LoggingManager } from './components/logging/logging_manager';
1515
import { ChildLogger } from './components/logging/child_logger';
1616
import { RootLogger } from './components/logging/root_logger';
17+
import { CargoInvocationManager } from './CargoInvocationManager';
1718
import { LegacyModeManager } from './legacy_mode_manager';
1819
import * as OutputChannelProcess from './OutputChannelProcess';
1920
import { Toolchain } from './Toolchain';
@@ -85,19 +86,22 @@ class RlsMode {
8586
private _configuration: Configuration;
8687
private _rlsConfiguration: RlsConfiguration;
8788
private _rustup: Rustup | undefined;
89+
private _cargoInvocationManager: CargoInvocationManager;
8890
private _logger: ChildLogger;
8991
private _extensionContext: ExtensionContext;
9092

9193
public constructor(
9294
configuration: Configuration,
9395
rlsConfiguration: RlsConfiguration,
9496
rustup: Rustup | undefined,
97+
cargoInvocationManager: CargoInvocationManager,
9598
logger: ChildLogger,
9699
extensionContext: ExtensionContext
97100
) {
98101
this._configuration = configuration;
99102
this._rlsConfiguration = rlsConfiguration;
100103
this._rustup = rustup;
104+
this._cargoInvocationManager = cargoInvocationManager;
101105
this._logger = logger;
102106
this._extensionContext = extensionContext;
103107
}
@@ -240,9 +244,10 @@ class RlsMode {
240244
switch (choice) {
241245
case installRustfmtChoice:
242246
logger.debug('User decided to install rustfmt');
247+
const { executable, args } = this._cargoInvocationManager.getExecutableAndArgs();
243248
const result = await OutputChannelProcess.create(
244-
this._configuration.getCargoPath(),
245-
['install', 'rustfmt'],
249+
executable,
250+
[...args, 'install', 'rustfmt'],
246251
undefined,
247252
'Installing rustfmt'
248253
);
@@ -399,6 +404,7 @@ export async function activate(ctx: ExtensionContext): Promise<void> {
399404
}
400405
const rustSource = await RustSource.create(rustup);
401406
const configuration = new Configuration(logger.createChildLogger('Configuration: '));
407+
const cargoInvocationManager = new CargoInvocationManager(configuration, rustup);
402408
const rlsConfiguration = await RlsConfiguration.create(rustup, rustSource);
403409
if (configuration.mode() === undefined) {
404410
// The current configuration does not contain any specified mode and hence we should try to
@@ -419,6 +425,7 @@ export async function activate(ctx: ExtensionContext): Promise<void> {
419425
const cargoManager = new CargoManager(
420426
ctx,
421427
configuration,
428+
cargoInvocationManager,
422429
currentWorkingDirectoryManager,
423430
logger.createChildLogger('Cargo Manager: ')
424431
);
@@ -428,6 +435,7 @@ export async function activate(ctx: ExtensionContext): Promise<void> {
428435
configuration,
429436
rlsConfiguration,
430437
rustup,
438+
cargoInvocationManager,
431439
logger.createChildLogger('RlsMode: '),
432440
ctx
433441
);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.