Skip to content

Commit

Permalink
Backport getsentry#1374
Browse files Browse the repository at this point in the history
  • Loading branch information
timfish committed Oct 28, 2022
1 parent ca56162 commit ef649d1
Show file tree
Hide file tree
Showing 4 changed files with 267 additions and 21 deletions.
26 changes: 17 additions & 9 deletions js/helper.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
'use strict';

const path = require('path');
const childProcess = require('child_process');

/**
* Absolute path to the sentry-cli binary (platform dependent).
* @type {string}
* This convoluted function resolves the path to the `sentry-cli` binary in a
* way that can't be analysed by @vercel/nft.
*
* Without this, the binary can be detected as an asset and included by bundlers
* that use @vercel/nft.
* @returns {string} The path to the sentry-cli binary
*/
let binaryPath = eval(
"require('path').resolve(__dirname, require('os').platform() === 'win32' ? '..\\sentry-cli.exe' : '../sentry-cli')"
);
function getBinaryPath() {
const parts = [];
parts.push(__dirname);
parts.push('..');
parts.push(`sentry-cli${process.platform === 'win32' ? '.exe' : ''}`);
return path.resolve(...parts);
}

/**
* NOTE: `eval` usage is a workaround for @vercel/nft detecting the binary itself as the hard dependency
* and effectively always including it in the bundle, which is not what we want.
* ref: https:/getsentry/sentry-javascript/issues/3865
* ref: https:/vercel/nft/issues/203
* Absolute path to the sentry-cli binary (platform dependent).
* @type {string}
*/
let binaryPath = getBinaryPath();

/**
* Overrides the default binary path with a mock value, useful for testing.
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@
"fix": "npm-run-all fix:eslint fix:prettier",
"fix:eslint": "eslint --fix bin/* scripts/**/*.js js/**/*.js",
"fix:prettier": "prettier --write bin/* scripts/**/*.js js/**/*.js",
"test": "npm-run-all test:jest test:eslint test:prettier",
"test": "npm-run-all test:jest test:eslint test:prettier test:vercel-nft",
"test:jest": "jest",
"test:watch": "jest --watch --notify",
"test:eslint": "eslint bin/* scripts/**/*.js js/**/*.js",
"test:prettier": "prettier --check bin/* scripts/**/*.js js/**/*.js"
"test:prettier": "prettier --check bin/* scripts/**/*.js js/**/*.js",
"test:vercel-nft": "node scripts/test-vercel-nft.js"
},
"dependencies": {
"https-proxy-agent": "^5.0.0",
Expand All @@ -44,6 +45,7 @@
"which": "^2.0.2"
},
"devDependencies": {
"@vercel/nft": "^0.22.1",
"eslint": "^6.8.0",
"eslint-config-airbnb-base": "^14.1.0",
"eslint-config-prettier": "^6.10.1",
Expand Down
16 changes: 16 additions & 0 deletions scripts/test-vercel-nft.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const { nodeFileTrace } = require('@vercel/nft');

const entryPoint = require.resolve('..');

// Trace the module entrypoint
nodeFileTrace([entryPoint]).then((result) => {
console.log('@vercel/nft traced dependencies:', Array.from(result.fileList));

// If either binary is picked up, fail the test
if (result.fileList.has('sentry-cli') || result.fileList.has('sentry-cli.exe')) {
console.error('ERROR: The sentry-cli binary should not be found by @vercel/nft');
process.exit(-1);
} else {
console.log('The sentry-cli binary was not traced by @vercel/nft');
}
});
Loading

0 comments on commit ef649d1

Please sign in to comment.