Skip to content
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

esbuild: graceful continue when bundling dead code #3215

Merged
merged 2 commits into from
Jun 5, 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
1 change: 1 addition & 0 deletions LICENSE-3rdparty.csv
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ dev,glob,ISC,Copyright Isaac Z. Schlueter and Contributors
dev,graphql,MIT,Copyright 2015 Facebook Inc.
dev,int64-buffer,MIT,Copyright 2015-2016 Yusuke Kawasaki
dev,jszip,MIT,Copyright 2015-2016 Stuart Knightley and contributors
dev,knex,MIT,Copyright (c) 2013-present Tim Griesser
dev,mkdirp,MIT,Copyright 2010 James Halliday
dev,mocha,MIT,Copyright 2011-2018 JS Foundation and contributors https://js.foundation
dev,multer,MIT,Copyright 2014 Hage Yaapa
Expand Down
1 change: 1 addition & 0 deletions integration-tests/esbuild/basic-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const tracer = require('../../').init() // dd-trace
const assert = require('assert')
const express = require('express')
const http = require('http')
require('knex') // has dead code paths for multiple instrumented packages

const app = express()
const PORT = 31415
Expand Down
13 changes: 12 additions & 1 deletion integration-tests/esbuild/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,18 @@ esbuild.build({
outfile: 'out.js',
plugins: [ddPlugin],
platform: 'node',
target: ['node16']
target: ['node16'],
external: [
// dead code paths introduced by knex
'pg',
'mysql2',
'better-sqlite3',
'sqlite3',
'mysql',
'oracledb',
'pg-query-stream',
'tedious'
]
}).catch((err) => {
console.error(err) // eslint-disable-line no-console
process.exit(1)
Expand Down
3 changes: 2 additions & 1 deletion integration-tests/esbuild/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"license": "ISC",
"dependencies": {
"esbuild": "0.16.12",
"express": "^4.16.2"
"express": "^4.16.2",
"knex": "^2.4.2"
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
"graphql": "0.13.2",
"int64-buffer": "^0.1.9",
"jszip": "^3.5.0",
"knex": "^2.4.2",
"mkdirp": "^0.5.1",
"mocha": "8",
"msgpack-lite": "^0.1.26",
Expand Down
14 changes: 13 additions & 1 deletion packages/datadog-esbuild/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,19 @@ module.exports.setup = function (build) {

if (args.namespace === 'file' && packagesOfInterest.has(packageName)) {
// The file namespace is used when requiring files from disk in userland
const pathToPackageJson = require.resolve(`${packageName}/package.json`, { paths: [ args.resolveDir ] })

let pathToPackageJson
try {
pathToPackageJson = require.resolve(`${packageName}/package.json`, { paths: [ args.resolveDir ] })
} catch (err) {
if (err.code === 'MODULE_NOT_FOUND') {
console.warn(`Unable to open "${packageName}/package.json". Is the "${packageName}" package dead code?`)
return
} else {
throw err
}
}

const pkg = require(pathToPackageJson)

if (DEBUG) {
Expand Down