Skip to content

Commit 64a3953

Browse files
committed
fix(execution): read backtick file references and never hoist imports from broken code
- A template literal with nothing substituted into it is one string token again, so getFileBase64(`photo-1`) keeps its file reference. - The import scan parses strictly: a recoverable error is still an error, so imports are never hoisted out of code that will not run.
1 parent e7fa51b commit 64a3953

5 files changed

Lines changed: 25 additions & 15 deletions

File tree

‎apps/sim/lib/execution/javascript-syntax.ts‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,25 @@ import { type ParseResult, parse } from '@babel/parser'
22
import type { File } from '@babel/types'
33

44
interface UserJavaScriptParseOptions {
5-
/** Keep parsing past errors that leave the tree intact, listing them on `errors`. */
6-
errorRecovery?: boolean
75
/** Include every token, comments among them, on `tokens`. */
86
tokens?: boolean
97
}
108

119
/**
1210
* Parses JavaScript a user wrote, which runs as the body of an async function: top-level
1311
* `return` and `await` are allowed, static imports may sit anywhere, and the source is
14-
* sloppy-mode script, as the sandboxes evaluate it. Throws a `SyntaxError` carrying `loc`
15-
* when the source does not parse.
12+
* sloppy-mode script, as the sandboxes evaluate it. Throws a `SyntaxError` carrying `loc` at
13+
* the first error, recoverable or not: code that does not parse is never half-read.
1614
*/
1715
export function parseUserJavaScript(
1816
code: string,
19-
{ errorRecovery = false, tokens = false }: UserJavaScriptParseOptions = {}
17+
{ tokens = false }: UserJavaScriptParseOptions = {}
2018
): ParseResult<File> {
2119
return parse(code, {
2220
sourceType: 'script',
2321
allowImportExportEverywhere: true,
2422
allowReturnOutsideFunction: true,
2523
allowAwaitOutsideFunction: true,
26-
errorRecovery,
2724
tokens,
2825
plugins: ['jsx'],
2926
})

‎apps/sim/lib/function-execution/execute-request.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,11 @@ describe('Function execution request', () => {
847847
it.each([
848848
['a require call', 'const path = require("node:path")\nreturn path.sep', 1],
849849
['code that does not parse', 'import path from "node:path"\nreturn (', 0],
850+
[
851+
'code whose only error a parser can recover from',
852+
'import path from "node:path"\nlet a = 1\nlet a = 2\nreturn path.sep',
853+
0,
854+
],
850855
])('sends %s to the remote sandbox only when it loads modules', async (_, code, calls) => {
851856
envFlagsMock.isRemoteSandboxEnabled = true
852857

‎apps/sim/lib/function-execution/execute-request.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ function extractJavaScriptImports(code: string): {
418418
} {
419419
let program: Program
420420
try {
421-
program = parseUserJavaScript(code, { errorRecovery: true }).program
421+
program = parseUserJavaScript(code).program
422422
} catch {
423423
return { imports: '', remainingCode: code, hasRequireCalls: false, identifierNames: new Set() }
424424
}

‎apps/sim/lib/uploads/documents/compile.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ describe('collectReferencedFileIds', () => {
9292
expect(collectReferencedFileIds(src, 'javascript')).toEqual(new Set(['pdf-img-2']))
9393
})
9494

95+
it('captures the id from a template literal with nothing substituted into it', () => {
96+
expect(collectReferencedFileIds('await getFileBase64(`photo-1`)', 'javascript')).toEqual(
97+
new Set(['photo-1'])
98+
)
99+
})
100+
95101
it('still supports the legacy /home/user/inputs/<id> path form', () => {
96102
expect(
97103
collectReferencedFileIds(`fs.readFileSync('/home/user/inputs/legacy-1')`, 'javascript')

‎apps/sim/lib/uploads/documents/references.ts‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ function* javascriptTokens(source: string): Generator<SourceToken> {
5252
if (error instanceof SyntaxError) throw new DocCompileUserError(error.message)
5353
throw error
5454
}
55-
for (const [index, token] of lexed.entries()) {
55+
for (let index = 0; index < lexed.length; index++) {
56+
const token = lexed[index]
5657
if (token.label === 'eof') continue
5758
if (token.label === 'string') {
5859
yield {
@@ -62,13 +63,14 @@ function* javascriptTokens(source: string): Generator<SourceToken> {
6263
}
6364
continue
6465
}
65-
/* A template is a string only when nothing is substituted into it: `...` with one chunk. */
66-
if (
67-
token.label === 'template' &&
68-
lexed[index - 1]?.label === '`' &&
69-
lexed[index + 1]?.label === '`'
70-
) {
71-
yield { kind: 'string', value: source.slice(token.start, token.end), start: token.start }
66+
/*
67+
* A template with nothing substituted into it is one string, as a quoted literal is: its
68+
* backticks are not tokens of their own, so a call's `(` stays right before the string.
69+
*/
70+
const chunk = lexed[index + 1]
71+
if (token.label === '`' && chunk?.label === 'template' && lexed[index + 2]?.label === '`') {
72+
yield { kind: 'string', value: source.slice(chunk.start, chunk.end), start: chunk.start }
73+
index += 2
7274
continue
7375
}
7476
yield {

0 commit comments

Comments
 (0)