Skip to content

Commit a758e34

Browse files
committed
Document CargoTaskDefinition and factor out converting TaskDefinition to Execution
1 parent e8d6a5e commit a758e34

2 files changed

Lines changed: 48 additions & 34 deletions

File tree

‎editors/code/src/run.ts‎

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import * as vscode from "vscode";
22
importtype*aslcfrom"vscode-languageclient";
33
import*asrafrom"./lsp_ext";
44
import*astasksfrom"./tasks";
5-
import*astoolchainfrom"./toolchain";
65

76
importtype{CtxInit}from"./ctx";
87
import{makeDebugConfig}from"./debug";
@@ -112,22 +111,12 @@ export async function createTask(runnable: ra.Runnable, config: Config): Promise
112111
throw`Unexpected runnable kind: ${runnable.kind}`;
113112
}
114113

115-
letprogram: string;
116-
letargs=createArgs(runnable);
117-
if(runnable.args.overrideCargo){
118-
// Split on spaces to allow overrides like "wrapper cargo".
119-
constcargoParts=runnable.args.overrideCargo.split(" ");
120-
121-
program=unwrapUndefinable(cargoParts[0]);
122-
args=[...cargoParts.slice(1), ...args];
123-
}else{
124-
program=awaittoolchain.cargoPath();
125-
}
114+
constargs=createArgs(runnable);
126115

127116
constdefinition: tasks.CargoTaskDefinition={
128117
type: tasks.TASK_TYPE,
129-
program,
130-
args,
118+
command: unwrapUndefinable(args[0]),// run, test, etc...
119+
args: args.slice(1),
131120
cwd: runnable.args.workspaceRoot||".",
132121
env: prepareEnv(runnable,config.runnablesExtraEnv),
133122
overrideCargo: runnable.args.overrideCargo,

‎editors/code/src/tasks.ts‎

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,26 @@ import * as vscode from "vscode";
22
import*astoolchainfrom"./toolchain";
33
importtype{Config}from"./config";
44
import{log}from"./util";
5+
import{unwrapUndefinable}from"./undefinable";
56

67
// This ends up as the `type` key in tasks.json. RLS also uses `cargo` and
78
// our configuration should be compatible with it so use the same key.
89
exportconstTASK_TYPE="cargo";
10+
911
exportconstTASK_SOURCE="rust";
1012

1113
exportinterfaceCargoTaskDefinitionextendsvscode.TaskDefinition{
12-
program: string;
13-
args: string[];
14+
// The cargo command, such as "run" or "check".
15+
command: string;
16+
// Additional arguments passed to the cargo command.
17+
args?: string[];
18+
// The working directory to run the cargo command in.
1419
cwd?: string;
20+
// The shell environment.
1521
env?: {[key: string]: string};
22+
// Override the cargo executable name, such as
23+
// "my_custom_cargo_bin".
24+
overrideCargo?: string;
1625
}
1726

1827
classRustTaskProviderimplementsvscode.TaskProvider{
@@ -37,14 +46,12 @@ class RustTaskProvider implements vscode.TaskProvider {
3746
{command: "run",group: undefined},
3847
];
3948

40-
constcargoPath=awaittoolchain.cargoPath();
41-
4249
consttasks: vscode.Task[]=[];
4350
for(constworkspaceTargetofvscode.workspace.workspaceFolders||[]){
4451
for(constdefofdefs){
4552
constvscodeTask=awaitbuildRustTask(
4653
workspaceTarget,
47-
{type: TASK_TYPE,program: cargoPath,args: [def.command]},
54+
{type: TASK_TYPE,command: def.command},
4855
`cargo ${def.command}`,
4956
this.config.problemMatcher,
5057
this.config.cargoRunner,
@@ -86,10 +93,28 @@ export async function buildRustTask(
8693
customRunner?: string,
8794
throwOnError: boolean=false,
8895
): Promise<vscode.Task>{
89-
letexec: vscode.ProcessExecution|vscode.ShellExecution|undefined=undefined;
96+
constexec=awaitcargoToExecution(definition,customRunner,throwOnError);
9097

98+
returnnewvscode.Task(
99+
definition,
100+
// scope can sometimes be undefined. in these situations we default to the workspace taskscope as
101+
// recommended by the official docs: https://code.visualstudio.com/api/extension-guides/task-provider#task-provider)
102+
scope??vscode.TaskScope.Workspace,
103+
name,
104+
TASK_SOURCE,
105+
exec,
106+
problemMatcher,
107+
);
108+
}
109+
110+
asyncfunctioncargoToExecution(
111+
definition: CargoTaskDefinition,
112+
customRunner: string|undefined,
113+
throwOnError: boolean,
114+
): Promise<vscode.ProcessExecution|vscode.ShellExecution>{
91115
if(customRunner){
92116
construnnerCommand=`${customRunner}.buildShellExecution`;
117+
93118
try{
94119
construnnerArgs={
95120
kind: TASK_TYPE,
@@ -100,7 +125,7 @@ export async function buildRustTask(
100125
constcustomExec=awaitvscode.commands.executeCommand(runnerCommand,runnerArgs);
101126
if(customExec){
102127
if(customExecinstanceofvscode.ShellExecution){
103-
exec=customExec;
128+
returncustomExec;
104129
}else{
105130
log.debug("Invalid cargo ShellExecution",customExec);
106131
throw"Invalid cargo ShellExecution.";
@@ -113,20 +138,20 @@ export async function buildRustTask(
113138
}
114139
}
115140

116-
if(!exec){
117-
exec=newvscode.ProcessExecution(definition.program,definition.args,definition);
118-
}
141+
// Check whether we must use a user-defined substitute for cargo.
142+
// Split on spaces to allow overrides like "wrapper cargo".
143+
constcargoPath=awaittoolchain.cargoPath();
144+
constcargoCommand=definition.overrideCargo?.split(" ")??[cargoPath];
119145

120-
returnnewvscode.Task(
121-
definition,
122-
// scope can sometimes be undefined. in these situations we default to the workspace taskscope as
123-
// recommended by the official docs: https://code.visualstudio.com/api/extension-guides/task-provider#task-provider)
124-
scope??vscode.TaskScope.Workspace,
125-
name,
126-
TASK_SOURCE,
127-
exec,
128-
problemMatcher,
129-
);
146+
constargs=[definition.command].concat(definition.args??[]);
147+
constfullCommand=[...cargoCommand, ...args];
148+
149+
constprocessName=unwrapUndefinable(fullCommand[0]);
150+
151+
returnnewvscode.ProcessExecution(processName,fullCommand.slice(1),{
152+
cwd: definition.cwd,
153+
env: definition.env,
154+
});
130155
}
131156

132157
exportfunctionactivateTaskProvider(config: Config): vscode.Disposable{

0 commit comments

Comments
 (0)