Skip to content

fix: resolve variable collection by key when cross-file ID lookup fails#3888

Open
akshay-gupta7 wants to merge 2 commits into
mainfrom
akshay/fix-cross-file-collection-lookup
Open

fix: resolve variable collection by key when cross-file ID lookup fails#3888
akshay-gupta7 wants to merge 2 commits into
mainfrom
akshay/fix-cross-file-collection-lookup

Conversation

@akshay-gupta7

@akshay-gupta7 akshay-gupta7 commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

Why does this PR exist?

Closes #0000

When a $themes.json is used in a different Figma file than where it was created (e.g. a consumer file subscribed to a published library), applying an active theme with "Swap Figma modes" enabled fails with:

"One of the variable collections linked to this theme no longer exists"

$figmaCollectionId is scoped to the file it was created in. In any other file, getVariableCollectionByIdAsync returns null and the mode swap is skipped entirely.

What does this pull request do?

Adds a fallback in swapFigmaModes for when the stored collection ID can't be resolved. It takes the first variable key from $figmaVariableReferences, calls importVariableByKeyAsync to resolve it in the current file, and derives the correct collection and mode from the result. Variable keys are stable across files, so this works for subscribed library collections.

The error toast is preserved for cases where the fallback also fails (e.g. the library is not subscribed, or $figmaVariableReferences is empty).

Testing this change

  1. In file A, create variables via Export to Figma with at least one theme — this populates $figmaCollectionId and $figmaVariableReferences in $themes.json
  2. Publish the variable collection from file A as a library
  3. Open file B, subscribe to file A's library
  4. Load the same $themes.json into the plugin in file B
  5. Enable Swap Figma modes in plugin settings
  6. Set one of the themes as active in the theme switcher
  7. Hit Apply to canvas

Before: error toast fires, mode is not swapped
After: mode swaps silently

Additional Notes (if any)

The fallback requires $figmaVariableReferences to be populated. If a theme has no variable references stored (i.e. "Export to Figma" was never run), the fallback cannot resolve the collection and the error toast still appears. Users in that state need to run "Export to Figma" in the source file first.

@changeset-bot

changeset-bot Bot commented Jun 1, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 8216b49

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@github-actions

github-actions Bot commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

⤵️ 📦 ✨ The artifact was successfully created! Want to test it? Download it here 👀 🎁

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves swapFigmaModes in the plugin sandbox so that applying an active theme can still “Swap Figma modes” when the stored $figmaCollectionId cannot be resolved in the current file (e.g., when using a $themes.json created in a different file and consuming variables from a subscribed library).

Changes:

  • Adds a fallback resolution strategy when getVariableCollectionByIdAsync($figmaCollectionId) returns null.
  • Attempts to resolve the collection (and mode) via a stable variable key from $figmaVariableReferences using importVariableByKeyAsync.

Comment on lines +60 to +68
if (!resolvedCollection) {
// Fallback: the stored ID is from a different file (e.g. a published library used in a consumer file,
// or a collection that was deleted and recreated). Try to resolve via a variable key from
// $figmaVariableReferences — variable keys are stable across files.
const variableKey = Object.values(themeObject.$figmaVariableReferences ?? {})[0];
if (variableKey) {
try {
const importedVariable = await figma.variables.importVariableByKeyAsync(variableKey);
if (importedVariable) {
@github-actions

github-actions Bot commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

Commit SHA:3368a994a4de710d3f64ec9a2bdb6a107e903a25

Test coverage results 🧪

Code coverage diff between base branch:main and head branch: akshay/fix-cross-file-collection-lookup 
Status File % Stmts % Branch % Funcs % Lines
🟢 total 61.54 (0.04) 53.01 (0.04) 59.2 (0.01) 61.95 (0.04)
🟢 packages/tokens-studio-for-figma/src/plugin/asyncMessageHandlers/swapFigmaModes.ts 96.55 (1.1) 83.87 (-0.34) 100 (0) 96.29 (1.17)
🟢 packages/tokens-studio-for-figma/src/utils/truncateName.ts 100 (20) 66.66 (33.33) 100 (0) 100 (20)

@akshay-gupta7 akshay-gupta7 self-assigned this Jun 8, 2026
@akshay-gupta7 akshay-gupta7 requested a review from six7 June 8, 2026 18:34
@akshay-gupta7 akshay-gupta7 marked this pull request as ready for review June 8, 2026 18:34
@anathaniel-TS

Copy link
Copy Markdown

🛡️ Hyma Compliance Check

⚠️ 1 finding(s) across 1 file(s)

Severity Category File Headline
🟢 LOW ROPA src/plugin/asyncMessageHandlers/swapFigmaModes.ts New figma.variables.importVariableByKeyAsync() call transmits variable keys to Figma's library infrastructure
Full finding details

[ROPA] importVariableByKeyAsync sends variable keys to Figma infrastructure

  • File: packages/tokens-studio-for-figma/src/plugin/asyncMessageHandlers/swapFigmaModes.ts
  • Implication: importVariableByKeyAsync is a Figma API that can trigger network activity to fetch variables from published libraries in other Figma files. Variable keys from $figmaVariableReferences are transmitted as lookup parameters to Figma's platform. Figma is already a processor, so no new subprocessor is introduced, but the scope of data shared with Figma expands slightly to include cross-file variable key identifiers.
  • Recommended action: Confirm Figma's DPA covers variable key lookups via the library import API. Add a note to the ROPA entry for Figma plugin data processing that variable key identifiers may be transmitted during theme-swap operations involving cross-file libraries.

ℹ️ This check is informational — findings do not block merging.
🛡️ Hyma Compliance Check · automated

@anathaniel-TS

Copy link
Copy Markdown

🛡️ Hyma Compliance Check

No compliance-relevant changes detected

1 file(s) scanned · 0 findings

ℹ️ This check is informational.
🛡️ Hyma Compliance Check · automated

@anathaniel-TS

Copy link
Copy Markdown

🛡️ Hyma Compliance Check

No compliance-relevant changes detected

Files scanned · 0 findings

No new external calls, personal data processing changes, or authentication/authorization modifications detected. No compliance action required.

ℹ️ This check is informational.
🛡️ Hyma Compliance Check · automated

@anathaniel-TS

Copy link
Copy Markdown

🛡️ Hyma Compliance Check

No compliance-relevant changes detected

2 file(s) scanned · 0 findings

ℹ️ This check is informational.
🛡️ Hyma Compliance Check · automated

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.

3 participants