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

Construct compiled to .NET throws "System.Exception: Cannot use import statement outside a module" when used #4510

Closed
hakenmt opened this issue May 10, 2024 · 2 comments
Labels
bug This issue is a bug. needs-triage This issue or PR still needs to be triaged.

Comments

@hakenmt
Copy link

hakenmt commented May 10, 2024

Describe the bug

An unhandled exception is thrown when using a .NET construct compiled using jsii. No idea what is causing this (exception is being thrown at var app = new App();. I don't import anything in the project that isn't from the CDK library except for path and fs. I've tried setting "type": "module" in my package.json, but it doesn't prevent this error from occurring. The stack trace doesn't really appear to be helpful in determining the cause.

Expected Behavior

Not to receive an exception.

Current Behavior

Unhandled exception. System.Exception: Cannot use import statement outside a module
at Amazon.JSII.Runtime.Services.Client.TryDeserialize[TResponse](String responseJson)
at Amazon.JSII.Runtime.Services.Client.ReceiveResponseTResponse
at Amazon.JSII.Runtime.Services.Client.Send[TRequest,TResponse](TRequest requestObject)
at Amazon.JSII.Runtime.Services.Client.Load(LoadRequest request)
at Amazon.JSII.Runtime.Services.Client.Load(String name, String version, String tarball)
at Amazon.JSII.Runtime.Services.Client.LoadPackage(String package, String version, String tarballPath)
at Amazon.JSII.Runtime.Deputy.JsiiTypeAttributeBase.Load(Assembly assembly)
at Amazon.JSII.Runtime.Deputy.JsiiTypeAttributeBase..ctor(Type nativeType, String fullyQualifiedName)
at Amazon.JSII.Runtime.Deputy.JsiiClassAttribute..ctor(Type nativeType, String fullyQualifiedName, String parametersJson)
at System.Reflection.CustomAttribute._CreateCaObject(RuntimeModule pModule, RuntimeType type, IRuntimeMethodInfo pCtor, Byte** ppBlob, Byte* pEndBlob, Int32* pcNamedArgs)
at System.Reflection.CustomAttribute.CreateCaObject(RuntimeModule module, RuntimeType type, IRuntimeMethodInfo ctor, IntPtr& blob, IntPtr blobEnd, Int32& namedArgs)
at System.Reflection.CustomAttribute.AddCustomAttributes(ListBuilder1& attributes, RuntimeModule decoratedModule, Int32 decoratedMetadataToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, ListBuilder1 derivedAttributes)
at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeType type, RuntimeType caType, Boolean inherit)
at System.Attribute.GetCustomAttributes(MemberInfo element, Type attributeType, Boolean inherit)
at System.Reflection.CustomAttributeExtensions.GetCustomAttribute[T](MemberInfo element)
at Amazon.JSII.Runtime.Services.TypeCache.CacheTypes(Assembly assembly)
at Amazon.JSII.Runtime.Services.TypeCache.Refresh()
at Amazon.JSII.Runtime.Services.TypeCache.TryGetType[T](String fullyQualifiedName)
at Amazon.JSII.Runtime.Services.TypeCache.TryGetInterfaceType(String fullyQualifiedName)
at Amazon.JSII.Runtime.Services.Converters.ValueConverter.g__IsReferenceType|17_0(<>c__DisplayClass17_0&)
at Amazon.JSII.Runtime.Services.Converters.ValueConverter.TryConvertCustomType(Type type, IReferenceMap referenceMap, Object value, Boolean isOptional, String fullyQualifiedName, Object& result)
at Amazon.JSII.Runtime.Services.Converters.ValueConverter.TryConvert(IOptionalValue optionalValue, Type type, IReferenceMap referenceMap, Object value, Object& result)
at Amazon.JSII.Runtime.Services.Converters.FrameworkToJsiiConverter.TryConvert(IOptionalValue optionalValue, IReferenceMap referenceMap, Object value, Object& result)
at Amazon.JSII.Runtime.Deputy.DeputyBase.<>c__DisplayClass20_0.b__0(Parameter parameter, Object frameworkArgument)
at System.Linq.Enumerable.ZipIterator[TFirst,TSecond,TResult](IEnumerable1 first, IEnumerable1 second, Func3 resultSelector)+MoveNext() at System.Collections.Generic.LargeArrayBuilder1.AddRange(IEnumerable1 items) at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable1 source)
at Amazon.JSII.Runtime.Deputy.DeputyBase.ConvertArguments(Parameter[] parameters, Object[] arguments)
at Amazon.JSII.Runtime.Deputy.DeputyBase..ctor(DeputyProps props)
at Constructs.Construct..ctor(DeputyProps props)
at Amazon.CDK.Stage..ctor(DeputyProps props)
at Amazon.CDK.App..ctor(IAppProps props)
at Program.Main(String[] args) in /Users/mhaken/source/workshop/cdk/Program.cs:line 16

Reproduction Steps

Add the nuget package and run var app = new App()

Possible Solution

No response

Additional Information/Context

No response

SDK version used

5.4.9 (build 91580f2)

Environment details (OS name and version, etc.)

darwin

@hakenmt hakenmt added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels May 10, 2024
@hakenmt
Copy link
Author

hakenmt commented May 10, 2024

Going to answer my own question in case this comes up in a search. I was exporting a bunch of classes and interfaces in my "main" file. In a few other .ts files i was importing from this main file, effectively making a circular import/export. Importing directly from the appropriate file containing the class fixed the problem.

@hakenmt hakenmt closed this as completed May 10, 2024
Copy link
Contributor

This issue is now closed. Comments on closed issues are hard for our team to see.
If you need more assistance, please open a new issue that references this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. needs-triage This issue or PR still needs to be triaged.
Projects
None yet
Development

No branches or pull requests

1 participant