2

I am trying to create a documentation for a Julia module using Documenter.jl. Now I imported a module which documenter cannot find for some reason. More explicitly: I imported SparseArrays.jl via import SparseArrays and am referencing SparseArrays.AbstractSparseArray in a docstring. (I also have SparseArrays.jl installed.) Yet I get ERROR: LoadError: UndefVarError: SparseArrays not defined. What's the reason and how can I fix this?

EDIT: This is what the relevant parts of the code look like:

module ExampleModule
import SparseArrays
include("example.jl")
end

example.jl:

"""
    f
does stuff.
"""
function f(x::SparseArrays.AbstractSparseArray)
    return
end

index.md:

```@docs
f(x::SparseArrays.AbstractSparseArray)
```
BallpointBen
  • 9,406
  • 1
  • 32
  • 62
joinijo
  • 353
  • 2
  • 9

1 Answers1

1

Most likely you have imported it in a separate code block. See here for an explanation of the issue.

Also you might need to add import SparseArrays in setup code as explained here. This is needed if e.g. you have doctests inside docstrings.

Here is an example how it is done in DataFrames.jl (in general DataFrames.jl has doctests enabled both in docstrings and in documentation code so you can have a look at the whole setup we have there).

If this is not the reason then could you please share your code in the question so that it can be inspected?

Bogumił Kamiński
  • 66,844
  • 3
  • 80
  • 107
  • I'm not sure I get what you mean. I have imported it at the beginning of the module. It should be useable within a docstring within the module then, no? I'll add the important parts of the code to the question. – joinijo Jun 06 '21 at 16:47
  • I have added an additional comment - I hope it helps. – Bogumił Kamiński Jun 06 '21 at 16:54
  • My question wasn't precise I suppose, I am not actually referencing it inside the docstring. I just have a docstring for a function that takes as input an object of that type. And that makes `documenter` throw an error. Do you see anything wrong with my code above? – joinijo Jun 06 '21 at 16:59
  • This is strange (I assume you have Project.toml properly set up for documenter). I would open an issue in Documenter.jl for this. – Bogumił Kamiński Jun 06 '21 at 19:21
  • I also just added the relevant code in the `index.md` file. I don't simply have `f` in the @docs environment but `f(x::SparseArrays.AbstractSparseArray)` because I have different versions of `f` with different docstrings. So `SparseArrays` shows up in the `.md` file. Could that cause the problem and if yes how could I solve it? – joinijo Jun 07 '21 at 11:53
  • I think `Project.toml` is set up correctly but someone else set it up and I'm fairly new to Julia, I'm not really sure what it is supposed to look like. – joinijo Jun 07 '21 at 11:54
  • ok I figured it out, probably a beginner's mistake, I didn't know I have to add `using SparseArrays` to `make.jl`, I thought it's enough if that's contained in the module file. Thanks for your help though. – joinijo Jun 07 '21 at 12:52