Wrong path aggregation with `include` under vscode - terminal session is fine

hi Path aggregation seems to have declared independence from my instructions. Here the problem (with vscode Repl)

The bad behavior

julia> cd("C:\\proj\\Rprojs\\PS1908\\julia\\21T4_vormdp")
       using Pkg
       Pkg.activate(".")
  Activating environment at `C:\proj\Rprojs\PS1908\julia\21T4_vormdp\Project.toml`

julia> pwd()
"C:\\proj\\Rprojs\\PS1908\\julia\\21T4_vormdp"

julia> include("./src/orders.jl")
ERROR: LoadError: SystemError: opening file "c:\\proj\\Rprojs\\PS1908\\julia\\21T4_vormdp\\mains\\src\\orders.jl": No such file or directory
Stacktrace:
  [1] systemerror(p::String, errno::Int32; extrainfo::Nothing)
    @ Base .\error.jl:168
  • the include instruction should try to open the combination of

    • C:\\proj\\Rprojs\\PS1908\\julia\\21T4_vormdp
    • with ./src/orders.jl
  • but instead it tries to open a different file…c:\\proj\\Rprojs\\PS1908\\julia\\21T4_vormdp\\mains\\src\\orders.jl

The same from a terminal session- works fine.

PS C:\proj\Rprojs\PS1908\julia\21T4_vormdp> julia
               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.6.1 (2021-04-23)
 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

julia> cd("C:\\proj\\Rprojs\\PS1908\\julia\\21T4_vormdp")

julia> using Pkg

julia> Pkg.activate(".")
  Activating environment at `C:\proj\Rprojs\PS1908\julia\21T4_vormdp\Project.toml`       

julia> pwd()
"C:\\proj\\Rprojs\\PS1908\\julia\\21T4_vormdp"

julia> include("./src/orders.jl")
Main.orders

  • Would you know about this? Does vscode maintain a separate “current” directory? like a “shadow” pwd?

How did you run the include("./src/orders.jl") line? Via inline evaluation or by typing it into the REPL?

Nice catch! It was indeed via inline evaluation. Here below a vscode REPL session using include 3 times:

  • first by direct typing (success, include works),
  • then by inline evaluation (error)
  • then by typing again (success).

This gives an obvious workaround. Do you also know an easy fix to the “inline” way?

Thank you!

julia> cd("C:\\proj\\Rprojs\\PS1908\\julia\\21T4_vormdp")

julia> using Pkg

julia> Pkg.activate(".")
  Activating environment at `C:\proj\Rprojs\PS1908\julia\21T4_vormdp\Project.toml`

julia> include("./src/orders.jl") # direct typing
Main.orders

julia> 

julia> include("./src/orders.jl") #inline
ERROR: LoadError: SystemError: opening file "c:\\proj\\Rprojs\\PS1908\\julia\\21T4_vormdp\\mains\\src\\orders.jl": No such file or directory    
Stacktrace:
  [1] systemerror(p::String, errno::Int32; extrainfo::Nothing)
    @ Base .\error.jl:168
  [2] #systemerror#62
    @ .\error.jl:167 [inlined]
  
[..snipped...]

julia> include("./src/orders.jl") # direct typing
WARNING: replacing module orders.
Main.orders


```

I’d say this is working as intended and there’s nothing to fix there :slight_smile:

Generally, Julia will resolve includes relative to @__DIR__, which is the directory of whatever file you’re in – unless you’re using the REPL, in which case it’s relative to pwd().

So you really should make sure to specify include paths that way to make sure your file works fine when calling it with e.g. julia main.jl.

1 Like

Indeed that’s it exactly!
Thank you!