aws / jsii

jsii allows code in any language to naturally interact with JavaScript classes. It is the technology that enables the AWS Cloud Development Kit to deliver polyglot libraries from a single codebase!
https://aws.github.io/jsii
Apache License 2.0
2.63k stars 244 forks source link

JSII compiled Python package fails in Python CDK application [ERR_PACKAGE_PATH_NOT_EXPORTED] #4259

Open johntaormina opened 1 year ago

johntaormina commented 1 year ago

Describe the bug

Using JSII to compile a TS/JS library into Python, and importing the library into a Python CDK application is failing when attempting to run any cdk commands. The failure response returned is:

jsii.errors.JavaScriptError:
  Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in /var/folders/gz/.../T/jsii-kernel-ku2ecO/node_modules/<library_name>/package.json

The Library Package is created using

The package.json contains

"main": "index.js",
"exports": {
    ".": {
      "types": "./index.d.ts",
      "import": "./index.js"
    },
}

as well as various exports of each sub-package similar to aws-cdk-lib like so

Expected Behavior

I expect cdk synth to synthesize the Python CDK code into a CloudFormation template by compiling/processing the Javascript artifact through the Python interface.

Current Behavior

When using the JSII translated library in Python, the Interfaces are all read correctly. However, at the time of compiling the Javascript package when running cdk commands... the compilation fails with the error message

jsii.errors.JavaScriptError:
  Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in /var/folders/gz/.../T/jsii-kernel-ku2ecO/node_modules/<library-name>/package.json
      at new NodeError (node:internal/errors:405:5)
      at exportsNotFound (node:internal/modules/esm/resolve:259:10)
      at packageExportsResolve (node:internal/modules/esm/resolve:533:13)
      at resolveExports (node:internal/modules/cjs/loader:571:36)
      at Module._findPath (node:internal/modules/cjs/loader:645:31)
      at Module._resolveFilename (node:internal/modules/cjs/loader:1058:27)
      at Function.resolve (node:internal/modules/helpers:127:19)
      at Kernel._Kernel_load (/private/var/folders/gz/zv011yyj62qd1rkfkx54q5zh0000gq/T/tmpqrzgy3_p/lib/program.js:10002:83)
      at /private/var/folders/gz/zv011yyj62qd1rkfkx54q5zh0000gq/T/tmpqrzgy3_p/lib/program.js:9722:181
      at Kernel._Kernel_debugTime (/private/var/folders/gz/zv011yyj62qd1rkfkx54q5zh0000gq/T/tmpqrzgy3_p/lib/program.js:10434:24)

Reproduction Steps

Create CDK Construct Library Using JSII

  1. Create CDK Construct Library using JSII (for our use case, we have used projen and followed similar guidelines used here https://github.com/projen/projen/blob/main/docs/awscdk-construct.md)
  2. Compile library package into Python using jsii-pacmak

Import Library into Python CDK Application

  1. mkdir python-test-app && cd python-test-app
  2. cdk init app --language python
  3. Install library and import into app.py with the code
    import library_name as foo
  4. Run cdk synth

Possible Solution

The main suspect is the package.json file, as the error clearly mentions it not containing "exports" main. There is nothing within our pacakge.json that seems to be incorrect or misaligned from other libraries.

Additional Information/Context

For security purposes, I have not included the full contents of any file. Please follow up with any questions regarding files for information. This library has been confirmed to work for TS and Go applications, however has not worked for Python.

SDK version used

"jsii": "~5.0.0","jsii-pacmak": "^1.83.0","aws-cdk-lib": "2.92.0", "constructs": "10.1.243","typescript": "^5.1.3"

Environment details (OS name and version, etc.)

Mac on Venture. Python Versions 3.7, 3.8, 3.10, 3.11 using venv

johntaormina commented 1 year ago

We were able to work around the error by completely removing the "exports" section from the package.json - this allowed the JSII Assembly

jsii.JSIIAssembly.load()

to not fail.

However, we would still like to use "exports" in the future