設定

零設定是預設——唯一的選擇性檔是自訂副檔名對映與排除規則
codegraph.json(專案根目錄)· gitignore 優先 · 內建排除不可覆蓋

零設定預設

CodeGraph 開箱即零設定:語言支援依副檔名自動,沒什麼要逐語言接線。唯一的選擇性檔是 codegraph.json,放在專案根目錄。

開箱即跳過:

排除 / 納入

想把東西排除:加進 .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_MSwatcher debounce 窗口(100ms–60s,預設 2000)
CODEGRAPH_MCP_TOOLSMCP 工具 allowlist(預設只有 explore)
CODEGRAPH_NO_DAEMON跳過共享背景 server,每個 session 自己跑
CODEGRAPH_DIR換 .codegraph 目錄名(Windows/WSL 共存用)
CODEGRAPH_TELEMETRY=0 / DO_NOT_TRACK=1關閉匿名 telemetry
看完這頁你應該能說出:三個開箱即跳過的規則、exclude vs include 的語意(含為何不能靠 .gitignore 丟已 commit 目錄)、extensions 對映的合併/覆蓋規則、以及改對映後要 re-index。

🎓 教學解說

CodeGraph 的設定哲學是「Convention over Configuration」——預設值已經夠好用,只有特殊需求才需要設定檔。

三個「開箱即跳過」的規則背後的考量:

Worked Example:自訂專案設定

假設你有一個 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

練習:驗收清單


🔍 進階真實情境 Worked Example:雙 VCS legacy 專案的索引策略

前面的範例是乾淨 monorepo。這裡換成更棘手的 legacy:專案同時用 git 與 SVN,真正的原始碼被 .gitignore 掉(公司強制用 SVN 管 src/),同時已 commit 的 vendored theme 躺在 static/。你要讓圖譜只看「人寫的程式」。

legacy 設定實作
# ① 檢查「人寫的程式」是否真的被 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 比對,不是對絕對路徑。

大家以為分析正確但其實有誤:以為「exclude 用 glob 對整個絕對路徑比對」。實際是對 repo-root-relative 路徑比對的 gitignore 風格 pattern——寫成 /abs/path 或帶前導 slash 的寫法行為可能不如預期。另一個誤解:以為改了 extensions 對映後下次查詢就生效,其實需要 codegraph index 重跑(metadata 已寫進 DB)。

🩺 診斷式疑難排解表

症狀可能原因解決方案
改了 extensions 但沒生效沒 re-indexcodegraph 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

🧩 進階挑戰題

  1. 一個專案把 Tools/ 寫進 .gitignore。為什麼 .gitignore 的 !Tools/ 無法救回它、而 include: ["Tools/"] 可以?列出兩者在機制上的行為差異。
  2. 解釋「內建排除永不重開」的設計理由。如果硬要索引 node_modules,你能想出哪些 workaround?各有什麼代價?
  3. codegraph.json 該 commit 嗎?為什麼?哪些設定(如 CODEGRAPH_DIR)不該 commit?

🏗️ 專案級端到端 Worked Example:複雜 monorepo 的完整設定策略

一個同時有 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非標準副檔名對映語言支援設定頁講怎麼對映,語言支援講哪些語言被支援
.gitignoregitignore 無法丟已 commit 目錄隱私與安全設定頁的排除是索引範圍,安全頁的排除是資料保護
環境變數CODEGRAPH_DIR 等速查自動同步設定頁列變數,auto-sync 講 debounce 變數的行為

✅ 進階互動式檢核清單

- [ ] 能解釋三層排除的優先權(內建跳過 → gitignore → exclude),並說出 include 的限制 - [ ] 能建立 codegraph.json 並同時設定 exclude、include、extensions - [ ] 能用 git check-ignore 確認哪些檔案被 gitignore,並決定是否需要 include - [ ] 能區分 include 與 includeIgnored 的不同用途 - [ ] 能在改 extensions 對映後正確執行 index --force 重跑 - [ ] 能決定 codegraph.json 的哪些設定該 commit、哪些不該 commit