Skip to content

Commit 151fdf7

Browse files
Pipeline vm2 fix (#405)
* Pipeline: Fix error position parsing in VM2 * Pipeline: upgrade vm2 version
1 parent ef999cb commit 151fdf7

File tree

4 files changed

+75
-14
lines changed

4 files changed

+75
-14
lines changed

pipeline/package-lock.json

Lines changed: 46 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pipeline/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"body-parser": "^1.19.0",
2424
"cors": "^2.8.5",
2525
"express": "^4.17.1",
26-
"vm2": "^3.9.5"
26+
"vm2": "^3.9.9"
2727
},
2828
"devDependencies": {
2929
"@jvalue/eslint-config-jvalue": "^1.1.0",

pipeline/src/pipeline-execution/sandbox/vm2SandboxExecutor.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,27 @@ export default class VM2SandboxExecutor implements SandboxExecutor {
2626

2727
const json = JSON.stringify(data);
2828

29-
const script = new VMScript(code, 'main');
30-
script.wrap('function main(data) {\n', `\n}\nmain(${json});`);
29+
const wrappedCode =
30+
'function main(data) {\n' + `${code}\n` + '}\n' + `main(${json})`;
31+
const script = new VMScript(wrappedCode, 'main');
3132

3233
try {
3334
script.compile();
3435
} catch (err) {
3536
const error = err as Error;
36-
return { error: convertSyntaxError(error, FUNCTION_WRAP_PREFIX_LENGTH) };
37+
return {
38+
error: convertSyntaxError(error, FUNCTION_WRAP_PREFIX_LENGTH, code),
39+
};
3740
}
3841

3942
try {
4043
const result = this.vm.run(script) as Record<string, unknown>;
4144
return { data: result };
4245
} catch (err) {
4346
const error = err as Error;
44-
return { error: convertRuntimeError(error, FUNCTION_WRAP_PREFIX_LENGTH) };
47+
return {
48+
error: convertRuntimeError(error, FUNCTION_WRAP_PREFIX_LENGTH),
49+
};
4550
}
4651
}
4752
}

pipeline/src/pipeline-execution/sandbox/vm2StacktraceParser.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,12 @@ function rewriteStacktrace(oldLines: string[], prefixLength: number): string[] {
6969
* at VMScript.compile (/home/ods-main/pipeline/node_modules/vm2/lib/main.js:80:20)
7070
* ...
7171
* @param error The original javascript syntax error
72+
* @param codeSnippet The original executed code snippet
7273
*/
7374
export function convertSyntaxError(
7475
error: Error,
7576
prefixLength: number,
77+
originalCode: string,
7678
): JobError {
7779
if (error.name !== 'SyntaxError') {
7880
throw new Error('Not a syntax error');
@@ -86,13 +88,28 @@ export function convertSyntaxError(
8688
const header = lines[0];
8789
const [, lineNumber] = parseSyntaxErrorHeader(header);
8890
const lineNumberAdjusted = lineNumber - prefixLength;
91+
const message = lines[4];
92+
93+
const codeSnippetLines = originalCode.split('\n').length;
94+
const errorProjectedToWrappingMain =
95+
lineNumberAdjusted === codeSnippetLines + 1;
96+
if (errorProjectedToWrappingMain) {
97+
// Special case: the error concerns the wrapping main function
98+
const lastCodeLine = originalCode.split('\n')[codeSnippetLines - 1];
99+
const lastPositionInLastCodeLine = lastCodeLine.length - 1;
100+
return {
101+
name: error.name,
102+
message,
103+
position: +lastPositionInLastCodeLine,
104+
lineNumber: codeSnippetLines,
105+
stacktrace: [],
106+
};
107+
}
89108

90109
const markers = lines[2];
91110
const markerIndex = markers.indexOf('^');
92111
const position = markerIndex > 0 ? markerIndex : 0;
93112

94-
const message = lines[4];
95-
96113
return {
97114
name: error.name,
98115
message,

0 commit comments

Comments
 (0)