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

install plugin modules without npm view #5084

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
12 changes: 2 additions & 10 deletions packages/dd-trace/test/llmobs/sdk/typescript/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,14 @@ const testCases = [
}
]

// a bit of devex to show the version we're actually testing
// so we don't need to know ahead of time
function getLatestVersion (range) {
const command = `npm show typescript@${range} version`
const output = execSync(command, { encoding: 'utf-8' }).trim()
const versions = output.split('\n').map(line => line.split(' ')[1].replace(/'/g, ''))
return versions[versions.length - 1]
}

describe('typescript', () => {
let agent
let proc
let sandbox

for (const version of testVersions) {
context(`with version ${getLatestVersion(version)}`, () => {
// TODO: Figure out the real version without using `npm show` as it's broken.
context(`with version ${version}`, () => {
before(async function () {
this.timeout(20000)
sandbox = await createSandbox(
Expand Down
80 changes: 40 additions & 40 deletions scripts/install_plugin_modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const path = require('path')
const crypto = require('crypto')
const semver = require('semver')
const exec = require('./helpers/exec')
const childProcess = require('child_process')
const externals = require('../packages/dd-trace/test/plugins/externals')
const { getInstrumentation } = require('../packages/dd-trace/test/setup/helpers/load-inst')

Expand All @@ -16,7 +15,6 @@ const requirePackageJsonPath = require.resolve('../packages/dd-trace/src/require
// Can remove couchbase after removing support for couchbase <= 3.2.0
const excludeList = os.arch() === 'arm64' ? ['aerospike', 'couchbase', 'grpc', 'oracledb'] : []
const workspaces = new Set()
const versionLists = {}
const deps = {}
const filter = process.env.hasOwnProperty('PLUGINS') && process.env.PLUGINS.split('|')

Expand All @@ -43,6 +41,8 @@ async function run () {
// Some native addon packages rely on libraries that are not supported on ARM64
excludeList.forEach(pkg => delete workspaces[pkg])
install()
assertPeerDependencies(path.join(__dirname, '..', 'versions'))
install()
}

async function assertVersions () {
Expand Down Expand Up @@ -107,9 +107,6 @@ function assertFolder (name, version) {

async function assertPackage (name, version, dependencyVersionRange, external) {
const dependencies = { [name]: dependencyVersionRange }
if (deps[name]) {
await addDependencies(dependencies, name, dependencyVersionRange)
}
const pkg = {
name: [name, sha1(name).substr(0, 8), sha1(version)].filter(val => val).join('-'),
version: '1.0.0',
Expand All @@ -132,45 +129,47 @@ async function assertPackage (name, version, dependencyVersionRange, external) {
fs.writeFileSync(filename(name, version, 'package.json'), JSON.stringify(pkg, null, 2) + '\n')
}

async function addDependencies (dependencies, name, versionRange) {
const versionList = await getVersionList(name)
const version = semver.maxSatisfying(versionList, versionRange)
const pkgJson = await npmView(`${name}@${version}`)
for (const dep of deps[name]) {
for (const section of ['devDependencies', 'peerDependencies']) {
if (pkgJson[section] && dep in pkgJson[section]) {
if (pkgJson[section][dep].includes('||')) {
// Use the first version in the list (as npm does by default)
dependencies[dep] = pkgJson[section][dep].split('||')[0].trim()
} else {
// Only one version available so use that.
dependencies[dep] = pkgJson[section][dep]
}
break
}
async function assertPeerDependencies (rootFolder, parent) {
const entries = fs.readdirSync(rootFolder)

for (const entry of entries) {
const folder = path.join(rootFolder, entry)

if (!fs.lstatSync(folder).isDirectory()) continue
if (entry === 'node_modules') continue
if (entry.startsWith('@')) {
assertPeerDependencies(folder, entry)
continue
}
}
}

async function getVersionList (name) {
if (versionLists[name]) {
return versionLists[name]
}
const list = await npmView(`${name} versions`)
versionLists[name] = list
return list
}
const name = [parent, entry.split('@')[0]].filter(v => v).join('/')

function npmView (input) {
return new Promise((resolve, reject) => {
childProcess.exec(`npm view ${input} --json`, (err, stdout) => {
if (err) {
reject(err)
return
if (!deps[name]) continue

const versionPkgJsonPath = path.join(folder, 'package.json')
const versionPkgJson = require(versionPkgJsonPath)

for (const dep of deps[name]) {
const pkgJsonPath = require(folder).pkgJsonPath()
const pkgJson = require(pkgJsonPath)

for (const section of ['devDependencies', 'peerDependencies']) {
if (pkgJson[section] && dep in pkgJson[section]) {
const peerVersion = pkgJson[section][dep].includes('||')
// Use the first version in the list (as npm does by default)
? pkgJson[section][dep].split('||')[0].trim()
// Only one version available so use that.
: pkgJson[section][dep]

versionPkgJson.dependencies[dep] = peerVersion

fs.writeFileSync(versionPkgJsonPath, JSON.stringify(versionPkgJson, null, 2))

break
}
}
resolve(JSON.parse(stdout.toString('utf8')))
})
})
}
}
}

function assertIndex (name, version) {
Expand All @@ -181,6 +180,7 @@ const requirePackageJson = require('${requirePackageJsonPath}')
module.exports = {
get (id) { return require(id || '${name}') },
getPath (id) { return require.resolve(id || '${name}' ) },
pkgJsonPath (id) { return require.resolve((id || '${name}') + '/package.json') },
version () { return requirePackageJson('${name}', module).version }
}
`
Expand Down
Loading