Skip to content

Commit e907af9

Browse files
committed
fix: Now world error failing to parse
Signed-off-by: Gordon Smith <GordonJSmith@gmail.com>
1 parent be62473 commit e907af9

File tree

2 files changed

+79
-32
lines changed

2 files changed

+79
-32
lines changed

src/errorParser.ts

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,22 @@
33
*/
44

55
/**
6-
* Regex pattern to extract error information from WIT validation errors
7-
* Groups:
8-
* 1. Main error message (after "Error: ")
9-
* 2. Detailed error message (after "Caused by:")
10-
* 3. File path (from the arrow line)
11-
* 4. Row number
12-
* 5. Column number
6+
* Regex patterns to extract error information from WIT validation errors
137
*/
14-
const witErrorRegex =
8+
9+
/**
10+
* Pattern for errors with file location information
11+
* Groups: 1=main error, 2=detailed error, 3=file path, 4=row, 5=column
12+
*/
13+
const witErrorWithLocationRegex =
1514
/Error:\s*([^\n]+)(?:\n\nCaused by:\s*\n\s*([^\n]+(?:\n[^\n-]+)*?))?[\s\S]*?-->\s*([^:]+):(\d+):(\d+)/;
1615

16+
/**
17+
* Pattern for errors without file location information
18+
* Groups: 1=main error
19+
*/
20+
const witErrorWithoutLocationRegex = /Error:\s*([^\n]+)/;
21+
1722
/**
1823
* Extracts error information from a WIT validation error stack trace
1924
* @param errorStack - The error stack trace string
@@ -26,19 +31,33 @@ export function extractErrorInfo(errorStack: string): {
2631
row?: number;
2732
column?: number;
2833
} | null {
29-
const match = errorStack.match(witErrorRegex);
34+
// First try to match errors with file location
35+
let match = errorStack.match(witErrorWithLocationRegex);
3036

31-
if (!match) {
32-
return null;
37+
if (match) {
38+
const [, mainError, detailedError, filePath, rowStr, columnStr] = match;
39+
return {
40+
mainError: mainError.trim(),
41+
detailedError: detailedError?.trim(),
42+
filePath: filePath.trim(),
43+
row: parseInt(rowStr, 10),
44+
column: parseInt(columnStr, 10),
45+
};
3346
}
3447

35-
const [, mainError, detailedError, filePath, rowStr, columnStr] = match;
48+
// If no location match, try simple error format
49+
match = errorStack.match(witErrorWithoutLocationRegex);
50+
51+
if (match) {
52+
const [, mainError] = match;
53+
return {
54+
mainError: mainError.trim(),
55+
detailedError: undefined,
56+
filePath: undefined,
57+
row: undefined,
58+
column: undefined,
59+
};
60+
}
3661

37-
return {
38-
mainError: mainError.trim(),
39-
detailedError: detailedError?.trim(),
40-
filePath: filePath.trim(),
41-
row: parseInt(rowStr, 10),
42-
column: parseInt(columnStr, 10),
43-
};
62+
return null;
4463
}

tests/errorParser.test.ts

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ Caused by:
3232
at async syntaxCheckCurrentFile (/home/gordon/vscode-wit/dist/extension.js:152:21)
3333
at async /home/gordon/vscode-wit/dist/extension.js:142:5
3434
at async Xb.h (file:///home/gordon/.vscode-server/bin/dfaf44141ea9deb3b4096f7cd6d24e00c147a4b1/out/vs/workbench/api/node/extensionHostProcess.js:120:41516)`,
35+
`Error: The main package \`test:comments\` contains no worlds
36+
at generateTypes (file:///home/gordon/vscode-wit/dist/extension.js:28632:11)
37+
at typesComponent (file:///home/gordon/vscode-wit/dist/extension.js:28853:14)
38+
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
39+
at async WitSyntaxValidator.validate (file:///home/gordon/vscode-wit/dist/extension.js:28933:7)
40+
at async syntaxCheckCurrentFile (file:///home/gordon/vscode-wit/dist/extension.js:29078:23)
41+
at async file:///home/gordon/vscode-wit/dist/extension.js:29046:9`,
3542
];
3643

3744
describe("WIT Error Parser", () => {
@@ -62,6 +69,17 @@ describe("WIT Error Parser", () => {
6269
expect(result?.column).toBe(1);
6370
});
6471

72+
it("should extract error info from third sample stack (error without file location)", () => {
73+
const result = extractErrorInfo(testSampleStacks[2]);
74+
75+
expect(result).not.toBeNull();
76+
expect(result?.mainError).toBe("The main package `test:comments` contains no worlds");
77+
expect(result?.detailedError).toBeUndefined();
78+
expect(result?.filePath).toBeUndefined();
79+
expect(result?.row).toBeUndefined();
80+
expect(result?.column).toBeUndefined();
81+
});
82+
6583
it("should return null for invalid error stack", () => {
6684
const invalidStack = "This is not a valid WIT error stack";
6785
const result = extractErrorInfo(invalidStack);
@@ -153,20 +171,30 @@ Caused by:
153171

154172
// Verify the extracted information is reasonable
155173
expect(result?.mainError?.length, "Main error should not be empty").toBeGreaterThan(0);
156-
expect(result?.detailedError?.length, "Detailed error should not be empty").toBeGreaterThan(0);
157-
expect(result?.filePath, "File path should contain .wit").toContain(".wit");
158-
expect(result?.row, "Row should be positive").toBeGreaterThan(0);
159-
expect(result?.column, "Column should be positive").toBeGreaterThan(0);
160-
161-
// Check specific expected values
162-
if (index === 0) {
163-
expect(result?.filePath).toContain("floats.wit");
164-
} else if (index === 1) {
165-
expect(result?.filePath).toContain("integers.wit");
166-
}
167174

168-
expect(result?.row).toBe(3);
169-
expect(result?.column).toBe(1);
175+
if (index < 2) {
176+
// First two samples have detailed error and file location
177+
expect(result?.detailedError?.length, "Detailed error should not be empty").toBeGreaterThan(0);
178+
expect(result?.filePath, "File path should contain .wit").toContain(".wit");
179+
expect(result?.row, "Row should be positive").toBeGreaterThan(0);
180+
expect(result?.column, "Column should be positive").toBeGreaterThan(0);
181+
182+
// Check specific expected values
183+
if (index === 0) {
184+
expect(result?.filePath).toContain("floats.wit");
185+
} else if (index === 1) {
186+
expect(result?.filePath).toContain("integers.wit");
187+
}
188+
189+
expect(result?.row).toBe(3);
190+
expect(result?.column).toBe(1);
191+
} else {
192+
// Third sample has no detailed error or file location
193+
expect(result?.detailedError).toBeUndefined();
194+
expect(result?.filePath).toBeUndefined();
195+
expect(result?.row).toBeUndefined();
196+
expect(result?.column).toBeUndefined();
197+
}
170198
});
171199
});
172200
});

0 commit comments

Comments
 (0)