Unverified Commit 225e9246 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Fix module resolving error in tracer (#47786)

Fix the module resolving error found during development, because now few
app route code is bundled into RSC server layer, it can't resolve
opentelementry properly and will throw the error blow due to the check
of `require('@opentelemetry/api')` to see if it's installed.

```
Module not found: Can't resolve '@opentelemetry/api'
```

Thus I alias it to default `next/dist/compiled/@opentelemetry/api` when
it doesn't have external module

---------
parent 5c6b8ac3
......@@ -952,10 +952,10 @@ export default async function getBaseWebpackConfig(
const reactDir = path.dirname(require.resolve('react/package.json'))
const reactDomDir = path.dirname(require.resolve('react-dom/package.json'))
let hasOptionalOTELAPIPackage = false
let hasExternalOtelApiPackage = false
try {
require('@opentelemetry/api')
hasOptionalOTELAPIPackage = true
require.resolve('@opentelemetry/api')
hasExternalOtelApiPackage = true
} catch {}
const resolveConfig: webpack.Configuration['resolve'] = {
......@@ -999,12 +999,14 @@ export default async function getBaseWebpackConfig(
'next/dist/client/components/navigation',
[require.resolve('next/dist/client/components/headers')]:
'next/dist/client/components/headers',
'@opentelemetry/api': hasOptionalOTELAPIPackage
? '@opentelemetry/api'
: 'next/dist/compiled/@opentelemetry/api',
}
: undefined),
// For RSC server bundle
...(!hasExternalOtelApiPackage && {
'@opentelemetry/api': 'next/dist/compiled/@opentelemetry/api',
}),
...(config.images.loaderFile
? {
'next/dist/shared/lib/image-loader': config.images.loaderFile,
......
......@@ -6,9 +6,9 @@ import type {
SpanOptions,
Tracer,
AttributeValue,
} from '@opentelemetry/api'
} from 'next/dist/compiled/@opentelemetry/api'
let api: typeof import('@opentelemetry/api')
let api: typeof import('next/dist/compiled/@opentelemetry/api')
// we want to allow users to use their own version of @opentelemetry/api if they
// want to, so we try to require it first, and if it fails we fall back to the
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment