File system paths in Meta.parse end up in error on Windows

Is this to be expected? How should it be handled?

Windows 10:

julia> p = pwd()
"C:\\Users\\adrian\\testapp"

julia> Main, Meta.parse("""joinpath("$p", "src")""")
ERROR: Base.Meta.ParseError("invalid escape sequence")
Stacktrace:
 [1] #parse#1(::Bool, ::Bool, ::Bool, ::Function, ::String, ::Int64) at .\meta.jl:129
 [2] #parse at .\none:0 [inlined]
 [3] #parse#4(::Bool, ::Bool, ::Function, ::String) at .\meta.jl:164
 [4] parse(::String) at .\meta.jl:164
 [5] top-level scope at none:0

macOS:

julia> p = pwd()
"/Users/adrian"

julia> Meta.parse("""joinpath("$p", "src")""")
:(joinpath("/Users/adrian", "src"))

That’s annoying indeed, but has an easy fix:

Meta.parse("""joinpath($(repr(p)), "src")""")

repr is pretty handy for such tasks :wink:

1 Like

Wow, that’s a cool function! I didn’t know about it. It does fix it, thanks!

Unless you are just playing with the parser, you shouldn’t use Meta.parse for this. Just interpolate the expression directly. There’s absolutely no way you can make this generic and safe.

1 Like