Closed gabe-l-hart closed 4 years ago
Some further investigation is showing that this is likely caused due to the name collision with generated
triggering jsii
to fail to properly install the second instance. This section of the jsii
kernel seems to be performing the untar operation directly into the temporary installDir
, but silently ignoring errors. I suspect that the second attempt to untar the package named generated
is silently failing. When I look at the contents of the temporary installDir
after a failed execution, I see the fully expanded package
from the first import, but not the second.
More investigation:
I was able to get my repro case "working" by doing the following steps (not a fix, but hopefully another helpful pointer towards one):
jsii
tarballs, untar it in a temporary directorygenerated
with generatedFoo
in the .jsii
file and package.json
filepackage
directory as generatedFoo@0.0.0.jsii.tgz
and place it alongside generated@0.0.0.jsii.tgz
__init__.py
and the _jsii/__init__.py
with the same generated
-> generatedFoo
replacementI will look into scripting these steps for the time being to unblock my project, but it feels like the fix is probably to update jsii-srcmak
to allow for a flexible naming scheme and to generate some sort of unique name for each import
in the cdk8s
usage.
As a temporary workaround, I'm going to use the following script to refactor the generated imports after the initial generation pass:
#!/usr/bin/env bash
# This script fixes an problem caused by cdk8s + jsii + jsii-srcmak when using
# multiple imports.
#
# Open Issue: https://github.com/awslabs/cdk8s/issues/273
import_path=""
while [[ $# > 0 ]]
do
key="$1"
case $key in
-i|--import-path)
shift
import_path=$(cd $1 && pwd)
;;
*)
echo "Unrecognized option [$key]"
exit 1
;;
esac
shift
done
if [ "$import_path" == "" ]
then
echo "Missing required arg [--import-path]"
exit 1
fi
# Compute a unique name hash for this import based on the content of the
# top-level init (which will be unique)
suffix="$(md5sum $import_path/__init__.py | cut -c1-8)"
replace="generated$suffix"
echo "Replace: [$replace]"
# Function to replace 'generated' everywhere in the given file
function do_replace {
fname=$1
sed -i "s,generated,$replace,g" $fname
}
# Replace in each of the __init__ files
do_replace $import_path/__init__.py
do_replace $import_path/_jsii/__init__.py
# Use a temporary directory to fix the content of the tar file
temp_dir="$(mktemp -d -t cdk8s-fix-XXXXXXXXXX)"
echo "Using temp_dir=$temp_dir"
# Untar the packed content into the temp dir
cd "$temp_dir"
tar -xzvf "$import_path/_jsii/generated@0.0.0.jsii.tgz"
# Replace in the .jsii file and package.json
do_replace package/.jsii
do_replace package/package.json
# Re-tar the file with the new name and put it back into the import dir
new_tar_name="$replace@0.0.0.jsii.tgz"
tar -czvf "$new_tar_name" package
mv "$new_tar_name" "$import_path/_jsii/"
# Remove the old tar file and the temp dir
cd -
rm "$import_path/_jsii/generated@0.0.0.jsii.tgz"
rm -rf $temp_dir
@gabe-l-hart thanks so much for the great report and analysis. Sounds like indeed the issue is in jsii-srcmak. ~I am transferring the issue to srcmak for follow up.~ (since srcmak is in a different repo, we will continue the follow up here).
@campionfellin would you like to take a look at this?
I'll take a look!
Indeed, it does look like this line: jsii-srcmak/src/compile.ts#L51 is the issue here.
Replaced it with a random string, and re-built everything and it seemed to work.
I'll look for a way to replace that generated
with something that makes sense, and I'll update the Python examples. Thanks for the detailed bug report @gabe-l-hart !
Thank you for the investigation! I'll keep an eye out here for an updated release with the new changes.
Description of the bug:
When working on a project with multiple imports (such as the example for CRDs), the
cdk8s import
function works smoothly to generate the full set of definitions, butcdk8s synth
(or invokingapp.synth()
from code) results in ajsii.errors.JavaScriptError
due to conflicting definitions of thegenerated
package innodejs
. I've observed this both in the crd sample and in an internal project, so the behavior seems to be consistent for any project using multipleimports
.There seem to also be some secondary bugs in the
crd
example. The paths generated viacdk8s imports
did not match the coded import paths inmain.py
. I needed to change them fromto
Reproduction Steps:
cd examples/python/crd
cdk8s import
main.py
cdk8s synth
Error Log:
Full Error
``` jsii.errors.JavaScriptError: Error: Type 'generated.ClusterInstallation' not found at Kernel._typeInfoForFqn (/Users/ghart/.local/share/virtualenvs/crd-3rAX9go0/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:8200:19) at Kernel._findCtor (/Users/ghart/.local/share/virtualenvs/crd-3rAX9go0/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7898:31) at Kernel._create (/Users/ghart/.local/share/virtualenvs/crd-3rAX9go0/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7920:33) at Kernel.create (/Users/ghart/.local/share/virtualenvs/crd-3rAX9go0/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7666:21) at KernelHost.processRequest (/Users/ghart/.local/share/virtualenvs/crd-3rAX9go0/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7446:28) at KernelHost.run (/Users/ghart/.local/share/virtualenvs/crd-3rAX9go0/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7384:14) at Immediate._onImmediate (/Users/ghart/.local/share/virtualenvs/crd-3rAX9go0/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7387:37) at processImmediate (internal/timers.js:439:21) The above exception was the direct cause of the following exception: Traceback (most recent call last): File "./main.py", line 49, inEnvironment:
For my internal project, the build environment is configured via
docker
. Here is aDockerfile
that will reproduce the issue with thecrd
example:Framework Version:
ubuntu:bionic
I've also reproduced this running natively on MacOS and in a docker file based onrhel
.Other:
I've been trying to learn the interplay between
cdk8s
,jsii
, and (just discovered)jsii-srcmak
. From what I can tell, the issue is arising because thename
field in thepackage.json
inside of the generatednodejs
package isgenerated
for all packages generated withjsii-srcmak
here. My best guess so far is that thegenerated
node package is conflicting and whichever gets imported first inpython
is the one that gets correctly registered with thenode
process, and all others that are imported from python after that are not correctly imported.This is :bug: Bug Report