Skip to content

css modules: locate cross-file composes errors on the composes declaration - #38574

Closed
robobun wants to merge 3 commits into
mainfrom
farm/d171f678/css-composes-missing-name-source
Closed

robobun wants to merge 3 commits into
mainfrom
farm/d171f678/css-composes-missing-name-source

Conversation

@robobun

@robobun robobun commented Aug 14, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

  • Two composes: name from "./other.css" errors are located in the wrong file. The message text is right, but the file, line and excerpt are those of other.css, at whatever line a byte offset taken from the composing file happens to land on (clamped to the end of other.css when it is shorter):
    • The name "zzz" never appears in "other.css" as a CSS modules locally scoped class name... when other.css has no local zzz (src/bundler/linker_context/scanImportsAndExports.rs, validate_css_import_composes).
    • The composes property cannot be used with "zzz", because it is not a single class name. when other.css defines zzz as something other than a class, e.g. #zzz (src/bundler/linker_context/generateCodeForLazyExport.rs, warn_non_single_class_composes). Its The definition of "zzz" is here. note was already correct.
  • Cause, same in both: the diagnostic is built from the imported file's Source and compose.loc, which is an offset into the composing file. In the second site this happened because the function took one source index and used it for both the note (definition, other file) and the error (declaration, composing file).
  • esbuild, and bun's own same-file variants (composes: zzz;), locate these errors on the composes declaration. The mismatch dates back to the CSS modules implementation; two existing tests encoded the wrong file.

Fix

  • validate_css_import_composes: attach the error to the composing file's Source (input_files[id]); the message still names the file that was searched.
  • warn_non_single_class_composes: take the composing file's index alongside the defining file's. The error is attached to the composing file at compose.loc; the note is built with range_data against the defining file, the same way the linker builds its other multi-file notes (add_range_error_fmt_with_notes). The same-file arm passes the same index twice and is unchanged in behavior.
  • Correct because compose.loc is recorded by the composes value parser as a position in the file being parsed, so it can only be resolved against that file's contents; every other composes diagnostic in these two functions already pairs sources and offsets this way.
  • Tests, all in test/bundler/esbuild/css.test.ts:
    • css/ImportCSSFromJSComposesFromMissingImport and css/ImportCSSFromJSComposesImportedNonClass now expect the errors under styles.module.css (they expected the imported file, including the case where it is a plain, non-module stylesheet).
    • New css/ImportCSSFromJSComposesFromMissingImportLocation and css/ImportCSSFromJSComposesNonClassNameLocation check file, line, column and line text of every error and note reported by Bun.build: two names in one cross-file composes, a cross-file non-class name, and a same-file case of each for comparison. The notes still point at the definitions in the other file.
  • Verified: the four tests fail on the released 1.4.0 (USE_SYSTEM_BUN=1 bun test) and pass with bun bd test; the rest of css.test.ts and test/bundler/css/css-modules.test.ts pass.

Background

  • composes is the CSS modules property that makes one class's exported name also include other classes. composes: a from "./x.css" looks a up among x.css's locally scoped names. The linker validates that lookup twice: validate_css_import_composes while scanning imports (is the name there at all), and generate_code_for_lazy_export while building the file's export object (is it a class that can be composed).
  • A bundler diagnostic is a message plus a Source (one input file) and a Loc (byte offset). Line, column and excerpt are computed by resolving the offset against that Source's contents, so the Source has to be the file the offset was taken from. A note is a second such (text, Source, offset) triple attached to the message and may point into a different file.
Before / after with bun build entry.js --outdir out

styles.module.css has a ~200 byte comment before .foo { composes: zzz from "./file.module.css"; }; file.module.css is the one-line .x{color:red}.

Before (1.4.0):

1 | .x{color:red}
                 ^
error: The name "zzz" never appears in "file.module.css" as a CSS modules locally scoped class name. Note that "composes" only works with single class selectors.
    at /tmp/repro/file.module.css:1:14

After:

4 |   composes: zzz from "./file.module.css";
               ^
error: The name "zzz" never appears in "file.module.css" as a CSS modules locally scoped class name. Note that "composes" only works with single class selectors.
    at /tmp/repro/styles.module.css:4:12

esbuild reports the same thing as styles.module.css:4:12: ERROR: The name "zzz" never appears in "file.module.css" ....

Same layout, but other.module.css is #bar {\n color: blue;\n} and the declaration is composes: bar from "./other.module.css". Before:

3 | }
     ^
error: The composes property cannot be used with "bar", because it is not a single class name.
    at /tmp/repro/other.module.css:3:2

1 | #bar {
    ^
note: The definition of "bar" is here.
   at /tmp/repro/other.module.css:1:1

After:

4 |   composes: bar from "./other.module.css";
               ^
error: The composes property cannot be used with "bar", because it is not a single class name.
    at /tmp/repro/styles.module.css:4:12

1 | #bar {
    ^
note: The definition of "bar" is here.
   at /tmp/repro/other.module.css:1:1

…ng file

The "never appears in <file>" error for `composes: x from "./other.css"`
was added to the log with the imported file's Source but with the Loc of
the composes declaration, which is an offset into the composing file. The
reported location was therefore the other file, at whatever line that
offset happened to land on. Attach it to the composing file, like the
same-file variant and like esbuild.
@coderabbitai

coderabbitai Bot commented Aug 14, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

@robobun, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 1 minute

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 51088929-d2cd-443f-9f9e-9c3ed6c6b337

📥 Commits

Reviewing files that changed from the base of the PR and between eabb96d and a6acf80.

📒 Files selected for processing (3)
  • src/bundler/linker_context/generateCodeForLazyExport.rs
  • src/bundler/linker_context/scanImportsAndExports.rs
  • test/bundler/esbuild/css.test.ts

Comment @coderabbitai help to get the list of available commands.

@robobun

robobun commented Aug 14, 2026

Copy link
Copy Markdown
Collaborator Author
Updated 8:47 AM PT - Aug 14th, 2026

@robobun, your commit b3f69f9 is building: #96203

@robobun

robobun commented Aug 14, 2026 •

Copy link
Copy Markdown
Collaborator Author

Status: ready for review.

Reproduced on bun 1.4.0 with bun build entry.js --outdir out where styles.module.css has composes: zzz from "./file.module.css" and file.module.css is a one-line file: the error was printed at file.module.css:1:14 with that file's text as the excerpt. With this branch it is printed at styles.module.css:4:12, on the composes declaration (esbuild reports the same location). The review pointed out the same mismatch in the "not a single class name" error (composes: bar from "./other.module.css" with #bar in the other file); that is fixed here too, with its note still pointing at the definition.

Tests: test/bundler/esbuild/css.test.ts (ImportCSSFromJSComposesFromMissingImport, ImportCSSFromJSComposesImportedNonClass, new ImportCSSFromJSComposesFromMissingImportLocation and ImportCSSFromJSComposesNonClassNameLocation) fail on 1.4.0 and pass with this change.

The CodeRabbit comments above are only its rate-limit notices; nothing to address from them.

… the composes declaration

warn_non_single_class_composes took one source index and used it both for
the definition note and for the composes declaration. In the cross-file arm
that index is the file defining the name, while compose.loc is an offset
into the composing file, so the error was reported in the wrong file at an
unrelated line. Take both indices: the error goes on the composing file, the
note stays on the definition.
@robobun robobun changed the title css modules: report a missing cross-file composes name on the composing file css modules: locate cross-file composes errors on the composes declaration Aug 14, 2026

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM — the follow-up commits addressed the sibling warn_non_single_class_composes mismatch and the comment-cop notes.

What was reviewed:

  • validate_css_import_composes: error now attached to input_files[id] with compose.loc; message still names other_source.path.pretty.
  • warn_non_single_class_composes: split into definition_idx (note via range_data) and composing_idx (primary via add_range_error_fmt_with_notes); both call sites pass the right pair.
  • New tests assert exact file/line/column/lineText for both errors and their notes, cross-file and same-file.
Extended reasoning...

Overview

This PR fixes two CSS modules composes diagnostics that paired a byte offset from the composing file with the imported file's Source, producing correct message text but wrong file/line/excerpt. Changes: scanImportsAndExports.rs (validate_css_import_composes — swap the Source to the composing file, keep the searched file's name in the message text), generateCodeForLazyExport.rs (warn_non_single_class_composes — take separate definition_idx/composing_idx, build the note with range_data against the defining file and the primary with add_range_error_fmt_with_notes against the composing file), and test/bundler/esbuild/css.test.ts (two existing tests moved their expected errors under /styles.module.css; two new tests assert exact position and lineText for every error and note via Bun.build).

Prior feedback addressed

My earlier review flagged that the cross-file arm of visit_composes still passed import_record.source_index.get() as the sole index to warn_non_single_class_composes, keeping the same mismatch for the "not a single class name" error. Commit a6acf80 fixes it exactly as suggested (two indices, range_data + add_range_error_fmt_with_notes, matching the shape used elsewhere in the linker for multi-file notes), and css/ImportCSSFromJSComposesNonClassNameLocation now locks the primary at styles.module.css:5:12 with the note at other.module.css:4:1. The comment-cop notes about long inline comments were addressed in 413329d — the parameter names source/other_source and definition_idx/composing_idx carry the intent.

Security risks

None. This only changes which Source a diagnostic Loc is resolved against; no parsing, allocation, or control-flow changes on the success path.

Level of scrutiny

Low-to-medium. The change is a targeted diagnostic-location fix with no effect on generated output. I confirmed add_range_error_fmt_with_notes (src/ast/lib.rs:1750) resolves the primary against its source argument while each note carries its own Location via range_data (src/ast/lib.rs:2753), so the two-source split is sound. add_error_fmt returns (), so dropping the old let _ = is harmless. visit_composes is always entered with idx = the file whose ast.composes is being iterated, so compose.loc is always an offset into all_sources[idx] and the same-file arm passing idx, idx is unchanged in behavior.

Other factors

The new tests use backend: "api" + onAfterApiBundle to inspect build.logs positions directly, cover two names in one declaration, and include a same-file control for each cross-file case. The author verified the four affected tests fail on 1.4.0 and pass with the change, and that the rest of css.test.ts and css-modules.test.ts still pass.

@github-actions

Copy link
Copy Markdown
Contributor

Closing this PR because it has been inactive for more than 90 days.

@github-actions github-actions Bot closed this Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant