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`: ```ts 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: ```json 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()`: ```ts 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: ```text 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: ```ts 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: ```js 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: ```bash Terminal elements build ``` ```text Terminal State: Ok Elapsed: 0.000020s ```

One of the most important data structures in the Elements build server is the source graph. I designed it around two ideas. One, your editor, your terminal, your agents and every phase of the build read the same graph, in memory. Two, we never redo work. If it's impossible that a file changed, we don't read it, parse it, resolve it or type check it. So a build takes milliseconds. And we can do program analysis and emit transforms that aren't possible when every tool has its own graph. It starts with an import. This file imports LiveTable from @elements/app. That's a dependency, and in the graph it's an edge. But an edge needs a file at the other end, so we'll have to resolve that path.

We walk up to node modules. We find @elements, and then app. We read the exports in its package.json, and we land on index.js. Normally every one of those steps is a system call, for every import in your program. We don't do that. The build server scans the disk once when it starts, and keeps the file tree in memory. The file watcher updates it when a file changes. So the resolver makes no system calls. But that one path doesn't resolve to one file. For the server, it's 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 us four files, and that gives us four graphs. Server runtime, server types, browser runtime, browser types. In Elements, a source file is read once and parsed once, and it has four vertices, one in each graph. And we need those graphs together. When we emit, we use the types graph to transform the code. When we analyze, we add runtime libraries to the runtime graph. You can't do that when the type checker and the bundler are separate tools, because neither one can read the other's graph. A build runs in phases. We sync the file tree, then config, install, crawl, analyze and emit. Then we migrate the test database and run the tests.

Only if the tests pass do we migrate the app database and release. We build the graphs in the crawl. We start with your project files. We read each one, parse it, resolve its imports, and follow those edges into the packages. So a package file is only read if your program can reach it. The elements.dev site has seven and a half thousand files in node modules. The build reads about two thousand of them. Emit works the same way. For the server we write your project files and the package files they can reach. For the browser, we only write a file if a page can actually reach it. Analyze runs on the types graphs. We type check the server and the browser separately, each one through its own graph.

Then program analysis follows your function calls from file to file. So it can add async and await automatically for you. It knows when code in the browser can reach a SQL call. Here's a function that calls SQL. I call it from a button in this template, and save. And the build has a security error. You can't call server code from the browser without going through an RPC function. The type checker alone reports nothing for this code. So I add the RPC build tag, and the build passes. And in this file we wrote for the browser, the SQL is gone. The function body is a call to the server. That was the first build. Here's the part I care about most.

After that, we never redo work. The graph stays in memory. When you save a file, the file watcher tells us which file, so we don't scan the disk. If the modified time is the same, we don't read it. If the text hash is the same, we don't parse it. If the imports are the same, we don't resolve anything. Watch. I change the text inside this query, and save. One file was type checked for each target, out of about two hundred. Three milliseconds. The files that import it weren't checked, because its exported types didn't change. Now I rename a field in the interface, and that does change its exported types. So the server rechecks three files, the browser rechecks two, and the template has the error.

Seven milliseconds. Every phase works that way. We only emit a file if its output might have changed. We only run the tests that depend on the change. And a deploy only uploads the files whose hash changed. And a build where nothing changed takes less than a millisecond. Your editor connects to the same build server. But the text in your editor isn't on disk until you save. So the graph has layers. Layer zero is the disk. Your editor gets its own layer, and it's copy on open. A file you have open gets a copy with your unsaved text. 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.

Now think about what you run without this. 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. The bundler doesn't type check. This developer finally tracked a bug down to a typo in an import. The type checker would have caught it, but his bundler doesn't run the type checker. And the graphs go stale separately. This one names two stale caches in one line. Deleting the bundler's cache is the new restart the TS server.

Your agent runs these tools one at a time, and waits. This is from Claude Code's own issue tracker. To see type errors, it runs the full compiler, ten to sixty seconds each time. And then 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. In Elements those tools are phases of one build, in one build server, on one source graph. That was my decision. It meant writing our own resolver and our own file watcher, and making Microsoft's type checker read our graph. I did it so every file is read once, no work is done twice, and your editor, your terminal, your agents, they all get the same build result.

And when an agent asks if the build has errors, it's one command, and the answer comes back in microseconds.

Add a comment