如何在VSCode调试TypeScript时将输出管道传输至其他程序?
VSCode调试TypeScript测试文件时管道符转义问题解决方法
在VSCode中调试单个TypeScript测试文件时,尝试将测试输出通过管道符(|)传输给tap-arc工具,但VSCode会自动转义管道符,导致实际执行的命令中管道符变成\|,无法实现输出转发。当前使用的launch.json配置如下:
{ "type": "node", "request": "launch", "name": "tap-single", "skipFiles": [ "<node_internals>/**", "${workspaceFolder}/node_modules/**" ], "program": "${file}", "runtimeExecutable": "/home/user/.npm-global/bin/ts-node", "runtimeArgs": [ "--transpileOnly", ], "args": [ "|", // 此处管道符在控制台中被转义为 `\|` "tap-arc" ], "console": "integratedTerminal", }
解决方法
问题根源在于VSCode会将args数组中的每个元素作为程序的参数传递,而非交给shell解析管道逻辑。因此需要通过shell来执行完整命令,具体有两种配置方式:
方式一:通过bash执行完整命令
直接将整个命令作为shell的执行参数,让bash处理管道符:
{ "type": "node", "request": "launch", "name": "tap-single", "skipFiles": [ "<node_internals>/**", "${workspaceFolder}/node_modules/**" ], "runtimeExecutable": "bash", "runtimeArgs": [ "-c", "/home/user/.npm-global/bin/ts-node --transpileOnly ${file} | tap-arc" ], "console": "integratedTerminal" }
这种方式明确通过shell解析完整命令,管道符不会被转义,${file}会自动替换为当前打开的测试文件路径。
方式二:启用shell模式
在launch.json中添加shell: true,让VSCode通过shell执行命令:
{ "type": "node", "request": "launch", "name": "tap-single", "skipFiles": [ "<node_internals>/**", "${workspaceFolder}/node_modules/**" ], "program": "${file}", "runtimeExecutable": "/home/user/.npm-global/bin/ts-node", "runtimeArgs": [ "--transpileOnly" ], "console": "integratedTerminal", "shell": true, "args": [ "|", "tap-arc" ] }
不过这种方式在部分环境下可能存在兼容性问题,更推荐第一种方式。
内容的提问来源于stack exchange,提问作者myol
相关产品推荐
相关产品推荐

