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

Add warning if multiple versions are running (addresses #1470) #1677

Merged
merged 2 commits into from
Dec 24, 2019

Conversation

ajs139
Copy link
Contributor

@ajs139 ajs139 commented Dec 6, 2019

What:
Add a register of all the @emotion packages, so that a warning can be displayed if the same package is registered with multiple versions.

Why:
Running multiple versions of the packages can cause hard to trace issues, see #1470.

How:
Each module "registers" itself providing its name and version into a global object. If that module name is already registered for a different version, a warning is displayed.

This is just a draft, for discussion on the approach.

Checklist:

  • Documentation
  • Tests
  • Code complete
  • Changeset

@changeset-bot
Copy link

changeset-bot bot commented Dec 6, 2019

🦋 Changeset is good to go

Latest commit: b29b958

We got this.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@ajs139 ajs139 force-pushed the warn_multiple_versions branch from dd191ac to 5c548f3 Compare December 6, 2019 19:23
Comment on lines 29 to 32
registerEmotionModule(
require('../package.json').name,
require('../package.json').version
)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is more here for usage, it may be decided that it's OK for this module to have multiple concurrent versions.

@@ -1,4 +1,5 @@
// @flow
import { registerEmotionModule } from '@emotion/utils'
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 only matters for @emotion/core package - don't think there is a much need for this in other packages


export function registerEmotionModule(name: string, version: string) {
if (versions[name] && versions[name] !== version) {
console.warn(
Copy link
Member

Choose a reason for hiding this comment

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

It's not only about different versions (but that too) - the problem might also be caused by requiring the same version twice (from different locations at disk).

I think react-router prepared this pretty much in a way that I'd like to have this included here, take a look:
https://github.com/ReactTraining/react-router/blob/e81dfa2d01937969ee3f9b1f33c9ddd319f9e091/packages/react-router/modules/index.js#L1

For that to work exactly like in their case we'd have to have process.env.ROLLUP_MODULE_FORMAT available - this could potentially be added in our build tool (preconstruct). I expect supporting this would only require changes around those lines:
https://github.com/preconstruct/preconstruct/blob/120f9b6d747e985ed917a898f13d7177fd74ef73/packages/cli/src/build/rollup.ts#L164-L168

Copy link
Contributor Author

@ajs139 ajs139 Dec 10, 2019

Choose a reason for hiding this comment

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

If we want to warn based on module type as well as version, we would want to split this up:
https://github.com/preconstruct/preconstruct/blob/c1cfc0503f73a197487d6cba6ada607fe957f527/packages/cli/src/build/config.ts#L132-L153
If we allowed the messaging to be more vague, then we wouldn't need to do an explicit check of the module type. For example, we could change the code to this:

if (__DEV__) {
  if (versions[name]) {
    if (versions[name] === version) {
      console.warn(
        `Package ${name}@${versions[name]} is already registered,` +
         `so things won't work right. This would only happen if you are `+
         `loading two different builds of ${name}.`
      );
    } else {
      console.warn(
        `Package ${name}@${versions[name]} is already registered.` +
        `Running ${version} may cause problems.`
      );
    }
  }
  versions[name] = version;
}

Copy link
Member

Choose a reason for hiding this comment

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

The goal of this is to warn about second copy of @emotion/core being loaded. It doesn't really matter what was the first version being loaded and it doesn't even matter what is the second one - we only need types of both to provide a helpful error message. I would limit this to a build type error - not sure if we need to complicate this with providing also an exact version.

@ajs139 ajs139 force-pushed the warn_multiple_versions branch from 5c548f3 to 2d82de4 Compare December 17, 2019 17:00
@codesandbox-ci
Copy link

codesandbox-ci bot commented Dec 17, 2019

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit b29b958:

Sandbox Source
Emotion Configuration

@ajs139 ajs139 force-pushed the warn_multiple_versions branch from 2d82de4 to 0a2cdcc Compare December 17, 2019 17:02
const instances: ModuleRegister = getGlobalInstances(globalContext)

export function registerEmotionModule(name: string) {
if (process.env.NODE_ENV !== 'production') {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

tried using babel-plugin-dev-expression, but it was causing failures when running the test suite.

@codecov
Copy link

codecov bot commented Dec 17, 2019

Codecov Report

Merging #1677 into next will increase coverage by <.01%.
The diff coverage is 100%.

Impacted Files Coverage Δ
packages/core/src/index.js 100% <100%> (ø)
packages/utils/src/index.js 100% <100%> (ø) ⬆️
packages/cache/src/index.js 98.9% <0%> (-0.03%) ⬇️
packages/sheet/src/index.js 100% <0%> (ø) ⬆️


const instances: ModuleRegister = getGlobalInstances(globalContext)

export function registerEmotionModule(name: string) {
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this abstraction is much needed, we only need this for a single module (@emotion/core), so I would prefer avoiding extra abstractions and just inlining this stuff to @emotion/core's index

I also don't think we really need tests for this - it's rather hard to break it and writing a proper test (without testing implementation details) for this thing would be rather brittle and complicated

@ajs139 ajs139 force-pushed the warn_multiple_versions branch from 0a2cdcc to 9955147 Compare December 19, 2019 16:16
const globalKey = '__EMOTION_CORE__'
const globalContext = isBrowser ? window : global

export function checkForMultipleVersions() {
Copy link
Member

Choose a reason for hiding this comment

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

this would have to be called to actually make it work :p however, calling this as a function could lead to this not being properly removed in production, so I would just move this function's content to core/src/index.js and this should be good to go 👍

@ajs139 ajs139 force-pushed the warn_multiple_versions branch from 9955147 to e275b57 Compare December 20, 2019 14:00
@ajs139 ajs139 force-pushed the warn_multiple_versions branch from e275b57 to 1410fcb Compare December 20, 2019 14:25
@Andarist Andarist requested a review from emmatown December 21, 2019 09:54
@Andarist
Copy link
Member

@ajs139 the only thing missing is an appropriate changeset, could you add it?

Copy link
Member

@emmatown emmatown left a comment

Choose a reason for hiding this comment

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

Other than adding a changeset, LGTM

@emmatown emmatown marked this pull request as ready for review December 24, 2019 02:31
@emmatown emmatown merged commit d62d910 into emotion-js:next Dec 24, 2019
louisgv pushed a commit to louisgv/emotion that referenced this pull request Sep 6, 2020
…) (emotion-js#1677)

* Add warning if multiple versions are running (addresses emotion-js#1470)

* Add changeset
@github-actions github-actions bot mentioned this pull request Nov 10, 2020
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.

3 participants