5:55

The one source graph behind millisecond builds in Elements

0

In this video we explain the source graph, one of the most important data structures in the Elements build server. We follow one import from a path to a file, show why that path gives four files and four graphs, and then follow a build through its phases.

Two Ideas Behind the Source Graph

We designed the source graph around two ideas.

One, your editor, your terminal, your agents and every phase of the build read the same graph, in memory.

Two, the build never redoes work. If it is impossible that a file changed, the build does not read it, parse it, resolve it or type check it.

So a build takes milliseconds. And the build can do program analysis and emit transforms that are not possible when every tool has its own graph.

Resolving an Import With No System Calls

A file in the app imports LiveTable:

app/shared/services/people.ts
import { LiveTable } from "@elements/app";

That import is a dependency, and in the graph it is an edge. An edge needs a file at the other end, so the build has to resolve the path. It walks up to node_modules, finds @elements, then app, reads the exports in package.json, and lands on index.js.

Normally every one of those steps is a system call, for every import in your program. The Elements build server scans the disk once when it starts and keeps the file tree in memory. The file watcher updates the tree when a file changes. So the resolver walks the tree in memory and makes no system calls.

One Import Path, Four Files, Four Graphs

That one path does not resolve to one file. Here is the exports block the resolver read:

node_modules/@elements/app/package.json
"exports": { ".": { "browser": { "types": "./browser/index.d.ts", "default": "./browser/index.js" }, "default": { "types": "./server/index.d.ts", "default": "./server/index.js" } } }

For the server, the path resolves to server/index.js. For the browser, the package redirects it to browser/index.js. Those are runtime files, the JavaScript that runs. The type checker wants different files, the .d.ts files, and those are different for the server and the browser too.

So one import path gives four files, and that gives four graphs: server runtime, server types, browser runtime and browser types. In Elements, a source file is read once and parsed once, and it has four vertices, one in each graph.

The build needs those graphs together. When it emits, it uses the types graph to transform the code. When it analyzes, it adds runtime libraries to the runtime graph. A type checker and a bundler that are separate tools cannot do that, because neither one can read the other's graph.

The Phases of a Build

A build runs in phases. It syncs the file tree, then runs config, install, crawl, analyze and emit. Then it migrates the test database and runs the tests. Only if the tests pass does it migrate the app database and release.

The graphs are built in the crawl. The build starts with your project files. It reads each one, parses it, resolves its imports, and follows those edges into the packages. So a package file is read only if your program can reach it. The elements.dev site has 7,491 files in node_modules, and the build reads 2,058 of them.

Emit works the same way. For the server, the build writes your project files and the package files they reach. For the browser, it writes a file only if a page can reach it.

Analyze: Type Checking and Program Analysis

Analyze runs on the types graphs. The build type checks the server and the browser separately, each one through its own graph.

Then program analysis follows your function calls from file to file. It adds async and await for you, and it knows when code in the browser can reach a sql() call.

Here is a function that calls sql():

app/shared/services/users.ts
import { sql } from "@elements/app"; export interface User { id: number; name: string; } export function listUsers(): User[] { return sql<User>(` select 1 as id, 'Ada' as name `).all(); }

We call listUsers() from a button in a template and save. The build is in the error state:

Terminal
Security error: cannot call server code from the browser without going through an rpc function.

The type checker alone reports nothing for this code. We add the @rpc build tag above the function:

app/shared/services/users.ts
/** @rpc */ export function listUsers(): User[] {

The build passes. In the file the build wrote for the browser, the SQL is gone, and the function body is a call to the server:

return app.callAsync("app/shared/services/users:listUsers", ...arguments);

Every Build After the First

After the first build, the graph stays in memory. When you save a file, the file watcher tells the build which file changed, so the build does not scan the disk.

  • If the file's modified time is the same, the build does not read it.
  • If the text hash is the same, the build does not parse it.
  • If the imports are the same, the build does not resolve anything.

We change the text inside the query, from 'Ada' to 'Grace', and save. One file is type checked for each target, out of about two hundred, in 3 milliseconds. The files that import users.ts are not checked, because its exported types did not change.

Then we rename a field in the User interface, from name to fullName, and that does change its exported types. The server rechecks three files, the browser rechecks two, and the template has the error, because it still reads name. That build takes 7 milliseconds.

Every phase works that way. The build emits a file only if its output might have changed. It runs only the tests that depend on the change. And a deploy uploads only the files whose hash changed. A build where nothing changed takes less than a millisecond.

The Editor's Layer of the Graph

Your editor connects to the same build server. But the text in your editor is not on disk until you save. So the graph has layers.

Layer 0 is the disk. Your editor gets its own layer, and it is copy on open: a file you have open gets a copy with your unsaved text, and every other file comes from the disk layer. So the error in your editor is the build's error, for both targets, before you save.

Without a Shared Graph

Think about what you run without a shared graph: a package installer, a type checker for the server and another one for the browser, a bundler, a test runner, and your editor's TypeScript server. Each one reads the same files, parses them again, and keeps its own graph in memory.

  • By Microsoft's own numbers, one type check of the VS Code codebase takes over four gigabytes.
  • A bundler does not type check. One developer tracked a bug down to a typo in an import that the type checker would have caught, because his bundler does not run the type checker.
  • The graphs go stale separately, so a developer ends up deleting one tool's cache and restarting another tool's server.
  • An agent runs these tools one at a time, and waits. An issue in Claude Code's own tracker says the full compiler takes ten to sixty seconds each time it runs.
  • CI runs all of them again on every push, starting from nothing. One team wrote that their CI took seventeen minutes a run, fifty to a hundred times a day.

One Build Server, One Source Graph

In Elements those tools are phases of one build, in one build server, on one source graph. That meant writing our own resolver and our own file watcher, and making Microsoft's type checker read our graph.

We did it so every file is read once, no work is done twice, and your editor, your terminal and your agents all get the same build result. When an agent asks if the build has errors, it is one command:

Terminal
elements build
Terminal
State: Ok Elapsed: 0.000020s

Add a comment