Skip to content

feat(remix): Add debugid injection and map deletion to sourcemaps script #8814

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/remix/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"access": "public"
},
"dependencies": {
"@sentry/cli": "2.2.0",
"@sentry/cli": "2.20.5",
"@sentry/core": "7.66.0",
"@sentry/node": "7.66.0",
"@sentry/react": "7.66.0",
Expand Down
18 changes: 15 additions & 3 deletions packages/remix/scripts/createRelease.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
/* eslint-disable no-console */
const SentryCli = require('@sentry/cli');

const { deleteSourcemaps } = require('./deleteSourcemaps');

const sentry = new SentryCli();

async function createRelease(argv, DEFAULT_URL_PREFIX, DEFAULT_BUILD_PATH) {
async function createRelease(argv, URL_PREFIX, BUILD_PATH) {
const RELEASE = argv.release || (await sentry.releases.proposeVersion());
const URL_PREFIX = argv.urlPrefix || DEFAULT_URL_PREFIX;
const BUILD_PATH = argv.buildPath || DEFAULT_BUILD_PATH;

await sentry.releases.new(RELEASE);

await sentry.releases.uploadSourceMaps(RELEASE, {
urlPrefix: URL_PREFIX,
include: [BUILD_PATH],
useArtifactBundle: !argv.disableDebugIds,
});

await sentry.releases.finalize(RELEASE);

if (argv.deleteAfterUpload) {
try {
deleteSourcemaps(BUILD_PATH);
} catch (error) {
console.warn(`[sentry] Failed to delete sourcemaps in build directory: ${BUILD_PATH}`);
console.error(error);
}
}
}

module.exports = {
Expand Down
22 changes: 22 additions & 0 deletions packages/remix/scripts/deleteSourcemaps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* eslint-disable no-console */
const fs = require('fs');
const path = require('path');

const glob = require('glob');

function deleteSourcemaps(buildPath) {
console.info(`[sentry] Deleting sourcemaps from ${buildPath}`);

// Delete all .map files in the build folder and its subfolders
const mapFiles = glob.sync('**/*.map', { cwd: buildPath });

mapFiles.forEach(file => {
fs.unlinkSync(path.join(buildPath, file));

console.info(`[sentry] Deleted ${file}`);
});
}

module.exports = {
deleteSourcemaps,
};
19 changes: 19 additions & 0 deletions packages/remix/scripts/injectDebugId.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* eslint-disable no-console */
const { execSync } = require('child_process');

const SentryCli = require('@sentry/cli');

function injectDebugId(buildPath) {
const cliPath = SentryCli.getPath();

try {
execSync(`${cliPath} sourcemaps inject ${buildPath}`);
} catch (error) {
console.warn('[sentry] Failed to inject debug ids.');
console.error(error);
}
}

module.exports = {
injectDebugId,
};
22 changes: 21 additions & 1 deletion packages/remix/scripts/sentry-upload-sourcemaps.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const yargs = require('yargs');

const { createRelease } = require('./createRelease');
const { injectDebugId } = require('./injectDebugId');

const DEFAULT_URL_PREFIX = '~/build/';
const DEFAULT_BUILD_PATH = 'public/build';
Expand All @@ -24,16 +25,35 @@ const argv = yargs(process.argv.slice(2))
describe: 'The path to the build directory',
default: DEFAULT_BUILD_PATH,
})
.option('disableDebugIds', {
type: 'boolean',
describe: 'Disable the injection and upload of debug ids',
default: false,
})
.option('deleteAfterUpload', {
type: 'boolean',
describe: 'Delete sourcemaps after uploading',
default: true,
})
.usage(
'Usage: $0\n' +
' [--release RELEASE]\n' +
' [--urlPrefix URL_PREFIX]\n' +
' [--buildPath BUILD_PATH]\n\n' +
' [--disableDebugIds true|false]\n\n' +
' [--deleteAfterUpload true|false]\n\n' +
'This CLI tool will upload sourcemaps to Sentry for the given release.\n' +
'It has defaults for URL prefix and build path for Remix builds, but you can override them.\n\n' +
'If you need a more advanced configuration, you can use `sentry-cli` instead.\n' +
'https://github.com/getsentry/sentry-cli',
)
.wrap(120).argv;

createRelease(argv, DEFAULT_URL_PREFIX, DEFAULT_BUILD_PATH);
const buildPath = argv.buildPath || DEFAULT_BUILD_PATH;
const urlPrefix = argv.urlPrefix || DEFAULT_URL_PREFIX;

if (!argv.disableDebugIds) {
injectDebugId(buildPath);
}

createRelease(argv, urlPrefix, buildPath);
11 changes: 7 additions & 4 deletions packages/remix/test/scripts/upload-sourcemaps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ describe('createRelease', () => {
expect(uploadSourceMapsMock).toHaveBeenCalledWith('0.1.2.3', {
urlPrefix: '~/build/',
include: ['public/build'],
useArtifactBundle: true,
});
expect(finalizeMock).toHaveBeenCalledWith('0.1.2.3');
});
Expand All @@ -48,15 +49,16 @@ describe('createRelease', () => {
expect(uploadSourceMapsMock).toHaveBeenCalledWith('0.1.2.3.4', {
urlPrefix: '~/build/',
include: ['public/build'],
useArtifactBundle: true,
});
expect(finalizeMock).toHaveBeenCalledWith('0.1.2.3.4');
});

it('should use given buildPath and urlPrefix over the defaults when given.', async () => {
await createRelease(
{
urlPrefix: '~/build/subfolder',
buildPath: 'public/build/subfolder',
urlPrefix: '~/build/',
buildPath: 'public/build',
},
'~/build/',
'public/build',
Expand All @@ -65,8 +67,9 @@ describe('createRelease', () => {
expect(proposeVersionMock).toHaveBeenCalled();
expect(newMock).toHaveBeenCalledWith('0.1.2.3.4');
expect(uploadSourceMapsMock).toHaveBeenCalledWith('0.1.2.3.4', {
urlPrefix: '~/build/subfolder',
include: ['public/build/subfolder'],
urlPrefix: '~/build/',
include: ['public/build'],
useArtifactBundle: true,
});
expect(finalizeMock).toHaveBeenCalledWith('0.1.2.3.4');
});
Expand Down
11 changes: 5 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4428,14 +4428,13 @@
unplugin "1.0.1"
webpack-sources "3.2.3"

"@sentry/cli@2.2.0":
version "2.2.0"
resolved "https://registry.yarnpkg.com/@sentry/cli/-/cli-2.2.0.tgz#0cf4d529d87e290dea54d7e58fa5ff87ea200e4e"
integrity sha512-ywFtB8VHyWN248LuM67fsRtdMLif/SOHYY3zyef5WybvnAmRLDmGTWK//hSUCebsHBpehRIkmt4iMiyUXwgd5w==
"@sentry/cli@2.20.5":
version "2.20.5"
resolved "https://registry.yarnpkg.com/@sentry/cli/-/cli-2.20.5.tgz#255a5388ca24c211a0eae01dcc4ad813a7ff335a"
integrity sha512-ZvWb86eF0QXH9C5Mbi87aUmr8SH848yEpXJmlM2AoBowpE9kKDnewCAKvyXUihojUFwCSEEjoJhrRMMgmCZqXA==
dependencies:
https-proxy-agent "^5.0.0"
node-fetch "^2.6.7"
npmlog "^6.0.1"
progress "^2.0.3"
proxy-from-env "^1.1.0"
which "^2.0.2"
Expand Down Expand Up @@ -20552,7 +20551,7 @@ npmlog@^4.1.2:
gauge "~2.7.3"
set-blocking "~2.0.0"

npmlog@^6.0.0, npmlog@^6.0.1, npmlog@^6.0.2:
npmlog@^6.0.0, npmlog@^6.0.2:
version "6.0.2"
resolved "https://registry.yarnpkg.com/npmlog/-/npmlog-6.0.2.tgz#c8166017a42f2dea92d6453168dd865186a70830"
integrity sha512-/vBvz5Jfr9dT/aFWd0FIRf+T/Q2WBsLENygUaFUqstqsycmZAP/t5BvFJTK0viFmSUxiUKTUplWy5vt+rvKIxg==
Expand Down