Plandex 如何用 .plandexignore 防止敏感文件被加载进上下文
【免费下载链接】plandexOpen source AI coding agent. Designed for large projects and real world tasks.项目地址: https://gitcode.com/GitHub_Trending/pl/plandex
使用 Plandex 时,plandex load会把你指定的文件、目录(含--recursive递归加载的整个目录树)放进 plan 的上下文,供 LLM 读取。问题在于,目录里往往混着.env、密钥等不该让模型看到的内容。本文说明如何用.plandexignore文件声明忽略规则,让这些敏感文件在加载上下文时被跳过,并用plandex ls验证结果。
忽略机制的两种来源
Plandex 判断一个文件是否被忽略时参考两个来源(见 Security 文档 和 Context Management 文档):
.gitignore:如果你在 git 仓库中工作,Plandex 会遵守.gitignore,被忽略的文件不会加载。.plandexignore:你可以在任意目录下放置一个.plandexignore文件,里面写入 ignore patterns,对匹配的文件生效。
也就是说,.gitignore依赖项目是 git 仓库,而.plandexignore是 Plandex 自己的补充手段——即使文件没被 git 忽略,也可以单独排除它。CLI 端在 GetPlandexIgnore 中读取并解析该文件,解析失败时会报error reading .plandexignore file之类的错误,所以写入内容后值得实际跑一次加载确认格式没问题。
配置 .plandexignore
在要保护的文件所在目录(通常是项目根目录)创建.plandexignore文件,每行写一条 ignore pattern。文档给出的典型对象是.env:
.env如果敏感文件在其他位置,按同样方式把对应路径写入文件即可。该文件不影响 git,只影响 Plandex 的上下文加载。
执行加载并观察跳过提示
在已有 plan 的终端目录下加载当前目录(或具体目录):
plandex load .如果有路径因.gitignore或.plandexignore被跳过,CLI 会在加载输出后显示如下提示(见 printIgnoredMsg):
ℹ️ Due to .gitignore or .plandexignore, some paths weren't loaded. Use --force / -f to load ignored paths.看到这条提示,说明忽略规则已经生效,被匹配的路径没有被加载。
验证敏感文件确实不在上下文里
用plandex ls列出当前 plan 的全部上下文,输出包含索引、名称、类型和 token 大小:
plandex ls确认被忽略的文件(如.env)不在列表中。如果怀疑某个已加载的条目包含不该有的内容,还可以用plandex show查看具体内容:
plandex show component.ts # show the content of component.ts plandex show 2 # show the content of the second item in the `plandex ls` list反过来,如果确实需要临时加载一个被忽略的文件,用--force/-f跳过忽略规则:
plandex load .env --force # loads the .env file even if it's in .gitignore or .plandexignore该 flag 在 CLI Reference 中的说明是:--force/-f: Load files even when ignored by .gitignore or .plandexignore。
限制与边界
.gitignore的自动遵守只在 git 仓库中生效;非 git 目录里只能靠.plandexignore。- 忽略规则只影响"是否加载进上下文"。如果文件已经在上下文里(忽略规则加入之前加载的),需要先用
plandex rm <name>或plandex rm 2-5(按plandex ls的索引)移除。 - 上下文变更本身有版本控制记录,加载与移除操作都作用于当前 plan。
完成以上步骤后,敏感文件即被排除在当前及后续 plan 的上下文加载之外;后续如果调整了.plandexignore的内容,重复一次plandex load加plandex ls的核对即可确认新的生效范围。
【免费下载链接】plandexOpen source AI coding agent. Designed for large projects and real world tasks.项目地址: https://gitcode.com/GitHub_Trending/pl/plandex
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考