# Literate and Artifacts

**URL:** <https://discourse.julialang.org/t/literate-and-artifacts/79808>\
**Category:** General Usage\
**Tags:** literate-programming, artifacts\
**Created:** [April 22, 2022, 2:51am UTC](https://discourse.julialang.org/t/literate-and-artifacts/79808 "2022-04-22T02:51:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cortner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cortner/32/204_2.png) [@cortner](https://discourse.julialang.org/u/cortner)\
**Post date:** [April 22, 2022, 2:51am UTC](https://discourse.julialang.org/t/literate-and-artifacts/79808/1 "2022-04-22T02:51:13Z")

</div>

I seem to be unable to use Literate when the script I’m trying to convert tries to load an artifact. The full setup - all files in the same folder - is printed at the bottom of this post.

- If I execute the script by activating the package and then using `include("test.jl")` it executes correctely.
- If I activate the package followed by

```nohighlight
using Literate
Literate.markdown("test.jl"; execute=true)

```

I get the following error message:

````julia
[ Info: generating markdown page from `~/gits/ACE1pack/tmp/test.jl`
ERROR: LoadError: Cannot locate '(Julia)Artifacts.toml' file when attempting to use artifact 'TiAl_tutorial' in 'Main.var"##292"'
in expression starting at string:1
in expression starting at string:1
when executing the following code block in file `~/gits/ACE1pack/tmp/test.jl`

# ```
data_file = joinpath(artifact"TiAl_tutorial", "TiAl_tutorial.xyz")
# ```

Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:35
 [2] execute_block(sb::Module, block::String; inputfile::String)
   @ Literate ~/.julia/packages/Literate/PIFdc/src/Literate.jl:822
 [3] execute_markdown!(io::IOBuffer, sb::Module, block::String, outputdir::String; inputfile::String, flavor::Literate.DocumenterFlavor, image_formats::Vector{Tuple{MIME, String}})
   @ Literate ~/.julia/packages/Literate/PIFdc/src/Literate.jl:567
 [4] markdown(inputfile::String, outputdir::String; config::Dict{Any, Any}, kwargs::Base.Pairs{Symbol, Bool, Tuple{Symbol}, NamedTuple{(:execute,), Tuple{Bool}}})
   @ Literate ~/.julia/packages/Literate/PIFdc/src/Literate.jl:546
 [5] top-level scope
   @ REPL[6]:1

````

I have found no information anywhere on how to use artifacts within `Literate.jl` - any suggests or pointes to working examples would be greatly appreciated.

DETAILS / FILE CONTENTS

```julia
# Project.toml 
[deps]
LazyArtifacts = "4af54fe1-eca0-43a8-85a7-787d91b784e3"
Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306"

# Artifacts.toml
[TiAl_tutorial]
git-tree-sha1 = "b437d7d5fac4424b8203d0afc31732879d3da5b2"
lazy = true

    [[TiAl_tutorial.download]]
    sha256 = "3677174b6ab4adfd07dc03235b797c88234515df793d8603cce5ab7ce0db637d"
    url = "https://github.com/ACEsuit/ACEData/blob/master/trainingsets/TiAl_tutorial.tar.gz?raw=true"

# test.jl 

# We need LazyArtifacts to obtain a dataset 

using LazyArtifacts

# ... try to obtain the dataset

data_file = joinpath(artifact"TiAl_tutorial", "TiAl_tutorial.xyz")

```

---

<div class="post-metadata">

**Author:** ![cortner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cortner/32/204_2.png) [@cortner](https://discourse.julialang.org/u/cortner)\
**Post date:** [April 22, 2022, 2:54am UTC](https://discourse.julialang.org/t/literate-and-artifacts/79808/2 "2022-04-22T02:54:24Z")

</div>

EDIT: previous message deleted, seems to have been an incorrect guess.

But I’ve now narrowed the problem down to the following:

```nohighlight
using Pkg.Artifacts
block = "data_file = joinpath(artifact\"TiAl_tutorial\", \"TiAl_tutorial.xyz\")"
include_string(Main, block) # same error message 
eval(Meta.parse(block)) # same error message 
data_file = joinpath(artifact"TiAl_tutorial", "TiAl_tutorial.xyz") # works as expected

```

I am now guessing that the `@artifact_str` macro behind the `artifact"..."` syntax sugar is the culprit? If I do the following instead, then Literate generates the MD without problems:

```nohighlight
data_file = joinpath(artifact_path(artifact_hash("TiAl_tutorial", find_artifacts_toml("."))), "TiAl_tutorial.xyz")

```

Not particularly elegant though …

---

<div class="post-metadata">

**Author:** ![cortner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cortner/32/204_2.png) [@cortner](https://discourse.julialang.org/u/cortner)\
**Post date:** [April 22, 2022, 5:10am UTC](https://discourse.julialang.org/t/literate-and-artifacts/79808/3 "2022-04-22T05:10:50Z")

</div>

unless there is something nicer, does this sound like an issue / PR for `Pkg.jl`?

---

<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:** [April 22, 2022, 12:43pm UTC](https://discourse.julialang.org/t/literate-and-artifacts/79808/4 "2022-04-22T12:43:54Z")

</div>

Fixed in [Literate.jl#195](https://github.com/fredrikekre/Literate.jl/pull/195) included in [Literate version 2.13.2](https://github.com/fredrikekre/Literate.jl/releases/tag/v2.13.2).

---

<div class="post-metadata">

**Author:** ![cortner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cortner/32/204_2.png) [@cortner](https://discourse.julialang.org/u/cortner)\
**Post date:** [April 22, 2022, 2:16pm UTC](https://discourse.julialang.org/t/literate-and-artifacts/79808/5 "2022-04-22T14:16:48Z")

</div>

thank you for the amazing turnaround. So just to check that I understand correctly - if I had somehow specified the working directory or output directory manually it would still have worked? From `cd(config["literate_outputdir"])` I gather I should set `literate_outputdir`? This requires that the artifact is in that directory (or a parent)?

---

<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:** [April 22, 2022, 11:54pm UTC](https://discourse.julialang.org/t/literate-and-artifacts/79808/6 "2022-04-22T23:54:53Z")

</div>

No that was a separate bug I found when fixing this, not related to your problem. `@artifact_str` looks for `Artifacts.toml` upwards in the file hierarchy starting with the file (`@ __FILE__ `), not the working directory.

---

<div class="post-metadata">

**Author:** ![cortner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cortner/32/204_2.png) [@cortner](https://discourse.julialang.org/u/cortner)\
**Post date:** [April 23, 2022, 3:15am UTC](https://discourse.julialang.org/t/literate-and-artifacts/79808/7 "2022-04-23T03:15:53Z")

</div>

Ok - thank you
