Compiler: fuzzy token resolution silently binds non-existent $tokens to unrelated variables (e.g. $spacing/md → font/size/md) — request strict mode
Summary
resolveTokenRef() in lib/compiler/resolve.ts falls back to fuzzy segment scoring (scoreCandidate, threshold > 40) when a $token has no exact registry match. On a real design system this silently resolves non-existent tokens to unrelated variables, which defeats the "no hardcoded / no invented value" guarantee the compiler is meant to provide: the scene graph compiles with exit 0 and the wrong variable gets bound in Figma.
Reproduction (Bridge 7.3.1, main)
KB extracted from a production Figma library (173 variables, 4 collections). Variable families present: spacing/1…50 (numeric scale), radius/sm|md|lg|xl|2xl…, stroke/xs|sm|md, font/size/xs|sm|md|lg|…, font/weight/*, font/family/primary, bg/*, text/*, interactive/*, feedback/*, border/*, color/<family>/<step>.
const { loadRegistry } = require("./dist/lib/compiler/registry.js");
const { resolveTokenRef } = require("./dist/lib/compiler/resolve.js");
const r = loadRegistry("bridge-ds");
for (const t of ["$spacing/md","$spacing/xs","$radius/xs","$radius/medium","$color/primary","$color/brand","$spacing/7","$spacing/100","$color/galaxie/500"])
console.log(t, "→", resolveTokenRef(t, r).resolved?.name);
Output — none of these tokens exist in the registry, all resolve silently:
$token (does not exist) |
resolves to | why it is wrong |
|---|---|---|
$spacing/md |
font/size/md |
typography size used as a gap/padding |
$spacing/xs |
stroke/xs |
stroke weight used as spacing |
$radius/xs |
stroke/xs |
stroke weight used as radius |
$radius/medium |
font/weight/medium |
font weight used as radius |
$color/primary |
font/family/primary |
a STRING variable bound as a fill |
$color/brand |
bg/brand |
plausible, but still a guess |
$spacing/7 |
font/size/7xl |
|
$spacing/100 |
color/lilas/100 |
a COLOR bound as spacing |
$color/galaxie/500 |
color/bleu-galaxie/500 |
In a battery of 22 non-existent references, 14 resolved silently (the 8 others correctly produced RESOLVE_TOKEN_NOT_FOUND). Every one of the 173 real names resolves exactly, so the fuzzy path only ever fires for invented tokens.
Same for text styles: $text/heading/xl → Heading/6xl, $text/body → Body/lg.
Root cause
scoreCandidate() gives 80 when every search segment appears as a substring anywhere in a candidate name ("md" ⊂ "font/size/md"), and 40 when any segment does. The acceptance threshold is bestScore > 40, so a single shared segment plus the s1 >= 80 ? +10 bias is enough. The resolvedType of the matched variable is never checked against the requested category ($color/… can bind a FLOAT or STRING).
Proposal
- Strict mode by default (or at least a
--strictflag onbridge-ds compile): exact-name candidates only (exactCandidatesalready exist); on miss, emitRESOLVE_TOKEN_NOT_FOUNDwith the fuzzy candidates assuggestioninstead of silently accepting them. This keeps the helpful suggestions and removes the silent guess. - Type guard:
$color/…must resolve toresolvedType: "COLOR",$spacing/…and$radius/…toFLOAT; otherwise error. - Optionally keep fuzzy as an explicit opt-in (
--fuzzy-tokens) for early prototyping.
Happy to open a PR along those lines if you agree with the direction. We are currently working around it with a pre-compile lint that only accepts exact registry names.