Skip to content

fix(extractor): compile @vanilla-extract/css-utils calc to its strings - #972

Open
owjs3901 wants to merge 5 commits into
mainfrom
fix/vanilla-extract-css-utils
Open

owjs3901 wants to merge 5 commits into
mainfrom
fix/vanilla-extract-css-utils

Conversation

@owjs3901

@owjs3901 owjs3901 commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Summary

VE-05: nothing handled calc from @vanilla-extract/css-utils, so importing it in a .css.ts stylesheet failed with Cannot load '@vanilla-extract/css-utils' without a module resolver, and the package was not in the default alias table either. The build now compiles calc to the same strings the package (@vanilla-extract/css-utils 0.1.6) produces.

  • alias: added @vanilla-extract/css-utils to DEFAULT_IMPORT_ALIASES (can be turned off with importAliases: { '@vanilla-extract/css-utils': false }). Types come from @devup-ui/react/compat/vanilla-extract-css-utils, so the package does not need to be installed.
  • .css.ts stylesheets (module_loader/css_utils.rs): loads a calc implementation identical to the package source as a module without a resolver and evaluates it as is. So operands can be any value the stylesheet computes, such as vars.space.md or createVar(). Using a calc(x) chain as a CSS value without .toString() is an error with a location (a case that silently breaks in VE).
  • Ordinary modules (vanilla_extract_calc.rs): in modules that are not evaluated, the alias pass replaces each calc call with a string literal and removes the import. Covered: calc.add/subtract/multiply/divide/negate, and calc(x) chains + .toString() or template interpolation; nested operations drop calc and keep only the parentheses, as the package does. Operands must be literals, module-level consts holding literals, or other calc results.
  • All of the following are build errors with a location (file:line:col, cause, Fix): unreadable operands, reading calc without calling it, calc(x) without .toString(), exports other than calc, default imports, and namespace imports / require() / re-exports in ordinary modules.
  • eslint-plugin: ImportStorage records css-utils calc bindings, and css-utils-literal-only reads calc calls as build-time values the same way the build does (following the @emotion/css pattern from PR fix(eslint-plugin): lint @emotion/css like the build #903).
  • docs: added a @vanilla-extract/css-utils section to the vanilla-extract migration page and updated the rule README.

Note: existing problem (out of scope for this PR)

If a .css.ts style() contains the form g(f(1)).d(3).toString() (a call nested inside a chained call), the default test-thread stack (2 MiB) is not enough in debug builds and a stack overflow occurs, regardless of calc (it passes with 3 MiB). So this PR's tests extract on an 8 MiB thread. This needs to be looked into separately.

Overlap with open PRs

Test plan

  • Confirmed that on main before the fix, a .css.ts importing calc fails with Cannot load '@vanilla-extract/css-utils' without a module resolver, and an ordinary module fails with css() "cannot use calc.add(...) at build time"
  • calc_gives_the_string_the_package_builds: compares 16 cases of strings returned by the real package in node, in both ordinary modules and .css.ts (nested, negate, chain, number/boolean operands, calc string removal, constant operands)
  • Stylesheet variables (createTheme vars, createVar) and namespace imports (insta snapshots)
  • 18 cases of non-computable usage and 7 import-form cases are errors with a location
  • Existing behavior is kept when the alias is turned off
  • cargo test -p extractor --tests -j 2, cargo clippy -p extractor --all-targets -- -D warnings, cargo fmt --all
  • bun test plugin-utils (merge-import-aliases, create-compat-types), next-plugin plugin.test, vite-plugin plugin.test (bunfig.vite.toml), eslint-plugin (import-storage / css-utils-literal-only 100% coverage)
  • tsc --noEmit (eslint-plugin, plugin-utils), bun run test:types (react), prettier

요약

VE-05: @vanilla-extract/css-utils의 calc를 처리하는 곳이 없어서, .css.ts 스타일시트에서 import하면 Cannot load '@vanilla-extract/css-utils' without a module resolver 에러가 났고, 기본 alias 테이블에도 이 패키지가 없었습니다. 이제 빌드가 calc를 패키지(@vanilla-extract/css-utils 0.1.6)가 만드는 것과 같은 문자열로 컴파일합니다.

  • alias: @vanilla-extract/css-utils를 DEFAULT_IMPORT_ALIASES에 추가했습니다 (importAliases: { '@vanilla-extract/css-utils': false }로 끌 수 있습니다). 타입은 @devup-ui/react/compat/vanilla-extract-css-utils가 제공해서 패키지를 설치하지 않아도 됩니다.
  • .css.ts 스타일시트(module_loader/css_utils.rs): 패키지 소스와 같은 calc 구현을 resolver 없이 모듈로 로드해서 그대로 평가합니다. 그래서 operand에 vars.space.md, createVar() 등 스타일시트가 계산하는 값을 무엇이든 쓸 수 있습니다. .toString() 없이 calc(x) 체인을 CSS 값으로 쓰면 위치가 붙은 에러가 납니다 (VE에서는 조용히 깨지는 경우).
  • 일반 모듈(vanilla_extract_calc.rs): 평가하지 않는 모듈에서는 alias 패스에서 각 calc 호출을 문자열 리터럴로 바꾸고 import를 제거합니다. 대상은 calc.add/subtract/multiply/divide/negate, calc(x) 체인 + .toString() 또는 템플릿 보간이고, 중첩 연산은 패키지처럼 calc를 지우고 괄호만 남깁니다. operand는 리터럴, 리터럴을 담은 모듈 레벨 const, 다른 calc 결과여야 합니다.
  • 다음은 모두 위치가 붙은 빌드 에러입니다 (file:line:col, 원인, Fix): 읽을 수 없는 operand, 호출하지 않은 calc 읽기, .toString() 없는 calc(x), calc 외의 export, default import, 그리고 일반 모듈에서의 namespace import / require() / re-export.
  • eslint-plugin: ImportStorage가 css-utils의 calc 바인딩을 기록하고, css-utils-literal-only가 빌드와 같은 방식으로 calc 호출을 build-time 값으로 읽습니다 (PR #903의 @emotion/css 패턴을 따랐습니다).
  • docs: vanilla-extract 마이그레이션 페이지에 @vanilla-extract/css-utils 섹션을 추가했고, 룰 README도 갱신했습니다.

참고: 기존 문제 (이 PR 범위 밖)

.css.ts의 style() 안에 g(f(1)).d(3).toString() 형태(체인 호출 안에 호출이 들어간 경우)가 있으면, calc과 상관없이 debug 빌드에서 테스트 스레드 기본 스택(2 MiB)이 부족해 stack overflow가 납니다 (3 MiB면 통과합니다). 그래서 이 PR의 테스트는 8 MiB 스레드에서 추출합니다. 별도로 확인이 필요합니다.

열린 PR과의 겹침

테스트 계획

  • 수정 전 main에서 .css.ts가 calc를 import하면 Cannot load '@vanilla-extract/css-utils' without a module resolver, 일반 모듈은 css() "cannot use calc.add(...) at build time"으로 실패하는 것 확인
  • calc_gives_the_string_the_package_builds: 실제 패키지가 node에서 반환한 문자열 16개 케이스를 일반 모듈과 .css.ts 양쪽에서 비교 (nested, negate, chain, number/boolean operand, calc 문자열 제거, 상수 operand)
  • 스타일시트 변수(createTheme vars, createVar)와 namespace import (insta 스냅샷)
  • 계산할 수 없는 사용 18개 케이스와 import 형태 7개 케이스가 위치가 붙은 에러
  • alias를 끄면 기존 동작 유지
  • cargo test -p extractor --tests -j 2, cargo clippy -p extractor --all-targets -- -D warnings, cargo fmt --all
  • bun test plugin-utils (merge-import-aliases, create-compat-types), next-plugin plugin.test, vite-plugin plugin.test (bunfig.vite.toml), eslint-plugin (import-storage / css-utils-literal-only 100% 커버리지)
  • tsc --noEmit (eslint-plugin, plugin-utils), bun run test:types (react), prettier

Refs #690

🤖 Generated with Claude Code

A stylesheet importing `calc` from `@vanilla-extract/css-utils` failed
with "Cannot load '@vanilla-extract/css-utils' without a module
resolver", and nothing aliased the package.

- `@vanilla-extract/css-utils` joins the default import aliases; while it
  is aliased the build compiles `calc` itself
- a `.css.ts` stylesheet evaluates the package's own `calc`, loaded
  without a resolver, so its operands may be anything the stylesheet
  computes (`vars.space.md`, `createVar()`); a `calc(x)` written as a CSS
  value without `.toString()` is a located error
- any other module writes each `calc` call as the string the package
  builds: `calc.add/subtract/multiply/divide/negate`, chained `calc(x)`
  with `.toString()` or template interpolation, nested operations only
  parenthesized; operands must be literals, module-level `const`s holding
  a literal or other `calc` results, and anything else is a located error
- `@devup-ui/react/compat/vanilla-extract-css-utils` declares the package
- css-utils-literal-only reads `calc` calls as the build does
- docs: the vanilla-extract migration page describes `calc`

Refs #690

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`bun test` aborted under the preloaded bun plugin with
`index out of bounds: the len is 0 but the index is 19` while it compiled
the ESLint plugin's own sources:

- The alias pass analysed a module naming `@vanilla-extract/css-utils`
  to compile `calc`, which set the AST's reference IDs, but kept that
  table to itself: the pass's own lazy table stayed empty, so reading the
  module's calls for Emotion's `css` prop resolved those IDs against an
  empty table. The pass now builds one analysis and shares it.
- The `undefined` absence proof analysed the program itself when the
  visit had no analysis, leaving IDs the visit's empty table could not
  resolve (pre-existing on main). It now analyses a copy.

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

codecov Bot commented Nov 15, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Files with missing lines Coverage Δ
libs/extractor/src/import_alias_visit.rs 100.00% <100.00%> (ø)
libs/extractor/src/lib.rs 100.00% <100.00%> (ø)
libs/extractor/src/module_loader.rs 100.00% <100.00%> (ø)
libs/extractor/src/module_loader/script/explain.rs 100.00% <100.00%> (ø)
libs/extractor/src/style_values/absence.rs 100.00% <100.00%> (ø)
libs/extractor/src/vanilla_extract_calc.rs 100.00% <100.00%> (ø)
libs/extractor/src/vanilla_extract_calc_tests.rs 100.00% <100.00%> (ø)
...t-plugin/src/rules/css-utils-literal-only/index.ts 100.00% <100.00%> (ø)
packages/eslint-plugin/src/utils/import-storage.ts 100.00% <100.00%> (ø)
packages/plugin-utils/src/types.ts 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

owjs3901 and others added 3 commits November 17, 2026 12:53
lib.rs keeps both module declarations and runs main's
runtime_option_errors before this branch's calc errors. In the eslint
plugin, main (#910) now reads the aliases from settings, so
@vanilla-extract/css-utils joins its mirrored default table and calc
bindings are recorded only while that alias is on, as the build does;
import-settings.test.ts covers the alias turned off.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Pick up the #983 snapshot fix so CI runs against current main.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Bring in #963 (per-session engine state) and #928. The only conflict was
the module list in libs/extractor/src/lib.rs, where main re-exports
ExtractorCaches next to this branch's vanilla_extract_calc module; both
are kept. The calc compiler adds no process-wide state, so nothing joins
the session swap.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant