Parser invocation breaks when any path or argument contains a space (`command.split(' ')`)
- CLI version: 1.4.4
- OS: any
- Parser:
customswiftorcompose
Description
In cli/src/connect/parser_executables.ts:120, the parser command is built as a single string and split on whitespace before being passed to cross-spawn:
const command = await parser.command(cwd, configWithVerbose, payload.mode)
// ...
const commandSplit = command.split(' ')
const child = spawn(commandSplit[0], commandSplit.slice(1), { cwd })
Any space in a resolved path (or in a user-provided argument) is treated as a separator, so the path gets fragmented across multiple argv entries and the spawn fails
The same file already mixes shell quoting and argv semantics in cli/src/parser_scripts/get_swift_parser_dir.ts:27,29,31,32, which apply xcodeprojPath.replace(/\s/g, '\\ ') to paths that are then passed to spawnSync in array form. With argv-form spawn, the backslash is preserved literally in the argument, so the escape makes the path invalid rather than fixing the space
Affected parsers
This affects every parser whose command contains a path that could include a space:
custom:parserCommandfrom the user'sfigma.config.jsonis passed through verbatim. Any user whose tool path contains a space (e.g.node /Users/me/My Tool/parse.js) hits this on the first runcompose: triggers whengradleWrapperPathin config is set to an absolute path containing a spaceswift: triggers when the resolved Code Connect Swift package directory contains a space, which can happen when the user has a local checkout of the package in a folder with a space, or in some XcodeDerivedDataconfigurations
To reproduce (custom parser, simplest case)
- Create a directory with a space and a parser script:
mkdir -p "/tmp/My Test Dir"
cat > "/tmp/My Test Dir/parser.js" <<'EOF'
process.stdin.on('data', () => {})
process.stdin.on('end', () => {
process.stdout.write(JSON.stringify({ docs: [], messages: [] }))
})
EOF
- Configure a project to use it:
{
"codeConnect": {
"parser": "custom",
"parserCommand": "node /tmp/My Test Dir/parser.js",
"include": ["**/*.foo"]
}
}
- Run
npx figma connect parse. CLI exits with:
Error calling parser: Error: Cannot find module '/tmp/My'
A standalone Node reproduction of the underlying behavior:
'node /tmp/My Test Dir/parser.js'.split(' ')
// [ 'node', '/tmp/My', 'Test', 'Dir/parser.js' ]
The first argument passed to node becomes /tmp/My which is not a valid module path
Expected behavior
The parser is invoked with the path intact regardless of whether it contains spaces
Suggested fix
Build the command and arguments as an array end-to-end and pass them to spawn directly, instead of joining and re-splitting:
type ParserInfo = {
command: (
cwd: string,
config: CodeConnectExecutableParserConfig | CodeConnectCustomExecutableParserConfig,
mode: ParserRequestPayload['mode'],
) => Promise<{ cmd: string; args: string[] }>
// ...
}
// swift:
command: async (cwd, config) => ({
cmd: 'swift',
args: ['run', '--package-path', await getSwiftParserDir(cwd, ...), 'figma-swift'],
})
// compose:
command: async (cwd, config, mode) => {
const gradlewPath = await getGradleWrapperPath(cwd, (config as any).gradleWrapperPath)
const args = [
'-p', gradlewPath,
mode === 'CREATE' ? 'createCodeConnect' : 'parseCodeConnect',
`-PfilePath=${temporaryInputFilePath}`,
...((config as any).verbose ? ['--stacktrace'] : []),
`-PoutputDir=${temporaryOutputDirectoryPath}`,
]
return { cmd: getGradleWrapperExecutablePath(gradlewPath), args }
}
For the custom parser where parserCommand is a free-form string from user config either:
a) Spawn through the shell so the users existing quoting works:
spawn(config.parserCommand, [], { cwd, shell: true })
b) or document that parserCommand accepts an array, and accept either form
The replace(/\s/g, '\\ ') calls in cli/src/parser_scripts/get_swift_parser_dir.ts:27,29,31,32 should be removed as part of the fix, since they are not needed (and are harmful) once arguments are passed as arrays
Additional context
- Bug location:
cli/src/connect/parser_executables.ts:120-124 - Related: the broken escape attempt in
cli/src/parser_scripts/get_swift_parser_dir.ts:27,29,31,32 - Test gap:
cli/src/connect/__test__/parser_executables.test.ts:44mocksgetGradleWrapperPathto return/path/to/gradle(no space). Adding a fixture with a space-containing path would be a natural regression test