Obtaining the path of the application program?

31 views
Skip to first unread message

Jeff Henrikson

unread,
Aug 26, 2021, 10:51:40 AM8/26/21
to Racket Users

Racket users,

Many software applications use their location in the filesystem to establish filesystem relationships between their executable file, their configuration and their data.  For this purpose, C provides the first element of the string array provided to main, python provides __file__, bash provides ${BASH_SOURCE[0]}, etc.

Racket strips said value before current-command-line-arguments receives the subsequent command-line arguments from the operating system.

A related value can be pulled from syntax-source as follows:

(define-syntax (get-current-source stx)
  (datum->syntax
   (quote-syntax here)
   (syntax-source stx)
   stx))

Macro get-current-source works as intended if called at program startup and the program is compiled on the fly with:

racket program.rkt

However get-current-source has the unintended property of memorizing the original source location if the program is compiled with raco exe (and then moved).

Is there a way in racket to obtain the path of the application program?


Jeff Henrikson


Matthew Flatt

unread,
Aug 26, 2021, 11:15:02 AM8/26/21
to Jeff Henrikson, Racket Users
The analog to the first argument to main in C is

(find-system-path 'run-file)

I think that's probably what you want.


Something closer to `get-current-source` but adapting to a run-time
file is

(variable-reference->module-source
(#%variable-reference))

Here, `(#%variable-reference)` is the primitive form that obtains a
connection to the enclosing module at run time. If you create an
executable with `raco exe`, though, the "source" of the module will be
a symbolic name instead of a path, because a module that's embedded in
an executable doesn't have a file identity anymore.
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email
> to racket-users...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/racket-users/a076dfa2-d1a5-3cda-72ae-a30cdb91d
> c31%40gmail.com.

Ryan Culpepper

unread,
Aug 26, 2021, 11:17:51 AM8/26/21
to Jeff Henrikson, Racket Users
Usually, if you want to refer to data files etc relative to the current module's file, a good solution is to use `define-runtime-path`. It uses `#%variable-reference` as the basis for its implementation, but adds other features like cooperation with `raco exe` (see docs for `define-runtime-path` near "an executable creator").

Ryan


Jeff Henrikson

unread,
Aug 27, 2021, 4:06:47 PM8/27/21
to Matthew Flatt, Racket Users, Ryan Culpepper
> On 8/26/21 8:14 AM, Matthew Flatt wrote:
>> The analog to the first argument to main in C is
>>
>> (find-system-path 'run-file)
>>
>> I think that's probably what you want.
Yes, this has the behavior I want.  Thank you.

Procedure find-system-path seems well documented once one finds its
section.  But not knowing what to search for, I found it hard to find in
the documentation.  It seems like one easy concrete discoverability
improvement would be to mention (find-system-path 'run-file) in the docs
for current-command-line-arguments, command-line, or both.  I was sure
to look carefully in those places.

Regards,


Jeff Henrikson
Reply all
Reply to author
Forward
0 new messages