fredrikekre / Literate.jl

Simple package for literate programming in Julia
https://fredrikekre.github.io/Literate.jl
Other
534 stars 61 forks source link

Issue with directories #228

Closed mtiller-jh closed 10 months ago

mtiller-jh commented 10 months ago

I've created the following repo that demonstrates an issue I'm seeing. I'm getting errors of the form:

SystemError: opening file "./other/sample-4.svg": No such file or directory

...and I cannot understand why. The README in the repo explains exactly how to reproduce the issue but the bottom line is that when running scripts from different directories, the behavior of Literate.jl seems to change although it isn't obvious to me why.

Am I doing something obviously wrong? The scripts are nearly identical, the only difference that I can really see is whether the file being executed is in the current directory or not.

jvaverka commented 10 months ago

Not sure as to the root cause, but this issue can be avoided by using the joinpath method instead of relative path strings. These files updates both run successfully.

# run-from-root.jl
using Literate

Literate.markdown(
    joinpath(@__DIR__, "docs", "sample.jl"),
    joinpath(@__DIR__, "other");
    execute=true,
    flavor=Literate.CommonMarkFlavor()
)
# docs/run-from-docs.jl
using Literate

Literate.markdown(
    joinpath(@__DIR__, "sample.jl"),
    joinpath(@__DIR__, "..", "other");
    execute=true,
    flavor=Literate.CommonMarkFlavor()
)
fredrikekre commented 10 months ago

Thanks for the report, this is fixed in release 2.15.1 (https://github.com/JuliaRegistries/General/pull/94947 for the registry PR) by 7fc9958

When executing code Literate cd to the output directory. When writing the image file the un-processed raw output directory was still used. The fact that working case worked was that Literate cd do other, then write the image to ../other, which is undoes the cd and it ended up in the correct directory...

I tend to always be explicit about paths and use absolute paths though, but it should of course work otherwise as well.

mtiller-jh commented 10 months ago

The issue I see here is that by cding to the output directory, any paths referenced in the input files are invalid. For example, if I do:

using Pkg
Pkg.activate(".")

...in my input file, it tries to look for a Project.toml in my output file which is quite counter intuitive. It seems like, when evaluating a Julia script, we should set the current directory to where that Julia script is, no?

fredrikekre commented 10 months ago

I am sure there are arguments for both options, I just copied the behavior from Documenter. The current behavior is nice if you save files, for example, since they end up where the code is executed.

mtiller-jh commented 10 months ago

I see. Yes, that makes sense. The flip side is, of course, what happens if the script needs to read input files. I'm trying to keep my input files and output files in completely different directories. One option could be for the Runlit approach to inject a variable that provides context on where the input files are located. I'll explore that. Thanks.

mtiller-jh commented 10 months ago

OK, I resolved it for myself at least.