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

Prisma instrumentation no longer works on >5.9.0 #1984

Closed
bizob2828 opened this issue Jan 30, 2024 · 7 comments · Fixed by prisma/prisma#22880 or #1993
Closed

Prisma instrumentation no longer works on >5.9.0 #1984

bizob2828 opened this issue Jan 30, 2024 · 7 comments · Fixed by prisma/prisma#22880 or #1993
Assignees

Comments

@bizob2828
Copy link
Member

bizob2828 commented Jan 30, 2024

Description

Prisma just released 5.9.0. It looks like they restructured exports to where instrumenting @prisma/client is not sufficient.

Expected Behavior

Instrumentation of prisma 5.9.0+ will still function

Steps to Reproduce

cd test/versioned/prisma
npm i prisma@latest @prisma/client@5.9.0 --no-save --no-package-lock
node prisma.tap.js

Additional context

This will be an exercise in figuring out the appropriate paths to give to shimmer.registerInstrumentations. I've tried the following so far and it's very hard to determine the right paths. WIP branch

You can see the export changed

5.8.1 with debug on in require-in-the-middle

2024-01-30T16:54:52.135Z require-in-the-middle resolved filename to module: @prisma/client (id: @prisma/client, resolved: @prisma/client, basedir: /Users/revans/code/release/node-newrelic/test/versioned/prisma/node_modules/@prisma/client)
2024-01-30T16:54:52.136Z require-in-the-middle calling require hook: @prisma/client

5.9.0

2024-01-30T16:39:22.506Z require-in-the-middle resolved filename to module: @prisma/client (id: @prisma/client, resolved: @prisma/client/default, basedir: /Users/revans/code/pull-requests/node-newrelic/test/versioned/prisma/node_modules/@prisma/client)
2024-01-30T16:39:22.506Z require-in-the-middle ignoring require of non-main module file: /Users/revans/code/pull-requests/node-newrelic/test/versioned/prisma/node_modules/@prisma/client/index.js
@workato-integration
Copy link

@jsumners-nr
Copy link
Contributor

@millsp
Copy link

millsp commented Jan 30, 2024

In order to find out what went wrong, I created a minimal reproduction to try and understand how your tests were failing. Thanks to the context you left in this issue and elsewhere, I took a look at how you were resolving and injecting the Prisma Client. Then I came up with this reproduction:

// index.ts

import { Hook } from 'require-in-the-middle' 

new Hook(['@prisma/client'], {
}, function (exports, name) {
  console.log('loading %s', name) 

  return exports
})

require('@prisma/client')

With prisma@5.8.0 it produced the following output:

$ node -r esbuild-register index.ts

loading @prisma/client

With prisma@5.9.0 it produced no output at all:

$ node -r esbuild-register index.ts

Somehow according to Hook, nothing was loaded, and that's obviously not true. To investigate, I then passed internals: true as a Hook option, that surprisingly made it display something, so it seemed like I hit a bug or an edge case?

new Hook(['@prisma/client'], {
  internals: true
}, function (exports, name) {
  console.log('loading %s', name) 

  return exports
})
$ node -r esbuild-register index.ts

loading @prisma/client/runtime/library.js
loading @prisma/client/default.js

Further down, I tried to understand why this would be an edge case if other libraries like xstate or preact seemed to work just fine with Hook. After proceeding by elimination, I found one key difference between @prisma/client's exports map and other libs. In short:

The main field in @prisma/client was pointing to index.js while the exports was pointing the default to default.js. To my surprise this was the reason. I pointed main to default.js as well and Hook started working again. We do need to have different index.js and default.js files though.

@millsp
Copy link

millsp commented Jan 31, 2024

After taking a look at the source code of require-in-the-middle, it does expect the resolved exports and main to be the same unless you specify internals, so that confirms what I observed. Given this behavior, this is how I would do it:

import { Hook } from 'require-in-the-middle' 

async function main() {
  new Hook(['@prisma/client/default'], {
    internals: true
  }, function (exports, name, basedir) {
    console.log('loading %s', name) 

    return exports
  })

  require('@prisma/client')
}

void main()

Let me know if that works for you.

@bizob2828
Copy link
Member Author

@millsp thanks for taking time to triage and fix. We do not pass internals: true to require-in-the-middle for "reasons". However I was able to determine 5.10.0-integration-fix-client-exports-map.4 fixes our issues with no changes. do you plan on releasing this in 5.10.0? or a 5.9.1? How I tested was

cd test/versioned/prisma
npm i prisma@latest @prisma/client@5.10.0-integration-fix-client-exports-map.4 --no-save --no-package-lock
node prisma.tap.js

@millsp
Copy link

millsp commented Jan 31, 2024

Happy that we found a solution, and thanks for confirming the fix.
We are planning to release this via 5.9.1 once the PR is ready.

@bizob2828
Copy link
Member Author

This issue will serve to update the version ranges for our prisma versioned tests and just skip 5.9.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
3 participants