# Reexport.@reexport from submodule does not work: how to diagnose?

**URL:** https://discourse.julialang.org/t/reexport-reexport-from-submodule-does-not-work-how-to-diagnose/116285
**Category:** General Usage
**Created:** [June 27, 2024, 3:24am UTC](https://discourse.julialang.org/t/reexport-reexport-from-submodule-does-not-work-how-to-diagnose/116285 "2024-06-27T03:24:18Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![wsshin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsshin/32/360_2.png) [@wsshin](https://discourse.julialang.org/u/wsshin)
#### Post date: [June 27, 2024, 3:24am UTC](https://discourse.julialang.org/t/reexport-reexport-from-submodule-does-not-work-how-to-diagnose/116285/1 "2024-06-27T03:24:18Z")

</div>

I have been using `Reexport.@reexport` in many of my packages and have never had an issue. At the moment, however, it is not working in one package, even though I think it is used in the same way as in my other packages.

The structure of the problematic package looks basically like this:

```julia
module MyModule

export MyType

struct MyType
end

module MySubModule
    using Reexport
    @reexport using ..MyModule

    export MyType2

    struct MyType2
    end

end # MySubModule

end # MyModule

```

The goal here is to make all the exported symbols of the enclosing `MyModule` become available when the submodule `MySubModule` is used. When this works (like my other packages), I am able to do this:

```julia-repl
julia> using MyModule.MySubModule

julia> MyType() # defined in MyModule, but still exported

```

However, at the moment I get

```julia-repl
julia> MyType()
ERROR: UndefVarError: `MyType` not defined
[...]

```

Strangely, I can see that `MyType` is available:

```julia-repl
julia> names(MyModule)
2-element Vector{Symbol}:
 :MyModule
 :MyType

```

Also, qualifying `MyType` with the module name works:

```julia-repl
julia> MyModule.MyType()
MyModule.MyType()

```

In fact, If I define `MyModule` as shown above, it works as intended, but the particular package I am developing is experiencing the issue described here, even though I think its structure is the same as `MyModule`. Maybe I have made some trivial mistakes, but I cannot identify it.

Are there ways to understand what is causing this issue?

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [June 27, 2024, 8:22am UTC](https://discourse.julialang.org/t/reexport-reexport-from-submodule-does-not-work-how-to-diagnose/116285/2 "2024-06-27T08:22:51Z")

</div>

Which Julia version? Perhaps this is [breaking change in `names` · Issue #54885 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/54885#issuecomment-2188152011) if you are using the master branch.

---

<div class="post-metadata">

### Author: ![wsshin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsshin/32/360_2.png) [@wsshin](https://discourse.julialang.org/u/wsshin)
#### Post date: [June 27, 2024, 12:45pm UTC](https://discourse.julialang.org/t/reexport-reexport-from-submodule-does-not-work-how-to-diagnose/116285/3 "2024-06-27T12:45:48Z")

</div>

Sorry, forgot to put the version number. I am not using master:

```julia-repl
julia> VERSION
v"1.10.4"

```

---

<div class="post-metadata">

### Author: ![wsshin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsshin/32/360_2.png) [@wsshin](https://discourse.julialang.org/u/wsshin)
#### Post date: [June 27, 2024, 4:10pm UTC](https://discourse.julialang.org/t/reexport-reexport-from-submodule-does-not-work-how-to-diagnose/116285/4 "2024-06-27T16:10:39Z")

</div>

I realized why. My package was actually like

```julia
module MyModule

module MySubModule
    using Reexport
    @reexport using ..MyModule

    export MyType2

    struct MyType2
    end

export MyType # export after defining MySubModule

struct MyType
end

end # MySubModule

end # MyModule

```

The difference from the code shown previously is that `MyType` is exported and defined after `MySubModule` is defined. So, at the point where `MySubModule` was defined, `MyType` was yet to be exported. This made `@reexport using ..MyModule` in `MySubModule` unaware that `MyType` was exported from `MyModule`.

Exporting `MyType` before defining `MySubModule` removed the error. Here is the working code:

```julia
module MyModule

export MyType # export before defining MySubModule

module MySubModule
    using Reexport
    @reexport using ..MyModule

    export MyType2

    struct MyType2
    end

struct MyType
end

end # MySubModule

end # MyModule

```

The reason I was exporting `MyType` after defining `MySubModule` was because I use the coding style of exporting symbols in the file where they are defined. My actual code was organized into several files such that

```julia
module MyModule

include("mysubmodule.jl") # define MySubModule
include("mytype.jl") # define and export MyType

end # MyModule

```

In the actual code, `mysubmodule.jl` contained types used as the parameters of `MyType`, so I had to include `mytype.jl` after `mysubmodule.jl`. This made `MyType` exported after defining `MySubModule`, because `MyType` was exported in `mytype.jl`.

I know some people export all the symbols in `<package name>.jl` file, such that the contents of the file looks like

```julia
module MyModule

export MyType # exported here, even though defined in mytype.jl

include("mysubmodule.jl") # define MySubModule
include("mytype.jl") # define but not export MyType

end # MyModule

```

I thought exporting symbols in the file where they are defined looks more modular, but maybe I should consider changing my coding style because it has the pitfall explained above…
