After compiling my Racket application to an executable with raco, I'm finding that namespace-require re-instantiates several modules. Consequently, the namespace I'm trying to load into has its contents split across two different namespace bindings. Is there some setting I can pass to raco to ensure this doesn't happen? The application where this occurs is here:
namespace-require is used only in lang/namespace-requires.rkt.
I'm compiling with the command raco exe --cs -o shen ++lib racket/lang/reader repl.rkt
Notably this behavior does not result if I load repl.rkt into the Racket REPL and call shen-repl from there.
I posted this in Discord first, but I'll answer here too for the benefit of the forum. I've edited out some other potential approaches I considered for fixing the bug, since define-runtime-module-path works very well here.
I debugged basically just by adding a bunch more " instantiated" messages. I'm just gonna share what I've found:
The module lang/interposition-points.rkt requires lang/macros.rkt, which requires lang/packages.rkt, which displays the "packages.rkt instantiated" message that you're seeing twice from the executable.
The executable is based on repl.rkt.
The module repl.rkt requires lang/interposition-points.rkt.
The module repl.rkt requires lang/namespace-requires.rkt, which dynamically requires lang/interposition-points.rkt.
Thus the program loads two different versions of lang/interposition-points.rkt and all the modules it requires: It loads the version of lang/interposition-points.rkt that was bundled into the executable file thanks to (2) and (3), and it also loads the version of lang/interposition-points.rkt relative to the working directory thanks to (2) and (4). Thanks to (1), each of these versions gives you its own "packages.rkt instantiated" message.
Other things that can be observed about this situation, which might indicate good things to check during future debugging:
If the executable is moved into another directory and run from there, the dynamic load of lang/interposition-points.rkt fails, causing the program to exit with an error message.
If the executable is built and then the "packages.rkt instantiated" message in lang/packages.rkt is modified to say "packages.rkt instantiated from another file", then the executable displays both "packages.rkt instantiated" and "packages.rkt instantiated from another file".
As for what to do about it, you could change the dynamic namespace-require calls so they refer to the modules inside the executable file rather than the modules in the current directory. The raco exe docs say "When a module is embedded in an executable, it gets a symbolic name instead of its original filesystem-based name. The module-name resolver is configured in the embedding executable to map collection-based module paths to the embedded symbolic name, but no such mapping is created for filesystem paths. By default, a module’s symbolic name is generated in an unspecified but deterministic way[...]". So the module you need to require has a difficult-to-predict name.
It looks like define-runtime-module-path is perfect for this, using the generated name when it's embedded within the executable but still using the filesystem path when run in other ways: