Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

import value is 'undefined' when using esm in an application that uses packages with esm #325

Closed
CacheControl opened this issue Mar 21, 2018 · 5 comments
Labels

Comments

@CacheControl
Copy link
Contributor

CacheControl commented Mar 21, 2018

This is a very weird bug I encountered this week with esm. I had a package that was on esm 3.0.6, which was installed in an application using esm (same 3.0.6 version). When running tests in my application, the import value of the package consistently resulted in undefined whenever run with nyc + mocha.

Thinking it was a special combination of mocha/nyc/esm, I created a repo to reproduce, and was able to get the behavior to happen with only mocha (no nyc), however this time it was seemingly random and would resolve itself after running the tests a few more times.

The repo is here, and includes a screen capture of the behavior and installation instructions. It only fails for me every so often, and randomly. You'll see in the screencap I edited package.json, because at the time I observed the problem goes away after changing a local file (I thought perhaps it was some kind of caching bug), however later on I realized that running the tests over and over again would eventually result in success. It may be my imagination, but the problem seems to occur more frequently when run through mocha + nyc, though again I can make it happen with mocha alone.

edit: I've reproduced this with both a standard npm install of the esm package, and when npm linking it.

Node version: v8.9.4
npm version: 5.7.1

@jdalton
Copy link
Member

jdalton commented Mar 21, 2018

Hi @CacheControl!

I can't seem to reproduce this using Node v8.10.0 (npm v5.7.1).
Are you on Windows?

Update:

I tested this on Mac OS and Windows 10 with Node v8.10.0 on both without issue
(though on Windows I removed the .npmrc file).

It looks like your example isn't really using nyc.
Is it missing some parts to be more representative of your issue?

@CacheControl
Copy link
Contributor Author

@jdalton I'm on OS X El Capitan (10.11.6)

nyc isn't required to reproduce the issue (can do with a simple npm test, though I seem to able to make it happen more consistently when running nyc npm test.

I committed that repo as-is immediately after taking the screencap

@jdalton
Copy link
Member

jdalton commented Mar 21, 2018

Ah nice, I got it on my second run with npx nyc npm test. Digging in!

Update:

This is interesting... the instrumented files by esm aren't changing from run to run so it's something else. What is loading is changing though. In working cases more is loaded. I see it fail one run, succeed another, fail the next, succeed after that, and so on.

Update:

It looks like something odd when reading the cache folder via readdir. Still digging...

Update:

Ok, it's the data map in the .data.json that is changing between runs. That data is what informs the loader of what is or isn't exported which is why when it's missing an entry it's reporting undefined. Now to find out why it's toggling state.

Update:

Ok, it's not merging old state with new state correctly.

Update:

Fixed it! Now to make super sure!

Update:

Patched 1c93540 and 6b8a605.

Besides fixing the issue this avoids parsing files known to have no transformations and makes the cache less chatty too 😋

Update:

v3.0.8 is released 🎉

@jdalton jdalton closed this as completed Mar 22, 2018
@jdalton jdalton added the bug label Mar 22, 2018
@jdalton
Copy link
Member

jdalton commented Mar 23, 2018

@CacheControl

This scenario caught 2 issues.
Would you be up for creating a simplified scenario test for esm?

@CacheControl
Copy link
Contributor Author

@jdalton heck yes I'm up for it!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

No branches or pull requests

2 participants