aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/commands/cargo_watch.ts
blob: 512362eb1884dd96b403784a3b48c6c1c5c4d044 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import * as child_process from 'child_process';
import * as path from 'path';
import * as vscode from 'vscode';

import { Server } from '../server';
import { terminate } from '../utils/processes';
import { LineBuffer } from './line_buffer';
import { StatusDisplay } from './watch_status';

import {
    mapRustDiagnosticToVsCode,
    RustDiagnostic,
} from '../utils/diagnostics/rust';
import SuggestedFixCollection from '../utils/diagnostics/SuggestedFixCollection';
import { areDiagnosticsEqual } from '../utils/diagnostics/vscode';

export async function registerCargoWatchProvider(
    subscriptions: vscode.Disposable[],
): Promise<CargoWatchProvider | undefined> {
    let cargoExists = false;

    // Check if the working directory is valid cargo root path
    const cargoTomlPath = path.join(vscode.workspace.rootPath!, 'Cargo.toml');
    const cargoTomlUri = vscode.Uri.file(cargoTomlPath);
    const cargoTomlFileInfo = await vscode.workspace.fs.stat(cargoTomlUri);

    if (cargoTomlFileInfo) {
        cargoExists = true;
    }

    if (!cargoExists) {
        vscode.window.showErrorMessage(
            `Couldn\'t find \'Cargo.toml\' at ${cargoTomlPath}`,
        );
        return;
    }

    const provider = new CargoWatchProvider();
    subscriptions.push(provider);
    return provider;
}

export class CargoWatchProvider implements vscode.Disposable {
    private readonly diagnosticCollection: vscode.DiagnosticCollection;
    private readonly statusDisplay: StatusDisplay;
    private readonly outputChannel: vscode.OutputChannel;

    private suggestedFixCollection: SuggestedFixCollection;
    private codeActionDispose: vscode.Disposable;

    private cargoProcess?: child_process.ChildProcess;

    constructor() {
        this.diagnosticCollection = vscode.languages.createDiagnosticCollection(
            'rustc',
        );
        this.statusDisplay = new StatusDisplay(
            Server.config.cargoWatchOptions.command,
        );
        this.outputChannel = vscode.window.createOutputChannel(
            'Cargo Watch Trace',
        );

        // Track `rustc`'s suggested fixes so we can convert them to code actions
        this.suggestedFixCollection = new SuggestedFixCollection();
        this.codeActionDispose = vscode.languages.registerCodeActionsProvider(
            [{ scheme: 'file', language: 'rust' }],
            this.suggestedFixCollection,
            {
                providedCodeActionKinds:
                    SuggestedFixCollection.PROVIDED_CODE_ACTION_KINDS,
            },
        );
    }

    public start() {
        if (this.cargoProcess) {
            vscode.window.showInformationMessage(
                'Cargo Watch is already running',
            );
            return;
        }

        let args =
            Server.config.cargoWatchOptions.command +
            ' --all-targets --message-format json';
        if (Server.config.cargoWatchOptions.command.length > 0) {
            // Excape the double quote string:
            args += ' ' + Server.config.cargoWatchOptions.arguments;
        }
        // Windows handles arguments differently than the unix-likes, so we need to wrap the args in double quotes
        if (process.platform === 'win32') {
            args = '"' + args + '"';
        }

        const ignoreFlags = Server.config.cargoWatchOptions.ignore.reduce(
            (flags, pattern) => [...flags, '--ignore', pattern],
            [] as string[],
        );

        // Start the cargo watch with json message
        this.cargoProcess = child_process.spawn(
            'cargo',
            ['watch', '-x', args, ...ignoreFlags],
            {
                stdio: ['ignore', 'pipe', 'pipe'],
                cwd: vscode.workspace.rootPath,
                windowsVerbatimArguments: true,
            },
        );

        const stdoutData = new LineBuffer();
        this.cargoProcess.stdout.on('data', (s: string) => {
            stdoutData.processOutput(s, line => {
                this.logInfo(line);
                try {
                    this.parseLine(line);
                } catch (err) {
                    this.logError(`Failed to parse: ${err}, content : ${line}`);
                }
            });
        });

        const stderrData = new LineBuffer();
        this.cargoProcess.stderr.on('data', (s: string) => {
            stderrData.processOutput(s, line => {
                this.logError('Error on cargo-watch : {\n' + line + '}\n');
            });
        });

        this.cargoProcess.on('error', (err: Error) => {
            this.logError(
                'Error on cargo-watch process : {\n' + err.message + '}\n',
            );
        });

        this.logInfo('cargo-watch started.');
    }

    public stop() {
        if (this.cargoProcess) {
            this.cargoProcess.kill();
            terminate(this.cargoProcess);
            this.cargoProcess = undefined;
        } else {
            vscode.window.showInformationMessage('Cargo Watch is not running');
        }
    }

    public dispose(): void {
        this.stop();

        this.diagnosticCollection.clear();
        this.diagnosticCollection.dispose();
        this.outputChannel.dispose();
        this.statusDisplay.dispose();
        this.codeActionDispose.dispose();
    }

    private logInfo(line: string) {
        if (Server.config.cargoWatchOptions.trace === 'verbose') {
            this.outputChannel.append(line);
        }
    }

    private logError(line: string) {
        if (
            Server.config.cargoWatchOptions.trace === 'error' ||
            Server.config.cargoWatchOptions.trace === 'verbose'
        ) {
            this.outputChannel.append(line);
        }
    }

    private parseLine(line: string) {
        if (line.startsWith('[Running')) {
            this.diagnosticCollection.clear();
            this.suggestedFixCollection.clear();
            this.statusDisplay.show();
        }

        if (line.startsWith('[Finished running')) {
            this.statusDisplay.hide();
        }

        interface CargoArtifact {
            reason: string;
            package_id: string;
        }

        // https://github.com/rust-lang/cargo/blob/master/src/cargo/util/machine_message.rs
        interface CargoMessage {
            reason: string;
            package_id: string;
            message: RustDiagnostic;
        }

        // cargo-watch itself output non json format
        // Ignore these lines
        let data: CargoMessage;
        try {
            data = JSON.parse(line.trim());
        } catch (error) {
            this.logError(`Fail to parse to json : { ${error} }`);
            return;
        }

        if (data.reason === 'compiler-artifact') {
            const msg = data as CargoArtifact;

            // The format of the package_id is "{name} {version} ({source_id})",
            // https://github.com/rust-lang/cargo/blob/37ad03f86e895bb80b474c1c088322634f4725f5/src/cargo/core/package_id.rs#L53
            this.statusDisplay.packageName = msg.package_id.split(' ')[0];
        } else if (data.reason === 'compiler-message') {
            const msg = data.message as RustDiagnostic;

            const mapResult = mapRustDiagnosticToVsCode(msg);
            if (!mapResult) {
                return;
            }

            const { location, diagnostic, suggestedFixes } = mapResult;
            const fileUri = location.uri;

            const diagnostics: vscode.Diagnostic[] = [
                ...(this.diagnosticCollection!.get(fileUri) || []),
            ];

            // If we're building multiple targets it's possible we've already seen this diagnostic
            const isDuplicate = diagnostics.some(d =>
                areDiagnosticsEqual(d, diagnostic),
            );
            if (isDuplicate) {
                return;
            }

            diagnostics.push(diagnostic);
            this.diagnosticCollection!.set(fileUri, diagnostics);

            if (suggestedFixes.length) {
                for (const suggestedFix of suggestedFixes) {
                    this.suggestedFixCollection.addSuggestedFixForDiagnostic(
                        suggestedFix,
                        diagnostic,
                    );
                }

                // Have VsCode query us for the code actions
                vscode.commands.executeCommand(
                    'vscode.executeCodeActionProvider',
                    fileUri,
                    diagnostic.range,
                );
            }
        }
    }
}