Publish and use a dialect package
A published dialect is a dialect that lives in an npm package. Publish one when several workspaces need the same dialect and copying its folder into each of them would let the copies drift apart. When only one workspace needs it, a local dialect is simpler.
A workspace that uses the package declares it in the Manifest (project-builder.json) with a
version range. builder sync fetches a version that satisfies the range, runs the package’s
conformance fixture, and pins the version in the Lock (project-builder-lock.json). A schematic
then imports the dialect by its package name.
This page uses an ini dialect with one op, setKey, published as @e2e-test/dialect-ini.
Every file and message below comes from a run of these steps.
Requirements
Section titled “Requirements”You need the builder CLI v0.19.7 or later and @pbuilder/sdk 0.21.0 or later. The steps on this
page were run with exactly those two versions, against a local npm-compatible registry. The
workspace declares the SDK in its package.json, as builder init --package-manager npm writes
it.
The package
Section titled “The package”A dialect package has four parts: a package.json, a base file that calls defineDialect, the
AST code, and a conformance fixture.
{ "name": "@e2e-test/dialect-ini", "version": "1.0.0", "type": "module", "exports": { ".": "./index.js", "./conformance": "./conformance.js" }, "files": ["index.js", "ast.js", "conformance.js"], "peerDependencies": { "@pbuilder/sdk": "^0.21.0" }}exportslists the base file as.and the fixture as./conformance. The sync fails when the package has no./conformanceexport.- The package names
@pbuilder/sdkonly as a peer dependency. A package that lists it underdependenciesfails the sync, because it would bring its own copy of the SDK. - This package has no other dependencies.
index.js calls defineDialect from @pbuilder/sdk/dialect. The name must be the package
name. The fixture reads find and astLibrary from this module, so it exports both:
import { defineDialect } from "@pbuilder/sdk/dialect";import * as ast from "./ast.js";
export * as astLibrary from "./ast.js";
export const ini = defineDialect({ name: "@e2e-test/dialect-ini", extensions: [".ini"], ast: { parse: ast.parse, print: ast.print }, ops: { setKey: ast.setKey },});
export const find = ini.find;An op cannot take the name of a handle verb, path included; see
Op names.
ast.js holds the parser, the printer and the op. print(parse(source)) must give back the source
byte for byte:
export function parse(source) { return { lines: source.split(/(?<=\n)/) };}
export function print(ast) { return ast.lines.join("");}
export function setKey(ast, section, key, value) { let current = null; let lastInSection = -1; for (let i = 0; i < ast.lines.length; i++) { const text = ast.lines[i].replace(/\r?\n$/, ""); const eol = ast.lines[i].slice(text.length); const header = /^\[(.*)\]\s*$/.exec(text); if (header) { current = header[1]; if (current === section) lastInSection = i; continue; } if (current !== section) continue; lastInSection = i; if (new RegExp("^" + key + "\\s*=").test(text)) { ast.lines[i] = key + "=" + value + eol; return; } } const line = key + "=" + value + "\n"; if (lastInSection >= 0) ast.lines.splice(lastInSection + 1, 0, line); else ast.lines.push("[" + section + "]\n", line);}conformance.js default-exports the fixture. It holds the dialect, sample sources, the whole base
module, the AST library the module must expose, and a library exercise:
import * as module from "./index.js";import * as expectedAstLibrary from "./ast.js";
export default { dialect: module.ini, samples: ["[server]\nport=80\n"], module, expectedAstLibrary, libraryExercise: { path: "example.ini", seed: "[server]\nport=80\n", expect: "[server]\nport=8080\n", modify: (ast, library) => library.setKey(ast, "server", "port", "8080"), },};Publish the package with npm publish. The sync loads the dialect from @pbuilder/sdk/dialect
without a node_modules folder in the package.
Declare it in the Manifest
Section titled “Declare it in the Manifest”In the workspace that uses the dialect, add a dialects entry. The key is the package name, and
the value gives the registry and a version range:
"collections": { "default": { "set-ini": { "path": "./schematics/set-ini" } } },"dialects": { "@e2e-test/dialect-ini": { "registry": "npm", "range": "^1.0.0" } }builder syncThe first sync exits with code 0 and prints wrote project-builder-lock.json. It fetches the
package, runs the fixture once, and pins the version in the Lock:
"dialects": { "@e2e-test/dialect-ini": { "version": "1.0.0", "integrity": "sha512-…", "source": { "ecosystem": "npm", "registry": "…" }, "declaredBy": ["."] } }The Lock also pins the SDK version. The sync writes the package to the builder cache under
npm/@e2e-test/dialect-ini/1.0.0/, next to a digests file with a sha512 per file and an
integrity file. It installs nothing into the workspace: there is no node_modules/@e2e-test.
A second builder sync with nothing changed prints wrote project-builder-lock.json again, does
not run the fixture, and leaves the Lock byte for byte the same.
builder sync --frozen installs what the Lock pins. With nothing changed
it exits with code 0, prints installed what project-builder-lock.json pins, does not run the
fixture, and leaves the Lock as it was.
Use it from a schematic
Section titled “Use it from a schematic”The factory imports the dialect by its package name:
import type { Input } from "./schema.generated.ts";import { ini } from "@e2e-test/dialect-ini";
export default async (input: Input) => { await ini.find(input.file).setKey(input.section, input.key, input.value);};The schema and the codegen step are the same as in the
local dialect guide: give every property a
label and run pbuilder-codegen on the schematic folder.
The schematic edits a file that must already exist:
printf '[server]\nport=80\n' > app.iniPreview the run first. --dry-run is a global option, so it goes right after execute:
builder execute --dry-run default:set-ini --file=app.ini --section=server --key=port --value=8080The preview exits with code 0, prints ✓ done — 1 modify, plans
[{"class":"modify","path":"app.ini"}], warns warn_dry_run, and leaves app.ini as it was.
Then run it for real:
builder execute default:set-ini --file=app.ini --section=server --key=port --value=8080It prints ✓ done — 1 modify, exits with code 0, and app.ini becomes [server]\nport=8080\n.
Versions and ranges
Section titled “Versions and ranges”builder sync takes the lowest version that satisfies the range. Two things follow.
A new release inside the range is not picked up. With the Lock pinning 1.0.0 and the range
^1.0.0, publishing 1.1.0 changes nothing: a plain builder sync exits with code 0, keeps 1.0.0,
does not run the fixture, and leaves the Lock byte for byte the same. To move to 1.1.0, change the
range to ^1.1.0.
A range change needs a plain sync. After you change the range, builder sync --frozen exits
with code 3, and the Lock stays as it was:
✗ [CLI] the Manifest and the Lock disagree: project-builder-lock.json was resolved from other Manifest inputs sync_lock_drift Fix: run 'builder sync' to resolve the Manifest and rewrite project-builder-lock.jsonA plain builder sync then pins the new version and runs the fixture once for it. The old
version’s folder stays in the builder cache.
A version that fails the fixture
Section titled “A version that fails the fixture”Say 1.2.0 of the package fails the fixture: its print joins lines with "\n", so CRLF endings do
not round-trip. With the Lock pinning 1.1.0 and the range ^1.1.0, a sync never looks at 1.2.0.
With the range ^1.2.0, the sync exits with code 3 and the Lock stays unchanged:
✗ [SDK] the dialect "@e2e-test/dialect-ini" at version 1.2.0 fails the conformance fixture: case "round_trip": A-11: print(parse(source)) must give back the source byte for byte sync_dialect_fails_fixture ask the dialect's author for a version that passes the conformance fixture, then narrow its rangeThe refused version may stay unpacked in the builder cache. Running the sync again runs the fixture again and fails the same way.
Publishing a good 1.2.1 does not fix this on its own. With the range still ^1.2.0, the sync
still picks 1.2.0, the lowest version in the range, and fails. Narrow the range to exclude the bad
release, here ^1.2.1. The sync then exits with code 0, pins 1.2.1 and runs the fixture once for
it.
The cache check
Section titled “The cache check”Before each run, the engine checks that the package’s copy in the builder cache still matches what
was unpacked. When the copy changed, builder execute stops before the factory runs, with or
without --dry-run. It exits with code 3, leaves your files unchanged, and prints one JSON line
that names the dialect:
{"error":{"code":"execute_dialect_cache_changed","op":"native.execute","message":"the Frame stopped before its factory ran: dialect \"@e2e-test/dialect-ini\": its cache copy changed since it was unpacked; run `builder sync`","suggestions":["run 'builder sync' to fetch the dialect's copy again"],"reason":"workspace_inconsistent","layer":"engine","detail":"","log_written":false},"warnings":[]}Run builder sync or builder sync --frozen. Either one restores the copy to the published bytes,
removes files that were added to it, does not run the fixture, and leaves the Lock byte for byte
the same. builder execute then works again.
This check makes sure the dialect code that runs is the code the Lock pins. It is a consistency check, not a security boundary.
Errors you may see
Section titled “Errors you may see”Each of these stops the sync or the run before it writes anything. A failed sync writes no Lock.
| Error code | Exit | Cause | Fix |
|---|---|---|---|
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 |
Ask the dialect’s author for a version that passes the conformance fixture, then narrow its range |
sync_dialect_fails_fixture |
3 | The name in defineDialect is not the package name: case "name_mismatch": A-16: the dialect's name is not the name of its package |
Set name to the package name |
sync_dialect_fails_fixture |
3 | The package has no ./conformance export: case "fixture_missing": CA-13: the package exports no ./conformance fixture |
Export the fixture as ./conformance |
sync_dialect_own_sdk |
3 | The package brings its own copy of the SDK: the dialect "@e2e-test/ini-ownsdk" at version 1.0.0 fails the sync as bringing its own copy of the SDK |
Ask the dialect’s author to name @pbuilder/sdk only as a peer dependency |
sync_dialect_wildcard_export |
3 | The package exports a wildcard subpath: …exports the wildcard subpath "./*"; a Frame can only reach subpaths it can list |
Ask the dialect’s author to list every exports subpath explicitly |
sync_resolution_conflict |
3 | No SDK version satisfies the workspace and the dialect: the workspace requires the SDK in the range "^0.21.0"; "@e2e-test/ini-sdkmajor" requires the SDK in the range "^1.0.0" |
Align the ranges the failure names, in project-builder.json or by choosing other library versions |
sync_fixture_could_not_run |
5 | The fixture could not run: the conformance fixture could not run on the dialect "@e2e-test/ini-crash" at version 1.0.0 |
Re-run builder sync; if it keeps happening, check that the SDK’s Bun can start |
sync_lock_drift |
3 | builder sync --frozen after a change to the Manifest, such as a new range |
Run builder sync to resolve the Manifest and rewrite the Lock |
execute_dialect_cache_changed |
3 | The cache copy of a dialect changed since it was unpacked | Run builder sync or builder sync --frozen to fetch the dialect’s copy again |