No such file when using backticks for running external program

Hello,

I’m sorry to bother with such a seemingly trivial problem but I’m stuck with a stupid problem. I have a simple file that I’d like to read.

open(path) do f
       readdlm(f,header=true)[1]
end

where path contains the absolute path to the file. This command works perfectly fine as the matrix contained in the file is correctly returned.
However, if I use the same path to call cat, then it pretends the file is non-existent.

julia> `cat $path`
`cat 'C:\Users\dehaybe\OneDrive - UCL\Doctorat\julia\BalancedMinimumEvolution\data\01-Primates12.txt'`

julia> run(`cat $path`)
ERROR: IOError: could not spawn `cat 'C:\Users\dehaybe\OneDrive - UCL\Doctorat\julia\BalancedMinimumEvolution\data\01-Primates12.txt'`: no such file or directory (ENOENT)
Stacktrace:
 [1] _spawn_primitive(file::String, cmd::Cmd, stdio::Vector{Union{RawFD, Base.Libc.WindowsRawSocket, IO}})
   @ Base .\process.jl:128
 [2] #784
   @ .\process.jl:139 [inlined]
 [3] setup_stdios(f::Base.var"#784#785"{Cmd}, stdios::Vector{Union{RawFD, Base.Libc.WindowsRawSocket, IO}})
   @ Base .\process.jl:223
 [4] _spawn
   @ .\process.jl:138 [inlined]
 [5] run(::Cmd; wait::Bool)
   @ Base .\process.jl:479
 [6] run(::Cmd)
   @ Base .\process.jl:477
 [7] top-level scope
   @ REPL[47]:1

So, how is this possible ?

Are you sure there is such a command on Windows?

1 Like

It works for me in the Windows Terminal but can’t get it to work in run either, so perhaps the Windows Terminal does something fancy? But Julia is running in the Windows Terminal, so shouldn’t that be where this command is being sent?

cat is available in the power shell, but not in cmd. So make sure you are running julia from the power shell.

Edit: @kristoffer.carlsson is right, cat is a commandlet in powershell, and it probably worked for me only because I run julia from bash.

That is not how Julia commands work. run invokes an executable, they do not run through any shell.

This is a good talk about it

3 Likes

Thank you but really cat is an MWE. In practice, I have this problem with an ExecutableProduct that I obtain from a jll package.

If foo is the product, I have that

open(path) do f
       readdlm(f,header=true)[1]
end

works (so the path is correct).

run(`$(foo()) -h`)

works (so the ExecutableProduct works).
But

run(`$(foo()) --input $path`)

does not. So I’m guessing the problem is with path, when interpolated in a Cmd.

What is the exact error you get with your real code?

function fastme_test()
    run(`$(fastme()) $("-i data/01-Primates12.txt")`)
end
julia> fastme_test()

 . Error: -i option: ' data\01-Primates12.txt' file does not exist.
ERROR: failed process: Process(setenv(`'C:\Users\Henri\.julia\artifacts\a68fec90869961d649fa73d3871638e4acbb44b5\bin\fastme.exe' '-i data\01-Primates12.txt'`,["WINDIR=C:\\WINDOWS", "PATH=C:\\Users\\Henri\\.julia\\juliaup\\julia-1.10.0+0.x64.w64.mingw32\\bin;C:\\Users\\Henri\\.julia\\juliaup\\julia-1.10.0+0.x64.w64.mingw32\\bin\\..\\lib\\julia;C:\\Users\\Henri\\.julia\\juliaup\\julia-1.10.0+0.x64.w64.mingw32\\bin\\..\\lib;C:\\Users\\Henri\\.julia\\juliaup\\julia-1.10.0+0.x64.w64.mingw32\\bin;C:\\Program Files (x86)\\gurobi1100\\win64\\bin;C:\\xpressmp\\bin;C:\\xpressmp\\workbench;C:\\Program Files\\xpressmp\\bin;C:\\Program Files\\xpressmp\\workbench;C:\\Program Files\\xpress\\bin;C:\\Program Files\\xpress\\workbench;C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Windows\\System32\\OpenSSH\\;C:\\Program Files\\Git\\cmd;C:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common;C:\\Program Files (x86)\\ZeroTier\\One\\;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;C:\\WINDOWS\\System32\\OpenSSH\\;C:\\Program Files\\NVIDIA Corporation\\NVIDIA NvDLISR;C:\\Program Files\\dotnet\\;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;C:\\WINDOWS\\System32\\OpenSSH\\;C:\\Users\\Henri\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Users\\Henri\\AppData\\Local\\GitHubDesktop\\bin;C:\\Users\\Henri\\AppData\\Local\\Programs\\Microsoft VS Code\\bin;C:\\Users\\Henri\\AppData\\Local\\Programs\\MiKTeX\\miktex\\bin\\x64\\;C:\\Users\\Henri\\AppData\\Local\\gitkraken\\bin;C:\\Users\\Henri\\AppData\\Local\\Microsoft\\WindowsApps", "USERDOMAIN_ROAMINGPROFILE=DESKTOP-BI15UF5", "KEYSHOT_EXTERNAL_LICENSE_FOLDER=C:\\Program Files\\DesignSpark\\DesignSpark Mechanical 5.0\\", "LOCALAPPDATA=C:\\Users\\Henri\\AppData\\Local", "HOMEPATH=\\Users\\Henri", "PROCESSOR_IDENTIFIER=AMD64 Family 25 Model 33 Stepping 0, AuthenticAMD", "NUMBER_OF_PROCESSORS=24", "GIT_ASKPASS=c:\\Users\\Henri\\AppData\\Local\\Programs\\Microsoft VS Code\\resources\\app\\extensions\\git\\dist\\askpass.sh", "ANS_OLD_ATTACH=1"  …  "PROCESSOR_LEVEL=25", "SYSTEMDRIVE=C:", "=C:=C:\\Users\\Henri\\OneDrive - UCL\\Doctorat\\julia\\BalancedMinimumEvolution", "FPS_BROWSER_APP_PROFILE_STRING=Internet Explorer", "PROGRAMW6432=C:\\Program Files", "TEMP=C:\\Users\\Henri\\AppData\\Local\\Temp", "HOMEDRIVE=C:", "OPENBLAS_MAIN_FREE=1", "PROCESSOR_ARCHITECTURE=AMD64", "COLORTERM=truecolor"]), ProcessExited(1)) [1]

Stacktrace:
 [1] pipeline_error
   @ Base .\process.jl:565 [inlined]
 [2] run(::Cmd; wait::Bool)
   @ Base .\process.jl:480
 [3] run
   @ .\process.jl:477 [inlined]
 [4] fastme_test()
   @ Main c:\Users\Henri\OneDrive - UCL\Doctorat\julia\BalancedMinimumEvolution\src\fastme.jl:5
 [5] top-level scope
   @ REPL[3]:1

but the file does exist:

julia> open("data/01-Primates12.txt") do f
           D = readdlm(f, header = true)[1]
           end
12×12 Matrix{Float64}:
...

This is wrong, it forces -i filename to be a single string. This means the executable will receive it as a single argument while it expects a -i argument followed by a filename argument. Try this instead:

run(`$(fastme()) -i data/01-Primates12.txt`)

Ha thanks, it works. I think I was mislead by some documentation I misunderstood.

A good thing to remember is that if you get a different error with an MWE, it’s often not representative of your real problem.

2 Likes