From 286866aa2a0c01adf90923524fce2fd2c6ff9c25 Mon Sep 17 00:00:00 2001 From: Romain Marcadier-Muller Date: Thu, 23 May 2019 11:33:38 +0200 Subject: [PATCH] fix(core): Correctly search for loaded modules in node 12 (#2612) Node 12 has corrected how the require.resolve function applied un-documented behavior, so that it is needed to make relative search paths explicit (./). Related: nodejs/node#27583 --- packages/@aws-cdk/cdk/lib/node-version.ts | 19 ++++++ packages/@aws-cdk/cdk/lib/runtime-info.ts | 7 ++- packages/@aws-cdk/cdk/package-lock.json | 73 +++++++++++++---------- packages/@aws-cdk/cdk/package.json | 2 +- 4 files changed, 66 insertions(+), 35 deletions(-) create mode 100644 packages/@aws-cdk/cdk/lib/node-version.ts diff --git a/packages/@aws-cdk/cdk/lib/node-version.ts b/packages/@aws-cdk/cdk/lib/node-version.ts new file mode 100644 index 0000000000000..f7ebf928ed773 --- /dev/null +++ b/packages/@aws-cdk/cdk/lib/node-version.ts @@ -0,0 +1,19 @@ +import process = require('process'); + +// process.versions.node is like "12.3.1" +const [strMajor, strMinor, strPatch, ] = process.versions.node.split('.'); + +/** + * The major version of the node runtime. + */ +export const major = Number(strMajor); + +/** + * The minor version of the node runtime. + */ +export const minor = Number(strMinor); + +/** + * The revision of the node runtime. + */ +export const patch = Number(strPatch); diff --git a/packages/@aws-cdk/cdk/lib/runtime-info.ts b/packages/@aws-cdk/cdk/lib/runtime-info.ts index 3bd6790634656..c978691acc808 100644 --- a/packages/@aws-cdk/cdk/lib/runtime-info.ts +++ b/packages/@aws-cdk/cdk/lib/runtime-info.ts @@ -1,4 +1,5 @@ import cxapi = require('@aws-cdk/cx-api'); +import { major as nodeMajorVersion } from './node-version'; /** * Returns a list of loaded modules and their versions. @@ -55,7 +56,11 @@ function findNpmPackage(fileName: string): { name: string, version: string, priv const paths = mod.paths.map(stripNodeModules); try { - const packagePath = require.resolve('package.json', { paths }); + const packagePath = require.resolve( + // Resolution behavior changed in node 12.0.0 - https://github.com/nodejs/node/issues/27583 + nodeMajorVersion >= 12 ? './package.json' : 'package.json', + { paths } + ); return require(packagePath); } catch (e) { return undefined; diff --git a/packages/@aws-cdk/cdk/package-lock.json b/packages/@aws-cdk/cdk/package-lock.json index ff4995939ee42..525adfddc50e6 100644 --- a/packages/@aws-cdk/cdk/package-lock.json +++ b/packages/@aws-cdk/cdk/package-lock.json @@ -1,35 +1,42 @@ { - "name": "@aws-cdk/cdk", - "version": "0.31.0", - "lockfileVersion": 1, - "requires": true, - "dependencies": { - "@types/lodash": { - "version": "4.14.123", - "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.123.tgz", - "integrity": "sha512-pQvPkc4Nltyx7G1Ww45OjVqUsJP4UsZm+GWJpigXgkikZqJgRm4c48g027o6tdgubWHwFRF15iFd+Y4Pmqv6+Q==", - "dev": true - }, - "fast-check": { - "version": "1.14.0", - "resolved": "https://registry.npmjs.org/fast-check/-/fast-check-1.14.0.tgz", - "integrity": "sha512-haI2vSLhtIUhxJJT4Ys67IEINkou9GBlKP+GNr0AqtZUszY6Od+pzHlLki4I3pM9q6G9LTFX03ZyJ9zVCWXUXA==", - "dev": true, - "requires": { - "pure-rand": "^1.6.2" - } - }, - "lodash": { - "version": "4.17.11", - "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.11.tgz", - "integrity": "sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg==", - "dev": true - }, - "pure-rand": { - "version": "1.6.2", - "resolved": "https://registry.npmjs.org/pure-rand/-/pure-rand-1.6.2.tgz", - "integrity": "sha512-HNwHOH63m7kCxe0kWEe5jSLwJiL2N83RUUN8POniFuZS+OsbFcMWlvXgxIU2nwKy2zYG2bQan40WBNK4biYPRg==", - "dev": true - } - } + "name": "@aws-cdk/cdk", + "version": "0.31.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "@types/lodash": { + "version": "4.14.130", + "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.130.tgz", + "integrity": "sha512-H++wk0tbneBsRVfLkgAAd0IIpmpVr2Bj4T0HncoOsQf3/xrJexRYQK2Tqo0Ej3pFslM8GkMgdis9bu6xIb1ycw==", + "dev": true + }, + "fast-check": { + "version": "1.15.1", + "resolved": "https://registry.npmjs.org/fast-check/-/fast-check-1.15.1.tgz", + "integrity": "sha512-AhVx0sam1qDBmrEFRD3OufJHekzka7pyOj3zVAz6H0QenxUCIXG0LFLxcju6oRQNOt8FZOq3AfX+B0GtPNDjkA==", + "dev": true, + "requires": { + "pure-rand": "^1.6.2", + "tslib": "^1.9.3" + } + }, + "lodash": { + "version": "4.17.11", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.11.tgz", + "integrity": "sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg==", + "dev": true + }, + "pure-rand": { + "version": "1.6.2", + "resolved": "https://registry.npmjs.org/pure-rand/-/pure-rand-1.6.2.tgz", + "integrity": "sha512-HNwHOH63m7kCxe0kWEe5jSLwJiL2N83RUUN8POniFuZS+OsbFcMWlvXgxIU2nwKy2zYG2bQan40WBNK4biYPRg==", + "dev": true + }, + "tslib": { + "version": "1.9.3", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.9.3.tgz", + "integrity": "sha512-4krF8scpejhaOgqzBEcGM7yDIEfi0/8+8zDRZhNZZ2kjmHJ4hv3zCbQWxoJGz1iw5U0Jl0nma13xzHXcncMavQ==", + "dev": true + } + } } diff --git a/packages/@aws-cdk/cdk/package.json b/packages/@aws-cdk/cdk/package.json index d764290a98fb3..152318440374b 100644 --- a/packages/@aws-cdk/cdk/package.json +++ b/packages/@aws-cdk/cdk/package.json @@ -67,7 +67,7 @@ }, "license": "Apache-2.0", "devDependencies": { - "@types/lodash": "^4.14.123", + "@types/lodash": "^4.14.130", "cdk-build-tools": "^0.31.0", "cfn2ts": "^0.31.0", "fast-check": "^1.14.0",