Skip to content

Write a local dialect

A local dialect is dialect code that lives in your workspace. It teaches Project Builder a file format that the SDK has no dialect for. You do not publish it as a package and you do not install it: you write it in a folder, declare it in the Manifest (project-builder.json), and builder sync checks it and records it in the Lock (project-builder-lock.json). A schematic in the same workspace then imports it like any other dialect.

Write one when your schematics need structural edits to a format that only your project uses, and the built-in dialects do not cover it. To share a dialect across workspaces, see Publish and use a dialect package.

This page builds an ini dialect with one op, setKey, and a set-ini schematic that uses it. Every file and message below comes from a run of these steps.

You need the builder CLI v0.19.6 or later and @pbuilder/sdk 0.20.0 or later. The steps on this page were run with exactly those two versions. Checking the fixture by hand also needs bun on your PATH.

The SDK can live inside the workspace or outside it.

Inside the workspace, builder init --package-manager npm writes a package.json that declares "@pbuilder/sdk": "^0.20.0" and installs it:

Terminal window
mkdir ws && cd ws
builder init --package-manager npm
builder new schematic set-ini --title "Set an INI key" --job "set a key in an INI file"

Outside the workspace, the workspace has no package.json and no node_modules. Set BUILDER_SDK_ROOT to the path of an installed @pbuilder/sdk for every command, and add the SDK version to the Manifest:

project-builder.json
"sdk": { "version": "0.20.0" }

Without sdk.version, builder sync exits with code 3:

✗ [CLI] nothing requires the SDK: declare @pbuilder/sdk in the workspace's package.json, or set sdk.version in project-builder.json

The error code is sync_sdk_not_required. With the SDK outside, every builder execute also warns warn_sdk_root_ambient (“sdk root came from BUILDER_SDK_ROOT”), and the first real run creates the link node_modules/@pbuilder/sdk pointing at BUILDER_SDK_ROOT.

The dialect gets its own folder. The schematic stays where builder new schematic put it:

ws/
├── project-builder.json
├── dialects/
│ └── ini/
│ ├── ast.ts
│ ├── index.ts
│ └── conformance.ts
└── schematics/
└── set-ini/
├── factory.ts
└── schema.json

The folder can sit at any depth inside the workspace, for example tools/dialects/ini/, as long as the conformance file sits beside the base file. Give the dialect a folder of its own: the Lock hashes every file in the base file’s folder. With the base file in a shared folder such as src/, editing an unrelated file there makes builder sync --frozen and builder execute fail until the next builder sync. A conformance file in a parent folder of the base file is refused with exit code 2.

ast.ts holds the parser and the printer. This one splits the source into lines and keeps each line ending next to its line, so print(parse(source)) gives back the source byte for byte, CRLF endings included:

dialects/ini/ast.ts
export type IniAst = { lines: string[]; eols: string[] };
export function parse(source: string): IniAst {
const parts = source.split(/(\r\n|\n)/);
const lines: string[] = [];
const eols: string[] = [];
for (let i = 0; i < parts.length; i += 2) {
lines.push(parts[i]!);
eols.push(parts[i + 1] ?? "");
}
return { lines, eols };
}
export function print(ast: IniAst): string {
return ast.lines.map((line, i) => line + ast.eols[i]).join("");
}
export const library = { parse, print };

index.ts is the dialect’s base file, the one the Manifest points at. It calls defineDialect from @pbuilder/sdk/dialect with the dialect’s name, the file extensions it opens, the AST pair, and its ops. It exports the result as ini, plus find and astLibrary. The name is ini, the same as the key the Manifest declares the dialect under.

dialects/ini/index.ts
import { defineDialect } from "@pbuilder/sdk/dialect";
import { parse, print, library, type IniAst } from "./ast.ts";
const header = (line: string) => /^\s*\[([^\]]+)\]\s*$/.exec(line)?.[1];
function setKey(ast: IniAst, section: string, key: string, value: string): void {
const start = ast.lines.findIndex((line) => header(line) === section);
const eol = ast.eols.find((e) => e !== "") ?? "\n";
if (start === -1) {
if (ast.lines.length === 1 && ast.lines[0] === "") ast.lines.pop(), ast.eols.pop();
else if (ast.eols.at(-1) === "") ast.eols[ast.eols.length - 1] = eol;
ast.lines.push(`[${section}]`, `${key}=${value}`, "");
ast.eols.push(eol, eol, "");
return;
}
let end = ast.lines.length;
for (let i = start + 1; i < ast.lines.length; i++) {
if (header(ast.lines[i]!) !== undefined) { end = i; break; }
if (ast.lines[i]!.split("=")[0]!.trim() === key) { ast.lines[i] = `${key}=${value}`; return; }
}
let at = end;
while (at > start + 1 && ast.lines[at - 1]!.trim() === "") at--;
ast.lines.splice(at, 0, `${key}=${value}`);
ast.eols.splice(at, 0, eol);
}
export const ini = defineDialect({
name: "ini",
extensions: [".ini"],
ast: { parse, print },
ops: { setKey },
});
export const find = ini.find;
export const astLibrary = library;

An op cannot take the name of a handle verb: then, path, read, raw, modify, inspect, replaceContent, rename, move, copy or remove. defineDialect throws when one of its ops has such a name, and so does withOps from @pbuilder/sdk/dialect when an op pack it composes has one. The dialect is never built. For an op named path, the error reads:

op-pack composition failed: op "path" collides with a reserved handle verb

Rename the op. path became a reserved name in @pbuilder/sdk 0.25.0, when every handle gained a path field, so a dialect with an op named path that worked on 0.24.0 throws from 0.25.0 on.

conformance.ts is the conformance fixture. builder sync runs it before it records the dialect in the Lock. It default-exports a DialectFixture from @pbuilder/sdk/conformance with these fields:

Field What it holds
dialect The dialect that defineDialect returned
samples Source texts in the format, here with LF and CRLF line endings
module The whole base module, imported from ./index.ts
expectedAstLibrary The AST library, here library from ./ast.ts
libraryExercise A file path, a seed text, a modify function that edits its AST, and the text to expect after the edit
dialects/ini/conformance.ts
import type { DialectFixture } from "@pbuilder/sdk/conformance";
import * as module from "./index.ts";
import { library } from "./ast.ts";
import type { IniAst } from "./ast.ts";
const fixture: DialectFixture<typeof library> = {
dialect: module.ini,
samples: ["[server]\nport=80\n", "; comment\r\n[a]\r\nb = 1\r\n", "no-trailing-newline=1"],
module,
expectedAstLibrary: library,
libraryExercise: {
path: "example.ini",
seed: "[server]\nport=80\n",
expect: "[server]\nport=8080\n",
modify: (ast) => { (ast as IniAst).lines[1] = "port=8080"; },
},
};
export default fixture;

The samples include CRLF line endings and a file with no trailing newline. A printer that drops the CRLF endings fails the fixture’s round_trip case, and the sync fails (see Errors you may see).

Add a dialects entry to project-builder.json. The key is the dialect’s name, path is the base file, and conformance is the fixture:

project-builder.json
"collections": { "default": { "set-ini": { "path": "./schematics/set-ini" } } },
"dialects": { "ini": { "path": "./dialects/ini/index.ts", "conformance": "./dialects/ini/conformance.ts" } }

The conformance file must sit in the base file’s folder, and the base file must sit in a folder inside the workspace, below its root.

The factory imports the dialect from its base file and calls its op:

schematics/set-ini/factory.ts
import type { Input } from "./schema.generated.ts";
import { ini } from "../../dialects/ini/index.ts";
export default async (input: Input) => {
await ini.find(input.file).setKey(input.section, input.key, input.value);
};

The schema declares four required string inputs. Give every property a label: pbuilder-codegen refuses a property without one and exits with code 1.

schematics/set-ini/schema.json
{
"properties": {
"file": { "type": "string", "label": "File" },
"section": { "type": "string", "label": "Section" },
"key": { "type": "string", "label": "Key" },
"value": { "type": "string", "label": "Value" }
},
"required": ["file", "section", "key", "value"],
"description": "Set a key in an INI file through the local ini dialect"
}

Generate schema.generated.ts from the schema:

Terminal window
node node_modules/@pbuilder/sdk/dist/bin/pbuilder-codegen.js schematics/set-ini

You can run the fixture before a sync, with the SDK’s conformance runner:

Terminal window
bun node_modules/@pbuilder/sdk/dist/bin/pbuilder-conformance.js --local ini --conformance dialects/ini/conformance.ts --path dialects/ini/index.ts

A passing dialect prints:

{"passed":true,"dialect":"ini","version":"","failures":[]}
Terminal window
builder sync

The sync runs the fixture, exits with code 0 and prints wrote project-builder-lock.json. The Lock records the dialect under localDialects, with a hash of its folder and a hash of each file in it:

project-builder-lock.json
"localDialects": { "ini": { "path": "./dialects/ini/index.ts", "conformance": "./dialects/ini/conformance.ts", "hash": "sha512-…", "files": { "ast.ts": "sha512-…", "conformance.ts": "sha512-…", "index.ts": "sha512-…" }, "declaredBy": ["."] } }

The schematic edits a file that must already exist, so create it first:

Terminal window
printf '[server]\nport=80\n' > app.ini

Without it, the run fails with exit code 5 and the note dialect operation failed: "app.ini" does not exist — create it first in this run.

Preview the run first. --dry-run is a global option, so it goes before the schematic name:

Terminal window
builder execute --dry-run default:set-ini --file=app.ini --section=server --key=port --value=8080

The preview exits with code 0, plans [{"class":"modify","path":"app.ini"}], and leaves app.ini as it was. Placed after the schematic name, the option fails with exit code 2:

flag --dry-run is a reserved global option and cannot be used as a schematic flag

Then run it for real:

Terminal window
builder execute default:set-ini --file=app.ini --section=server --key=port --value=8080

It prints ✓ done — 1 modify, exits with code 0, and edits app.ini.

The Lock pins the dialect’s files. After you edit them, the CLI notices the difference.

A plain builder sync runs the fixture again only when something it pins changed: the dialect’s folder or its declaration in the Manifest. With nothing changed, the fixture does not run and the Lock stays byte for byte the same, although the CLI still prints wrote project-builder-lock.json. A .DS_Store or Thumbs.db file in the folder does not count as a change. When you point conformance at another file in the same folder, the sync runs the fixture on the new file, and in the Lock only manifestInputHash and conformance change.

builder sync --frozen fails when the dialect changed. After an edit to index.ts and a new notes.txt, it exits with code 3 and the error code sync_local_dialect_changed, and it names each file that was added or changed:

the local dialect "ini" changed since the Lock: the hash of its folder "dialects/ini" differs from the one the Lock records, naming the file "dialects/ini/notes.txt" as added, the file "dialects/ini/index.ts" as changed

A deleted file shows up as the file "dialects/ini/ast.ts" as removed. The fix line tells you to run builder sync. The fixture does not run, and the Lock is left as it was.

builder execute stops before the factory runs, with or without --dry-run. It exits with code 3 and the error code execute_dialect_changed, and leaves app.ini unchanged:

the Frame stopped before its factory ran: dialect "ini": its folder changed since the Lock was written; run `builder sync`

Its fix line reads run 'builder sync' to bring the dialect in line with project-builder-lock.json.

Run a plain builder sync. It runs the fixture again and updates hash and files in the Lock. After that, builder execute works again and builder sync --frozen prints installed what project-builder-lock.json pins.

Each of these stops the sync or the run before it writes anything.

Error code Exit Cause Fix
sync_local_dialect_sdk_name 3 The key is the name of an SDK dialect, such as json: the local dialect "json" has the name of one of the SDK's dialects, which are never declared Pick a name that no SDK dialect uses
invalid_input 2 The conformance file sits outside the base file’s folder: the local dialect "ini" has the conformance file "./tests/ini-conformance.ts" outside the folder of its base file. builder execute stops with the same error move the conformance file into dialects/ini/, the folder of its base file, then re-run 'builder sync'
invalid_input 2 The base file sits at the workspace root: the local dialect "ini" has its base file "./ini.ts" at the workspace root move the base file into a folder of its own, such as dialects/ini/, then re-run 'builder sync'
invalid_input 2 The base file is outside the workspace: the local dialect "ini" has the path "../shared/ini/index.ts" outside the workspace point "path" at a file inside the workspace, then re-run 'builder sync'
sync_local_dialect_unhashable 3 The folder holds a symlink: the folder "dialects/ini" of the local dialect "ini" holds the symlink "dialects/ini/shared.ts" Replace the symlink with a regular file
sync_dialect_fails_fixture 3 The dialect fails a fixture case, for example case "round_trip": A-11: print(parse(source)) must give back the source byte for byte. No Lock is written, and an existing Lock stays unchanged Fix the dialect so the case passes
sync_dialect_fails_fixture 3 The declaration has no conformance key: …its declaration names no conformance file Add conformance to the declaration
sync_dialect_fails_fixture 3 The conformance file does not exist: …its conformance file "./dialects/ini/missing.ts" does not exist Point conformance at the file that exports the fixture

The fix lines of the three invalid_input rows are the ones builder v0.19.10 prints, in builder sync and builder execute alike.