Skip to content

fix: Vercel ai ESM patching #16152

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

Merged
merged 4 commits into from
Apr 29, 2025
Merged

fix: Vercel ai ESM patching #16152

merged 4 commits into from
Apr 29, 2025

Conversation

timfish
Copy link
Collaborator

@timfish timfish commented Apr 28, 2025

With ESM patching you need to retain the original module and just overwrite the exports you want to wrap. This usually works for CJS too but unfortunately, the CJS exports of ai only have getters and no setters so this route is not possible.

This PR changes the patching so that it works slightly differently for ESM and CJS. The original code that outputs a newly created object is retained for CJS but for ESM we use the preferred route of replacing the required exports.

To detect whether the module we're patching is an ES module we check Object.prototype.toString.call(moduleExports) === '[object Module]' which is documented here.

This PR also adds an ESM test for ai.

Copy link
Contributor

github-actions bot commented Apr 28, 2025

size-limit report 📦

⚠️ Warning: Base artifact is not the latest one, because the latest workflow run is not done yet. This may lead to incorrect results. Try to re-run all tests to get up to date results.

Path Size % Change Change
@sentry/browser 23.28 KB - -
@sentry/browser - with treeshaking flags 23.1 KB - -
@sentry/browser (incl. Tracing) 36.93 KB - -
@sentry/browser (incl. Tracing, Replay) 74.1 KB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 67.47 KB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 78.76 KB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 90.58 KB - -
@sentry/browser (incl. Feedback) 39.68 KB - -
@sentry/browser (incl. sendFeedback) 27.9 KB - -
@sentry/browser (incl. FeedbackAsync) 32.67 KB - -
@sentry/react 25.09 KB - -
@sentry/react (incl. Tracing) 38.93 KB - -
@sentry/vue 27.5 KB - -
@sentry/vue (incl. Tracing) 38.69 KB - -
@sentry/svelte 23.31 KB - -
CDN Bundle 24.49 KB - -
CDN Bundle (incl. Tracing) 36.96 KB - -
CDN Bundle (incl. Tracing, Replay) 72 KB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 77.3 KB - -
CDN Bundle - uncompressed 71.5 KB - -
CDN Bundle (incl. Tracing) - uncompressed 109.33 KB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 220.62 KB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 233.15 KB - -
@sentry/nextjs (client) 40.51 KB - -
@sentry/sveltekit (client) 37.41 KB - -
@sentry/node 143.4 KB +0.03% +36 B 🔺
@sentry/node - without tracing 96.49 KB - -
@sentry/aws-serverless 120.82 KB - -

View base workflow run

@timfish timfish requested a review from AbhiPrasad April 28, 2025 18:20
@timfish timfish force-pushed the fix/vercel-ai-iitm-issue branch 2 times, most recently from 513e696 to f9b14f4 Compare April 28, 2025 18:35
@timfish timfish force-pushed the fix/vercel-ai-iitm-issue branch from f9b14f4 to d48395d Compare April 28, 2025 18:37
@timfish timfish changed the title fix: Vercel ai import-in-the-middle patching fix: Vercel ai ESM patching Apr 28, 2025
@timfish timfish marked this pull request as ready for review April 28, 2025 20:20
@timfish timfish requested a review from Lms24 April 29, 2025 08:36
Copy link
Member

@Lms24 Lms24 left a comment

Choose a reason for hiding this comment

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

Thanks for fixing this and for the great context around the root cause!

@Lms24
Copy link
Member

Lms24 commented Apr 29, 2025

hmm look slike CI is stuck for some reason 🤔 @timfish mind rebasing to see if this fixes CI? thx!

@timfish
Copy link
Collaborator Author

timfish commented Apr 29, 2025

Not sure how this made it onto master. Maybe a caching issue?

/home/runner/work/sentry-javascript/sentry-javascript/packages/core/src/mcp-server.ts
  1:1  error  Run autofix to sort these imports!  simple-import-sort/imports

@Lms24
Copy link
Member

Lms24 commented Apr 29, 2025

hmm maybe 🤔 we did enable import sorting again via #16134, so this is likely related

@timfish
Copy link
Collaborator Author

timfish commented Apr 29, 2025

No idea why remix-hydrogen e2e test is failing 🤷‍♂️

@Lms24
Copy link
Member

Lms24 commented Apr 29, 2025

hmm this looks unrelated indeed. Will take a look

update: yeah, it's also failing in other PRs. We'll fix this

@Lms24
Copy link
Member

Lms24 commented Apr 29, 2025

CI should be unblocked -- @timfish please rebase once more, thx!

@chargome chargome merged commit ffdd2dd into develop Apr 29, 2025
109 checks passed
@chargome chargome deleted the fix/vercel-ai-iitm-issue branch April 29, 2025 14:37
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.

Crash with Vercel AI SDK when used in ESM
3 participants