Skip to content

fix(web): guard theme.js against missing browser globals (#9912)#10058

Open
alokkumardalei-wq wants to merge 1 commit intoThe-OpenROAD-Project:masterfrom
alokkumardalei-wq:fix/theme-js-browser-globals
Open

fix(web): guard theme.js against missing browser globals (#9912)#10058
alokkumardalei-wq wants to merge 1 commit intoThe-OpenROAD-Project:masterfrom
alokkumardalei-wq:fix/theme-js-browser-globals

Conversation

@alokkumardalei-wq
Copy link
Copy Markdown
Contributor

@alokkumardalei-wq alokkumardalei-wq commented Apr 5, 2026

Problem

After ca12251 added the light/dark theme toggle, clock-tree-widget.js started importing getThemeColors() from theme.js. This function calls getComputedStyle(document.documentElement), which doesn't exist in Node.js. Any js_test that transitively imports clock-tree-widget.js now crashes with ReferenceError: document is not defined before a single test case runs.

What does this PR do?

Fixes #9912
Added a typeof document === 'undefined' guard at the top of getThemeColors(). In Node.js environments, it returns hardcoded dark-theme defaults instead of reading CSS custom properties. These values are only used by canvas rendering code that never runs in tests, so the fallback is safe.

Changes Made
src/web/src/theme.js — added typeof document check with dark-theme fallback values in getThemeColors()

Testing & Verification

bazelisk test //src/web/test:clock_tree_widget_test 

Expected output:

alokkumardalei@Aloks-MacBook-Air OpenROAD % bazelisk test //src/web/test:clock_tree_widget_test  
INFO: Invocation ID: 4c68bbc6-aa16-4fe6-ad16-c102cba097f8
INFO: Analyzed target //src/web/test:clock_tree_widget_test (119 packages loaded, 11108 targets configured).
INFO: Found 1 test target...
Target //src/web/test:clock_tree_widget_test up-to-date:
  bazel-bin/src/web/test/clock_tree_widget_test_/clock_tree_widget_test
INFO: Elapsed time: 0.808s, Critical Path: 0.41s
INFO: 1 process: 26 action cache hit, 1 internal.
INFO: Build completed successfully, 1 total action
//src/web/test:clock_tree_widget_test                           (cached) PASSED in 1.6s

Executed 0 out of 1 test: 1 test passes.
bazelisk test //src/web/test:charts_widget_test 

Expected output:

alokkumardalei@Aloks-MacBook-Air OpenROAD % bazelisk test //src/web/test:charts_widget_test      
INFO: Invocation ID: 6c6f5d0f-d680-4b03-9709-37db5ced7ba4
INFO: Analyzed target //src/web/test:charts_widget_test (0 packages loaded, 2 targets configured).
INFO: Found 1 test target...
Target //src/web/test:charts_widget_test up-to-date:
  bazel-bin/src/web/test/charts_widget_test_/charts_widget_test
INFO: Elapsed time: 0.454s, Critical Path: 0.26s
INFO: 1 process: 8 action cache hit, 1 internal.
INFO: Build completed successfully, 1 total action
//src/web/test:charts_widget_test                               (cached) PASSED in 0.9s

Executed 0 out of 1 test: 1 test passes.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a check for the existence of the document object in getThemeColors, providing fallback theme values for environments where it is undefined. The review feedback highlights a discrepancy between these hardcoded fallback values and the project's CSS definitions, and recommends using named constants instead of magic numbers to improve maintainability.

Comment on lines +14 to +21
if (typeof document === 'undefined') {
return {
canvasBg: '#1a1a1a', canvasText: '#ccc', canvasAxis: '#555',
canvasLabel: '#aaa', canvasGrid: '#333', canvasTitle: '#fff',
fgPrimary: '#e0e0e0', fgMuted: '#888', bgPanel: '#252525',
bgMap: '#1a1a1a',
};
}
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.

medium

The hardcoded fallback values for the dark theme do not match the values defined in src/web/src/style.css. For consistency and to avoid potential discrepancies in tests, these values should be synchronized with the CSS source of truth.

Specifically:

  • canvasText: #ccc should be #666 (matches --canvas-text)
  • canvasTitle: #fff should be #888 (matches --canvas-title)
  • fgPrimary: #e0e0e0 should be #ccc (matches --fg-primary)
  • bgMap: #1a1a1a should be #111 (matches --bg-map)

Additionally, per the general rules, these tunable parameters should ideally be defined as named constants rather than hardcoded magic numbers within the function body.

References
  1. Define tunable parameters as named constants instead of using hardcoded magic numbers.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 5, 2026

clang-tidy review says "All clean, LGTM! 👍"

@alokkumardalei-wq alokkumardalei-wq force-pushed the fix/theme-js-browser-globals branch 2 times, most recently from 990fb98 to cacb52c Compare April 5, 2026 07:40
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 5, 2026

clang-tidy review says "All clean, LGTM! 👍"

1 similar comment
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 5, 2026

clang-tidy review says "All clean, LGTM! 👍"

@alokkumardalei-wq alokkumardalei-wq force-pushed the fix/theme-js-browser-globals branch from cacb52c to fe80920 Compare April 5, 2026 08:03
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 5, 2026

clang-tidy review says "All clean, LGTM! 👍"

@alokkumardalei-wq alokkumardalei-wq force-pushed the fix/theme-js-browser-globals branch from fe80920 to 860d1f0 Compare April 5, 2026 08:16
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 5, 2026

clang-tidy review says "All clean, LGTM! 👍"

…D-Project#9912)

Signed-off-by: alokkumardalei-wq <alokkumardalei2@gmail.com>
@alokkumardalei-wq alokkumardalei-wq force-pushed the fix/theme-js-browser-globals branch from 860d1f0 to 24cdda1 Compare April 6, 2026 08:29
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 6, 2026

clang-tidy review says "All clean, LGTM! 👍"

@alokkumardalei-wq
Copy link
Copy Markdown
Contributor Author

Hello @maliberty , @hzeller and @oharboe all checks are passing now , please have a look when you have time and would happy to address any feedback further.

Thank you !

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.

[web] theme.js crashes all Node.js tests that import clock-tree-widget

1 participant