Adding support for file:@Import to embedded Kotlin scripts

We have a Kotlin DSL and load Kotlin scripts at runtime in an application.
I have a version that works, based on the jvm-simple-script example (calling evalFile() to load a script from the application at runtime), however we would like to be able to share utility code between scripts, for example using @file:Import().

I tried changing the implementation to be based on simple-main-kts example, however attempts to use


result in this error: “This annotation is not applicable to target ‘expression’ and use site target ‘@file’”

Any idea what causes this error? The evalFile() function is based on the one in the in the test, and the scripts need to use the classpath of the running thread (plus anything defined by the annotations in the scripts).
(This is using Kotlin 1.6.21. The main application is in Scala and Java-17.)

It looks like the solution was to use the @file:Import() statement before the package declaration as the first statement in the script.