search.exclude 必须写在 .vscode/settings.json 才生效,VS Code 仅识别用户级或工作区级 settings.json 中的配置,其他文件中的 search.exclude 均被忽略。

search.exclude 必须写在 .vscode/settings.json 才生效
VS Code 的搜索排除规则只认两个位置:用户级 settings.json(全局生效),或工作区根目录下的 .vscode/settings.json(仅当前项目)。写在 package.json、tsconfig.json、eslint.config.js 里的 search.exclude 配置,VS Code 根本不读——改了等于没改。
多根工作区(monorepo)下,每个子项目必须各自有 .vscode/settings.json;如果想统一控制,得在 .code-workspace 文件的 settings 字段里配,不能只靠根目录一个配置覆盖全部。
-
"**/node_modules": true是有效写法;"node_modules": true或"node_modules/**": true都不匹配任意层级的node_modules - 路径必须以
**/开头(表示递归),或用${workspaceFolder}/dist/**这类带变量的绝对路径 - Windows 用户注意:一律用正斜杠
/,\会导致整个规则失效
search.include 白名单机制比界面输入更可靠
想让 Ctrl+Shift+F 只搜 src 和 include?别依赖搜索框底部临时填的 src/**,它只是弱过滤器,且没有根路径上下文,容易漏匹配。直接在 .vscode/settings.json 里设 search.include:
{
"search.include": {
"${workspaceFolder}/src/**": true,
"${workspaceFolder}/include/**": true
}
}
这个白名单优先级最高,未列出的路径完全不扫描,连文件元数据都不读——不是“搜完再过滤”,是“根本不去看”。
- 多个源码目录就都列出来,比如
"${workspaceFolder}/lib/**": true - 第三方头文件路径可以硬编码,如
"D:/SDK/opencv/include/**": true,只对该工作区生效 - 如果同时配了
search.exclude,它仍起作用:白名单内被排除的子路径(如"${workspaceFolder}/src/generated/**": true)照样跳过
为什么改了 search.exclude 还是搜到 dist/ 里的结果?
最常见原因是 VS Code 没刷新索引缓存。它不会自动感知配置变更,尤其大项目里,旧索引可能持续生效数分钟。
- 按
Ctrl+P输入workbench.action.reloadWindow强制重载窗口(比关再开快) - 或者手动删掉工作区根目录下的
.vscode/.search文件夹,重启后重建索引 - 检查右下角是否显示「x files searched」——如果数字远小于预期,大概率是
search.useIgnoreFiles(默认启用)读了.gitignore把dist/整个拦住了 - 临时验证:按
Ctrl+Shift+P→ 输入Search: Toggle Search Exclude Rules关掉它再搜,若结果暴增,就是.gitignore在作祟
files.exclude 和 search.exclude 必须同时设
files.exclude 控制侧边栏是否显示文件,search.exclude 控制 Ctrl+Shift+F 是否扫描——两者作用域完全不同,缺一不可。
只设 search.exclude 而不设 files.exclude,VS Code 仍会为 node_modules 建立文件监视器(watcher),导致 inotify 句柄耗尽、CPU 持续飙升;反之,只设 files.exclude,搜索时照样扫那些被隐藏的目录,照样卡。
- 推荐同步配置:
"files.exclude": { "**/node_modules": true, "**/dist": true },\n"search.exclude": { "**/node_modules": true, "**/dist": true } -
search.maxFileSize默认跳过 >12.5KB 的文件(比如打包后的bundle.js),要搜这类内容得显式设为5242880(5MB) - 真正想搜
node_modules里的代码?不能删配置,得写"**/node_modules": false显式覆盖默认排除规则
.gitignore 的隐式干扰——这两点不排查,光调配置毫无意义。


















