CodeGraph 開箱即零設定:語言支援依副檔名自動,沒什麼要逐語言接線。唯一的選擇性檔是 codegraph.json,放在專案根目錄。
開箱即跳過:
node_modules、vendor、dist、build、target、.venv、Pods、.next…(即使沒有 .gitignore 也成立)——圖是你的程式,不是第三方噪音。.gitignore(根與巢狀)。想把東西排除:加進 .gitignore。想把預設排除的目錄拉回來(例如你真的想索引 vendored 依賴):加否定式 !vendor/。
但 .gitignore 無法丟掉你已 commit 的目錄。對已 commit 的 vendored theme / SDK(例如 Metronic theme 在 static/),在 codegraph.json 的 exclude 下列出(gitignore 風格 pattern,對 repo-root-relative 路徑比對,index/sync/watch 都適用):
{
"exclude": ["static/", "**/vendor/**"]
}
反過來,當真原始碼被刻意 gitignore(專案同時用第二套 VCS——SVN、Perforce——把自己原始碼 gitignore 掉),用 include 強制拉回(include 是 exclude 的反面;includeIgnored 只救回 embedded git repos,不是普通原始碼):
{
"include": ["Tools/", "Local/typescript/"]
}
CodeGraph 在 index/sync/watch 時從磁碟發現這些檔、覆寫 .gitignore。顯式 exclude 仍優先;內建跳過(node_modules、dist、.git)永不重開。
專案用非標準副檔名對應受支援語言時(例如 .dota_lua 是 Lua、.tpl 是 PHP),用 extensions 對映:
{
"extensions": {
".dota_lua": "lua",
".tpl": "php"
}
}
每個值是受支援的語言 id。對映會疊在內建預設之上、衝突時取勝,所以也能改指內建(例如 ".h": "cpp")。commit 這個檔與團隊共享。打錯語言或格式壞掉的檔只會警告並跳過,不會中斷索引;改完對映要 codegraph index 重跑。
| 變數 | 作用 |
|---|---|
CODEGRAPH_WATCH_DEBOUNCE_MS | watcher debounce 窗口(100ms–60s,預設 2000) |
CODEGRAPH_MCP_TOOLS | MCP 工具 allowlist(預設只有 explore) |
CODEGRAPH_NO_DAEMON | 跳過共享背景 server,每個 session 自己跑 |
CODEGRAPH_DIR | 換 .codegraph 目錄名(Windows/WSL 共存用) |
CODEGRAPH_TELEMETRY=0 / DO_NOT_TRACK=1 | 關閉匿名 telemetry |
CodeGraph 的設定哲學是「Convention over Configuration」——預設值已經夠好用,只有特殊需求才需要設定檔。
三個「開箱即跳過」的規則背後的考量:
假設你有一個 monorepo,想排除 vendor 但保留 tools 目錄:
# 步驟 1:建立 codegraph.json
cat > codegraph.json << 'EOF'
{
"exclude": [
"vendor/",
"**/generated/**",
"**/*.min.js"
],
"include": [
"tools/",
"scripts/"
],
"extensions": {
".dota_lua": "lua",
".tpl": "php",
".hbs": "javascript"
}
}
EOF
# 步驟 2:重新索引
codegraph index --force
# 步驟 3:驗證
codegraph files --max-depth 2 | head -20
關鍵觀察:include 可以「拉回」被 gitignore 的目錄,但 exclude 永遠優先。這讓你可以精確控制索引範圍。
| 症狀 | 原因 | 解法 |
|---|---|---|
| 某些檔案未被索引 | 被 exclude 或 gitignore 排除 | 用 codegraph files 檢查,必要時用 include |
| 非標準副檔名未解析 | 未設定 extensions 對映 | 在 codegraph.json 的 extensions 加入對映 |
| 改了設定但沒效 | 需要重新索引 | 執行 codegraph index --force |
| vendor 目錄仍被索引 | 用了 include 但未在 exclude 排除 | 確認 exclude 優先於 include |
exclude 與 include 的語意codegraph.json 並自訂排除/納入規則extensions 對映非標準副檔名index --force前面的範例是乾淨 monorepo。這裡換成更棘手的 legacy:專案同時用 git 與 SVN,真正的原始碼被 .gitignore 掉(公司強制用 SVN 管 src/),同時已 commit 的 vendored theme 躺在 static/。你要讓圖譜只看「人寫的程式」。
# ① 檢查「人寫的程式」是否真的被 gitignore 排除
git check-ignore src/App.ts # 輸出 src/App.ts → 確實被擋
# ② 在 codegraph.json 用 include 強制拉回(exclude 的反面)
cat > codegraph.json << 'EOF'
{
"include": ["src/", "Tools/"],
"exclude": ["static/", "**/vendor/**", "**/*.min.js"]
}
EOF
# ③ 重新索引並驗證
codegraph index --force
codegraph files --filter "src/" | head -20 # 確認 src/ 進圖
codegraph query static # 應該查不到 static 的符號
為什麼走這條路徑:`.gitignore` 無法丟掉已 commit 的目錄,也無法「拉回」被自己 ignore 掉的檔案——所以雙向要靠 codegraph.json:include 救回真原始碼、exclude 丟掉已 commit 的噪音。顯式 exclude 永遠優先於 include,這個優先權是這個設定能同時成立的原因。
內部語意:三層排除的優先權。CodeGraph 的排除語意是疊加的,從低到高:內建跳過(node_modules、dist、.git…)→ gitignore(git repo 讀 git;非 git 讀 .gitignore)→ codegraph.json exclude。要拉回某個東西,只有 include 做得到,而且內建跳過永不重開。另外一個關鍵細節:includeIgnored 只救回「embedded git repos」,不是普通被 ignore 的原始碼——很多人的直覺會拿錯工具。pattern 是 repo-root-relative 比對,不是對絕對路徑。
/abs/path 或帶前導 slash 的寫法行為可能不如預期。另一個誤解:以為改了 extensions 對映後下次查詢就生效,其實需要 codegraph index 重跑(metadata 已寫進 DB)。| 症狀 | 可能原因 | 解決方案 |
|---|---|---|
| 改了 extensions 但沒生效 | 沒 re-index | codegraph index --force 重跑 |
| include 拉不回某目錄 | 該目錄在內建排除清單(如 node_modules) | 內建不可覆蓋——改用 rename 或 symlink 策略繞過 |
| 已 commit 的 vendor 還是進圖 | 只寫 .gitignore、沒寫 exclude | 在 codegraph.json 的 exclude 列出 |
| Windows/WSL 兩邊索引不同步 | 兩邊共用同一 .codegraph/ | 用 CODEGRAPH_DIR=.codegraph-win 分開 |
| 某副檔名被當成錯語言 | extensions 對映值打錯語言 id | 檢查對映值是否為受支援語言 id,再 re-index |
Tools/ 寫進 .gitignore。為什麼 .gitignore 的 !Tools/ 無法救回它、而 include: ["Tools/"] 可以?列出兩者在機制上的行為差異。codegraph.json 該 commit 嗎?為什麼?哪些設定(如 CODEGRAPH_DIR)不該 commit?一個同時有 git + SVN 雙 VCS、vendored theme、自訂副檔名的 legacy monorepo,如何用一份 codegraph.json 精確控制索引範圍:
# ① 先看哪些檔案「不該進圖」
git check-ignore vendor/ # vendor 被 gitignore → include 可救回
git check-ignore src/ # src 被 SVN 管所以被 gitignore → 需要 include
# ② 建立 codegraph.json(三種設定一次到位)
cat > codegraph.json << 'EOF'
{
"include": ["src/", "Tools/"],
"exclude": [
"static/metronic-theme/",
"**/vendor/**",
"**/*.min.js",
"**/*.generated.dart"
],
"extensions": {
".dota_lua": "lua",
".tpl": "php",
".hbs": "javascript",
".bridge": "objective-c"
}
}
EOF
# ③ 重新索引
codegraph index --force
# ④ 驗證「進圖的」與「沒進圖的」
codegraph files --filter "src/" | head -10 # src 進圖了
codegraph files --filter "static/" | head -5 # static/metronic 沒進圖
codegraph query metronic # 應該查不到
# ⑤ 驗證 extensions 對映
codegraph query "@interface" --kind class --limit 5 # ObjC 進圖
codegraph files --filter "*.dota_lua" | head -5 # Lua 進圖
# ⑥ 確認排除優先權:include 被 exclude 覆蓋的情況
codegraph files | grep "vendor" | wc -l # 應該是 0
關鍵觀察:三層排除的優先權是 內建跳過 → gitignore → exclude;include 只能救回被 gitignore 排除的,不能救回內建跳過的。改 extensions 對映後必須 index --force 重跑。
| 面向 | 考量 | 實務建議 |
|---|---|---|
| 效能 | 過多 include 會拖慢索引 | 只 include 真正需要分析的目錄;vendored 依賴通常不值得索引 |
| 品質 | extensions 對映值打錯不會報錯 | 只會警告並跳過。用 codegraph status 檢查語言分佈,確認新語言有進圖 |
| 安全 | codegraph.json 該不該 commit | 排除/納入/對映設定應 commit(團隊共享);CODEGRAPH_DIR 等環境變數不該 commit |
| 面向 | 本文(設定) | 相關文 | 差異說明 |
|---|---|---|---|
| 排除規則 | 三層排除優先權 + include/exclude | 運作原理 | 設定頁講規則,運作原理講「為什麼要跳過」 |
| extensions | 非標準副檔名對映 | 語言支援 | 設定頁講怎麼對映,語言支援講哪些語言被支援 |
| .gitignore | gitignore 無法丟已 commit 目錄 | 隱私與安全 | 設定頁的排除是索引範圍,安全頁的排除是資料保護 |
| 環境變數 | CODEGRAPH_DIR 等速查 | 自動同步 | 設定頁列變數,auto-sync 講 debounce 變數的行為 |
include 的限制
- [ ] 能建立 codegraph.json 並同時設定 exclude、include、extensions
- [ ] 能用 git check-ignore 確認哪些檔案被 gitignore,並決定是否需要 include
- [ ] 能區分 include 與 includeIgnored 的不同用途
- [ ] 能在改 extensions 對映後正確執行 index --force 重跑
- [ ] 能決定 codegraph.json 的哪些設定該 commit、哪些不該 commit