-
Notifications
You must be signed in to change notification settings - Fork 14.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Add warnings if feature flags are read before initialization #21201
Conversation
Storybook has completed and can be viewed at |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
excellent improvement!
Codecov Report
@@ Coverage Diff @@
## master #21201 +/- ##
=======================================
Coverage 66.39% 66.39%
=======================================
Files 1781 1781
Lines 67893 67901 +8
Branches 7248 7246 -2
=======================================
+ Hits 45077 45085 +8
Misses 20954 20954
Partials 1862 1862
Flags with carried forward coverage won't be shown. Click here to find out more.
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
@@ -28,5 +28,13 @@ export function initFeatureFlags(featureFlags: FeatureFlagMap) { | |||
} | |||
|
|||
export function isFeatureEnabled(feature: FeatureFlag) { | |||
return window && window.featureFlags && !!window.featureFlags[feature]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is fine for this PR, but it seems like we should just be importing the above Superset UI module everywhere and avoid having duplicate code in the project. Maybe we can clean that up in some other PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM... though we can probably DRY the project up a little bit ;)
Unfortunately, when executing the tests, this change fills the logs with errors as an unintended effect. @codyml Can you provide a patch for this when running tests? |
Yikes, looking into this ASAP. Sorry! |
SUMMARY
This PR hopes to help future developers more easily identify issues arising from feature flag race conditions. Correct feature flag setting and reading on the frontend currently relies on JS module loading order and fails silently:
window.featureFlags
.preamble.js
(or other another module in some cases) is executed.window.featureFlags
is read by other modules at various points in the JS lifecycle, including module execution, React component mount, and React component update. In at least one case, a bug has been traced back to FFs being read before this object has been written.window.featureFlags
does not exist, theisFeatureEnabled
function just returnsfalse
.This PR keeps current behavior but prints a console error with a stack trace so developers can more easily link bugs with FF detection issues and update their code to check feature flags in a way that avoids this race condition. An ideal place to check for feature flag status is on React component mount, but making this change to all affected code is a larger process and will be tackled in a separate PR.
BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
TESTING INSTRUCTIONS
In local dev environment, you can simulate feature flag detection before initialization by replacing
superset-frontend/src/preamble.ts:67
withsetTimeout(() => initFeatureFlags(bootstrapData?.common?.feature_flags));
. Doing so should raise the console errors shown above.ADDITIONAL INFORMATION