You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js中spawnSync执行git ls-files与直接执行命令返回结果不一致的问题咨询

Node.js中spawnSync执行git ls-files与直接执行命令返回结果不一致的问题咨询

大家好,我最近碰到一个特别困惑的问题,想过来请教下各位大佬。

先跟大家说下我的项目文件结构:

foo/
├── bar/
│   └── a.json
└── baz/
    └── b.json

我在终端里直接执行 git ls-files foo/bar/**/* 这条命令时,Git会返回两个文件:foo/bar/a.json 和 foo/baz/b.json,这和我预期的匹配结果一致。

但是当我用Node.js的spawnSync写脚本执行同样的逻辑时,结果却只返回了foo/baz/b.json,完全找不到a.json的踪影。我的脚本代码如下:

import { spawnSync } from 'node:child_process';

const pattern = 'foo/bar/**/*';
const lsFiles = spawnSync(
  `git`,
  ['ls-files', pattern],
  { encoding: 'utf8' },
).stdout;

console.log(lsFiles);

我反复检查了好几遍代码,确认参数和命令都是对的,但就是搞不懂为什么直接在终端执行和通过Node.js子进程执行会有这么大的差异。有没有朋友遇到过类似的问题,或者能帮我分析下问题出在哪,以及该怎么调整脚本才能得到和终端里一样的结果呀?

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.08 08:58:09