Block cross-package imports with Biome plugins
Write a GritQL plugin that fails biome lint when one workspace package imports another's source, with real diagnostics, limits and alternatives.
Riley Hilliard7 min read
biomemonorepoagents
To block cross-package imports with Biome, write a GritQL plugin that matches every import source, resolves relative paths to a real location, and calls register_diagnostic when a file in one package reaches into another. Register it under plugins in biome.json, and biome lint fails with an error that points at the import. The plugin below is under 20 lines and catches relative imports, package-name imports that reach into a subpath, export ... from, and string-literal dynamic import().
The built-in noRestrictedImports rule can cover part of this, but it matches the text of the import specifier and doesn’t resolve paths. Everything here was run on Biome 2.6.8 in a throwaway two-package repo, and the limits I hit are listed so you know what the plugin does not see.
How do you write a Biome plugin that blocks cross-package imports?
The test repo has packages/a and packages/b, each with a src/index.ts, and packages/b/src/internal.ts that package a should never import. The rule: a may import @acme/b (the public entry) but not b’s source.
language js
or {
JsModuleSource() as $src,
`import($src)`
} where {
$filename <: r".*/packages/a/.*",
or {
and {
$src <: r"[\"']\.[^\"']*[\"']",
$path = trim(string=$src, trim_chars="\"'"),
$resolved = resolve(path=$path),
$resolved <: r".*/packages/b/.*"
},
$src <: r"[\"']@acme/b/.*[\"']"
},
register_diagnostic(span=$src, message="Import package b through its public entry point, not its source.")
}
The or at the top picks two shapes: JsModuleSource() matches the string in any import ... from, export ... from or export * from, and the backtick pattern matches a dynamic import() call. $filename is the path of the file being linted, so the first condition limits the rule to package a. A relative specifier is trimmed of its quotes and passed to resolve(), which gives an absolute path, and the rule fires when that path lands in package b. The second branch catches @acme/b/src/internal: the bare package name is allowed, a subpath is not.
Registering it takes one line in biome.json:
{
"plugins": ["./biome-plugins/no-cross-package-imports.grit"]
}
Biome’s plugin docs say a plugin entry can also be an object with path and includes globs, which would scope a plugin to a directory without the $filename check. I tried that form: with the $filename line removed and includes set to packages/a/**, the same probe files were flagged (seven diagnostics in five files).
Here is a file in package a that breaks the rule, and what biome lint packages printed for it:
import { secret } from "../../b/src/internal";
export const x = secret;
packages/a/src/relative.ts:1:24 plugin ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
× Import package b through its public entry point, not its source.
> 1 │ import { secret } from "../../b/src/internal";
│ ^^^^^^^^^^^^^^^^^^^^^^
2 │
3 │ export const x = secret;
The passing case is a file that imports @acme/b and its own ./index. Biome prints Checked 1 file in 2ms. No fixes applied. and exits 0, while the failing file exits 1. register_diagnostic also takes a severity of hint, info, warn or error, and the default is error. With severity="warn" the same import printed Found 1 warning., which is a way to roll a rule out across an existing codebase before it blocks anything.
What does the plugin miss?
It reads text, so it misses anything it can’t see in a string literal. In my run these slipped through:
const r = require("../../b/src/internal")was not flagged.import(name)wherenameis a variable was not flagged, because the specifier isn’t a literal.- A path alias such as
@/b/internalfromtsconfigpathswould only match if you add a pattern for it. The plugin knows nothing about your TypeScript config.
Biome’s plugin page lists the broader limits. GritQL plugins have no access to the semantic model or file kind, and they run on every file, so the $filename condition is how you keep the cost down. The page also lists register_diagnostic as the only function Biome adds to Grit’s built-ins. It doesn’t document resolve(), so treat it as a Grit built-in that worked in 2.6.8, and pin your Biome version.
The last limit cost me time. Piping a file to biome lint --stdin-file-path=packages/a/src/relative.ts printed the file back with The contents aren't fixed and no plugin diagnostic, even though the same file on disk fails. Stacks’ own AGENTS.md records the same finding: “Biome stdin suppresses useful plugin diagnostics, so boundary tests use actual temporary files.” Anything that lints through stdin, such as an editor integration or a hook, needs to lint the file on disk.
Why not use Biome’s noRestrictedImports rule?
If your boundary is “nobody imports this package name”, use it. It has been in Biome since 1.6.0, takes a message, and its patterns option (added in 2.2.0) takes gitignore-style globs. Biome leaves it off unless you enable it, and a warning is its default severity, so set the level to error. You can aim it at one package with an overrides block:
{
"overrides": [
{
"includes": ["packages/a/**"],
"linter": {
"rules": {
"style": {
"noRestrictedImports": {
"level": "error",
"options": {
"patterns": [
{
"group": ["@acme/b/**", "../../b/*", "../../b/**"],
"message": "Import package b through its public entry point."
}
]
}
}
}
}
}
}
]
}
I ran this against the same files. It matches the specifier as written, so ../../b/** caught packages/a/src/relative.ts but missed a file one directory deeper in package a, which spells the same target ../../../b/src/internal. The plugin caught both, because resolve() turns each into an absolute path first. A first attempt with @acme/b/* also missed @acme/b/src/internal, and @acme/b/** fixed it. Neither route flagged the require call.
So the rule is the cheaper tool when the thing to forbid has a stable name, and the plugin is the better one when the forbidden thing is “wherever that relative path lands”.
When do other tools fit better?
The plugin is a lint rule on import text. Three other tools solve neighboring problems, and each is better than the plugin at something.
eslint-plugin-boundaries classifies files into element types by folder pattern and applies allow or disallow policies between them, reporting a standard ESLint error at the import. Its README mentions a boundaries/dependencies rule for this. It fits when you already run ESLint and want a many-to-many dependency policy (features may import shared, never each other), which gets awkward as Grit regexes.
dependency-cruiser validates the whole dependency graph against forbidden rules written as from and to path regexes, with a severity per rule. It prints violations in an ESLint-like format, and it can draw the graph through GraphViz. Pick it when you want cycle detection, orphan detection or a diagram on top of boundary checks, and you’re fine running a separate command in CI.
TypeScript project references split a program into projects that each have a tsconfig.json, with composite set and a references list. Importing from a referenced project loads its .d.ts output, and tsc --build builds projects in order. The handbook lists faster builds and better enforcement of logical separation between components. In my test with TypeScript 5.9.3 and two composite projects, a relative import of b’s source from a failed to build (TS6059, TS6307) when a had no reference to b, and compiled once a referenced b. So it blocks reaching into a package you haven’t declared a dependency on, and does nothing about one you have.
If you’re already on Biome, the plugin gives you the feedback in the same command that runs the rest of lint, with nothing extra to install.
Why do coding agents erode package boundaries?
My explanation, not something I tested: an agent writing code in package a finds what it needs by searching the repo, and a relative path to an existing file always compiles. The sibling package’s internal helper is the nearest match to what it wants, so it imports that. A person may remember why the boundary is there. An agent gets that from the instructions it was handed, and instructions can be missed.
Stacks, our project starter, keeps this rule in two places. AGENTS.md says “Code two packages share goes in its own package, never imported across another package’s src/”, and the architecture rule adds that the root biome-plugins/no-cross-package-imports.grit enforces it. The sentence tells the agent what to do, and the plugin makes the same sentence fail a command when the agent doesn’t do it. The Stacks repo also has a lint-on-write.sh hook that, per its README, returns Biome diagnostics for the file the agent just wrote. A bad import shows up one edit after it’s written, with the message saying what to do instead. We cover that hook pattern in blocking dangerous commands and the final gate in making an agent run tests before it’s done.
The message text matters as much as the match. “Import package b through its public entry point” tells the agent the fix, where “boundary violation” only tells it to stop.
How do you test the plugin?
Write each rule’s probes as files and lint them. The Stacks repo’s packages/cli/tests/boundaries.test.ts writes source to a temporary directory, runs Biome’s lint on the file, and asserts on the message and a non-zero exit. For the starter plugins it also lints a near miss in an allowed location and asserts that one passes. A rule that silently matches nothing looks the same as a clean codebase, so the failing probes are what prove it works. Use real files on disk for this, for the stdin reason above.
Where does Stacks come in?
Stacks is a free, open-source (Apache-2.0) CLI that scaffolds a project from a stack, hands the agent a brief, and runs stacks verify until the project’s own checks pass. Its typescript-library and react-vite stacks ship a biome-plugins/boundaries.grit with tests/boundaries.test.ts probes, for platform and environment boundaries inside one package. The cross-package rule in this post is a two-package simplification of the one in the Stacks monorepo, which names its cli, format and mcp packages.
$ npm install -g @steamfield/stacks