Skip to content

Commit

Permalink
fix: node-gyp-build for @datadog/pprof (#419)
Browse files Browse the repository at this point in the history
- Fixes #410
- Closes #411

Note, this is easiest to review without whitespace
https://github.com/vercel/nft/pull/419/files?w=1
  • Loading branch information
styfle authored May 16, 2024
1 parent ddc5cba commit db6c65a
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 40 deletions.
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module.exports = {
coverageReporters: ['html', 'lcov'],
coverageThreshold: {
global: {
branches: 87.29,
branches: 87.25,
functions: 96.25,
lines: 90.85,
statements: -249,
Expand Down
56 changes: 56 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"devDependencies": {
"@azure/cosmos": "^2.1.7",
"@bugsnag/js": "^6.3.2",
"@datadog/pprof": "^5.2.0",
"@ffmpeg-installer/ffmpeg": "^1.1.0",
"@google-cloud/bigquery": "^4.1.4",
"@google-cloud/firestore": "^7.6.0",
Expand Down
68 changes: 30 additions & 38 deletions src/analyze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -754,46 +754,38 @@ export default async function analyze(
break;
case NODE_GYP_BUILD:
// handle case: require('node-gyp-build')(__dirname)
const withDirname =
node.arguments.length === 1 &&
node.arguments[0].type === 'Identifier' &&
node.arguments[0].name === '__dirname';

// handle case: require('node-gyp-build')(path.join(__dirname, '..'))
const withPathJoinDirname =
node.arguments.length === 1 &&
node.arguments[0].callee?.object?.name === 'path' &&
node.arguments[0].callee?.property?.name === 'join' &&
node.arguments[0].arguments.length === 2 &&
node.arguments[0].arguments[0].type === 'Identifier' &&
node.arguments[0].arguments[0].name === '__dirname' &&
node.arguments[0].arguments[1].type === 'Literal';

if (
knownBindings.__dirname.shadowDepth === 0 &&
(withDirname || withPathJoinDirname)
) {
const pathJoinedDir = withPathJoinDirname
? path.join(dir, node.arguments[0].arguments[1].value)
: dir;
// handle case: require('node-gyp-build')(join(__dirname, ''))
// also case: require('@aminya/node-gyp-build')(__dirname)

let resolved: string | undefined;
try {
// the pkg could be 'node-gyp-build' or '@aminya/node-gyp-build'
const pkgName = node.callee.arguments[0].value;
// use installed version of node-gyp-build since resolving
// binaries can differ among versions
const nodeGypBuildPath = resolveFrom(pathJoinedDir, pkgName);
resolved = require(nodeGypBuildPath).path(pathJoinedDir);
} catch (e) {
if (node.arguments.length) {
const arg = await computePureStaticValue(
node.arguments[0],
false,
);
if (arg && 'value' in arg && arg.value) {
const pathJoinedDir = arg.value;
let resolved: string | undefined;
try {
resolved = nodeGypBuild.path(pathJoinedDir);
} catch (e) {}
}
if (resolved) {
staticChildValue = { value: resolved };
staticChildNode = node;
await emitStaticChildAsset();
// the pkg could be 'node-gyp-build' or '@aminya/node-gyp-build'
const pkgName =
node?.callee?.arguments?.[0]?.value || 'node-gyp-build';
// use installed version of node-gyp-build since resolving
// binaries can differ among versions
const nodeGypBuildPath = resolveFrom(
pathJoinedDir,
pkgName,
);
resolved = require(nodeGypBuildPath).path(pathJoinedDir);
} catch (e) {
try {
resolved = nodeGypBuild.path(pathJoinedDir);
} catch (e) {}
}
if (resolved) {
staticChildValue = { value: resolved };
staticChildNode = node;
await emitStaticChildAsset();
}
}
}
break;
Expand Down
1 change: 1 addition & 0 deletions test/integration/datadog-pprof.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const dd = require('@datadog/pprof');
9 changes: 8 additions & 1 deletion test/unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ const readFile = gracefulFS.promises.readFile;

global._unit = true;

const nodeGypTests = [
'datadog-pprof-node-gyp',
'microtime-node-gyp',
'zeromq-node-gyp',
];

const skipOnWindows = [
'datadog-pprof-node-gyp',
'yarn-workspaces',
'yarn-workspaces-base-root',
'yarn-workspace-esm',
Expand Down Expand Up @@ -234,7 +241,7 @@ for (const { testName, isRoot } of unitTests) {
}
let sortedFileList = [...fileList].sort();

if (testName === 'microtime-node-gyp' || testName === 'zeromq-node-gyp') {
if (nodeGypTests.includes(testName)) {
let foundMatchingBinary = false;
sortedFileList = sortedFileList.filter((file) => {
if (file.includes('prebuilds') && file.endsWith('.node')) {
Expand Down
1 change: 1 addition & 0 deletions test/unit/datadog-pprof-node-gyp/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const dd = require('@datadog/pprof');
38 changes: 38 additions & 0 deletions test/unit/datadog-pprof-node-gyp/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[
"node_modules/@datadog/pprof/node_modules/node-gyp-build/index.js",
"node_modules/@datadog/pprof/node_modules/node-gyp-build/package.json",
"node_modules/@datadog/pprof/node_modules/source-map/lib/array-set.js",
"node_modules/@datadog/pprof/node_modules/source-map/lib/base64-vlq.js",
"node_modules/@datadog/pprof/node_modules/source-map/lib/base64.js",
"node_modules/@datadog/pprof/node_modules/source-map/lib/binary-search.js",
"node_modules/@datadog/pprof/node_modules/source-map/lib/mapping-list.js",
"node_modules/@datadog/pprof/node_modules/source-map/lib/mappings.wasm",
"node_modules/@datadog/pprof/node_modules/source-map/lib/read-wasm.js",
"node_modules/@datadog/pprof/node_modules/source-map/lib/source-map-consumer.js",
"node_modules/@datadog/pprof/node_modules/source-map/lib/source-map-generator.js",
"node_modules/@datadog/pprof/node_modules/source-map/lib/source-node.js",
"node_modules/@datadog/pprof/node_modules/source-map/lib/util.js",
"node_modules/@datadog/pprof/node_modules/source-map/lib/wasm.js",
"node_modules/@datadog/pprof/node_modules/source-map/package.json",
"node_modules/@datadog/pprof/node_modules/source-map/source-map.js",
"node_modules/@datadog/pprof/out/src/heap-profiler-bindings.js",
"node_modules/@datadog/pprof/out/src/heap-profiler.js",
"node_modules/@datadog/pprof/out/src/index.js",
"node_modules/@datadog/pprof/out/src/logger.js",
"node_modules/@datadog/pprof/out/src/profile-encoder.js",
"node_modules/@datadog/pprof/out/src/profile-serializer.js",
"node_modules/@datadog/pprof/out/src/sourcemapper/sourcemapper.js",
"node_modules/@datadog/pprof/out/src/time-profiler-bindings.js",
"node_modules/@datadog/pprof/out/src/time-profiler.js",
"node_modules/@datadog/pprof/package.json",
"node_modules/delay/index.js",
"node_modules/delay/package.json",
"node_modules/p-limit/index.js",
"node_modules/p-limit/package.json",
"node_modules/pprof-format/dist/index.js",
"node_modules/pprof-format/package.json",
"node_modules/yocto-queue/index.js",
"node_modules/yocto-queue/package.json",
"package.json",
"test/unit/datadog-pprof-node-gyp/input.js"
]

0 comments on commit db6c65a

Please sign in to comment.