@paretools/cargo 0.8.5 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/lib/cargo-runner.d.ts +1 -1
- package/dist/lib/cargo-runner.d.ts.map +1 -1
- package/dist/lib/cargo-runner.js +2 -2
- package/dist/lib/cargo-runner.js.map +1 -1
- package/dist/lib/formatters.d.ts +32 -7
- package/dist/lib/formatters.d.ts.map +1 -1
- package/dist/lib/formatters.js +192 -45
- package/dist/lib/formatters.js.map +1 -1
- package/dist/lib/parsers.d.ts +84 -20
- package/dist/lib/parsers.d.ts.map +1 -1
- package/dist/lib/parsers.js +758 -70
- package/dist/lib/parsers.js.map +1 -1
- package/dist/schemas/index.d.ts +147 -9
- package/dist/schemas/index.d.ts.map +1 -1
- package/dist/schemas/index.js +123 -11
- package/dist/schemas/index.js.map +1 -1
- package/dist/tools/add.d.ts.map +1 -1
- package/dist/tools/add.js +123 -16
- package/dist/tools/add.js.map +1 -1
- package/dist/tools/audit.d.ts.map +1 -1
- package/dist/tools/audit.js +102 -10
- package/dist/tools/audit.js.map +1 -1
- package/dist/tools/build.d.ts.map +1 -1
- package/dist/tools/build.js +101 -12
- package/dist/tools/build.js.map +1 -1
- package/dist/tools/check.d.ts.map +1 -1
- package/dist/tools/check.js +90 -15
- package/dist/tools/check.js.map +1 -1
- package/dist/tools/clippy.d.ts.map +1 -1
- package/dist/tools/clippy.js +138 -10
- package/dist/tools/clippy.js.map +1 -1
- package/dist/tools/doc.d.ts.map +1 -1
- package/dist/tools/doc.js +84 -14
- package/dist/tools/doc.js.map +1 -1
- package/dist/tools/fmt.d.ts.map +1 -1
- package/dist/tools/fmt.js +74 -11
- package/dist/tools/fmt.js.map +1 -1
- package/dist/tools/remove.d.ts.map +1 -1
- package/dist/tools/remove.js +60 -13
- package/dist/tools/remove.js.map +1 -1
- package/dist/tools/run.d.ts.map +1 -1
- package/dist/tools/run.js +104 -16
- package/dist/tools/run.js.map +1 -1
- package/dist/tools/test.d.ts.map +1 -1
- package/dist/tools/test.js +109 -12
- package/dist/tools/test.js.map +1 -1
- package/dist/tools/tree.d.ts.map +1 -1
- package/dist/tools/tree.js +120 -19
- package/dist/tools/tree.js.map +1 -1
- package/dist/tools/update.d.ts.map +1 -1
- package/dist/tools/update.js +65 -15
- package/dist/tools/update.js.map +1 -1
- package/package.json +2 -2
package/dist/lib/parsers.js
CHANGED
|
@@ -1,41 +1,84 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Parses `cargo build --message-format=json` output.
|
|
3
3
|
* Each line is a JSON object with a "reason" field.
|
|
4
|
-
* We care about reason="compiler-message" entries.
|
|
4
|
+
* We care about reason="compiler-message" entries and the "build-finished" event.
|
|
5
|
+
* Gap #89: Uses build-finished event's success field as authoritative success indicator.
|
|
5
6
|
*/
|
|
6
|
-
export function parseCargoBuildJson(stdout, exitCode) {
|
|
7
|
-
const diagnostics = parseCompilerMessages(stdout);
|
|
7
|
+
export function parseCargoBuildJson(stdout, exitCode, stderr) {
|
|
8
|
+
const { diagnostics: rawDiagnostics, buildFinishedSuccess } = parseCompilerMessages(stdout);
|
|
9
|
+
const diagnostics = deduplicateDiagnostics(rawDiagnostics);
|
|
8
10
|
const errors = diagnostics.filter((d) => d.severity === "error").length;
|
|
9
11
|
const warnings = diagnostics.filter((d) => d.severity === "warning").length;
|
|
10
|
-
|
|
11
|
-
|
|
12
|
+
// Gap #89: Use build-finished event's success field when available,
|
|
13
|
+
// fall back to exit code
|
|
14
|
+
const success = buildFinishedSuccess !== undefined ? buildFinishedSuccess : exitCode === 0;
|
|
15
|
+
const result = {
|
|
16
|
+
success,
|
|
12
17
|
diagnostics,
|
|
13
18
|
total: diagnostics.length,
|
|
14
19
|
errors,
|
|
15
20
|
warnings,
|
|
16
21
|
};
|
|
22
|
+
const timings = parseTimingsMetadata(stdout, stderr ?? "");
|
|
23
|
+
if (timings) {
|
|
24
|
+
result.timings = timings;
|
|
25
|
+
}
|
|
26
|
+
return result;
|
|
27
|
+
}
|
|
28
|
+
/** Parses cargo check output using the build parser, with check-specific mode metadata. */
|
|
29
|
+
export function parseCargoCheckJson(stdout, exitCode, stderr) {
|
|
30
|
+
const base = parseCargoBuildJson(stdout, exitCode, stderr);
|
|
31
|
+
return { ...base, mode: "check" };
|
|
17
32
|
}
|
|
18
33
|
/**
|
|
19
34
|
* Parses `cargo test` output.
|
|
20
35
|
* Format: "test name ... ok/FAILED/ignored"
|
|
21
36
|
* Summary: "test result: ok/FAILED. X passed; Y failed; Z ignored"
|
|
37
|
+
*
|
|
38
|
+
* For failed tests, captures stdout/stderr output between the "failures:" section
|
|
39
|
+
* and the "failures:" name list or "test result:" line.
|
|
40
|
+
*
|
|
41
|
+
* Gap #95: Also parses JSON message format output for compilation diagnostics.
|
|
22
42
|
*/
|
|
23
|
-
export function parseCargoTestOutput(stdout, exitCode) {
|
|
43
|
+
export function parseCargoTestOutput(stdout, exitCode, jsonOutput) {
|
|
24
44
|
const lines = stdout.split("\n");
|
|
25
45
|
const tests = [];
|
|
26
46
|
for (const line of lines) {
|
|
27
|
-
const match = line.match(/^test (.+?) \.\.\. (ok|FAILED|ignored)
|
|
47
|
+
const match = line.match(/^test (.+?) \.\.\. (ok|FAILED|ignored)(?: \(([^)]+)\))?$/);
|
|
28
48
|
if (match) {
|
|
29
|
-
|
|
49
|
+
const entry = {
|
|
30
50
|
name: match[1],
|
|
31
51
|
status: match[2],
|
|
32
|
-
}
|
|
52
|
+
};
|
|
53
|
+
if (match[3]) {
|
|
54
|
+
entry.duration = match[3];
|
|
55
|
+
}
|
|
56
|
+
tests.push(entry);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
// Parse failure output sections.
|
|
60
|
+
// Cargo test output has a "failures:" section containing stdout/stderr for each failed test:
|
|
61
|
+
// failures:
|
|
62
|
+
//
|
|
63
|
+
// ---- tests::test_div stdout ----
|
|
64
|
+
// thread 'tests::test_div' panicked at 'assertion failed: ...'
|
|
65
|
+
// ...
|
|
66
|
+
//
|
|
67
|
+
// failures:
|
|
68
|
+
// tests::test_div
|
|
69
|
+
//
|
|
70
|
+
// We parse output blocks between "---- <name> stdout ----" markers.
|
|
71
|
+
const failureOutputMap = parseFailureOutputSections(stdout);
|
|
72
|
+
// Attach captured output to failed tests
|
|
73
|
+
for (const test of tests) {
|
|
74
|
+
if (test.status === "FAILED" && failureOutputMap.has(test.name)) {
|
|
75
|
+
test.output = failureOutputMap.get(test.name);
|
|
33
76
|
}
|
|
34
77
|
}
|
|
35
78
|
const passed = tests.filter((t) => t.status === "ok").length;
|
|
36
79
|
const failed = tests.filter((t) => t.status === "FAILED").length;
|
|
37
80
|
const ignored = tests.filter((t) => t.status === "ignored").length;
|
|
38
|
-
|
|
81
|
+
const result = {
|
|
39
82
|
success: exitCode === 0,
|
|
40
83
|
tests,
|
|
41
84
|
total: tests.length,
|
|
@@ -43,16 +86,79 @@ export function parseCargoTestOutput(stdout, exitCode) {
|
|
|
43
86
|
failed,
|
|
44
87
|
ignored,
|
|
45
88
|
};
|
|
89
|
+
const summaryDurationMatch = stdout.match(/test result: .*?finished in ([0-9.]+s)/);
|
|
90
|
+
if (summaryDurationMatch) {
|
|
91
|
+
result.duration = summaryDurationMatch[1];
|
|
92
|
+
}
|
|
93
|
+
// Gap #95: Parse compilation diagnostics from JSON message format output
|
|
94
|
+
if (jsonOutput) {
|
|
95
|
+
const { diagnostics } = parseCompilerMessages(jsonOutput);
|
|
96
|
+
if (diagnostics.length > 0) {
|
|
97
|
+
result.compilationDiagnostics = diagnostics;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return result;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Extracts failure output sections from cargo test output.
|
|
104
|
+
* Looks for blocks starting with "---- <name> stdout ----" and ending at
|
|
105
|
+
* the next "---- " marker or "failures:" or "test result:" line.
|
|
106
|
+
* Returns a map of test name -> captured output.
|
|
107
|
+
*/
|
|
108
|
+
function parseFailureOutputSections(stdout) {
|
|
109
|
+
const map = new Map();
|
|
110
|
+
const lines = stdout.split("\n");
|
|
111
|
+
let currentName = null;
|
|
112
|
+
let currentOutput = [];
|
|
113
|
+
for (const line of lines) {
|
|
114
|
+
// Match "---- tests::test_name stdout ----"
|
|
115
|
+
const headerMatch = line.match(/^---- (.+?) stdout ----$/);
|
|
116
|
+
if (headerMatch) {
|
|
117
|
+
// Save previous block if any
|
|
118
|
+
if (currentName !== null && currentOutput.length > 0) {
|
|
119
|
+
map.set(currentName, currentOutput.join("\n").trim());
|
|
120
|
+
}
|
|
121
|
+
currentName = headerMatch[1];
|
|
122
|
+
currentOutput = [];
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
// End markers: next section, failures list, or test result
|
|
126
|
+
if (currentName !== null) {
|
|
127
|
+
if (line.match(/^failures:/) ||
|
|
128
|
+
line.match(/^test result:/) ||
|
|
129
|
+
line.match(/^---- .+ stdout ----$/)) {
|
|
130
|
+
if (currentOutput.length > 0) {
|
|
131
|
+
map.set(currentName, currentOutput.join("\n").trim());
|
|
132
|
+
}
|
|
133
|
+
currentName = null;
|
|
134
|
+
currentOutput = [];
|
|
135
|
+
// Check if this line starts a new output section
|
|
136
|
+
const newHeader = line.match(/^---- (.+?) stdout ----$/);
|
|
137
|
+
if (newHeader) {
|
|
138
|
+
currentName = newHeader[1];
|
|
139
|
+
}
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
currentOutput.push(line);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
// Save final block if any
|
|
146
|
+
if (currentName !== null && currentOutput.length > 0) {
|
|
147
|
+
map.set(currentName, currentOutput.join("\n").trim());
|
|
148
|
+
}
|
|
149
|
+
return map;
|
|
46
150
|
}
|
|
47
151
|
/**
|
|
48
152
|
* Parses `cargo clippy --message-format=json` output.
|
|
49
|
-
* Same JSON format as cargo build.
|
|
153
|
+
* Same JSON format as cargo build. Now includes success field.
|
|
154
|
+
* Gap #90: Captures suggestion text from JSON children.
|
|
50
155
|
*/
|
|
51
|
-
export function parseCargoClippyJson(stdout) {
|
|
52
|
-
const diagnostics = parseCompilerMessages(stdout);
|
|
156
|
+
export function parseCargoClippyJson(stdout, exitCode) {
|
|
157
|
+
const { diagnostics } = parseCompilerMessages(stdout);
|
|
53
158
|
const errors = diagnostics.filter((d) => d.severity === "error").length;
|
|
54
159
|
const warnings = diagnostics.filter((d) => d.severity === "warning").length;
|
|
55
160
|
return {
|
|
161
|
+
success: exitCode === 0,
|
|
56
162
|
diagnostics,
|
|
57
163
|
total: diagnostics.length,
|
|
58
164
|
errors,
|
|
@@ -62,71 +168,260 @@ export function parseCargoClippyJson(stdout) {
|
|
|
62
168
|
/**
|
|
63
169
|
* Parses `cargo run` output.
|
|
64
170
|
* Returns exit code, stdout, stderr, and success flag.
|
|
171
|
+
* Optionally truncates stdout/stderr to maxOutputSize bytes.
|
|
172
|
+
* Gap #94: Distinguishes compilation vs runtime vs timeout failure.
|
|
65
173
|
*/
|
|
66
|
-
export function parseCargoRunOutput(stdout, stderr, exitCode) {
|
|
67
|
-
|
|
174
|
+
export function parseCargoRunOutput(stdout, stderr, exitCode, maxOutputSize, timedOut) {
|
|
175
|
+
const limit = maxOutputSize ?? 0;
|
|
176
|
+
let stdoutTruncated = false;
|
|
177
|
+
let stderrTruncated = false;
|
|
178
|
+
let finalStdout = stdout;
|
|
179
|
+
let finalStderr = stderr;
|
|
180
|
+
if (limit > 0) {
|
|
181
|
+
if (stdout.length > limit) {
|
|
182
|
+
finalStdout = stdout.slice(0, limit);
|
|
183
|
+
stdoutTruncated = true;
|
|
184
|
+
}
|
|
185
|
+
if (stderr.length > limit) {
|
|
186
|
+
finalStderr = stderr.slice(0, limit);
|
|
187
|
+
stderrTruncated = true;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
const result = {
|
|
68
191
|
exitCode,
|
|
69
|
-
stdout,
|
|
70
|
-
stderr,
|
|
192
|
+
stdout: finalStdout,
|
|
193
|
+
stderr: finalStderr,
|
|
71
194
|
success: exitCode === 0,
|
|
72
195
|
};
|
|
196
|
+
// Gap #94: Determine failure type
|
|
197
|
+
if (exitCode !== 0) {
|
|
198
|
+
result.failureType = detectRunFailureType(stderr, exitCode, timedOut);
|
|
199
|
+
const signal = detectSignalFromExitCode(exitCode, stderr);
|
|
200
|
+
if (signal) {
|
|
201
|
+
result.signal = signal;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
if (stdoutTruncated)
|
|
205
|
+
result.stdoutTruncated = true;
|
|
206
|
+
if (stderrTruncated)
|
|
207
|
+
result.stderrTruncated = true;
|
|
208
|
+
return result;
|
|
73
209
|
}
|
|
74
210
|
/**
|
|
75
|
-
*
|
|
211
|
+
* Detects whether a cargo run failure is a compilation error, runtime error, or timeout.
|
|
212
|
+
* Gap #94: Compilation errors have specific patterns from rustc.
|
|
213
|
+
*/
|
|
214
|
+
export function detectRunFailureType(stderr, exitCode, timedOut) {
|
|
215
|
+
if (timedOut)
|
|
216
|
+
return "timeout";
|
|
217
|
+
// Compilation failure patterns from cargo/rustc
|
|
218
|
+
const compilationPatterns = [
|
|
219
|
+
/error\[E\d+\]/,
|
|
220
|
+
/could not compile/,
|
|
221
|
+
/aborting due to \d+ previous error/,
|
|
222
|
+
/^error: .+ not found/m,
|
|
223
|
+
/cannot find/,
|
|
224
|
+
];
|
|
225
|
+
for (const pattern of compilationPatterns) {
|
|
226
|
+
if (pattern.test(stderr)) {
|
|
227
|
+
return "compilation";
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
// Exit code 101 is commonly used by cargo for compilation failures,
|
|
231
|
+
// but also by panics. If we didn't match compilation patterns, it's runtime.
|
|
232
|
+
return "runtime";
|
|
233
|
+
}
|
|
234
|
+
/** Detects signal metadata from conventional Unix-style exit codes or stderr text. */
|
|
235
|
+
export function detectSignalFromExitCode(exitCode, stderr) {
|
|
236
|
+
const explicitSignalMatch = stderr.match(/\bSIG[A-Z0-9]+\b/);
|
|
237
|
+
if (explicitSignalMatch) {
|
|
238
|
+
return explicitSignalMatch[0];
|
|
239
|
+
}
|
|
240
|
+
// By convention, shell exits with 128 + signal number when terminated by a signal.
|
|
241
|
+
if (exitCode >= 129 && exitCode <= 255) {
|
|
242
|
+
const signalNumber = exitCode - 128;
|
|
243
|
+
const signalByNumber = {
|
|
244
|
+
1: "SIGHUP",
|
|
245
|
+
2: "SIGINT",
|
|
246
|
+
3: "SIGQUIT",
|
|
247
|
+
6: "SIGABRT",
|
|
248
|
+
9: "SIGKILL",
|
|
249
|
+
11: "SIGSEGV",
|
|
250
|
+
13: "SIGPIPE",
|
|
251
|
+
14: "SIGALRM",
|
|
252
|
+
15: "SIGTERM",
|
|
253
|
+
};
|
|
254
|
+
return signalByNumber[signalNumber] ?? `SIG${signalNumber}`;
|
|
255
|
+
}
|
|
256
|
+
return undefined;
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Parses `cargo add` output (including `--dry-run` mode).
|
|
76
260
|
* Lines like: " Adding serde v1.0.217 to dependencies"
|
|
77
261
|
* Also handles: " Adding serde v1.0.217 to dev-dependencies"
|
|
262
|
+
* Dry-run mode outputs the same Adding lines followed by:
|
|
263
|
+
* "warning: aborting add due to dry run"
|
|
264
|
+
* On failure, captures the error message.
|
|
265
|
+
*
|
|
266
|
+
* Gap #86: Accepts dependency type flags to include in output.
|
|
78
267
|
*/
|
|
79
|
-
export function parseCargoAddOutput(stdout, stderr, exitCode) {
|
|
268
|
+
export function parseCargoAddOutput(stdout, stderr, exitCode, depType) {
|
|
80
269
|
const combined = stdout + "\n" + stderr;
|
|
81
270
|
const lines = combined.split("\n");
|
|
82
271
|
const added = [];
|
|
272
|
+
// Detect dry-run mode from the cargo warning message
|
|
273
|
+
const isDryRun = /warning[:\s]*aborting add due to dry run/i.test(combined);
|
|
274
|
+
// Gap #86: Detect dependency type from output if not explicitly provided
|
|
275
|
+
let detectedType = depType;
|
|
276
|
+
let currentPackage;
|
|
277
|
+
let inFeaturesBlock = false;
|
|
83
278
|
for (const line of lines) {
|
|
84
|
-
const
|
|
85
|
-
if (
|
|
86
|
-
|
|
279
|
+
const trimmed = line.trim();
|
|
280
|
+
if (/^features:/i.test(trimmed)) {
|
|
281
|
+
inFeaturesBlock = true;
|
|
282
|
+
continue;
|
|
283
|
+
}
|
|
284
|
+
if (inFeaturesBlock) {
|
|
285
|
+
const featureMatch = trimmed.match(/^[+*-]\s+(.+)$/);
|
|
286
|
+
if (featureMatch && currentPackage) {
|
|
287
|
+
if (!currentPackage.featuresActivated) {
|
|
288
|
+
currentPackage.featuresActivated = [];
|
|
289
|
+
}
|
|
290
|
+
currentPackage.featuresActivated.push(featureMatch[1].trim());
|
|
291
|
+
continue;
|
|
292
|
+
}
|
|
293
|
+
if (trimmed === "") {
|
|
294
|
+
continue;
|
|
295
|
+
}
|
|
296
|
+
inFeaturesBlock = false;
|
|
297
|
+
}
|
|
298
|
+
// Match both "Adding" (normal and dry-run) and "Updating" lines
|
|
299
|
+
const addMatch = line.match(/Adding\s+(\S+)\s+v(\S+)\s+to\s+(\S+)/);
|
|
300
|
+
if (addMatch) {
|
|
301
|
+
currentPackage = { name: addMatch[1], version: addMatch[2] };
|
|
302
|
+
added.push(currentPackage);
|
|
303
|
+
// Detect type from "to dev-dependencies" / "to build-dependencies" / "to dependencies"
|
|
304
|
+
if (!depType) {
|
|
305
|
+
const target = addMatch[3];
|
|
306
|
+
if (target === "dev-dependencies")
|
|
307
|
+
detectedType = "dev";
|
|
308
|
+
else if (target === "build-dependencies")
|
|
309
|
+
detectedType = "build";
|
|
310
|
+
else if (target === "dependencies")
|
|
311
|
+
detectedType = "normal";
|
|
312
|
+
}
|
|
313
|
+
continue;
|
|
314
|
+
}
|
|
315
|
+
// Some cargo versions use "Updating" in dry-run output when a dep is already present
|
|
316
|
+
const updateMatch = line.match(/Updating\s+(\S+)\s+v\S+\s+->\s+v(\S+)/);
|
|
317
|
+
if (updateMatch) {
|
|
318
|
+
currentPackage = { name: updateMatch[1], version: updateMatch[2] };
|
|
319
|
+
added.push(currentPackage);
|
|
87
320
|
}
|
|
88
321
|
}
|
|
89
|
-
|
|
322
|
+
const result = {
|
|
90
323
|
success: exitCode === 0,
|
|
91
324
|
added,
|
|
92
325
|
total: added.length,
|
|
93
326
|
};
|
|
327
|
+
if (detectedType) {
|
|
328
|
+
result.dependencyType = detectedType;
|
|
329
|
+
}
|
|
330
|
+
if (isDryRun) {
|
|
331
|
+
result.dryRun = true;
|
|
332
|
+
}
|
|
333
|
+
if (exitCode !== 0) {
|
|
334
|
+
// Extract error message from stderr, filtering out dry-run warnings
|
|
335
|
+
const errorLines = stderr
|
|
336
|
+
.split("\n")
|
|
337
|
+
.filter((l) => !/aborting add due to dry run/i.test(l))
|
|
338
|
+
.map((l) => l.replace(/^\s*error\s*:\s*/i, "").trim())
|
|
339
|
+
.filter(Boolean);
|
|
340
|
+
if (errorLines.length > 0) {
|
|
341
|
+
result.error = errorLines.join("; ");
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
return result;
|
|
94
345
|
}
|
|
95
346
|
/**
|
|
96
347
|
* Parses `cargo remove` output.
|
|
97
348
|
* Lines like: " Removing serde from dependencies"
|
|
349
|
+
* On failure, captures the error message.
|
|
350
|
+
*
|
|
351
|
+
* Gap #93: Accepts dependency type flags to include in output.
|
|
98
352
|
*/
|
|
99
|
-
export function parseCargoRemoveOutput(stdout, stderr, exitCode) {
|
|
353
|
+
export function parseCargoRemoveOutput(stdout, stderr, exitCode, depType) {
|
|
100
354
|
const combined = stdout + "\n" + stderr;
|
|
101
355
|
const lines = combined.split("\n");
|
|
102
356
|
const removed = [];
|
|
357
|
+
const failedPackages = new Set();
|
|
358
|
+
// Gap #93: Detect dependency type from output if not explicitly provided
|
|
359
|
+
let detectedType = depType;
|
|
103
360
|
for (const line of lines) {
|
|
104
|
-
const match = line.match(/Removing\s+(\S+)\s+from\s
|
|
361
|
+
const match = line.match(/Removing\s+(\S+)\s+from\s+(\S+)/);
|
|
105
362
|
if (match) {
|
|
106
363
|
removed.push(match[1]);
|
|
364
|
+
// Detect type from "from dev-dependencies" / "from build-dependencies" / "from dependencies"
|
|
365
|
+
if (!depType) {
|
|
366
|
+
const target = match[2];
|
|
367
|
+
if (target === "dev-dependencies")
|
|
368
|
+
detectedType = "dev";
|
|
369
|
+
else if (target === "build-dependencies")
|
|
370
|
+
detectedType = "build";
|
|
371
|
+
else if (target === "dependencies")
|
|
372
|
+
detectedType = "normal";
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
const failedMatch = line.match(/dependency [`'"]([^`'"]+)[`'"] could not be found/i) ??
|
|
376
|
+
line.match(/package ID specification [`'"]([^`'"]+)[`'"] did not match/i);
|
|
377
|
+
if (failedMatch) {
|
|
378
|
+
failedPackages.add(failedMatch[1]);
|
|
107
379
|
}
|
|
108
380
|
}
|
|
109
|
-
|
|
381
|
+
const result = {
|
|
110
382
|
success: exitCode === 0,
|
|
111
383
|
removed,
|
|
112
384
|
total: removed.length,
|
|
113
385
|
};
|
|
386
|
+
if (detectedType) {
|
|
387
|
+
result.dependencyType = detectedType;
|
|
388
|
+
}
|
|
389
|
+
if (exitCode !== 0) {
|
|
390
|
+
if (removed.length > 0) {
|
|
391
|
+
result.partialSuccess = true;
|
|
392
|
+
}
|
|
393
|
+
if (failedPackages.size > 0) {
|
|
394
|
+
result.failedPackages = Array.from(failedPackages);
|
|
395
|
+
}
|
|
396
|
+
const errorLines = stderr
|
|
397
|
+
.split("\n")
|
|
398
|
+
.map((l) => l.replace(/^\s*error\s*:\s*/i, "").trim())
|
|
399
|
+
.filter(Boolean);
|
|
400
|
+
if (errorLines.length > 0) {
|
|
401
|
+
result.error = errorLines.join("; ");
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
return result;
|
|
114
405
|
}
|
|
115
406
|
/**
|
|
116
|
-
* Parses `cargo fmt
|
|
117
|
-
*
|
|
118
|
-
*
|
|
407
|
+
* Parses `cargo fmt` output in both check and fix (non-check) modes.
|
|
408
|
+
*
|
|
409
|
+
* Check mode: Gap #92 - uses --files-with-diff for more reliable file detection.
|
|
410
|
+
* Fix mode: parses file paths from `-- -l` (--files-with-diff) output, which lists
|
|
411
|
+
* files that were actually reformatted, one path per line on stdout.
|
|
119
412
|
*/
|
|
120
413
|
export function parseCargoFmtOutput(stdout, stderr, exitCode, checkMode) {
|
|
121
414
|
const files = [];
|
|
415
|
+
const diffLines = [];
|
|
122
416
|
if (checkMode) {
|
|
123
|
-
// In check mode
|
|
124
|
-
//
|
|
417
|
+
// Gap #92: In check mode with --files-with-diff, the output contains
|
|
418
|
+
// file paths directly, one per line. Parse those first.
|
|
125
419
|
const combined = stdout + "\n" + stderr;
|
|
126
420
|
const lines = combined.split("\n");
|
|
127
421
|
for (const line of lines) {
|
|
128
|
-
|
|
129
|
-
|
|
422
|
+
const trimmed = line.trim();
|
|
423
|
+
// Match "Diff in <path> at line N:" format (legacy/verbose output)
|
|
424
|
+
const diffMatch = trimmed.match(/^Diff in (.+?) at line/);
|
|
130
425
|
if (diffMatch) {
|
|
131
426
|
const file = diffMatch[1];
|
|
132
427
|
if (!files.includes(file)) {
|
|
@@ -134,96 +429,240 @@ export function parseCargoFmtOutput(stdout, stderr, exitCode, checkMode) {
|
|
|
134
429
|
}
|
|
135
430
|
continue;
|
|
136
431
|
}
|
|
137
|
-
//
|
|
138
|
-
const trimmed = line.trim();
|
|
432
|
+
// File paths from --files-with-diff or bare paths ending in .rs
|
|
139
433
|
if (trimmed &&
|
|
140
434
|
!trimmed.startsWith("+") &&
|
|
141
435
|
!trimmed.startsWith("-") &&
|
|
142
436
|
!trimmed.startsWith("@") &&
|
|
437
|
+
!trimmed.startsWith("warning") &&
|
|
438
|
+
!trimmed.startsWith("error") &&
|
|
143
439
|
trimmed.endsWith(".rs")) {
|
|
144
440
|
if (!files.includes(trimmed)) {
|
|
145
441
|
files.push(trimmed);
|
|
146
442
|
}
|
|
147
443
|
}
|
|
444
|
+
if (trimmed.startsWith("+") ||
|
|
445
|
+
trimmed.startsWith("-") ||
|
|
446
|
+
trimmed.startsWith("@@") ||
|
|
447
|
+
trimmed.startsWith("--- ") ||
|
|
448
|
+
trimmed.startsWith("+++ ") ||
|
|
449
|
+
/^diff --git /.test(trimmed) ||
|
|
450
|
+
/^Diff in .+ at line /.test(trimmed)) {
|
|
451
|
+
diffLines.push(line);
|
|
452
|
+
}
|
|
148
453
|
}
|
|
149
454
|
}
|
|
455
|
+
else {
|
|
456
|
+
// In fix mode with -l flag, rustfmt lists reformatted file paths on stdout,
|
|
457
|
+
// one per line. Parse those to report which files were actually changed.
|
|
458
|
+
const combined = stdout + "\n" + stderr;
|
|
459
|
+
const lines = combined.split("\n");
|
|
460
|
+
for (const line of lines) {
|
|
461
|
+
const trimmed = line.trim();
|
|
462
|
+
if (trimmed &&
|
|
463
|
+
!trimmed.startsWith("warning") &&
|
|
464
|
+
!trimmed.startsWith("error") &&
|
|
465
|
+
trimmed.endsWith(".rs")) {
|
|
466
|
+
if (!files.includes(trimmed)) {
|
|
467
|
+
files.push(trimmed);
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
// In check mode, exit code 1 with files means "needs formatting" (not a hard error).
|
|
473
|
+
// needsFormatting is true when check mode finds files that need formatting.
|
|
474
|
+
// In fix mode, needsFormatting is always false (files were already reformatted).
|
|
475
|
+
const needsFormatting = checkMode && (exitCode !== 0 || files.length > 0);
|
|
150
476
|
return {
|
|
151
477
|
success: exitCode === 0,
|
|
478
|
+
needsFormatting,
|
|
152
479
|
filesChanged: files.length,
|
|
153
480
|
files,
|
|
481
|
+
...(checkMode && diffLines.length > 0 ? { diff: diffLines.join("\n") } : {}),
|
|
154
482
|
};
|
|
155
483
|
}
|
|
156
484
|
/**
|
|
157
485
|
* Parses `cargo doc` output.
|
|
158
486
|
* Counts "warning:" or "warning[" lines from stderr,
|
|
159
487
|
* excluding the summary line "warning: N warnings emitted".
|
|
488
|
+
* Parses structured warning details with file location and message.
|
|
489
|
+
* Optionally extracts the output directory path.
|
|
160
490
|
*/
|
|
161
|
-
export function parseCargoDocOutput(stderr, exitCode) {
|
|
162
|
-
const
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
491
|
+
export function parseCargoDocOutput(stdout, stderr, exitCode, cwd) {
|
|
492
|
+
const fromJson = parseCompilerMessages(stdout)
|
|
493
|
+
.diagnostics.filter((d) => d.severity === "warning")
|
|
494
|
+
.map((d) => ({ file: d.file, line: d.line, message: d.message }));
|
|
495
|
+
const fromText = parseDocWarningsFromText(stderr);
|
|
496
|
+
const merged = new Map();
|
|
497
|
+
for (const w of [...fromJson, ...fromText]) {
|
|
498
|
+
const key = `${w.file}:${w.line}:${w.message}`;
|
|
499
|
+
if (!merged.has(key)) {
|
|
500
|
+
merged.set(key, w);
|
|
167
501
|
}
|
|
168
502
|
}
|
|
169
|
-
|
|
503
|
+
const warningDetails = Array.from(merged.values());
|
|
504
|
+
const result = {
|
|
170
505
|
success: exitCode === 0,
|
|
171
|
-
warnings,
|
|
506
|
+
warnings: warningDetails.length,
|
|
172
507
|
};
|
|
508
|
+
if (warningDetails.length > 0) {
|
|
509
|
+
result.warningDetails = warningDetails;
|
|
510
|
+
}
|
|
511
|
+
// Report the doc output directory if available
|
|
512
|
+
if (cwd) {
|
|
513
|
+
result.outputDir = `${cwd}/target/doc`;
|
|
514
|
+
}
|
|
515
|
+
return result;
|
|
173
516
|
}
|
|
174
517
|
/**
|
|
175
518
|
* Parses `cargo update` output.
|
|
176
|
-
* Returns success flag and combined output text.
|
|
519
|
+
* Returns success flag, parsed updated packages, and combined output text.
|
|
520
|
+
*
|
|
521
|
+
* Cargo update outputs lines like:
|
|
522
|
+
* " Updating serde v1.0.200 -> v1.0.217"
|
|
523
|
+
* " Updating crates.io index"
|
|
524
|
+
* " Adding new-crate v1.0.0"
|
|
525
|
+
* " Removing old-crate v0.5.0"
|
|
526
|
+
* " Downgrading foo v2.0.0 -> v1.5.0"
|
|
527
|
+
* " Locking serde v1.0.200 -> v1.0.217" (newer cargo versions)
|
|
528
|
+
*
|
|
529
|
+
* Gap #96: Compact mode now includes updateCount instead of dropping all data.
|
|
177
530
|
*/
|
|
178
531
|
export function parseCargoUpdateOutput(stdout, stderr, exitCode) {
|
|
179
532
|
const output = (stdout + "\n" + stderr).trim();
|
|
533
|
+
const combined = stdout + "\n" + stderr;
|
|
534
|
+
const lines = combined.split("\n");
|
|
535
|
+
const updated = [];
|
|
536
|
+
for (const line of lines) {
|
|
537
|
+
// Match "Updating <name> v<from> -> v<to>" or "Locking <name> v<from> -> v<to>"
|
|
538
|
+
// or "Downgrading <name> v<from> -> v<to>"
|
|
539
|
+
const match = line.match(/(?:Updating|Locking|Downgrading)\s+(\S+)\s+v(\S+)\s+->\s+v(\S+)/);
|
|
540
|
+
if (match) {
|
|
541
|
+
updated.push({ name: match[1], from: match[2], to: match[3] });
|
|
542
|
+
}
|
|
543
|
+
}
|
|
180
544
|
return {
|
|
181
545
|
success: exitCode === 0,
|
|
546
|
+
updated,
|
|
547
|
+
totalUpdated: updated.length,
|
|
182
548
|
output,
|
|
183
549
|
};
|
|
184
550
|
}
|
|
185
551
|
/**
|
|
186
|
-
* Parses `cargo tree` output.
|
|
187
|
-
* Returns the full tree text
|
|
552
|
+
* Parses `cargo tree` output into structured dependency data.
|
|
553
|
+
* Returns the full tree text, a flat list of dependencies with depth,
|
|
554
|
+
* counts unique package names, and success flag.
|
|
555
|
+
*
|
|
556
|
+
* Tree lines use ASCII art prefixes to indicate depth:
|
|
557
|
+
* "my-app v0.1.0 (/path/to/project)" -> depth 0
|
|
558
|
+
* "├── serde v1.0.217" -> depth 1
|
|
559
|
+
* "│ └── serde_derive v1.0.217" -> depth 2
|
|
560
|
+
* "└── anyhow v1.0.89" -> depth 1
|
|
188
561
|
*/
|
|
189
|
-
export function parseCargoTreeOutput(stdout) {
|
|
562
|
+
export function parseCargoTreeOutput(stdout, stderr, exitCode) {
|
|
563
|
+
if (exitCode !== 0) {
|
|
564
|
+
return {
|
|
565
|
+
success: false,
|
|
566
|
+
tree: stderr.trim() || undefined,
|
|
567
|
+
packages: 0,
|
|
568
|
+
};
|
|
569
|
+
}
|
|
190
570
|
const tree = stdout.trim();
|
|
191
571
|
const lines = tree.split("\n").filter(Boolean);
|
|
192
|
-
// Extract unique package names
|
|
193
|
-
// Typical line: "my-app v0.1.0 (/path/to/project)" or "├── serde v1.0.217"
|
|
572
|
+
// Extract unique package names and structured dependency list
|
|
194
573
|
const packageNames = new Set();
|
|
574
|
+
const dependencies = [];
|
|
195
575
|
for (const line of lines) {
|
|
196
576
|
// Match package name + version pattern like "name v1.2.3"
|
|
197
|
-
const match = line.match(/([a-zA-Z0-9_-]+)\s+v\d
|
|
577
|
+
const match = line.match(/([a-zA-Z0-9_-]+)\s+v(\d+[^\s]*)/);
|
|
198
578
|
if (match) {
|
|
199
579
|
packageNames.add(match[1]);
|
|
580
|
+
// Calculate depth from the tree indentation.
|
|
581
|
+
// Each level of depth is represented by 4 chars of tree drawing:
|
|
582
|
+
// depth 0: "name v1.0.0"
|
|
583
|
+
// depth 1: "├── name v1.0.0" or "└── name v1.0.0"
|
|
584
|
+
// depth 2: "│ ├── name v1.0.0" or "│ └── name v1.0.0"
|
|
585
|
+
// We find the position of the package name in the line.
|
|
586
|
+
const nameIndex = line.indexOf(match[1]);
|
|
587
|
+
// The prefix before the name contains tree-drawing characters.
|
|
588
|
+
// Each depth level is 4 characters wide (e.g., "├── " or "│ ")
|
|
589
|
+
// For depth 0, nameIndex is 0.
|
|
590
|
+
// For depth 1, nameIndex is 4 (after "├── ").
|
|
591
|
+
// For depth 2, nameIndex is 8 (after "│ ├── ").
|
|
592
|
+
const depth = nameIndex > 0 ? Math.round(nameIndex / 4) : 0;
|
|
593
|
+
dependencies.push({ name: match[1], version: match[2], depth });
|
|
200
594
|
}
|
|
201
595
|
}
|
|
202
596
|
return {
|
|
597
|
+
success: true,
|
|
598
|
+
dependencies,
|
|
203
599
|
tree,
|
|
204
600
|
packages: packageNames.size,
|
|
205
601
|
};
|
|
206
602
|
}
|
|
207
603
|
/**
|
|
208
|
-
* Converts a CVSS v3
|
|
604
|
+
* Converts a CVSS score (numeric string or v2/v3/v4 vector string) to a severity label.
|
|
209
605
|
* See https://www.first.org/cvss/specification-document#Qualitative-Severity-Rating-Scale
|
|
606
|
+
*
|
|
607
|
+
* Supports:
|
|
608
|
+
* - Plain numeric scores: "9.8", "5.5", "0.0"
|
|
609
|
+
* - CVSS v3.x vector strings: "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H"
|
|
610
|
+
* - CVSS v2 vector strings: "(AV:N/AC:L/Au:N/C:C/I:C/A:C)"
|
|
210
611
|
*/
|
|
211
|
-
function cvssToSeverity(cvss) {
|
|
612
|
+
export function cvssToSeverity(cvss) {
|
|
212
613
|
if (!cvss)
|
|
213
614
|
return "unknown";
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
const
|
|
217
|
-
if (
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
// In practice, cargo audit JSON may include a numeric score field too.
|
|
615
|
+
const trimmed = cvss.trim();
|
|
616
|
+
// Try CVSS v3.x vector string: "CVSS:3.0/..." or "CVSS:3.1/..."
|
|
617
|
+
const v3Match = trimmed.match(/^CVSS:3\.\d+\/(.*)/);
|
|
618
|
+
if (v3Match) {
|
|
619
|
+
const score = computeCvss3BaseScore(v3Match[1]);
|
|
620
|
+
if (score !== null)
|
|
621
|
+
return scoreToSeverity(score);
|
|
222
622
|
return "unknown";
|
|
223
623
|
}
|
|
224
|
-
|
|
624
|
+
// Try CVSS v2 vector string: "(AV:N/AC:L/Au:N/C:C/I:C/A:C)" or "AV:N/AC:L/..."
|
|
625
|
+
const v2Cleaned = trimmed.replace(/^\(|\)$/g, "");
|
|
626
|
+
if (/^AV:[NAL]\/AC:[HML]\/Au:[MSN]\/C:[NPC]\/I:[NPC]\/A:[NPC]/.test(v2Cleaned)) {
|
|
627
|
+
const score = computeCvss2BaseScore(v2Cleaned);
|
|
628
|
+
if (score !== null)
|
|
629
|
+
return scoreToSeverity(score);
|
|
630
|
+
return "unknown";
|
|
631
|
+
}
|
|
632
|
+
// Try plain numeric score
|
|
633
|
+
const score = parseFloat(trimmed);
|
|
225
634
|
if (isNaN(score))
|
|
226
635
|
return "unknown";
|
|
636
|
+
return scoreToSeverity(score);
|
|
637
|
+
}
|
|
638
|
+
/**
|
|
639
|
+
* Extracts a numeric CVSS score from a CVSS string.
|
|
640
|
+
* Gap #88: Used to populate cvssScore field in audit output.
|
|
641
|
+
*/
|
|
642
|
+
export function extractCvssScore(cvss) {
|
|
643
|
+
if (!cvss)
|
|
644
|
+
return undefined;
|
|
645
|
+
const trimmed = cvss.trim();
|
|
646
|
+
// Try CVSS v3.x vector string
|
|
647
|
+
const v3Match = trimmed.match(/^CVSS:3\.\d+\/(.*)/);
|
|
648
|
+
if (v3Match) {
|
|
649
|
+
const score = computeCvss3BaseScore(v3Match[1]);
|
|
650
|
+
return score !== null ? score : undefined;
|
|
651
|
+
}
|
|
652
|
+
// Try CVSS v2 vector string
|
|
653
|
+
const v2Cleaned = trimmed.replace(/^\(|\)$/g, "");
|
|
654
|
+
if (/^AV:[NAL]\/AC:[HML]\/Au:[MSN]\/C:[NPC]\/I:[NPC]\/A:[NPC]/.test(v2Cleaned)) {
|
|
655
|
+
const score = computeCvss2BaseScore(v2Cleaned);
|
|
656
|
+
return score !== null ? score : undefined;
|
|
657
|
+
}
|
|
658
|
+
// Try plain numeric score
|
|
659
|
+
const score = parseFloat(trimmed);
|
|
660
|
+
return isNaN(score) ? undefined : score;
|
|
661
|
+
}
|
|
662
|
+
/**
|
|
663
|
+
* Maps a numeric CVSS score (0.0-10.0) to a severity label.
|
|
664
|
+
*/
|
|
665
|
+
function scoreToSeverity(score) {
|
|
227
666
|
if (score >= 9.0)
|
|
228
667
|
return "critical";
|
|
229
668
|
if (score >= 7.0)
|
|
@@ -234,27 +673,191 @@ function cvssToSeverity(cvss) {
|
|
|
234
673
|
return "low";
|
|
235
674
|
return "informational";
|
|
236
675
|
}
|
|
676
|
+
/**
|
|
677
|
+
* Computes the CVSS v3 base score from a vector string (without the "CVSS:3.x/" prefix).
|
|
678
|
+
* Implements the CVSS v3.1 specification scoring equations.
|
|
679
|
+
* See https://www.first.org/cvss/v3.1/specification-document#7-4-Metric-Values
|
|
680
|
+
*/
|
|
681
|
+
function computeCvss3BaseScore(vector) {
|
|
682
|
+
const metrics = parseVectorMetrics(vector);
|
|
683
|
+
// Required base metrics
|
|
684
|
+
const av = metrics["AV"];
|
|
685
|
+
const ac = metrics["AC"];
|
|
686
|
+
const pr = metrics["PR"];
|
|
687
|
+
const ui = metrics["UI"];
|
|
688
|
+
const s = metrics["S"];
|
|
689
|
+
const c = metrics["C"];
|
|
690
|
+
const i = metrics["I"];
|
|
691
|
+
const a = metrics["A"];
|
|
692
|
+
if (!av || !ac || !pr || !ui || !s || !c || !i || !a)
|
|
693
|
+
return null;
|
|
694
|
+
// Attack Vector
|
|
695
|
+
const avScores = { N: 0.85, A: 0.62, L: 0.55, P: 0.2 };
|
|
696
|
+
// Attack Complexity
|
|
697
|
+
const acScores = { L: 0.77, H: 0.44 };
|
|
698
|
+
// User Interaction
|
|
699
|
+
const uiScores = { N: 0.85, R: 0.62 };
|
|
700
|
+
// Confidentiality, Integrity, Availability Impact
|
|
701
|
+
const impactScores = { H: 0.56, L: 0.22, N: 0 };
|
|
702
|
+
// Privileges Required (depends on Scope)
|
|
703
|
+
const scopeChanged = s === "C";
|
|
704
|
+
const prScores = {
|
|
705
|
+
U: { N: 0.85, L: 0.62, H: 0.27 },
|
|
706
|
+
C: { N: 0.85, L: 0.68, H: 0.5 },
|
|
707
|
+
};
|
|
708
|
+
const avVal = avScores[av];
|
|
709
|
+
const acVal = acScores[ac];
|
|
710
|
+
const prVal = prScores[scopeChanged ? "C" : "U"]?.[pr];
|
|
711
|
+
const uiVal = uiScores[ui];
|
|
712
|
+
const cVal = impactScores[c];
|
|
713
|
+
const iVal = impactScores[i];
|
|
714
|
+
const aVal = impactScores[a];
|
|
715
|
+
if (avVal === undefined ||
|
|
716
|
+
acVal === undefined ||
|
|
717
|
+
prVal === undefined ||
|
|
718
|
+
uiVal === undefined ||
|
|
719
|
+
cVal === undefined ||
|
|
720
|
+
iVal === undefined ||
|
|
721
|
+
aVal === undefined) {
|
|
722
|
+
return null;
|
|
723
|
+
}
|
|
724
|
+
// ISS = 1 - [(1 - C) x (1 - I) x (1 - A)]
|
|
725
|
+
const iss = 1 - (1 - cVal) * (1 - iVal) * (1 - aVal);
|
|
726
|
+
// Impact
|
|
727
|
+
let impact;
|
|
728
|
+
if (scopeChanged) {
|
|
729
|
+
impact = 7.52 * (iss - 0.029) - 3.25 * Math.pow(iss - 0.02, 15);
|
|
730
|
+
}
|
|
731
|
+
else {
|
|
732
|
+
impact = 6.42 * iss;
|
|
733
|
+
}
|
|
734
|
+
if (impact <= 0)
|
|
735
|
+
return 0;
|
|
736
|
+
// Exploitability = 8.22 x AV x AC x PR x UI
|
|
737
|
+
const exploitability = 8.22 * avVal * acVal * prVal * uiVal;
|
|
738
|
+
let baseScore;
|
|
739
|
+
if (scopeChanged) {
|
|
740
|
+
baseScore = Math.min(1.08 * (impact + exploitability), 10);
|
|
741
|
+
}
|
|
742
|
+
else {
|
|
743
|
+
baseScore = Math.min(impact + exploitability, 10);
|
|
744
|
+
}
|
|
745
|
+
// Round up to 1 decimal place (CVSS rounding)
|
|
746
|
+
return roundUp(baseScore);
|
|
747
|
+
}
|
|
748
|
+
/**
|
|
749
|
+
* Computes the CVSS v2 base score from a vector string.
|
|
750
|
+
* Implements the simplified CVSS v2 specification scoring equations.
|
|
751
|
+
* See https://www.first.org/cvss/v2/guide#3-2-1-Base-Equation
|
|
752
|
+
*/
|
|
753
|
+
function computeCvss2BaseScore(vector) {
|
|
754
|
+
const metrics = parseVectorMetrics(vector);
|
|
755
|
+
const av = metrics["AV"];
|
|
756
|
+
const ac = metrics["AC"];
|
|
757
|
+
const au = metrics["Au"];
|
|
758
|
+
const c = metrics["C"];
|
|
759
|
+
const i = metrics["I"];
|
|
760
|
+
const a = metrics["A"];
|
|
761
|
+
if (!av || !ac || !au || !c || !i || !a)
|
|
762
|
+
return null;
|
|
763
|
+
const avScores = { L: 0.395, A: 0.646, N: 1.0 };
|
|
764
|
+
const acScores = { H: 0.35, M: 0.61, L: 0.71 };
|
|
765
|
+
const auScores = { M: 0.45, S: 0.56, N: 0.704 };
|
|
766
|
+
const impactScores = { N: 0, P: 0.275, C: 0.66 };
|
|
767
|
+
const avVal = avScores[av];
|
|
768
|
+
const acVal = acScores[ac];
|
|
769
|
+
const auVal = auScores[au];
|
|
770
|
+
const cVal = impactScores[c];
|
|
771
|
+
const iVal = impactScores[i];
|
|
772
|
+
const aVal = impactScores[a];
|
|
773
|
+
if (avVal === undefined ||
|
|
774
|
+
acVal === undefined ||
|
|
775
|
+
auVal === undefined ||
|
|
776
|
+
cVal === undefined ||
|
|
777
|
+
iVal === undefined ||
|
|
778
|
+
aVal === undefined) {
|
|
779
|
+
return null;
|
|
780
|
+
}
|
|
781
|
+
const impact = 10.41 * (1 - (1 - cVal) * (1 - iVal) * (1 - aVal));
|
|
782
|
+
const exploitability = 20 * avVal * acVal * auVal;
|
|
783
|
+
const fImpact = impact === 0 ? 0 : 1.176;
|
|
784
|
+
const baseScore = (0.6 * impact + 0.4 * exploitability - 1.5) * fImpact;
|
|
785
|
+
return roundUp(Math.max(0, baseScore));
|
|
786
|
+
}
|
|
787
|
+
/**
|
|
788
|
+
* Parses a CVSS vector string into key-value metric pairs.
|
|
789
|
+
* Example: "AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H" -> {AV:"N", AC:"L", ...}
|
|
790
|
+
*/
|
|
791
|
+
function parseVectorMetrics(vector) {
|
|
792
|
+
const metrics = {};
|
|
793
|
+
for (const part of vector.split("/")) {
|
|
794
|
+
const colonIdx = part.indexOf(":");
|
|
795
|
+
if (colonIdx > 0) {
|
|
796
|
+
metrics[part.substring(0, colonIdx)] = part.substring(colonIdx + 1);
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
return metrics;
|
|
800
|
+
}
|
|
801
|
+
/**
|
|
802
|
+
* Rounds up to 1 decimal place per CVSS specification.
|
|
803
|
+
* "If the value to be rounded has more than one decimal place, round up."
|
|
804
|
+
*/
|
|
805
|
+
function roundUp(value) {
|
|
806
|
+
const rounded = Math.ceil(value * 10) / 10;
|
|
807
|
+
return rounded;
|
|
808
|
+
}
|
|
237
809
|
/**
|
|
238
810
|
* Parses `cargo audit --json` output.
|
|
239
|
-
* Returns structured vulnerability data with severity summary.
|
|
811
|
+
* Returns structured vulnerability data with severity summary and success flag.
|
|
812
|
+
* Gap #87: Supports cargo audit fix with fixesApplied count.
|
|
813
|
+
* Gap #88: Includes raw CVSS score and vector string.
|
|
240
814
|
*/
|
|
241
|
-
export function parseCargoAuditJson(jsonStr) {
|
|
242
|
-
|
|
243
|
-
|
|
815
|
+
export function parseCargoAuditJson(jsonStr, exitCode, fixMode, mode = "deps", auditedBinary) {
|
|
816
|
+
let data;
|
|
817
|
+
try {
|
|
818
|
+
data = JSON.parse(jsonStr);
|
|
819
|
+
}
|
|
820
|
+
catch {
|
|
821
|
+
return {
|
|
822
|
+
success: false,
|
|
823
|
+
vulnerabilities: [],
|
|
824
|
+
summary: {
|
|
825
|
+
total: 0,
|
|
826
|
+
critical: 0,
|
|
827
|
+
high: 0,
|
|
828
|
+
medium: 0,
|
|
829
|
+
low: 0,
|
|
830
|
+
informational: 0,
|
|
831
|
+
unknown: 0,
|
|
832
|
+
},
|
|
833
|
+
};
|
|
834
|
+
}
|
|
835
|
+
const vulnData = data.vulnerabilities;
|
|
836
|
+
const vulnList = Array.isArray(vulnData) ? vulnData : (vulnData?.list ?? []);
|
|
244
837
|
const vulnerabilities = vulnList.map((v) => {
|
|
245
838
|
const advisory = v.advisory ?? {};
|
|
246
839
|
const pkg = v.package ?? {};
|
|
247
840
|
const versions = v.versions ?? {};
|
|
248
|
-
|
|
841
|
+
const entry = {
|
|
249
842
|
id: advisory.id ?? "unknown",
|
|
250
843
|
package: pkg.name ?? "unknown",
|
|
251
844
|
version: pkg.version ?? "unknown",
|
|
252
845
|
severity: cvssToSeverity(advisory.cvss),
|
|
253
846
|
title: advisory.title ?? "Unknown vulnerability",
|
|
254
847
|
url: advisory.url || undefined,
|
|
848
|
+
date: advisory.date || undefined,
|
|
255
849
|
patched: versions.patched ?? [],
|
|
256
850
|
unaffected: versions.unaffected ?? [],
|
|
257
851
|
};
|
|
852
|
+
// Gap #88: Include raw CVSS score and vector
|
|
853
|
+
if (advisory.cvss) {
|
|
854
|
+
const score = extractCvssScore(advisory.cvss);
|
|
855
|
+
if (score !== undefined) {
|
|
856
|
+
entry.cvssScore = score;
|
|
857
|
+
}
|
|
858
|
+
entry.cvssVector = advisory.cvss;
|
|
859
|
+
}
|
|
860
|
+
return entry;
|
|
258
861
|
});
|
|
259
862
|
const summary = {
|
|
260
863
|
total: vulnerabilities.length,
|
|
@@ -271,11 +874,83 @@ export function parseCargoAuditJson(jsonStr) {
|
|
|
271
874
|
summary[sev]++;
|
|
272
875
|
}
|
|
273
876
|
}
|
|
274
|
-
|
|
877
|
+
const result = {
|
|
878
|
+
success: exitCode === 0 || vulnerabilities.length === 0,
|
|
879
|
+
mode,
|
|
880
|
+
...(auditedBinary ? { auditedBinary } : {}),
|
|
881
|
+
vulnerabilities,
|
|
882
|
+
summary,
|
|
883
|
+
};
|
|
884
|
+
// Gap #87: For fix mode, count fixes from patched vulnerabilities
|
|
885
|
+
if (fixMode) {
|
|
886
|
+
// In fix mode, cargo audit fix attempts to fix all vulnerabilities.
|
|
887
|
+
// The number of fixes is inferred from vulnerabilities that have patched versions.
|
|
888
|
+
const fixable = vulnerabilities.filter((v) => v.patched.length > 0).length;
|
|
889
|
+
result.fixesApplied = fixable;
|
|
890
|
+
}
|
|
891
|
+
return result;
|
|
892
|
+
}
|
|
893
|
+
function deduplicateDiagnostics(diagnostics) {
|
|
894
|
+
const seen = new Set();
|
|
895
|
+
const deduped = [];
|
|
896
|
+
for (const d of diagnostics) {
|
|
897
|
+
const code = "code" in d ? String(d.code ?? "") : "";
|
|
898
|
+
const message = "message" in d ? String(d.message ?? "") : "";
|
|
899
|
+
const key = `${d.file}:${d.line}:${d.column}:${d.severity}:${code}:${message}`;
|
|
900
|
+
if (!seen.has(key)) {
|
|
901
|
+
seen.add(key);
|
|
902
|
+
deduped.push(d);
|
|
903
|
+
}
|
|
904
|
+
}
|
|
905
|
+
return deduped;
|
|
906
|
+
}
|
|
907
|
+
function parseTimingsMetadata(stdout, stderr) {
|
|
908
|
+
const combined = `${stdout}\n${stderr}`;
|
|
909
|
+
const timingPathMatch = combined.match(/([^\s]+timings?[^\s]*\.(html|json))/i);
|
|
910
|
+
if (timingPathMatch) {
|
|
911
|
+
const format = timingPathMatch[2]?.toLowerCase();
|
|
912
|
+
return {
|
|
913
|
+
generated: true,
|
|
914
|
+
format: format === "html" || format === "json" ? format : "unknown",
|
|
915
|
+
reportPath: timingPathMatch[1],
|
|
916
|
+
};
|
|
917
|
+
}
|
|
918
|
+
if (/\b--timings\b|timings?/i.test(combined)) {
|
|
919
|
+
return { generated: true, format: "unknown" };
|
|
920
|
+
}
|
|
921
|
+
return undefined;
|
|
922
|
+
}
|
|
923
|
+
function parseDocWarningsFromText(stderr) {
|
|
924
|
+
const lines = stderr.split("\n");
|
|
925
|
+
const warningDetails = [];
|
|
926
|
+
for (let i = 0; i < lines.length; i++) {
|
|
927
|
+
const line = lines[i];
|
|
928
|
+
if (line.match(/\bwarning\b(\[|:)/) && !line.match(/\d+ warnings? emitted/)) {
|
|
929
|
+
const msgMatch = line.match(/\bwarning(?:\[[^\]]+\])?\s*:\s*(.+)/);
|
|
930
|
+
const message = msgMatch ? msgMatch[1].trim() : line.trim();
|
|
931
|
+
let file = "";
|
|
932
|
+
let lineNum = 0;
|
|
933
|
+
if (i + 1 < lines.length) {
|
|
934
|
+
const locMatch = lines[i + 1].match(/\s*-->\s+([^:]+):(\d+)/);
|
|
935
|
+
if (locMatch) {
|
|
936
|
+
file = locMatch[1];
|
|
937
|
+
lineNum = parseInt(locMatch[2], 10);
|
|
938
|
+
}
|
|
939
|
+
}
|
|
940
|
+
warningDetails.push({ file, line: lineNum, message });
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
return warningDetails;
|
|
275
944
|
}
|
|
945
|
+
/**
|
|
946
|
+
* Parses compiler messages from JSON output.
|
|
947
|
+
* Gap #89: Extracts build-finished event's success field.
|
|
948
|
+
* Gap #90: Extracts suggestion text from children array.
|
|
949
|
+
*/
|
|
276
950
|
function parseCompilerMessages(stdout) {
|
|
277
951
|
const lines = stdout.trim().split("\n").filter(Boolean);
|
|
278
952
|
const diagnostics = [];
|
|
953
|
+
let buildFinishedSuccess;
|
|
279
954
|
for (const line of lines) {
|
|
280
955
|
let msg;
|
|
281
956
|
try {
|
|
@@ -284,6 +959,11 @@ function parseCompilerMessages(stdout) {
|
|
|
284
959
|
catch {
|
|
285
960
|
continue;
|
|
286
961
|
}
|
|
962
|
+
// Gap #89: Capture build-finished event's success field
|
|
963
|
+
if (msg.reason === "build-finished") {
|
|
964
|
+
buildFinishedSuccess = msg.success;
|
|
965
|
+
continue;
|
|
966
|
+
}
|
|
287
967
|
if (msg.reason !== "compiler-message" || !msg.message)
|
|
288
968
|
continue;
|
|
289
969
|
const span = msg.message.spans[0];
|
|
@@ -292,15 +972,23 @@ function parseCompilerMessages(stdout) {
|
|
|
292
972
|
const severity = (["error", "warning", "note", "help"].includes(msg.message.level)
|
|
293
973
|
? msg.message.level
|
|
294
974
|
: "warning");
|
|
295
|
-
|
|
975
|
+
const diagnostic = {
|
|
296
976
|
file: span.file_name,
|
|
297
977
|
line: span.line_start,
|
|
298
978
|
column: span.column_start,
|
|
299
979
|
severity,
|
|
300
980
|
code: msg.message.code?.code || undefined,
|
|
301
981
|
message: msg.message.message,
|
|
302
|
-
}
|
|
982
|
+
};
|
|
983
|
+
// Gap #90: Extract suggestion text from children array
|
|
984
|
+
if (msg.message.children && msg.message.children.length > 0) {
|
|
985
|
+
const helpChild = msg.message.children.find((c) => c.level === "help" || c.level === "suggestion");
|
|
986
|
+
if (helpChild && helpChild.message) {
|
|
987
|
+
diagnostic.suggestion = helpChild.message;
|
|
988
|
+
}
|
|
989
|
+
}
|
|
990
|
+
diagnostics.push(diagnostic);
|
|
303
991
|
}
|
|
304
|
-
return diagnostics;
|
|
992
|
+
return { diagnostics, buildFinishedSuccess };
|
|
305
993
|
}
|
|
306
994
|
//# sourceMappingURL=parsers.js.map
|