Coder Social home page Coder Social logo

Comments (8)

mdjermanovic avatar mdjermanovic commented on June 8, 2024 2

I can reproduce this locally. Thanks for the detailed analysis of the cause!

https://github.com/StewEucen/eslint-bug-with-jest?tab=readme-ov-file#cause

  • Retrier#retry() uses Promise declared in lib.es2015.iterable.d.ts
  • fs.readFile() uses Promise declared in lib.es5.d.ts

So it seems that in Node.js different operations may use different Promise constructors?

@nzakas perhaps @humanwhocodes/retry could check if the result is a thenable instead of checking result instanceof Promise?

from eslint.

nzakas avatar nzakas commented on June 8, 2024 2

I can update retry. Never occurred to me that there might be a promise from another realm involved.

from eslint.

nzakas avatar nzakas commented on June 8, 2024 2

A new version of @humanwhocodes/retry was released as a patch version that should fix this.

from eslint.

mdjermanovic avatar mdjermanovic commented on June 8, 2024 2

Here's a PR to update the dependency: #18416

from eslint.

fasttime avatar fasttime commented on June 8, 2024 1

The Promise object returned by fs.readFile is from a different realm:

image

This only occurs when the Node.js process is launched with the --experimental-vm-modules flag. It may have something to do with Jest's experimental ESM support, which is enabled by the flag.

from eslint.

StewEucen avatar StewEucen commented on June 8, 2024

@mdjermanovic

  • Thank you for your confirmation :)

So it seems that in Node.js different operations may use different Promise constructors?

  • I think that since fs is old package, it uses polyfill Promise class.

  • I have already found two ways to fix now.

    1. Change argument of #retry() function to async in ESLint repository.

      -                return retrier.retry(() => fs.readFile(...)
      +                return retrier.retry(async () => fs.readFile(...)
    2. Change the logic to confirm Promise in #retry() of @humanwhocodes/retry repository.

      -        if (!(result instanceof Promise)) {
      +        if (
      +          !(result instanceof Promise)
      +          && (!result || typeof result.then !== 'function')
      +        ) {
  • I think that 2. is a fixing ad-hoc, thus we can select 1.

from eslint.

StewEucen avatar StewEucen commented on June 8, 2024

@fasttime

  • Thank you for your information

  • I used --experimental-vm-modules on run Jest, it was due to the following reasons.
  1. I had used ESLint#lintFiles() in Jest with [email protected]. As we know, Jest works as CommonJS. But It did not require to use --experimental-vm-modules flag.

  2. I changed the code to fit Flat Config with 9.1.1, but it was not work. Because new code of ESLint#lintFiles() uses dynamic import.

    https://github.com/eslint/eslint/blob/v9.1.1/lib/eslint/eslint.js#L317

    async function loadFlatConfigFile(filePath) {
        ...
    
        const config = (await import(fileURL)).default;
    
        ...
    }

    https://github.com/eslint/eslint/blob/v9.1.1/lib/eslint/eslint.js#L369

    async function calculateConfigArray(eslint, { ... }) {
        ...
    
        if (configFilePath) {
            const fileConfig = await loadFlatConfigFile(configFilePath);
    
            ...
        }
    
        ...
    }

    https://github.com/eslint/eslint/blob/v9.1.1/lib/eslint/eslint.js#L815

    class ESLint {
        ...
    
        async lintFiles(patterns) {
            ...
    
            const configs = await calculateConfigArray(this, eslintOptions);
    
            ...
        }
    
        ...
    }
  3. To solve the problem, I added a --experimental-vm-modules flag for Jest script.

    https://github.com/StewEucen/eslint-bug-with-jest/blob/main/package.json#L7

    {
      "scripts": {
        "test": "export NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\"; jest --forceExit"
      },
    }
  4. And then, I got the behavior that I reported in this issue.

from eslint.

StewEucen avatar StewEucen commented on June 8, 2024

@nzakas @mdjermanovic @fasttime

  • Thank you for your work related to this issue.
  • I am going to use ESLint patched version for my repository.

from eslint.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.