Configuring turbo.json
Hướng dẫn chi tiết cấu hình turbo.json trong Turborepo — tasks, caching, global options, environment variables, remote cache, boundaries và experimental observability.
turbo.json là file cấu hình trung tâm của Turborepo, đặt tại thư mục gốc của monorepo. Nó định nghĩa task graph, caching behavior, environment variables và các global options.
Hỗ trợ cả .jsonc (thêm comment).
1. Global Options
Các option áp dụng cho toàn bộ monorepo:
extends
Kế thừa cấu hình từ root turbo.json:
{
"extends": ["//"]
}"//" = reference root. Có thể extends từ nhiều package khác: ["//", "shared-config"].
globalDependencies
Glob patterns — file nào thay đổi thì all tasks miss cache:
{
"globalDependencies": ["tsconfig.json", ".env"]
}Mặc định root package.json + lockfile luôn được include.
globalEnv
Env vars ảnh hưởng đến hash của tất cả tasks:
globalPassThroughEnv
Cho phép env vars đi qua task runtime, nhưng không ảnh hưởng cache hash:
ui
Chọn terminal UI:
"tui"— interactive, xem từng log"stream"— log xuất trực tiếp (mặc định)
concurrency
Giới hạn số tasks chạy song song:
Số nguyên ≥ 1
Percentage:
"50%"— dùng 50% số cores1— chạy tuần tự
cacheDir
Thư mục lưu cache local:
cacheMaxAge
Tự động xóa cache entries cũ theo thời gian:
Hỗ trợ: 30s, 5m, 24h, 7d, 2w. Mặc định "0" (tắt).
cacheMaxSize
Giới hạn dung lượng cache local:
Hỗ trợ: 500MB, 10GB, 1TB. Chạy sau age-based eviction.
2. Tasks
dependsOn
Định nghĩa dependency giữa các tasks — ba loại:
Dependency relationship (^ prefix — chờ task build của dependencies xong trước):
Same-package relationship (chờ task khác trong cùng package):
Arbitrary task relationship (giữa các package cụ thể):
env
Khai báo env vars ảnh hưởng cache hash của task:
Hỗ trợ wildcards:
Hỗ trợ negation:
passThroughEnv
Cho phép env vars vào task runtime (strict mode) nhưng không ảnh hưởng hash:
outputs
Glob patterns chỉ định artifacts cần cache:
Bỏ qua hoặc
[]→ chỉ cache logsDùng negations:
"!.next/cache/**"
inputs
Kiểm soát file nào thay đổi mới invalidate cache:
Ghi chú quan trọng: Dùng inputs sẽ opt-out khỏi default .gitignore behavior. Dùng $TURBO_DEFAULT$ để giữ default + mở rộng:
$TURBO_ROOT$ — glob relative đến repository root:
Deferred Hashing — hash files sau khi dependencies chạy xong:
mode: "jit"— hash ngay trước khi task chạymode: "dependencyOutputs"— hash dựa trên outputs của dependency
cache
Tắt cache cho task (dev server):
outputLogs
Kiểm soát log verbosity:
full
Hiển thị tất cả logs (mặc định)
hash-only
Chỉ hiển thị hash
new-only
Chỉ log từ cache misses
errors-only
Chỉ log khi task fail
none
Ẩn tất cả
persistent / interactive / interruptible
persistent: true— task chạy lâu dài (dev server). Các task khác không được dependsOn nó.interactive: true— nhận input từstdin, cần đi vớipersistent.interruptible: true— cho phépturbo watchrestart task persistent.
with
Chạy task song song với task khác (hữu ích cho dev server):
description
Mô tả task (chỉ để documentation):
3. Remote Caching
signature: true— sign artifacts với HMAC-SHA256. Cần setTURBO_REMOTE_CACHE_SIGNATURE_KEY.preflight— OPTIONS request trước mỗi HTTP request.timeout/uploadTimeout— timeout tính bằng seconds.
4. Future Flags
Các tính năng thử nghiệm sẽ trở thành default trong tương lai:
errorsOnlyShowHash— khi dùngoutputLogs: "errors-only", vẫn hiện hash cho task thành công.longerSignatureKey— enforce key ≥ 32 bytes cho HMAC-SHA256.affectedUsingTaskInputs—--affectedhoạt động ở task-level (dựa trêninputs), không phải package-level.pruneIncludesGlobalFiles—turbo prunecopy cảglobalDependenciesfiles.filterUsingTasks—--filterhoạt động ở task-level.globalConfiguration— dồn global options vào keyglobal:
5. Boundaries
Enforce architectural rules giữa các package:
Tags được khai báo trong Package Configurations:
6. Experimental Observability
Gửi metrics đến OTLP collector (Datadog, Prometheus, etc.):
Lưu ý: Endpoint bắt buộc dùng https://. Headers và token gắn với endpoint — nếu override endpoint từ nguồn ưu tiên cao hơn, credentials không được kế thừa.
7. Env Mode
Kiểm soát env vars available cho task:
"strict"(default) — chỉ cho phép env vars khai báo trongenvvàglobalEnv."loose"— cho phép tất cả env vars của process.
8. Ví dụ Hoàn Chỉnh
Tài liệu tham khảo
Cập nhật lần cuối