Skip to content

Commit

Permalink
feat(jsii): enforce peer dependencies (#294)
Browse files Browse the repository at this point in the history
If a jsii module exposes a type from a dependency in it's public API,
jsii now enforces that this dependency is also defined as a "peer"
instead of a normal dependency.

This tells npm that if a user of this module already
installed a compatible verison of this dependency in their
closure, npm will pick the one installed by the user (as a peer)
instead of fetching another version.

jsii will also flag these dependencies as "peer" in the jsii spec. At the
moment, this won't have implications on generated language packages,
but in environments that have support for peer dependencies, we should
make sure the module's metadata reflects this idea as well.

A utility called "jsii-fix-peers" is included. It will inspect .jsii
and package.json and will add "peerDependencies" to package.json
for all dependencies that have types in the public API.

Related to aws/aws-cdk#979
  • Loading branch information
Elad Ben-Israel authored Nov 6, 2018
1 parent 4d135cf commit 1753910
Show file tree
Hide file tree
Showing 17 changed files with 221 additions and 28 deletions.
19 changes: 12 additions & 7 deletions package-lock.json

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

3 changes: 3 additions & 0 deletions packages/jsii-calc-base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
"dependencies": {
"@scope/jsii-calc-base-of-base": "^0.7.8"
},
"peerDependencies": {
"@scope/jsii-calc-base-of-base": "^0.7.8"
},
"author": {
"name": "Amazon Web Services",
"url": "https://aws.amazon.com",
Expand Down
3 changes: 2 additions & 1 deletion packages/jsii-calc-base/test/assembly.jsii
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
},
"dependencies": {
"@scope/jsii-calc-base-of-base": {
"peer": true,
"targets": {
"dotnet": {
"namespace": "Amazon.JSII.Tests.CalculatorNamespace.BaseOfBaseNamespace",
Expand Down Expand Up @@ -102,5 +103,5 @@
}
},
"version": "0.7.8",
"fingerprint": "G+ZeFV6LQWFdp9ZuDgqN4rS10q/JBHuy0Wo8qaTK/t8="
"fingerprint": "K1rAUs6WiQ5lF08T46B8v/5UL8T8Ot59e0Nc8rh2jiQ="
}
3 changes: 3 additions & 0 deletions packages/jsii-calc-lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
"dependencies": {
"@scope/jsii-calc-base": "^0.7.8"
},
"peerDependencies": {
"@scope/jsii-calc-base": "^0.7.8"
},
"author": {
"name": "Amazon Web Services",
"url": "https://aws.amazon.com",
Expand Down
4 changes: 3 additions & 1 deletion packages/jsii-calc-lib/test/assembly.jsii
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"@scope/jsii-calc-base": {
"dependencies": {
"@scope/jsii-calc-base-of-base": {
"peer": true,
"targets": {
"dotnet": {
"namespace": "Amazon.JSII.Tests.CalculatorNamespace.BaseOfBaseNamespace",
Expand All @@ -30,6 +31,7 @@
"version": "0.7.8"
}
},
"peer": true,
"targets": {
"dotnet": {
"namespace": "Amazon.JSII.Tests.CalculatorNamespace.BaseNamespace",
Expand Down Expand Up @@ -351,5 +353,5 @@
}
},
"version": "0.7.8",
"fingerprint": "HzcyHys0b9gFmP4dogeIJmGE6GVtrSo/P0S54Vd/X8U="
"fingerprint": "ENH00UDSeTnXKpvD3/vp0k8zyd+KCHzc9QWB151/gM8="
}
4 changes: 4 additions & 0 deletions packages/jsii-calc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
"@scope/jsii-calc-lib": "^0.7.8",
"jsii-calc-bundled": "^0.7.8"
},
"peerDependencies": {
"@scope/jsii-calc-base": "^0.7.8",
"@scope/jsii-calc-lib": "^0.7.8"
},
"devDependencies": {
"@types/node": "^8.10.37",
"jsii": "^0.7.8",
Expand Down
7 changes: 6 additions & 1 deletion packages/jsii-calc/test/assembly.jsii
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@scope/jsii-calc-base": {
"dependencies": {
"@scope/jsii-calc-base-of-base": {
"peer": true,
"targets": {
"dotnet": {
"namespace": "Amazon.JSII.Tests.CalculatorNamespace.BaseOfBaseNamespace",
Expand All @@ -56,6 +57,7 @@
"version": "0.7.8"
}
},
"peer": true,
"targets": {
"dotnet": {
"namespace": "Amazon.JSII.Tests.CalculatorNamespace.BaseNamespace",
Expand All @@ -79,6 +81,7 @@
"@scope/jsii-calc-base": {
"dependencies": {
"@scope/jsii-calc-base-of-base": {
"peer": true,
"targets": {
"dotnet": {
"namespace": "Amazon.JSII.Tests.CalculatorNamespace.BaseOfBaseNamespace",
Expand All @@ -98,6 +101,7 @@
"version": "0.7.8"
}
},
"peer": true,
"targets": {
"dotnet": {
"namespace": "Amazon.JSII.Tests.CalculatorNamespace.BaseNamespace",
Expand All @@ -117,6 +121,7 @@
"version": "0.7.8"
}
},
"peer": true,
"targets": {
"dotnet": {
"namespace": "Amazon.JSII.Tests.CalculatorNamespace.LibNamespace",
Expand Down Expand Up @@ -3407,5 +3412,5 @@
}
},
"version": "0.7.8",
"fingerprint": "jHSXTzCSZbwYMvLKpeZB6SE8hNgYgt9/2JF1ihM41SI="
"fingerprint": "Xn7Rk17rqR3AaMx3+ssxT0GR1sCWwz0OGC+C8QuLI3A="
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
},
"dependencies": {
"@scope/jsii-calc-base-of-base": {
"peer": true,
"targets": {
"dotnet": {
"namespace": "Amazon.JSII.Tests.CalculatorNamespace.BaseOfBaseNamespace",
Expand Down Expand Up @@ -102,5 +103,5 @@
}
},
"version": "0.7.8",
"fingerprint": "G+ZeFV6LQWFdp9ZuDgqN4rS10q/JBHuy0Wo8qaTK/t8="
"fingerprint": "K1rAUs6WiQ5lF08T46B8v/5UL8T8Ot59e0Nc8rh2jiQ="
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"@scope/jsii-calc-base": {
"dependencies": {
"@scope/jsii-calc-base-of-base": {
"peer": true,
"targets": {
"dotnet": {
"namespace": "Amazon.JSII.Tests.CalculatorNamespace.BaseOfBaseNamespace",
Expand All @@ -30,6 +31,7 @@
"version": "0.7.8"
}
},
"peer": true,
"targets": {
"dotnet": {
"namespace": "Amazon.JSII.Tests.CalculatorNamespace.BaseNamespace",
Expand Down Expand Up @@ -351,5 +353,5 @@
}
},
"version": "0.7.8",
"fingerprint": "HzcyHys0b9gFmP4dogeIJmGE6GVtrSo/P0S54Vd/X8U="
"fingerprint": "ENH00UDSeTnXKpvD3/vp0k8zyd+KCHzc9QWB151/gM8="
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@scope/jsii-calc-base": {
"dependencies": {
"@scope/jsii-calc-base-of-base": {
"peer": true,
"targets": {
"dotnet": {
"namespace": "Amazon.JSII.Tests.CalculatorNamespace.BaseOfBaseNamespace",
Expand All @@ -56,6 +57,7 @@
"version": "0.7.8"
}
},
"peer": true,
"targets": {
"dotnet": {
"namespace": "Amazon.JSII.Tests.CalculatorNamespace.BaseNamespace",
Expand All @@ -79,6 +81,7 @@
"@scope/jsii-calc-base": {
"dependencies": {
"@scope/jsii-calc-base-of-base": {
"peer": true,
"targets": {
"dotnet": {
"namespace": "Amazon.JSII.Tests.CalculatorNamespace.BaseOfBaseNamespace",
Expand All @@ -98,6 +101,7 @@
"version": "0.7.8"
}
},
"peer": true,
"targets": {
"dotnet": {
"namespace": "Amazon.JSII.Tests.CalculatorNamespace.BaseNamespace",
Expand All @@ -117,6 +121,7 @@
"version": "0.7.8"
}
},
"peer": true,
"targets": {
"dotnet": {
"namespace": "Amazon.JSII.Tests.CalculatorNamespace.LibNamespace",
Expand Down Expand Up @@ -3407,5 +3412,5 @@
}
},
"version": "0.7.8",
"fingerprint": "jHSXTzCSZbwYMvLKpeZB6SE8hNgYgt9/2JF1ihM41SI="
"fingerprint": "Xn7Rk17rqR3AaMx3+ssxT0GR1sCWwz0OGC+C8QuLI3A="
}
16 changes: 16 additions & 0 deletions packages/jsii-spec/lib/spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,22 @@ export interface PackageVersion {

/** Dependencies of this dependency */
dependencies?: { [assembly: string]: PackageVersion };

/**
* Indicates if this dependency is a peer dependency or a normal dependency.
*
* Peer dependencies are expected to be explicitly defined by the user of
* this library instead of brought in as transitive dependencies.
*
* jsii enforces that if this module exports a type from a dependency, this
* dependency must be defined as a peer and not as a normal dependency.
* Otherwise, it would be impossible to safely use two versions of this
* dependency in a closure.
*
* @see https://github.com/awslabs/aws-cdk/issues/979
* @see https://nodejs.org/en/blog/npm/peer-dependencies/
*/
peer?: boolean;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions packages/jsii/bin/jsii-fix-peers
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env node
require('./jsii-fix-peers.js');
95 changes: 95 additions & 0 deletions packages/jsii/bin/jsii-fix-peers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
//
// jsii-fix-peers
//
// Inspects the local .jsii file and adds "peerDependencies" to the local
// package.json for all modules that include types that are referenced as part
// of the current module's public API.
//
// This resolves all peer dependency warnings emitted by the jsii compiler.
//

// tslint:disable:no-console

import fs = require('fs-extra');
import spec = require('jsii-spec');

fixPeers().catch(e => {
console.error(e.stack);
process.exit(1);
});

async function fixPeers() {
const jsiiFile = './.jsii';
const pkgFile = './package.json';

if (!(await fs.pathExists(jsiiFile))) {
throw new Error(`Cannot find '${jsiiFile}. Make sure to compile first`);
}

if (!(await fs.pathExists(pkgFile))) {
throw new Error(`Cannot find '${pkgFile}' in current working directory`);
}

const jsii = await fs.readJson(jsiiFile) as spec.Assembly;
const pkg = await fs.readJson(pkgFile);

// find all type references (FQNs) in the assembly.
const fqns = findAllFQNs(jsii);

let updated = false;

// read "dependencies" and "peerDependencies" from package.json
if (!pkg.peerDependencies) { pkg.peerDependencies = {}; }
const peers = pkg.peerDependencies;
const deps = pkg.dependencies || {};

// iterate over all FQNs and ensure that all referenced modules appear as peer dependencies
for (const fqn of fqns) {
const [ module, ] = fqn.split('.');
if (module === jsii.name) {
continue;
}

// verify that `module` exists in peerDependencies
if (module in peers) {
continue;
}

// fetch the module's version requirement from "dependencies" (we expect it to be there)
const dep = deps[module];
if (!dep) {
throw new Error(`Cannot find ${module} neither under "peerDependencies" nor "dependencies". Can't fix.`);
}

// fixing time! add this module to "peerDependencies"
peers[module] = dep;
console.log(`added ${module}@${dep} to "peerDependencies"`);
updated = true;
}

if (updated) {
await fs.writeJson(pkgFile, pkg, { spaces: 2 });
}
}

/**
* Looks up all { "fqn": "boom" } objects in a JSON tree.
* @param node Initial node (i.e. a JSII assembly)
* @param out Used by the recursive call to collect outputs
*/
function findAllFQNs(node: any, out = new Set<string>()) {
if (typeof(node) === 'object' && 'fqn' in node) {
out.add(node.fqn);
}

for (const key of Object.keys(node)) {
const value = node[key];
if (Array.isArray(value)) {
value.forEach(v => findAllFQNs(v, out));
} else if (typeof(value) === 'object') {
findAllFQNs(value, out);
}
}

return Array.from(out);
}
Loading

0 comments on commit 1753910

Please sign in to comment.