@@ -678,6 +678,8 @@ func (c *FileCompiler) Compile(ctx context.Context) error {
678678
679679 // Write any imports that were added during analysis (e.g., for promoted methods)
680680 // but don't appear in the source AST
681+ // Note: c.Analysis.Imports is package-wide, so we need to filter to only imports
682+ // that are actually used in this specific file
681683 writtenImports := make (map [string ]bool )
682684 // Track imports that will be written from the AST
683685 for _ , imp := range f .Imports {
@@ -693,17 +695,47 @@ func (c *FileCompiler) Compile(ctx context.Context) error {
693695 }
694696 }
695697 }
696- // Write imports from analysis that aren't in the AST
697- var additionalImports []string
698- for pkgName := range c .Analysis .Imports {
699- if ! writtenImports [pkgName ] && pkgName != "$" {
700- additionalImports = append (additionalImports , pkgName )
698+ // Write any imports that were added during analysis (e.g., for promoted methods)
699+ // Check if this file defines any structs with embedded fields
700+ fileHasEmbeddedStructs := false
701+ for _ , decl := range f .Decls {
702+ genDecl , ok := decl .(* ast.GenDecl )
703+ if ! ok || genDecl .Tok != token .TYPE {
704+ continue
705+ }
706+ for _ , spec := range genDecl .Specs {
707+ typeSpec , ok := spec .(* ast.TypeSpec )
708+ if ! ok {
709+ continue
710+ }
711+ structType , ok := typeSpec .Type .(* ast.StructType )
712+ if ! ok {
713+ continue
714+ }
715+ // Check if any field is embedded
716+ for _ , field := range structType .Fields .List {
717+ if len (field .Names ) == 0 { // Embedded field
718+ fileHasEmbeddedStructs = true
719+ break
720+ }
721+ }
722+ if fileHasEmbeddedStructs {
723+ break
724+ }
725+ }
726+ if fileHasEmbeddedStructs {
727+ break
701728 }
702729 }
703- sort .Strings (additionalImports )
704- for _ , pkgName := range additionalImports {
705- fileImp := c .Analysis .Imports [pkgName ]
706- c .codeWriter .WriteImport (pkgName , fileImp .importPath + "/index.js" )
730+
731+ // If this file has embedded structs, write imports from analysis that aren't in AST
732+ if fileHasEmbeddedStructs {
733+ for pkgName , fileImport := range c .Analysis .Imports {
734+ if ! writtenImports [pkgName ] {
735+ c .codeWriter .WriteLinef ("import * as %s from \" %s/index.js\" " , pkgName , fileImport .importPath )
736+ writtenImports [pkgName ] = true
737+ }
738+ }
707739 }
708740
709741 c .codeWriter .WriteLine ("" ) // Add a newline after imports
0 commit comments