tl;dr Either of these would work:
- How can I get the path to the currently-running file?
- How can I determine the path to a file relative to the currently running file / my project / something?
I would like to include a file (a Racket program) which is going to be run via Process. As such, I need to know its filepath so that I can give that to the Process object. In any other language I would simply say "give me the filepath of the current file, then adjust that to get to the desired file which is in the same directory" but that doesn't seem to work. I've been digging around online trying to find how to do this and am finally giving up; code that I've found simply returns nil without error message and all other kinds of frustrations.
I have tried a whole lot of variants involving FileManager and Bundle and etc, but my code currently looks like this:
import Foundation
let engine = Process()
let stdout = Pipe()
let stdin = Pipe()
engine.standardOutput = stdout
engine.standardInput = stdin
engine.executableURL = URL(fileURLWithPath: "/Applications/Racket_v8.11.1/bin/racket")
engine.arguments = ["basic.rkt"]
try engine.run()
let outputData = try stdout.fileHandleForReading.readToEnd()
print( outputData)
The output is:
open-input-file: cannot open module file
module path: #<path:/Users/dstorrs/Library/Developer/Xcode/DerivedData/hello-world-bfqcjcdgvknrxefmtpulylvpzuru/Build/Products/Debug/basic.rkt>
path: /Users/dstorrs/Library/Developer/Xcode/DerivedData/hello-world-bfqcjcdgvknrxefmtpulylvpzuru/Build/Products/Debug/basic.rkt
system error: no such file or directory; rkt_err=3
nil