Skip to content

Commit

Permalink
Fix: esbuild plugin when requiring esm files (#4774)
Browse files Browse the repository at this point in the history
* fix esbuild issue when requiring esm files
  • Loading branch information
crysmags authored and rochdev committed Oct 31, 2024
1 parent cb24aeb commit 556398d
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 6 deletions.
1 change: 1 addition & 0 deletions LICENSE-3rdparty.csv
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ require,retry,MIT,Copyright 2011 Tim Koschützki Felix Geisendörfer
require,rfdc,MIT,Copyright 2019 David Mark Clements
require,semver,ISC,Copyright Isaac Z. Schlueter and Contributors
require,shell-quote,mit,Copyright (c) 2013 James Halliday
dev,@apollo/server,MIT,Copyright (c) 2016-2020 Apollo Graph, Inc. (Formerly Meteor Development Group, Inc.)
dev,@types/node,MIT,Copyright Authors
dev,autocannon,MIT,Copyright 2016 Matteo Collina
dev,aws-sdk,Apache 2.0,Copyright 2012-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
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 @@ -6,6 +6,7 @@ const assert = require('assert')
const express = require('express')
const http = require('http')
require('knex') // has dead code paths for multiple instrumented packages
require('@apollo/server')

const app = express()
const PORT = 31415
Expand Down
1 change: 1 addition & 0 deletions integration-tests/esbuild/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"author": "Thomas Hunter II <tlhunter@datadog.com>",
"license": "ISC",
"dependencies": {
"@apollo/server": "^4.11.0",
"aws-sdk": "^2.1446.0",
"axios": "^1.6.7",
"esbuild": "0.16.12",
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
"tlhunter-sorted-set": "^0.1.0"
},
"devDependencies": {
"@apollo/server": "^4.11.0",
"@types/node": "^16.18.103",
"autocannon": "^4.5.2",
"aws-sdk": "^2.1446.0",
Expand Down
6 changes: 4 additions & 2 deletions packages/datadog-esbuild/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ module.exports.setup = function (build) {

let pathToPackageJson
try {
pathToPackageJson = require.resolve(`${extracted.pkg}/package.json`, { paths: [args.resolveDir] })
// we can't use require.resolve('pkg/package.json') as ESM modules don't make the file available
pathToPackageJson = require.resolve(`${extracted.pkg}`, { paths: [args.resolveDir] })
pathToPackageJson = extractPackageAndModulePath(pathToPackageJson).pkgJson
} catch (err) {
if (err.code === 'MODULE_NOT_FOUND') {
if (!internal) {
Expand All @@ -111,7 +113,7 @@ module.exports.setup = function (build) {
}
}

const packageJson = require(pathToPackageJson)
const packageJson = JSON.parse(fs.readFileSync(pathToPackageJson).toString())

if (DEBUG) console.log(`RESOLVE: ${args.path}@${packageJson.version}`)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const NM = 'node_modules/'
* For a given full path to a module,
* return the package name it belongs to and the local path to the module
* input: '/foo/node_modules/@co/stuff/foo/bar/baz.js'
* output: { pkg: '@co/stuff', path: 'foo/bar/baz.js' }
* output: { pkg: '@co/stuff', path: 'foo/bar/baz.js', pkgJson: '/foo/node_modules/@co/stuff/package.json' }
*/
module.exports = function extractPackageAndModulePath (fullPath) {
const nm = fullPath.lastIndexOf(NM)
Expand All @@ -17,17 +17,20 @@ module.exports = function extractPackageAndModulePath (fullPath) {
const subPath = fullPath.substring(nm + NM.length)
const firstSlash = subPath.indexOf('/')

const firstPath = fullPath.substring(fullPath[0], nm + NM.length)

if (subPath[0] === '@') {
const secondSlash = subPath.substring(firstSlash + 1).indexOf('/')

return {
pkg: subPath.substring(0, firstSlash + 1 + secondSlash),
path: subPath.substring(firstSlash + 1 + secondSlash + 1)
path: subPath.substring(firstSlash + 1 + secondSlash + 1),
pkgJson: firstPath + subPath.substring(0, firstSlash + 1 + secondSlash) + '/package.json'
}
}

return {
pkg: subPath.substring(0, firstSlash),
path: subPath.substring(firstSlash + 1)
path: subPath.substring(firstSlash + 1),
pkgJson: firstPath + subPath.substring(0, firstSlash) + '/package.json'
}
}

0 comments on commit 556398d

Please sign in to comment.