bridge icon indicating copy to clipboard operation
bridge copied to clipboard

Compiler: fuzzy token resolution silently binds non-existent $tokens to unrelated variables (e.g. $spacing/md → font/size/md) — request strict mode

Open deriazjulien opened this issue 2 months ago • 0 comments

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

  1. Strict mode by default (or at least a --strict flag on bridge-ds compile): exact-name candidates only (exactCandidates already exist); on miss, emit RESOLVE_TOKEN_NOT_FOUND with the fuzzy candidates as suggestion instead of silently accepting them. This keeps the helpful suggestions and removes the silent guess.
  2. Type guard: $color/… must resolve to resolvedType: "COLOR", $spacing/… and $radius/… to FLOAT; otherwise error.
  3. 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.

deriazjulien avatar Sep 08 '26 09:09 deriazjulien