Skip to content
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

Merged
merged 1 commit into from
Aug 25, 2022

Conversation

codyml
Copy link
Member

@codyml codyml commented Aug 25, 2022

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:

  • Status of feature flags is made available to front-end code via an object written to window.featureFlags.
  • This object is written when 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.
  • If window.featureFlags does not exist, the isFeatureEnabled function just returns false.

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

Screen Shot 2022-08-25 at 2 17 30 PM

TESTING INSTRUCTIONS

In local dev environment, you can simulate feature flag detection before initialization by replacing superset-frontend/src/preamble.ts:67 with setTimeout(() => initFeatureFlags(bootstrapData?.common?.feature_flags));. Doing so should raise the console errors shown above.

ADDITIONAL INFORMATION

  • Has associated issue:
  • Required feature flags:
  • Changes UI
  • Includes DB Migration (follow approval process in SIP-59)
    • Migration is atomic, supports rollback & is backwards-compatible
    • Confirm DB migration upgrade and downgrade tested
    • Runtime estimates and downtime expectations provided
  • Introduces new feature or API
  • Removes existing feature or API

@github-actions
Copy link
Contributor

Storybook has completed and can be viewed at

Copy link
Contributor

@samtfm samtfm left a comment

Choose a reason for hiding this comment

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

excellent improvement!

@codecov
Copy link

codecov bot commented Aug 25, 2022

Codecov Report

Merging #21201 (20298a5) into master (17ad0d8) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           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           
Flag Coverage Δ
javascript 52.31% <100.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...ackages/superset-ui-core/src/utils/featureFlags.ts 100.00% <100.00%> (ø)
superset-frontend/src/featureFlags.ts 100.00% <100.00%> (ø)

📣 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];
Copy link
Member

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.

Copy link
Member

@rusackas rusackas left a 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 ;)

@rusackas rusackas merged commit 5811262 into apache:master Aug 25, 2022
@michael-s-molina
Copy link
Member

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?

@codyml
Copy link
Member Author

codyml commented Aug 31, 2022

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!

@mistercrunch mistercrunch added 🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels 🚢 2.1.0 and removed 🚢 2.1.3 labels Mar 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels size/S 🚢 2.1.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants