Skip to content

fix(render): unmask render errors and make w/h="fill" work (#42) - #43

Closed
clementcopper wants to merge 2 commits into
silships:mainfrom
clementcopper:fix/render-fill-and-error-masking
Closed

clementcopper wants to merge 2 commits into
silships:mainfrom
clementcopper:fix/render-fill-and-error-masking

Conversation

@clementcopper

Copy link
Copy Markdown
Contributor

Fixes #42.

Two commits, reviewable independently.

1. The catch block masked every render error

The generated root-frame code declared const frame inside the try and called frame.remove() from the catch, where that binding is not in scope. So every failure — bad prop, unsupported sizing keyword, missing parent — surfaced as ReferenceError: frame is not defined, and the [Node: …] <message> wrapper right below it was unreachable. frame.remove() never ran either, so a failed render left an orphan frame on the canvas.

let frame is now hoisted out of the try and the cleanup is guarded, so neither a throwing createFrame() nor a failing remove() can replace the original error.

Before / after, same command:

$ figma-cli render '<Frame name="E" w={50} h={50} bg="#fff" />' --parent 999:9999
✗ Render failed: ReferenceError: frame is not defined     # before
✗ Render failed at E:  Error: Parent not found: 999:9999   # after

…and no orphan node left behind in the second case.

2. The three fill bugs the masking hid

  • Root frame with --parent. frame.layoutSizingHorizontal = 'FILL' was emitted near the top of the template, but __p.appendChild(frame) runs at the very end (deliberately, so an auto-layout parent measures real content rather than the seed size). FILL was set while the frame was still parentless, which Figma rejects — it could never have worked. The assignment now runs after appendChild, guarded by a check that the parent actually uses auto-layout (with a layout warning if it does not).
  • Root frame without --parent. Nothing to fill. Emits a warning through the existing __layoutWarnings channel instead of throwing.
  • <Rectangle> / <Ellipse> / <Image>. These passed item.w straight into resize(), so w="fill" reached the Plugin API as the string "fill". They now follow the same shape as a Frame child: the keyword is kept out of resize(), and layoutSizing* plus __figHugWarn are emitted after appendChild. In a flex="none" parent the keyword is ignored rather than emitted, matching Frame children.

The decision logic sits in src/lib/fill-sizing.js as two pure functions (resolveLeafSizing, resolveRootFill) with unit tests, per the convention browserDebugArgs / src/platform.js established.

Tests

tests/render-fill-errors.test.js asserts on the generated code for every reproduction case in #42, including the render-batch path (it shares the child generator, so the leaf fix lands there too). tests/flex-none.test.js needed a one-word anchor update (const frame = figma.createFrame → frame = figma.createFrame).

543 tests pass, no Figma required.

Verified live against Figma Desktop (Yolo Mode, macOS):

w="fill" child in a 300px parent with p={8}   → width 284, layoutSizingHorizontal FILL
<Rectangle w="fill" h={2}/> in a 200px frame  → width 200, FILL, type RECTANGLE
root w="fill" without --parent                → renders, one clear warning
--parent with an unknown id                   → "Parent not found: …", no orphan

Out of scope

generateBatchCode has no try/catch at all — no masking there, but also no cleanup when a batch render fails midway. Left alone deliberately; happy to look at it separately if you want it.

clementcopper and others added 2 commits August 17, 2026 11:30
The generated root-frame code declared `const frame` inside the `try` and
called `frame.remove()` from the `catch`, where that binding is not in
scope. Every failure — a bad prop, an unsupported sizing keyword, a
missing parent — was therefore replaced by
`ReferenceError: frame is not defined`, and the `[Node: …] <message>`
wrapper built for exactly this purpose was unreachable.

Two consequences, both fixed here:

- the real error now reaches the caller
- cleanup runs, so a failed render no longer leaves an orphan frame on
  the canvas (it did: two failed renders, two frames left behind)

`let frame` is hoisted out of the `try` and the cleanup is guarded, so a
`createFrame()` that throws, or a `remove()` that fails, cannot replace
the original error either.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
With the error masking gone (previous commit), three separate `fill`
bugs became visible. All three used to surface as the same
ReferenceError.

1. Root frame with `--parent`: `frame.layoutSizingHorizontal = 'FILL'`
   was emitted near the top of the generated code, but `appendChild`
   runs at the very end (deliberately, so an auto-layout parent measures
   real content instead of the seed size). FILL was set while the frame
   was still parentless, which Figma rejects. The assignment now runs
   after `appendChild`, guarded by a check that the parent actually uses
   auto-layout.

2. Root frame without `--parent`: nothing to fill. Emits a layout
   warning through the existing `__layoutWarnings` channel instead of
   throwing.

3. `<Rectangle>` / `<Ellipse>` / `<Image>` passed `item.w` straight into
   `resize()`, so `w="fill"` arrived at the Plugin API as the string
   "fill". They now follow the same shape as a Frame child: the keyword
   is kept out of `resize()`, and `layoutSizing*` plus `__figHugWarn` are
   emitted after `appendChild`. In a `flex="none"` parent the keyword is
   ignored rather than emitted, matching Frame children.

The decision logic lives in `src/lib/fill-sizing.js` as two pure
functions, per the repo convention, with unit tests that need no Figma.
`tests/render-fill-errors.test.js` asserts on the generated code for
each reproduction case from the bug report, including the render-batch
path, which shares the child generator.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
clementcopper added a commit to clementcopper/figma-claude that referenced this pull request Aug 17, 2026
clementcopper added a commit to clementcopper/figma-claude that referenced this pull request Sep 3, 2026
Seven branches became one on 2026-09-03. The skill's first line still said the
default was v2, and it is the file you read right before merging from upstream.

What the cleanup was based on: `feat/swift-host` was v2 plus 26 commits with v2
fully contained, the only tree difference being `swift-host/`, and `app/` sitting
in both. The Electron app is a directory, not a branch — `make-app.sh` reads
`app/build/icon.icns` — so a second branch beside the trunk would have been a
copy of the same CLI falling behind. v2 had stood still since 22.08.

`v2` and `archive/draft-v1` are now the tags `v2-final` and `draft-v1`. The four
PR branches stay on origin because deleting them there closes PRs silships#40, silships#41, silships#43
and silships#44; that is written down so nobody tidies them away later.

Also noted: CI triggers on `branches: [main]`, which this fork has never had, so
it has never run on a push here. Left alone.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QuvZuDad3hi3CVwNDbTvr2
silships pushed a commit that referenced this pull request Nov 19, 2026
Ported from PR #43 by clementcopper onto current main. The catch-block
unmasking half of #43 already landed with #59, so only the fill sizing is
ported here:
- root frame w/h="fill" is set after appendChild when --parent is an
  auto-layout frame, warns otherwise instead of throwing
- Rectangle / Ellipse / Image keep "fill" out of resize() and set
  layoutSizing* after appendChild (ignored in flex="none" parents)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@silships

Copy link
Copy Markdown
Owner

Thank you! This is in figma-cli 2.4.0 on npm: w="fill" on root frames and leaf nodes. The error unmasking overlapped with #59 so it came in from there. Commit ef5a162 keeps you as the author

@silships silships closed this Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

render: catch block reads a try-scoped frame, masking every error as "frame is not defined" (and leaking orphan frames)

2 participants