How to set the working directory in DrRacket on Windows?

I've developed my app on Linux and run it like this:

projects/new> racket -t src/main.rkt

Now I want to run it on Windows and I'm getting this error:

open-input-file: cannot open input file
  path: ...\projects\new\src\assets\world.json
  system error: The system cannot find the path specified.; win_err=3

That makes sense, since the assets folder is not in src. This is my folder structure:

new/
 *  src/
     *  main.rkt
     *  ...
 *  assets/
     *  world.json
     *  ...

How can I tell DrRacket that the current working directory should be projects/new and not projects/new/src?

I would like to do this not in code, but more like an environment variable setup.

You need to use a "runtime path".
These are resolved relative to the source path,
not the current path.

I don't get it. I've read the documentation for define-runtime-path, but in the first example given (data.txt) it's also just a relative path. What's the point?

And furthermore, it seems that I would have to do that for every single file I want to open. That seems quite cumbersome...

If I'm making my life unnecessary hard with this folder structure, I wouldn't mind changing it. However, I'd like somehting a bit more sophisticated than just putting everything in a single folder.

I suppose you could do this:

(define project-root
  (or (getenv "RACKET_PROJECT_ROOT")
      (current-directory))) ; fallback if not set

(current-directory project-root)
1 Like
  1. The point is that when you use define-runtime-path, nothing depends on the current working directory. You use a relative path there, and it always opens the path relative to that source file.
  2. To avoid doing it for every distinct file, use define-runtime-path to specify a directory such as assets and then use build-path together with that path.
1 Like

I must've missed that the path for define-runtime-path is relative to the source file and not the working directory. With that information it makes perfect sense. Thank you for pointing that out.

Will this still work once I create an executable? The executable will be in new/.

Yes, one of the big advantages is that it works with executables. If you plan to move to a different place, you may also need raco distribute.

1 Like