getsentry / sentry-javascript

Official Sentry SDKs for JavaScript
https://sentry.io
MIT License
7.76k stars 1.52k forks source link

urlPrefix not woking in upload sourcemap #5072

Closed panghaoyuan closed 2 years ago

panghaoyuan commented 2 years ago

Is there an existing issue for this?

How do you use Sentry?

Self-hosted/on-premise

Which package are you using?

@sentry/nextjs

SDK Version

6.19.7

Framework Version

No response

Link to Sentry event

No response

Steps to Reproduce

  1. Add usePrefix setting in next.config.js file

// next.config.js

/**
 * @description For all available options, see
 * @see https://github.com/getsentry/sentry-webpack-plugin#options
 * */
const SentryWebpackPluginOptions = {
  silent: true,
  ignore: ['node_modules'],
  urlPrefix: '~/yuyang.m',
};

module.exports = withSentryConfig(moduleExports, SentryWebpackPluginOptions)
  1. Build NextJS to upload sourcemaps to Sentry server
    $ next build

Expected Result

The file path uploaded to the archive must start with ~/yuyang.m/_next.

Actual Result

The file path uploaded to the archive starts with ~/_next.

panghaoyuan commented 2 years ago

source code url

i am sure this bug is caused here But I don't know how to fixed it

/**
 * Combine default and user-provided SentryWebpackPlugin options, accounting for whether we're building server files or
 * client files.
 *
 * @param buildContext Nexjs-provided data about the current build
 * @param userPluginOptions User-provided SentryWebpackPlugin options
 * @returns Final set of combined options
 */
export function getWebpackPluginOptions(
  buildContext: BuildContext,
  userPluginOptions: Partial<SentryWebpackPluginOptions>,
): SentryWebpackPluginOptions {
  const { buildId, isServer, webpack, config: userNextConfig, dev: isDev, dir: projectDir } = buildContext;
  const distDir = userNextConfig.distDir ?? '.next'; // `.next` is the default directory

  const isWebpack5 = webpack.version.startsWith('5');
  const isServerless = userNextConfig.target === 'experimental-serverless-trace';
  const hasSentryProperties = fs.existsSync(path.resolve(projectDir, 'sentry.properties'));
  const urlPrefix = userNextConfig.basePath ? `~${userNextConfig.basePath}/_next` : '~/_next';

  const serverInclude = isServerless
    ? [{ paths: [`${distDir}/serverless/`], urlPrefix: `${urlPrefix}/serverless` }]
    : [{ paths: [`${distDir}/server/pages/`], urlPrefix: `${urlPrefix}/server/pages` }].concat(
        isWebpack5 ? [{ paths: [`${distDir}/server/chunks/`], urlPrefix: `${urlPrefix}/server/chunks` }] : [],
      );

  const clientInclude = userNextConfig.sentry?.widenClientFileUpload
    ? [{ paths: [`${distDir}/static/chunks`], urlPrefix: `${urlPrefix}/static/chunks` }]
    : [{ paths: [`${distDir}/static/chunks/pages`], urlPrefix: `${urlPrefix}/static/chunks/pages` }];

  const defaultPluginOptions = dropUndefinedKeys({
    include: isServer ? serverInclude : clientInclude,
    ignore:
      isServer || !userNextConfig.sentry?.widenClientFileUpload
        ? []
        : // Widening the upload scope is necessarily going to lead to us uploading files we don't need to (ones which
          // don't include any user code). In order to lessen that where we can, exclude the internal nextjs files we know
          // will be there.
          ['framework-*', 'framework.*', 'main-*', 'polyfills-*', 'webpack-*'],
    url: process.env.SENTRY_URL,
    org: process.env.SENTRY_ORG,
    project: process.env.SENTRY_PROJECT,
    authToken: process.env.SENTRY_AUTH_TOKEN,
    configFile: hasSentryProperties ? 'sentry.properties' : undefined,
    stripPrefix: ['webpack://_N_E/'],
    urlPrefix,
    entries: (entryPointName: string) => shouldAddSentryToEntryPoint(entryPointName, isServer),
    release: getSentryRelease(buildId),
    dryRun: isDev,
  });

  checkWebpackPluginOverrides(defaultPluginOptions, userPluginOptions);

  return { ...defaultPluginOptions, ...userPluginOptions };
}
panghaoyuan commented 2 years ago

here has the same bug 4835

lforst commented 2 years ago

Hi @panghaoyuan, we do not recommend overriding the urlPrefix option in the next SDK (see here). Can you share with us what you are trying to achieve?

Also, can you check if next's basePath option better fit your use case? The Sentry next.js SDK sets the urlPrefix option by looking at next's basePath setting. You can find the logic for that here: https://github.com/getsentry/sentry-javascript/blob/c518955b774e5bbc36e7f1a22410703469911a2a/packages/nextjs/src/config/webpack.ts#L290

panghaoyuan commented 2 years ago

@lforst

I think i set bastPath is not good. my siteUrl is www.xxx.com if i set basePath. my siteUrl will be change. such as www.xxx.com => www.xxx.com/[basePath] www.xxx.com/homePage => www.xxx.com/[basePath]/homePage

if i am not set urlPrefix. sentry ARTIFACT is ~/_next/server/chunks/1082.js my sourcemap is useless in sentry.

i set urlPrefix in next.config.js because I used CDN to deal with static resources. such as https://cdn_site/yuyang.m/_next/static/chunks/pages/_app-269c4298b3da0c34.js yuyang.m is my project name

if my senry ARTIFACT is ~/yuyang.m/_next/server/chunks/1082.js. sentry sourcemap is worked.

I change next config. JS solves this problem,but i think it not a good idea.

const SentryWebpackPluginOptions = {
  include: [
    { paths: ['.next/static/'], urlPrefix: '~/yuyang.m/_next/static' },
    {
      paths: ['.next/server/'],
      urlPrefix: '~/yuyang.m/_next/server',
    },
  ],
  silent: true,
  ignore: ['node_modules'],
};
github-actions[bot] commented 2 years ago

This issue has gone three weeks without activity. In another week, I will close it.

But! If you comment or otherwise update it, I will reset the clock, and if you label it Status: Backlog or Status: In Progress, I will leave it alone ... forever!


"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀

tomas-c commented 1 year ago

If this issue is solved it would fix https://github.com/getsentry/sentry-javascript/issues/4174 which is affecting me too