Skip to content

esbuild-plugin overrides injected variables #413

@ilijapuaca

Description

@ilijapuaca

Environment

What version are you running? Etc.

@sentry/esbuild-plugin@2.7.1, esbuild@0.19.4

Steps to Reproduce

This surfaced after upgrading to esbuild 0.19.0, but perhaps more widely spread and related to this issue?

When @sentry/esbuild-plugin is used, the built bundle would crash with ReferenceError: process is not defined when run. I spent some time comparing the output with and without Sentry plugin, here's the minimal repro setup:

build-config/build.js

import {sentryEsbuildPlugin} from '@sentry/esbuild-plugin';
import esbuild from 'esbuild';
import path from 'path';

const sentryPlugin = sentryEsbuildPlugin({
  org: 'foo',
  project: 'bar',
  authToken: 'token'
});

esbuild.build({
  bundle: true,
  entryPoints: [path.resolve(__dirname, '../src/index.ts')],
  outdir: path.resolve(__dirname, '../dist'),
  inject: [path.resolve(__dirname, './inject.ts')],
  plugins: [sentryPlugin],
  minify: false,
  sourcemap: true
});

build-config/inject.js

export const process = {cwd: () => '', env: {}};
export const global = globalThis;

src/index.ts

const FOO = process.env.FOO ? 'foo' : 'bar';
console.log(FOO);

Expected Result

Before esbuild 0.19 the output injection piece would look something like this:

...

  // build-config/inject.ts?sentryProxyModule=true
  var process, global;
  var init_inject = __esm({
    "build-config/inject.ts?sentryProxyModule=true"() {
      process = { cwd: () => "", env: {} };
      global = globalThis;
    }
  });

  // src/index.ts
  var require_src = __commonJS({
    "src/index.ts"() {
      init_inject();
      var FOO = process.env.FOO ? "foo" : "bar";
      console.log(FOO);
    }
  });

...

Actual Result

Instead, now it outputs something like this:

...

  // sentry-debug-id-stub:_sentry-debug-id-injection-stub?sentry-module-id=xxx
  var init_sentry_debug_id_injection_stub = __esm({
    "sentry-debug-id-stub:_sentry-debug-id-injection-stub?sentry-module-id=xxx"() {
      init_inject2();
      !function() {
        try {
          var e = "undefined" != typeof window ? window : "undefined" != typeof global ? global : "undefined" != typeof self ? self : {}, n = new Error().stack;
          n && (e._sentryDebugIds = e._sentryDebugIds || {}, e._sentryDebugIds[n] = "xxx", e._sentryDebugIdIdentifier = "sentry-dbid-xxx");
        } catch (e2) {
        }
      }();
    }
  });

  // build-config/inject.ts
  var init_inject = __esm({
    "build-config/inject.ts"() {
      init_inject2();
    }
  });

  // build-config/inject.ts?sentryProxyModule=true
  var init_inject2 = __esm({
    "build-config/inject.ts?sentryProxyModule=true"() {
      init_sentry_debug_id_injection_stub();
      init_inject();
      init_inject();
    }
  });
...

There seems to be some weird circular relationship between the inject functions, and the addition of init_sentry_debug_id_injection_stub which previously wasn't there. The piece causing the actual issue is the lack of var process, global; which seems to be gone altogether

Metadata

Metadata

Assignees

No one assigned

    Labels

    No fields configured for issues without a type.

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions