Closed wtlin1228 closed 1 week ago
Package Graph in Turbo
packages:
- "apps/*"
- "packages/*"
Graph {
Ty: "Directed",
node_count: 5,
edge_count: 1,
edges: (1, 0),
node weights: {
0: Root,
1: Workspace(
Root,
),
2: Workspace(
Other(
"@cytm/typescript-config",
),
),
3: Workspace(
Other(
"hawk",
),
),
4: Workspace(
Other(
"kirby",
),
),
},
}
Graph {
Ty: "Directed",
node_count: 5,
edge_count: 5,
edges: (1, 0), (4, 2), (2, 0), (1, 0), (3, 2),
node weights: {
0: Root,
1: Workspace(
Root,
),
2: Workspace(
Other(
"@cytm/typescript-config",
),
),
3: Workspace(
Other(
"hawk",
),
),
4: Workspace(
Other(
"kirby",
),
),
},
}
flowchart TD
root[Workspace Root] --> Root
kirby --> tsconfig[@cytm/typescript-config]
hawk --> tsconfig
tsconfig --> Root
Task Graph in Turbo:
When we run turbo run build
Given:
kirby
, hawk
and @cytm/typescript-config
.build
Add tasks to traversal_queue
using the following rules:
build
task defined in each package's turbo.json
turbo.json
and try to find the <package_name>:build
taskturbo.json
has defined the build
task, then <package_name>:build
So far, our traversal_queue
now has kirby:build
, hawk:build
and @cytm/typescript-config:build
.
traverse the traversal_queue
, find the package dependency using Package Graph constructed earlier.
flowchart TD
root[Workspace Root] --> Root
kirby --> tsconfig[@cytm/typescript-config]
hawk --> tsconfig
tsconfig --> Root
The task graph:
Graph {
Ty: "Directed",
node_count: 4,
edge_count: 3,
edges: (1, 2), (3, 2), (2, 0),
node weights: {
0: Root,
1: Task(
TaskId {
package: "kirby",
task: "build",
},
),
2: Task(
TaskId {
package: "@cytm/typescript-config",
task: "build",
},
),
3: Task(
TaskId {
package: "hawk",
task: "build",
},
),
},
}
flowchart TD
kirby[kirby:build] --> tsconfig[@cytm/typescript-config:build]
hawk[hawk:build] --> tsconfig
tsconfig --> Root
Scheduling in Turbo:
Use Waker
and Visitor
for scheduling tasks based on the topological order.
Survey:
$ turbo run build
cli::run()
command::run(base: CommandBase, telemetry: CommandEventBuilder)
RunBuilder::new(base: CommandBase)
RunBuilder::build(signal_handler: &SignalHandler, telemetry: CommandEventBuilder)
Run::run(ui_sender: Option<UISender>, is_watch: bool)
PackageInputsHashes::calculate_file_hashes(...)
git hash-object <file path>
to hash each filetwox_hash::XxHash64
to get the package hashengine.execute(...)
Engine::execute(options: ExecutionOptions, visitor: mpsc::Sender<Message<VisitorData, VisitorResult>>
Walker
to walk the graph in topological order and send each task to Visitor::visit
ExecContextFactory
exec_context
using factory
exec_context.execute()
Cache in Turbo:
Use git's hash and xxhash64
Always want to lean more about monorepo tools like Nx and Turborepo. Let's do it!