microsoft / microsoft/ApplicationInsights-JS

[BUG] `TypeError: Cannot redefine property: name` when using esbuild with `keepNames: true`, preventing VSCode extension telemetry from being sent

Open
#2,694 8 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
685
Forks
261
Avg merge
21h 33m
Merged PRs (30d)
5

Description

**Description/Screenshot**

Discovered from https://github.com/microsoft/vscode-containers/issues/367 originally. Related issues:
- https://github.com/microsoft/vscode-extension-telemetry/issues/237
- https://github.com/microsoft/vscode-extension-telemetry/issues/150
- This repo: #2523
- This repo: #2643

Quoting my original issue here, but please look there for the most up-to-date:

> Boring analysis: the reason it was happening was because we're using `keepNames` in esbuild, to make it not mangle class/function names (which we need if we want useful call stacks). Esbuild writes code into the bundle that, at load time (which is run time), tries to set the `name` property on the minified classes/functions to the original name.
>
> Some of the AppInsights packages are freezing some of their stuff, which makes it impossible to set the `name` property. The error that was thrown (and then caught and eaten) would resemble:
>
> ```
> TypeError: Cannot redefine property: name
> at defineProperty ()
> at __name (d:\\vscode-containers\\dist\\extension.bundle.js:8:33)
> at node_modules/@microsoft/1ds-core-js/dist-es5/AppInsightsCore.js (d:\\vscode-containers\\dist\\extension.bundle.js:16157:5)
> at __init (d:\\vscode-containers\\dist\\extension.bundle.js:10:56)
> at node_modules/@microsoft/1ds-core-js/dist-es5/Index.js (d:\\vscode-containers\\dist\\extension.bundle.js:16819:5)
> at __init (d:\\vscode-containers\\dist\\extension.bundle.js:10:56)
> at d:\\vscode-containers\\dist\\extension.bundle.js:20303:57
> at async getAICore (d:\\vscode-containers\\dist\\extension.bundle.js:20303:21)
> at async oneDataSystemClientFactory (d:\\vscode-containers\\dist\\extension.bundle.js:20343:29)
> ```
>
> The end result was that loading the AppInsights packages failed silently, and the AppInsights reporter never got built--so when we logged telemetry events, they were just screamed into the void.
>
> I considered three options. The first was to build the extension as ESM, but unfortunately this just didn't work at all.
>
> The second was to turn off `keepNames`...but that would negatively impact both telemetry and the issue reporter by giving us mangled names that we would have to translate after-the-fact if we wanted to investigate.
>
> The third was Opus' idea (nice job AI!). Basically, we build the `@vscode/extension-telemetry` package into its own bundle, with `keepNames` off, and then build everything else (extension and other dependencies) into the main bundle, with an alias to rewrite imports to `@vscode/extension-telemetry` to point to the other bundle.

**Steps to Reproduce**
1. Clone and `npm install` from https://github.com/microsoft/vscode-containers/tree/v2.4.0
2. In launch.json, change `DEBUGTELEMETRY` env var to 0
3. F5 and do anything in the Container Tools extension

- OS/Browser: Any OS
- SDK Version [e.g. 22]: (Below)
- How you initialized the SDK:
`@vscode/extension-telemetry@0.9.9`
`@microsoft/1ds-core-js@4.3.10`
`@microsoft/1ds-post-js@4.3.10`
`@microsoft/applicationinsights-web-basic@3.3.10`

**Expected behavior**
Works with ESBuild without issues

Contributor guide

Open the contributing guide

Research direction

Reproduce the failure using the linked vscode-containers v2.4.0 steps and inspect the esbuild configuration involving keepNames and the listed telemetry packages. Trace how @vscode/extension-telemetry is bundled and loaded; done means the Cannot redefine property: name error no longer prevents the AppInsights reporter from being built and telemetry events are sent.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
build-system, observability
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.