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

Test Visibility Github Action via dd-trace-js is failing due to import-in-the-middle dependency #4713

Open
karrui opened this issue Sep 23, 2024 · 13 comments
Assignees
Labels
bug Something isn't working ci-app

Comments

@karrui
Copy link

karrui commented Sep 23, 2024

Might be related to this NewRelic issue: newrelic/node-newrelic#1984

Using the latest Prisma v5.18.0 results in this error when running vitest tests in Github Action (with datadog/test-visibility-github-action@v1):

(node:2572) Error: 'import-in-the-middle' failed to wrap 
'file:///home/runner/work/<project>/node_modules/@prisma/client/default.js'

Same error also happens when running locally, so it is not the action causing this.

Unsure if this is a fix on dd-trace's end, import-in-the-middle, or Prisma.

Now back testing to see which version first caused dd-trace to stop working.
EDIT: First apeared in v5.11.0 of Prisma.

This error does not happen in v5.10.2 of Prisma.

karrui added a commit to opengovsg/isomer that referenced this issue Sep 23, 2024
already created an issue to track why higher versions fail.
see DataDog/dd-trace-js#4713
@juan-fernandez juan-fernandez self-assigned this Sep 23, 2024
@juan-fernandez
Copy link
Collaborator

hey! Thanks for reporting @karrui. I'll try to reproduce and let you know.

What vitest version are you using?

@karrui
Copy link
Author

karrui commented Sep 23, 2024

What vitest version are you using?

We are using vitest v2.1.1 (latest)

karrui added a commit to opengovsg/isomer that referenced this issue Sep 23, 2024
already created an issue to track why higher versions fail.
see DataDog/dd-trace-js#4713
@juan-fernandez
Copy link
Collaborator

What vitest version are you using?

We are using vitest v2.1.1 (latest)

hey @karrui, I can't seem to be able to reproduce with a simple prisma project:

import { describe, it, expect, beforeAll, afterAll } from 'vitest'
import { PrismaClient } from '@prisma/client'

const prisma = new PrismaClient();

describe('User Model', () => {
  beforeAll(async () => {
    // Clean up the database before running tests
    await prisma.user.deleteMany();
  });

  afterAll(async () => {
    // Disconnect Prisma Client after tests are done
    await prisma.$disconnect();
  });

  it('should create a new user', async () => {
    const user = await prisma.user.create({
      data: {
        name: 'Alice',
        email: '[email protected]',
      },
    });

    expect(user).toHaveProperty('id');
    expect(user.name).toBe('Alice');
    expect(user.email).toBe('[email protected]');
  });
  "devDependencies": {
    "@types/node": "^22.5.5",
    "dd-trace": "^5.22.0",
    "prisma": "^5.19.1",
    "ts-node": "^10.9.2",
    "typescript": "^5.6.2",
    "vitest": "^2.1.1"
  },
  "dependencies": {
    "@prisma/client": "^5.19.1"
  },

running with

NODE_OPTIONS='-r dd-trace/ci/init --import dd-trace/register.js' DD_SERVICE=testing-prisma-vitest npm test

which is what datadog/test-visibility-github-action uses.

Test seems to run correctly:

➜  prisma-example NODE_OPTIONS='-r dd-trace/ci/init --import dd-trace/register.js' DD_SERVICE=testing-prisma-vitest npm test

> [email protected] test
> vitest run


 RUN  v2.1.1 /Users/juan.fernandezdealba/Desktop/dev/prisma-example

 ✓ tests/user.test.js (1)
   ✓ User Model (1)
     ✓ should create a new user

 Test Files  1 passed (1)
      Tests  1 passed (1)
   Start at  17:48:56
   Duration  2.59s (transform 18ms, setup 0ms, collect 27ms, tests 12ms, environment 0ms, prepare 470ms)


What could I be missing?

@juan-fernandez juan-fernandez added the bug Something isn't working label Sep 23, 2024
@karrui
Copy link
Author

karrui commented Sep 27, 2024

Interestingly, after updating Prisma to v5.20.0, the error code also changed.
Seems unrelated!
Our project is open sourced, you may want to pull and test on your end.

Running the series of commands in apps/studio on my local machine:

npm i
npm run generate
NODE_OPTIONS='-r dd-trace/ci/init --import dd-trace/register.js' DD_SERVICE=isomer npm run test-ci:unit

results in the following error

> [email protected] test-ci:unit
> vitest run

/project/path/isomer/node_modules/import-in-the-middle/lib/register.js:20
    return getters.get(target)[name]()
                                    ^

TypeError: getters.get(...)[name] is not a function
    at Object.get (/project/path/isomer/node_modules/import-in-the-middle/lib/register.js:20:37)
    at /project/path/isomer/node_modules/dd-trace/packages/datadog-instrumentations/src/helpers/hook.js:41:40
    at callHookFn (/project/path/isomer/node_modules/import-in-the-middle/index.js:29:22)
    at Hook._iitmHook (/project/path/isomer/node_modules/import-in-the-middle/index.js:150:11)
    at /project/path/isomer/node_modules/import-in-the-middle/lib/register.js:37:31
    at Array.forEach (<anonymous>)
    at register (/project/path/isomer/node_modules/import-in-the-middle/lib/register.js:37:15)
    at file:///project/path/isomer/node_modules/vitest/dist/chunks/base.BlXpj3e_.js?iitm=true:122:1
    at ModuleJob.run (node:internal/modules/esm/module_job:222:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:323:24)

Node.js v20.12.2

EDIT: Opened a PR to hopefully fix that import-in-the-middle regression. However, that does not fix the original prisma issue.

@karrui
Copy link
Author

karrui commented Sep 27, 2024

This does not seem to be isolated to that repository; another template repository my organisation uses for a bunch of applications is also affected. Only started today with no changes to the test command!

See the recent commit checks https://github.com/opengovsg/starter-kit/commits/main/ failing on the CI / Unit & integration tests (push) steps (but not failing on the E2E test steps, indicating Vitest incompatibility)

EDIT: Traced to import-in-the-middle v1.11.1 update as stated in the following comment

@karrui
Copy link
Author

karrui commented Sep 27, 2024

Suspect this is due to import-in-the-middle's latest v1.11.1 update https://github.com/nodejs/import-in-the-middle/releases

EDIT: Confirmed due to v1.11.1 update. Different issue than the one in the beginning, but this might be more important to fix.

Overridden dependency for dd-trace in package.json:

  "overrides": {
    "dd-trace": {
      "import-in-the-middle": "1.11.0"
    }
  }

All tests pass (while using Prisma v5.10.2)

@juan-fernandez
Copy link
Collaborator

thanks for the investigation! That's really helpful. As a quicker fix I pushed #4732

@timfish
Copy link

timfish commented Sep 30, 2024

[email protected] has now been released

@juan-fernandez
Copy link
Collaborator

@karrui a new release just went out with your iitm fix: https://github.com/DataDog/dd-trace-js/releases/tag/v5.23.0

this fixes your second issue but I believe you're still facing the first one? Or am I wrong?

@karrui
Copy link
Author

karrui commented Oct 1, 2024

Will update and test to see if the first issue still happens

@karrui
Copy link
Author

karrui commented Oct 3, 2024

Confirming that latest dd-trace still has issues with iitm and Prisma (version v5.20.0); unsure why.

(node:95423) Error: 'import-in-the-middle' failed to wrap 'file:///path/to/project/node_modules/@prisma/client/default.js'

To replicate:

Please pull https://github.com/opengovsg/starter-kit, and run

npm i

npm i prisma@latest @prisma/client@latest

npm i dd-trace

npm run generate

NODE_OPTIONS='-r dd-trace/ci/init --import dd-trace/register.js' npm run test-dev:vitest

@juan-fernandez
Copy link
Collaborator

hey @karrui thanks, I can reproduce. There seems to be a bug in import-in-the-middle. It seems the problematic file is in node_modules/.prisma/client/default.js. If you change from the original content

module.exports = { ...require('#main-entry-point') }

to

const result = require('#main-entry-point')

module.exports = { ...result }

the vitest command runs smoothly and reports to datadog's test visibility.

We'll check this internally and I'll also create an issue in import-in-the-middle 😄

@juan-fernandez
Copy link
Collaborator

FYI: I've created nodejs/import-in-the-middle#157, in case you want to follow it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working ci-app
Projects
None yet
Development

No branches or pull requests

3 participants