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

Handle Python Dependency Versions Correctly #1078

Closed
Closed
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
26 changes: 15 additions & 11 deletions packages/jsii-pacmak/lib/targets/python.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path = require('path');

import * as path from 'path';
import * as semver from 'semver';
import { CodeMaker, toSnakeCase } from 'codemaker';
import * as escapeStringRegexp from 'escape-string-regexp';
import * as reflect from 'jsii-reflect';
Expand Down Expand Up @@ -1161,17 +1161,21 @@ class Package {
const expectedDeps = this.metadata.dependencies ?? {};
for (const depName of Object.keys(expectedDeps)) {
const depInfo = expectedDeps[depName];
// We need to figure out what our version range is.
// Basically, if it starts with Zero we want to restrict things to
// ~=X.Y.Z. If it does not start with zero, then we want to do ~=X.Y,>=X.Y.Z.
const versionParts = depInfo.version.split('.');
let versionSpecifier: string;
if (versionParts[0] === '0') {
versionSpecifier = `~=${versionParts.slice(0, 3).join('.')}`;
const { version } = depInfo;

// Parse either a single version or version range.
const parsedVersion = semver.valid(version);
const parsedVersionRange = semver.validRange(version);
let versionSpecifier;

// Convert parsed version info to expected format
if (parsedVersion) {
versionSpecifier = `==${version}`;
} else if (parsedVersionRange) {
versionSpecifier = parsedVersionRange.replace(' ', ', ');
} else {
versionSpecifier = `~=${versionParts.slice(0, 2).join('.')},>=${versionParts.slice(0, 3).join('.')}`;
throw new Error(`Unexpected version format '${version}' for dependency: 'depName'`);
}
Comment on lines +1172 to 1178
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels like it should have tests. Also - similar work should be done on Java and .NET dependencies.


dependencies.push(`${depInfo.targets!.python!.distName}${versionSpecifier}`);
}

Expand Down
2 changes: 2 additions & 0 deletions packages/jsii-pacmak/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"jsii-reflect": "^0.20.8",
"jsii-rosetta": "^0.20.8",
"jsii-spec": "^0.20.8",
"semver": "^6.3.0",
"spdx-license-list": "^6.1.0",
"xmlbuilder": "^13.0.2",
"yargs": "^15.0.2"
Expand All @@ -54,6 +55,7 @@
"@types/jest": "^24.0.23",
"@types/mock-fs": "^4.10.0",
"@types/node": "^10.17.6",
"@types/semver": "^6.2.0",
"@types/yargs": "^13.0.3",
"@typescript-eslint/eslint-plugin": "^2.9.0",
"@typescript-eslint/parser": "^2.9.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"install_requires": [
"jsii~=0.20.8",
"publication>=0.0.3",
"scope.jsii-calc-base-of-base~=0.20.8"
"scope.jsii-calc-base-of-base==0.20.8"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original dependency is a caret dependency (^), does == means the same thing in Python?

"@scope/jsii-calc-base-of-base": "^0.20.8"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that looks suspicious; but I reckon because the jsii compiler forwards the "resolved" (not "declared") version. That needs to be fixed as well (possibly in a separate PR, but then we probably shouldn't merge this without that).

],
"classifiers": [
"Intended Audience :: Developers",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"install_requires": [
"jsii~=0.20.8",
"publication>=0.0.3",
"scope.jsii-calc-base~=0.20.8"
"scope.jsii-calc-base==0.20.8"
],
"classifiers": [
"Intended Audience :: Developers",
Expand Down
6 changes: 3 additions & 3 deletions packages/jsii-pacmak/test/expected.jsii-calc/python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
"install_requires": [
"jsii~=0.20.8",
"publication>=0.0.3",
"scope.jsii-calc-base~=0.20.8",
"scope.jsii-calc-base-of-base~=0.20.8",
"scope.jsii-calc-lib~=0.20.8"
"scope.jsii-calc-base==0.20.8",
"scope.jsii-calc-base-of-base==0.20.8",
"scope.jsii-calc-lib==0.20.8"
],
"classifiers": [
"Intended Audience :: Developers",
Expand Down