# How to find an indirect dependency inside a module?

**URL:** https://discourse.julialang.org/t/how-to-find-an-indirect-dependency-inside-a-module/82559
**Category:** Internals & Design
**Tags:** pkg
**Created:** [June 10, 2022, 11:03am UTC](https://discourse.julialang.org/t/how-to-find-an-indirect-dependency-inside-a-module/82559 "2022-06-10T11:03:55Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![rikh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rikh/32/204104_2.png) [@rikh](https://discourse.julialang.org/u/rikh)
#### Post date: [June 10, 2022, 11:03am UTC](https://discourse.julialang.org/t/how-to-find-an-indirect-dependency-inside-a-module/82559/1 "2022-06-10T11:03:56Z")

</div>

Say that I have a package `Foo` with direct and indirect dependencies `A <- B <- C` (i.e., A loads B and B loads C) and my package looks like

```julia
module Foo

using A

[...]

end

```

also say that I’m given a type `T` in `C` printed as `C.T` as a `String`.

What is now the easiest way to find `C`? Because if I do `@show C`, then I get a “C undefined” error. I can do `@show A.B.C`, but then I first need to find it

The reason that I ask this is because I’m experimenting a bit with the `--trace-compile` flag.

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [June 10, 2022, 11:06am UTC](https://discourse.julialang.org/t/how-to-find-an-indirect-dependency-inside-a-module/82559/2 "2022-06-10T11:06:03Z")

</div>

```julia
help> parentmodule
[...]
  ──────────────────────────────────────────────────────────────────────

  parentmodule(t::DataType) -> Module

  Determine the module containing the definition of a (potentially
  UnionAll-wrapped) DataType.

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  julia> module Foo
             struct Int end
         end
  Foo
  
  julia> parentmodule(Int)
  Core
  
  julia> parentmodule(Foo.Int)
  Foo

[...]

```

---

<div class="post-metadata">

### Author: ![rikh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rikh/32/204104_2.png) [@rikh](https://discourse.julialang.org/u/rikh)
#### Post date: [June 10, 2022, 11:09am UTC](https://discourse.julialang.org/t/how-to-find-an-indirect-dependency-inside-a-module/82559/3 "2022-06-10T11:09:08Z")

</div>

Close, but what if I have a String object and not a DataType?

---

<div class="post-metadata">

### Author: ![rikh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rikh/32/204104_2.png) [@rikh](https://discourse.julialang.org/u/rikh)
#### Post date: [June 10, 2022, 11:12am UTC](https://discourse.julialang.org/t/how-to-find-an-indirect-dependency-inside-a-module/82559/4 "2022-06-10T11:12:03Z")

</div>

Ah. Think that I got it via `Base.loaded_modules`

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [June 10, 2022, 11:12am UTC](https://discourse.julialang.org/t/how-to-find-an-indirect-dependency-inside-a-module/82559/5 "2022-06-10T11:12:21Z")

</div>

I’d first question why you only have that as a string and see if you can get the type directly instead, but otherwise `parentmodule(eval(Meta.parse(...)))`?

Getting a proper type object first is of course preferrable - though your example with `@show C` implies that you want to already have some kind of reference to a type in the first place, right?

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [June 10, 2022, 11:13am UTC](https://discourse.julialang.org/t/how-to-find-an-indirect-dependency-inside-a-module/82559/6 "2022-06-10T11:13:58Z")

</div>

> [@rikh](#):
>
> Ah. Think that I got it via `Base.loaded_modules`

That looks like an internal variable to me - I’d look through the `Pkg` API for querying what dependencies the current active project has instead.

---

<div class="post-metadata">

### Author: ![rikh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rikh/32/204104_2.png) [@rikh](https://discourse.julialang.org/u/rikh)
#### Post date: [June 10, 2022, 11:15am UTC](https://discourse.julialang.org/t/how-to-find-an-indirect-dependency-inside-a-module/82559/7 "2022-06-10T11:15:34Z")

</div>

> [@Sukera](#):
>
> Getting a proper type object first is of course preferrable - though your example with `@show C` implies that you want to already have some kind of reference to a type in the first place, right?

Yeah true. My example wasn’t the clearest. I didn’t have a ref yet.

> [@Sukera](#):
>
> I’d first question why you only have that as a string and see if you can get the type directly instead, but otherwise `parentmodule(eval(Meta.parse(...)))`?

Because that is the output of `julia --trace-compile=traces.jl [...]`.

> [@Sukera](#):
>
> That looks like an internal variable to me - I’d look through the `Pkg` API for querying what dependencies the current active project has instead.

Check. I’ll try that

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [June 10, 2022, 11:45am UTC](https://discourse.julialang.org/t/how-to-find-an-indirect-dependency-inside-a-module/82559/8 "2022-06-10T11:45:03Z")

</div>

> [@rikh](#):
>
> Because that is the output of `julia --trace-compile=traces.jl [...]`.

Ah, I see 🤔 Sadly, that part lives in C (check `src/gf.c:2013`, the function is called `record_precompile_statement` and it’s called during compilation), so getting a type out there is difficult… Then I don’t think there’s a way around `eval(Meta.parse(...))`.

I do wish the compiler would expose some sort of diagnostics interface, for interacting with these sorts of things programmatically. Other kinds of diagnostics I’d be interested in would be getting a log of inlining/vectorization/etc. decisions and the reasoning why something was (not) done, without having to reparse emitted LLVM IR.

---

<div class="post-metadata">

### Author: ![rikh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rikh/32/204104_2.png) [@rikh](https://discourse.julialang.org/u/rikh)
#### Post date: [June 10, 2022, 12:08pm UTC](https://discourse.julialang.org/t/how-to-find-an-indirect-dependency-inside-a-module/82559/9 "2022-06-10T12:08:06Z")

</div>

> [@Sukera](#):
>
> I do wish the compiler would expose some sort of diagnostics interface, for interacting with these sorts of things programmatically. Other kinds of diagnostics I’d be interested in would be getting a log of inlining/vectorization/etc. decisions and the reasoning why something was (not) done, without having to reparse emitted LLVM IR.

Agreed
