Skip to content

Use an external SDK root

By default builder execute runs schematics against the @pbuilder/sdk installed in the workspace’s node_modules. An external SDK root points the run at an @pbuilder/sdk package directory somewhere else, typically a global install, so you can run schematics without adding the SDK to the project’s package.json.

Three sources can name that directory. They share the same validation, the same link and the same diagnostics.

Source Scope Set it with
--sdk-root=<dir> One run A flag before <collection>:<schematic>
BUILDER_SDK_ROOT Every run in that environment An environment variable
sdk.root Every run in that project project-builder.json

The value is the package directory itself, the one whose package.json has the name @pbuilder/sdk. Its dependencies must resolve from a node_modules at or above it, which a normal install gives you.

Install Directory
npm install -g @pbuilder/sdk <npm root -g>/@pbuilder/sdk
bun add -g @pbuilder/sdk ~/.bun/install/global/node_modules/@pbuilder/sdk
npm install @pbuilder/sdk in a separate folder <folder>/node_modules/@pbuilder/sdk

Copying only the package directory elsewhere fails, because its dependencies are left behind (see When it fails).

  1. Install the SDK outside the project, for example in a folder of its own:

    Terminal window
    mkdir ~/sdk-0.22.0 && cd ~/sdk-0.22.0
    npm init -y
    npm install @pbuilder/[email protected]
  2. Pass the package directory before the schematic:

    Terminal window
    builder execute --sdk-root="$HOME/sdk-0.22.0/node_modules/@pbuilder/sdk" default:hello

    The flag must come before <collection>:<schematic>. After it, the run fails with invalid_input and exit 2 (flag --sdk-root must come before the <collection>:<schematic> positional).

  3. Check which SDK the run used. execute created one entry in the workspace, a symbolic link:

    Terminal window
    readlink node_modules/@pbuilder/sdk

A run with --dry-run writes nothing, so it creates no link and no node_modules.

Set the variable once, in your shell profile or in the environment of your editor or agent:

Terminal window
export BUILDER_SDK_ROOT="$HOME/sdk-0.22.0/node_modules/@pbuilder/sdk"
builder execute default:hello

A shell expands ~ when you assign the variable, but a value quoted in the shell, or written into a settings file that does not expand it, keeps the ~ literally and fails with sdk_root_unresolvable. Write the full path there. An empty BUILDER_SDK_ROOT counts as unset.

Every run that takes the root from the variable prints a warning that names the variable and never the path:

sdk root came from BUILDER_SDK_ROOT

In the JSON envelope the same warning is warn_sdk_root_ambient. A root named by --sdk-root or sdk.root prints no such warning.

Add sdk.root to project-builder.json:

project-builder.json
{
"sdk": {
"root": "/Users/me/sdk-0.22.0/node_modules/@pbuilder/sdk"
}
}

An absolute path is used as is. A relative path resolves against the directory that holds project-builder.json, so "../sdk-0.22.0/node_modules/@pbuilder/sdk" works too. The value is checked when execute runs, not when the file is read.

execute takes the SDK from the first of these that is set:

  1. --sdk-root
  2. BUILDER_SDK_ROOT
  3. sdk.root
  4. The workspace’s installed node_modules/@pbuilder/sdk

A source that is set but unusable fails the run. execute never falls back to a lower one: with BUILDER_SDK_ROOT pointing at a missing directory and a valid sdk.root, the run fails with sdk_root_unresolvable instead of using sdk.root.

The same order applies to builder new schematic, which needs no prior execute and creates no link. builder info reads none of the three.

When a run commits, execute creates one entry, node_modules/@pbuilder/sdk, as a symbolic link to the canonical root. It creates node_modules/ and node_modules/@pbuilder/ when they are missing, and writes nothing else. Running again with the same root reuses the link, and naming another root repoints it.

Two consequences follow:

  • The link is not a package-manager install, so there is no node_modules/.bin/pbuilder-codegen shim. See Type generation.
  • A --dry-run that finds a link pointing at a different SDK than the one you named fails with execute_dry_run_sdk_link_stale. Run without --dry-run to repoint the link, or delete it.

Removing the flag, the variable or sdk.root does not remove the link. The next execute without an external root fails with execute_manifest_path_escape (exit 3), because the leftover link points outside the workspace. Confirm the entry is a symbolic link, never a directory, and delete it:

Terminal window
test -L node_modules/@pbuilder/sdk && rm node_modules/@pbuilder/sdk

If node_modules/@pbuilder/sdk is a real installed package that differs from the root you named, execute fails with execute_sdk_link_path_conflict and changes neither. Delete the installed copy to use the root, or stop naming a root to use the installed one. Before you set BUILDER_SDK_ROOT for every checkout, remove the SDK installed in each of them.

Every row below was reproduced on builder v0.19.12 with @pbuilder/sdk 0.22.0. The message names the source that supplied the value (--sdk-root, BUILDER_SDK_ROOT or sdk.root) and never prints the path.

Situation Code Reason Exit
No source set and no installed SDK execute_sdk_not_installed sdk_absent 4
Directory does not exist, or ~ left literal execute_sdk_root_invalid sdk_root_unresolvable 4
Directory is not @pbuilder/sdk (wrong package name, or the folder above the package) execute_sdk_root_invalid sdk_root_package_mismatch 4
--sdk-root= with no value execute_sdk_root_invalid sdk_root_value_invalid 4
Value starts with a URL scheme such as https:// execute_sdk_root_invalid sdk_root_unsupported_scheme 4
Package copied without its dependencies execute_sdk_root_invalid sdk_root_dependencies_unresolvable 4
A real, different SDK in node_modules/@pbuilder/sdk execute_sdk_link_path_conflict sdk_root_local_install_present 4
--dry-run with a link to another SDK execute_dry_run_sdk_link_stale sdk_link_on_factory_path 4
Leftover link and no source set execute_manifest_path_escape path_escapes_workspace 3
--sdk-root after the positional invalid_input input_invalid 2

For example, pointing --sdk-root at a directory whose package.json has another name prints this in the pretty output:

✗ [SDK] the directory named by --sdk-root is not @pbuilder/sdk
execute_sdk_root_invalid
Fix: point --sdk-root at the package directory whose package.json name is "@pbuilder/sdk"

One warning does not stop the run. If a directory above the root is writable by its group, execute continues and prints warn_sdk_root_group_writable, for example a directory 3 levels above --sdk-root is group-writable; anyone in that group can replace the SDK. Remove the group write bit with chmod g-w, or move the SDK under a directory only you can write to.