Skip to content

Commit 85aa992

Browse files
authored
fix: Fix MV2 build sourcemap upload (#26467) (#26582)
This is a cherry-pick of #26467 for v12.0.6. Original description: ## **Description** The sourcemaps for MV2 builds (used for Firefox) were not being uploaded to Sentry at all. This resulted in invalid stack traces for Firefox error reports. The Sentry initiatization has been updated to use a `dist` option, letting us differentiate between different types of build for the same version. This is now used to signify which builds are mv2 and which are mv3. Both distributions are uploaded separately as part of the release process (for Flask and main builds; we don't have MV2 builds for MMI or beta). [![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/26467?quickstart=1) ## **Related issues** Fixes #26466 ## **Manual testing steps** Unfortunately I don't know of an easy way to test the CircleCI changes. Those will just have to be read carefully. We can test the application changes and the Sentry script changes though. The two outcomes we want to test are: * The source code and sourcemaps are uploaded correctly for both MV2 and MV3 builds * Application bundles for MV2 and MV3 builds are reporting errors properly tagged as being from an MV2 or MV3 build, and are mapped correctly in Sentry. Here are the steps I used to test this: * Setup a personal Sentry account with a `metamask` project * Create a Custom Integration in Sentry (Settings -> Custom Integrations) * Steps: * Navigate to "Settings -> Custom Integrations" on the Sentry dashboard for your personal Sentry account * Click the purple "Create New Integration" button on the top-right of the page * Select "Internal integration" * Provide any name, and grant it "Admin" access to "Releases" * Alternatively an Auth Token might work as well, but some of the commands used by our script to check for pre-existing releases seem to not work with the `org:ci` scope, which is the only scope available for Sentry auth tokens. * Checkout this branch * Bump the patch version in `package.json` (just in case you need to re-test, this is an easy way to separate old errors/builds from new ones) * Before creating builds, make sure that `SENTRY_DSN_DEV` is set to the DSN of your personal Sentry account's `metamask` project * Run `yarn dist:mv2` to create an MV2 build * Move it to the `dist-mv2` directory (`mv dist dist-mv2`) * Run `yarn dist` to create an MV3 build * Before uploading sourcemaps, make sure that `SENTRY_AUTH_TOKEN` is set to the Auth Token generated from the custom integration earlier. * Run `yarn sentry:publish --org [your organization]` to upload the MV3 build * Run `yarn sentry:publish --org [your organization] --dist mv2` to upload the MV2 build At this point, you should be able to see the releases on the Sentry dashboard along with the artifacts. Look in "Settings -> Projects -> Source Maps" for these. They are labeled by release number and dist. Now, load each build in your browser (one at a time, never both enabled at once) and follow these steps: * Proceed through onboarding, opting in to MetaMetrics * Navigate to the test-dapp and connect to it * Click the "INVALID TRANSACTION TYPE (NOT SUPPORTED)" button in the "Malformed Transactions" section of the test dapp, then reject the confirmation after it shows up. This should trigger an error in Sentry. * Look for the error in Sentry and ensure that the frame that shows `transactionController.updateSecurityAlertResponse(` is mapped to source code correctly (it should resolve to `app/scripts/lib/ppom/ppom-util.ts`). Also check that the `dist` is correct in the "Tags" section of the Sentry issue page. <details> <summary> Here are screenshots of what that looked like for me: </summary> ![Screenshot 2024-08-20 at 19 19 15](https://github.com/user-attachments/assets/36ca0422-71f0-456a-8f1f-5ac980aa5fea) ![Screenshot 2024-08-20 at 19 06 08](https://github.com/user-attachments/assets/25fd681f-c933-41d5-be0e-102fd16f54b2) </details> ## **Screenshots/Recordings** N/A ## **Pre-merge author checklist** - [x] I've followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Extension Coding Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md). - [x] I've completed the PR template to the best of my ability - [x] I’ve included tests if applicable - [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [x] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **Pre-merge reviewer checklist** - [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots.
1 parent 4e45b22 commit 85aa992

File tree

5 files changed

+53
-31
lines changed

5 files changed

+53
-31
lines changed

.circleci/config.yml

+6
Original file line numberDiff line numberDiff line change
@@ -1657,9 +1657,15 @@ jobs:
16571657
- run:
16581658
name: Publish main release to Sentry
16591659
command: yarn sentry:publish
1660+
- run:
1661+
name: Publish main MV2 release to Sentry
1662+
command: yarn sentry:publish --dist mv2
16601663
- run:
16611664
name: Publish Flask release to Sentry
16621665
command: yarn sentry:publish --build-type flask
1666+
- run:
1667+
name: Publish Flask MV2 release to Sentry
1668+
command: yarn sentry:publish --build-type flask --dist mv2
16631669
- run:
16641670
name: Publish MMI release to Sentry
16651671
command: yarn sentry:publish --build-type mmi

app/scripts/lib/setupSentry.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as Sentry from '@sentry/browser';
22
import { Dedupe, ExtraErrorData } from '@sentry/integrations';
33
import browser from 'webextension-polyfill';
4+
import { isManifestV3 } from '../../../shared/modules/mv3.utils';
45
import { FilterEvents } from './sentry-filter-events';
56
import extractEthjsErrorMessage from './extractEthjsErrorMessage';
67

@@ -191,6 +192,7 @@ export default function setupSentry({ release, getState }) {
191192
Sentry.init({
192193
dsn: sentryTarget,
193194
debug: METAMASK_DEBUG,
195+
dist: isManifestV3 ? 'mv3' : 'mv2',
194196
/**
195197
* autoSessionTracking defaults to true and operates by sending a session
196198
* packet to sentry. This session packet does not appear to be filtered out

development/sentry-publish.js

+15-23
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,15 @@ async function start() {
3737
default: 0,
3838
description: 'The MetaMask extension build version',
3939
type: 'number',
40+
})
41+
.option('dist', {
42+
description:
43+
'The MetaMask extension build distribution (typically for MV2 builds, omit for MV3)',
44+
type: 'string',
4045
}),
4146
);
4247

43-
const { buildType, buildVersion, org, project } = argv;
48+
const { buildType, buildVersion, dist, org, project } = argv;
4449

4550
process.env.SENTRY_ORG = org;
4651
process.env.SENTRY_PROJECT = project;
@@ -75,19 +80,17 @@ async function start() {
7580
]);
7681
}
7782

78-
// check if version has artifacts or not
79-
const versionHasArtifacts =
80-
versionAlreadyExists && (await checkIfVersionHasArtifacts(version));
81-
if (versionHasArtifacts) {
82-
console.log(
83-
`Version "${version}" already has artifacts on Sentry, skipping sourcemap upload`,
84-
);
85-
return;
86-
}
87-
8883
const additionalUploadArgs = [];
84+
if (dist) {
85+
additionalUploadArgs.push('--dist', dist);
86+
}
8987
if (buildType !== loadBuildTypesConfig().default) {
90-
additionalUploadArgs.push('--dist-directory', `dist-${buildType}`);
88+
additionalUploadArgs.push(
89+
'--dist-directory',
90+
dist ? `dist-${buildType}-${dist}` : `dist-${buildType}`,
91+
);
92+
} else if (dist) {
93+
additionalUploadArgs.push('--dist-directory', `dist-${dist}`);
9194
}
9295
// upload sentry source and sourcemaps
9396
await runInShell('./development/sentry-upload-artifacts.sh', [
@@ -109,17 +112,6 @@ async function checkIfVersionExists(version) {
109112
);
110113
}
111114

112-
async function checkIfVersionHasArtifacts(version) {
113-
const [artifact] = await runCommand('sentry-cli', [
114-
'releases',
115-
'files',
116-
version,
117-
'list',
118-
]);
119-
// When there's no artifacts, we get a response from the shell like this ['', '']
120-
return artifact?.length > 0;
121-
}
122-
123115
async function doesNotFail(asyncFn) {
124116
try {
125117
await asyncFn();

development/sentry-upload-artifacts.sh

+15-2
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,23 @@ Upload JavaScript bundles and sourcemaps to Sentry
2323
Options:
2424
-h, --help Show help text
2525
-r, --release <release> Sentry release to upload files to (defaults to 'VERSION' environment variable)
26+
-d, --dist <dist> Sentry distribution (typically used to identify MV2 builds)
2627
--dist-directory <path> The 'dist' directory to use. Defaults to 'dist'.
2728
EOF
2829
}
2930

3031
function upload_sourcemaps {
3132
local release="${1}"; shift
3233
local dist_directory="${1}"; shift
34+
local dist="${1}"; shift
3335

34-
sentry-cli releases files "${release}" upload-sourcemaps "${dist_directory}"/chrome/ "${dist_directory}"/sourcemaps/ --rewrite --url-prefix '/metamask'
36+
sentry-cli releases files "${release}" upload-sourcemaps --dist "${dist}" "${dist_directory}"/chrome/ "${dist_directory}"/sourcemaps/ --rewrite --url-prefix '/metamask'
3537
}
3638

3739
function main {
3840
local release=VERSION
3941
local dist_directory='dist'
42+
local dist="mv3"
4043

4144
while :; do
4245
case "${1-default}" in
@@ -54,6 +57,16 @@ function main {
5457
release="${2}"
5558
shift
5659
;;
60+
-d|--dist)
61+
if [[ -z $2 ]]
62+
then
63+
printf "'dist' option requires an argument.\\n" >&2
64+
printf '%s\n' "${__SEE_HELP_MESSAGE__}" >&2
65+
exit 1
66+
fi
67+
dist="${2}"
68+
shift
69+
;;
5770
--dist-directory)
5871
if [[ -z $2 ]]
5972
then
@@ -83,7 +96,7 @@ function main {
8396
fi
8497

8598
printf 'uploading source files and sourcemaps for Sentry release "%s"...\n' "${release}"
86-
upload_sourcemaps "${release}" "${dist_directory}"
99+
upload_sourcemaps "${release}" "${dist_directory}" "${dist}"
87100
printf 'all done!\n'
88101
}
89102

shared/modules/mv3.utils.js

+15-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
1-
import browser from 'webextension-polyfill';
1+
const runtimeManifest =
2+
global.chrome?.runtime.getManifest() || global.browser?.runtime.getManifest();
23

34
/**
4-
* A boolean indicating whether the manifest of the current extension
5-
* is set to manifest version 3.
5+
* A boolean indicating whether the manifest of the current extension is set to manifest version 3.
6+
*
7+
* We have found that when this is run early in a service worker process, the runtime manifest is
8+
* unavailable. That's why we have a fallback using the ENABLE_MV3 constant. The fallback is also
9+
* used in unit tests.
610
*/
7-
export const isManifestV3 =
8-
browser.runtime.getManifest().manifest_version === 3;
11+
export const isManifestV3 = runtimeManifest
12+
? runtimeManifest.manifest_version === 3
13+
: // Our build system sets this as a boolean, but in a Node.js context (e.g. unit tests) it will
14+
// always be a string
15+
process.env.ENABLE_MV3 === true ||
16+
process.env.ENABLE_MV3 === 'true' ||
17+
process.env.ENABLE_MV3 === undefined;
918

1019
/**
1120
* A boolean indicating whether the browser supports the offscreen document api.
1221
* This is only available in when the manifest is version 3, and only in chromium
1322
* versions 109 and higher. As of June 7, 2024, it is not available in firefox.
1423
*/
15-
export const isOffscreenAvailable = Boolean(browser.offscreen);
24+
export const isOffscreenAvailable = Boolean(global.chrome?.offscreen);
1625

1726
/**
1827
* A boolean indicating whether the current extension's manifest is version 3

0 commit comments

Comments
 (0)