# Create new file using compiler plugin

**URL:** <https://discuss.kotlinlang.org/t/create-new-file-using-compiler-plugin/30225>\
**Category:** Support\
**Created:** [April 13, 2025, 11:57am UTC](https://discuss.kotlinlang.org/t/create-new-file-using-compiler-plugin/30225 "2025-04-13T11:57:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sksamuel](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/sksamuel/32/1904_2.png) [@sksamuel](https://discuss.kotlinlang.org/u/sksamuel)\
**Post date:** [April 13, 2025, 11:57am UTC](https://discuss.kotlinlang.org/t/create-new-file-using-compiler-plugin/30225/1 "2025-04-13T11:57:56Z")

</div>

Not having much luck trying to figure out how to add a new file via a compiler plugin (the reason is I need to add a top level function with a specific package name, so I can’t just add a function to an existing file).

I’ve tried creating a IrFileImpl and adding it to the module fragment, but then the compiler complains that source files != ir files.

I’ve tried using buildFile {} inside IrGenerationExtension but that returns an FirFile whereas the module framegment is existing an IrFile.

Just looking if anyone can point me in the right direction - some docs or a project that’s doing the same thing that I can draw inspiration from.

---

<div class="post-metadata">

**Author:** ![xyzboom](https://avatars.discourse-cdn.com/v4/letter/x/f0a364/32.png) [@xyzboom](https://discuss.kotlinlang.org/u/xyzboom)\
**Post date:** [April 16, 2025, 5:55am UTC](https://discuss.kotlinlang.org/t/create-new-file-using-compiler-plugin/30225/2 "2025-04-16T05:55:52Z")

</div>

Try KSP, that is easier.

> **[GitHub - google/ksp: Kotlin Symbol Processing API](https://github.com/google/ksp/tree/main)**
>
> main
