How do I properly set current-module-name-resolver for a process?

24 views
Skip to first unread message

Sage Gerard

unread,
May 19, 2020, 4:10:12 PM5/19/20
to users\@racket-lang.org
I'm trying to figure out how to set current-module-name-resolver in advance of all other code for a process. I'm guessing this would require a custom launcher that does (for example) `racket -l foo/replace-resolver bar.rkt`.

In that sense, I expect that a `replace-resolver` of the form below wouldn't work because it runs at phase 0, after the expander resolved everything for `bar.rkt`.

How do I do what I mean here across all phases, for all uses of (dynamic-|local-)require everywhere in my program?

(let ([old (current-module-name-resolver)])
  (current-module-name-resolver
   (case-lambda
     [(declared-module-name src-namespace)
      (old declared-module-name src-namespace)]
     [(module-path source-module stx should-load?)
      (old (maybe-rewrite-module-path module-path source-module)
           source-module
           stx
           should-load?)
      r])))

~slg


Matthew Flatt

unread,
May 19, 2020, 4:49:50 PM5/19/20
to Sage Gerard, users\@racket-lang.org
At Tue, 19 May 2020 20:10:01 +0000, Sage Gerard wrote:
> I'm trying to figure out how to set current-module-name-resolver in
> advance of all other code for a process. I'm guessing this would
> require a custom launcher that does (for example) `racket -l
> foo/replace-resolver bar.rkt`.

That can work if

* you either add a `-t` before `bar.rkt` to load it or
`foo/replace-resolver` handles command-line arguments; and

* you don't need to adjust resolution for any modules that are used in
the implementation of `foo/replace-resolver`.

> In that sense, I expect that a `replace-resolver` of the form below
> wouldn't work because it runs at phase 0, after the expander resolved
> everything for `bar.rkt`.

If you go the route of adding `-t` before `bar.rkt`, the resolution
will not happen until after `foo/replace-resolver` runs.

> How do I do what I mean here across all phases, for all uses of
> (dynamic-|local-)require everywhere in my program?

If you set the module name resolver, it applies to all phases.

(That's a hole in Racket's phase separation, and we don't like to talk
about it. :)

Reply all
Reply to author
Forward
0 new messages