# Proper usage of make-base-eval-factory

**URL:** <https://racket.discourse.group/t/proper-usage-of-make-base-eval-factory/3809>\
**Category:** General\
**Created:** [June 27, 2025, 3:59pm UTC](https://racket.discourse.group/t/proper-usage-of-make-base-eval-factory/3809 "2025-06-27T15:59:47Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Laurent.O](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/laurent.o/32/18_2.png) [@Laurent.O](https://racket.discourse.group/u/Laurent.O)\
**Post date:** [June 27, 2025, 3:59pm UTC](https://racket.discourse.group/t/proper-usage-of-make-base-eval-factory/3809/1 "2025-06-27T15:59:47Z")

</div>

Say I have a module `my-pkg/foo.rkt` which exports `bar`.  
The doc `mypkg/scribblings/foo.scrbl` uses `scribble/example` like:

```scheme
@(define (make-my-eval) (make-base-eval '(require "../foo.rkt")))

@examples[#:eval (make-my-eval) bar]

```

This works fine in DrRacket, but fails with `raco setup`. To make it work for the latter it seems I need instead:

```scheme
@(define (make-my-eval) (make-base-eval '(require "foo.rkt")))

```

although this isn't consistent path-wise.  
Ideally I would use

```scheme
@(define make-my-eval (make-base-eval-factory (list "../foo.rkt")))

```

but I couldn't make this work, either with or without "../", or runtime paths (converted to strings, which also seems wrong).

I found only very few instances of `make-base-eval-factory` on github or Google. I've tried quite a number of combinations, but at this stage I'm really confused and can't form a correct model of how it works (in particular what's the current-directory when). So what's the proper way to use `make-base-eval-factory` in the setup above?

---

<div class="post-metadata">

**Author:** ![Laurent.O](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/laurent.o/32/18_2.png) [@Laurent.O](https://racket.discourse.group/u/Laurent.O)\
**Post date:** [June 27, 2025, 4:23pm UTC](https://racket.discourse.group/t/proper-usage-of-make-base-eval-factory/3809/2 "2025-06-27T16:23:57Z")

</div>

Also

```scheme
@(define (make-my-eval) (make-base-eval '(require my-pkg/foo)))

```

works in both DrRacket and with `raco setup`, but I'm not convinced that's the right way to write it, as `my-pkg/foo` might refer to a differently installed package than the one for which the doc is being generated — or actually not?

And anyway, this still doesn't work:

```scheme
@(define make-my-eval (make-base-eval-factory '(my-pkg/foo)))

```

---

<div class="post-metadata">

**Author:** ![benknoble](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/benknoble/32/16_2.png) [@benknoble](https://racket.discourse.group/u/benknoble)\
**Post date:** [June 27, 2025, 6:55pm UTC](https://racket.discourse.group/t/proper-usage-of-make-base-eval-factory/3809/3 "2025-06-27T18:55:57Z")

</div>

Perhaps examples [from GitHub](https://github.com/search?q=make-base-eval-factory&type=code) may help?

---

<div class="post-metadata">

**Author:** ![Laurent.O](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/laurent.o/32/18_2.png) [@Laurent.O](https://racket.discourse.group/u/Laurent.O)\
**Post date:** [June 27, 2025, 8:33pm UTC](https://racket.discourse.group/t/proper-usage-of-make-base-eval-factory/3809/4 "2025-06-27T20:33:09Z")

</div>

Your link doesn't work it seems.

I already looked at github and didn't find anything relevant enough — though possibly I missed some.

---

<div class="post-metadata">

**Author:** ![soegaard](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/soegaard/32/19_2.png) [@soegaard](https://racket.discourse.group/u/soegaard)\
**Post date:** [June 27, 2025, 8:47pm UTC](https://racket.discourse.group/t/proper-usage-of-make-base-eval-factory/3809/5 "2025-06-27T20:47:15Z")

</div>

FWIW here is a snippet from the documentation for Sketching.

```scheme
@; Note: The documentation won't build on the package server if racket/gui is instantiated.
@; This means we need to use racket/draw and sketching/exports-no-gui
@; instead of racket/gui and sketching.
@(define draw-namespace (make-base-namespace))
@(define factory (make-base-eval-factory (list 'racket/draw)))
@(define (make-sketching-eval)   
   (let ([e (factory)])
     (e '(require sketching/exports-no-gui sketching/parameters racket/draw))
     (e '(define (new-bitmap-dc w h)
           (define dc (new bitmap-dc% [bitmap (make-bitmap w h)]))
           (send dc set-smoothing 'aligned)
           dc))
     (e '(current-dc (new-bitmap-dc 100 100)))
     e))
@(define se (make-sketching-eval))

```

I see that I avoided relative filenames and instead went with `sketching/exports-no-gui` and `sketching/parameters`. The alternative must be to use runtime paths.

---

<div class="post-metadata">

**Author:** ![Laurent.O](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/laurent.o/32/18_2.png) [@Laurent.O](https://racket.discourse.group/u/Laurent.O)\
**Post date:** [June 28, 2025, 8:41pm UTC](https://racket.discourse.group/t/proper-usage-of-make-base-eval-factory/3809/6 "2025-06-28T20:41:55Z")

</div>

You also don't include the ad-hoc requires in the factory list, which suggests this list is only for 'core' libraries? I also couldn't use it for my ad-hoc libraries (which kind of defeats the purpose of using `make-eval-base-factory` I suppose).

---

<div class="post-metadata">

**Author:** ![soegaard](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/soegaard/32/19_2.png) [@soegaard](https://racket.discourse.group/u/soegaard)\
**Post date:** [June 28, 2025, 11:57pm UTC](https://racket.discourse.group/t/proper-usage-of-make-base-eval-factory/3809/7 "2025-06-28T23:57:55Z")

</div>

I would have thought something like:

```scheme
(make-base-eval-factory (list 'my-pkg/foo)

```

would have worked.

That path is independent of the current directory.

---

<div class="post-metadata">

**Author:** ![LiberalArtist](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/liberalartist/32/151_2.png) [@LiberalArtist](https://racket.discourse.group/u/LiberalArtist)\
**Post date:** [June 29, 2025, 12:14am UTC](https://racket.discourse.group/t/proper-usage-of-make-base-eval-factory/3809/8 "2025-06-29T00:14:46Z")

</div>

The [documentation](https://docs.racket-lang.org/reference/require.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._require%29%29) says that the _`rel-string`_ variant of _`root-module-path`_ is “relative to the containing source (as determined by [`current-load-relative-directory`](https://docs.racket-lang.org/reference/eval.html#%28def._%28%28quote._~23~25kernel%29._current-load-relative-directory%29%29) or [`current-directory`](https://docs.racket-lang.org/reference/Filesystem.html#%28def._%28%28quote._~23~25kernel%29._current-directory%29%29))”. The sandbox functions do top-level evaluation, so those parameters are consulted at runtime, i.e. _not_ when compiling your module that uses `make-base-eval-factory`.

DrRacket parameterizes `current-directory` to the directory containing the file for which you pressed the “Run” button. I don't think `raco setup` specifies how those parameters are set: it might be the OS-level current directory from which you invoke `raco setup`, or it might be some other directory convenient for `raco setup`'s purposes. Running a program with `racket` at the command line definitely does use the OS-level working directory in which the process is started.

> [@Laurent.O](#):
>
> Also
> 
> ```scheme
> @(define (make-my-eval) (make-base-eval '(require my-pkg/foo)))
> 
> ```
> 
> works in both DrRacket and with `raco setup`, but I'm not convinced that's the right way to write it, as `my-pkg/foo` might refer to a differently installed package than the one for which the doc is being generated — or actually not?

I'm not sure what you mean by “might refer to a differently installed package than the one for which the doc is being generated”.

I would definitely refer to a module that is part of a collection as `my-pkg/foo` or `(lib "my-pkg/foo.rkt")`.

Nonetheless, it is possible to refer to a non-collection-based module using `make-base-eval-factory`. For example, considering the following directory layout:

```scheme
philip@bastet:~$ tree /tmp/mpi
/tmp/mpi
|-- count.rkt
`-- eval.rkt

1 directory, 2 files

```

where `count.rkt` is:

```scheme
#lang racket/base
(provide count!)
(define n 0)
(define (count!)
  (begin0 n
    (set! n (add1 n))))

```

and `eval.rkt` is:

```scheme
#lang racket
(require scribble/example
         racket/runtime-path)
(define-runtime-path count.rkt "count.rkt") ; [1]
count.rkt
(define make-count-eval
  (make-base-eval-factory (list count.rkt)))
(current-load-relative-directory)
(current-directory)
(define e
  (make-count-eval))
(e `(#%require ,count.rkt)) ; [2]
(e `(list (count!) (count!) (count!)))
((make-count-eval) `(begin (#%require ,count.rkt) ; [3]
                           (count!))) ; [4]

```

Here's how it works:

```scheme
philip@bastet:~$ racket /tmp/mpi/eval.rkt
#<path:/tmp/mpi/count.rkt>
#f
#<path:/home/philip/>
'(0 1 2)
0
philip@bastet:~$ cd /tmp/mpi/
philip@bastet:/tmp/mpi$ racket eval.rkt
#<path:/tmp/mpi/count.rkt>
#f
#<path:/tmp/mpi/>
'(0 1 2)
0

```

Some notes:

1. I used `define-runtime-path` because the resulting complete `path?` satisfies `module-path?`. Usually you'd want to use `define-runtime-module-path-index` or `define-runtime-module-path`, but `make-base-eval-factory` wants a `(listof module-path?)`, and I couldn't figure out how to turn a `module-path-index?` or a `resolved-module-path?` into a `module-path?`. It seems possible that `make-base-eval-factory` might be able to accept something like `(listof (or/c module-path? resolved-module-path?))`, or maybe there's a conversion I missed.
2. A complete `path?` is accepted by `#%require`, but not `require`.
3. You need to `#%require` the module again for the new evaluator. Using `make-eval-factory`, instead, would require the modules automatically.
4. I was surprised that this returned `0`, not `3`, given that [the docs for `make-base-eval-factory`](https://docs.racket-lang.org/scribble/eval.html#%28def._%28%28lib._scribble%2Feval..rkt%29._make-base-eval-factory%29%29) say that “the modules are loaded and instantiated once (when the returned [`make-base-eval`](https://docs.racket-lang.org/scribble/eval.html#%28def._%28%28lib._scribble%2Feval..rkt%29._make-base-eval%29%29)-like function is called the first time) and then attached to each evaluator that is created”.

---

<div class="post-metadata">

**Author:** ![Laurent.O](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/laurent.o/32/18_2.png) [@Laurent.O](https://racket.discourse.group/u/Laurent.O)\
**Post date:** [June 30, 2025, 2:09pm UTC](https://racket.discourse.group/t/proper-usage-of-make-base-eval-factory/3809/9 "2025-06-30T14:09:02Z")

</div>

@soegaard This works (both in drr and `raco setup`):

```scheme
@(define make-my-eval
   (λ () (make-base-eval '(require my-pkg/foo))))

```

but this doesn't (in either drr or `raco setup`):

```scheme
@(define make-my-eval
   (make-base-eval-factory '(my-pkg/foo)))

```

@LiberalArtist

> I'm not sure what you mean by “might refer to a differently installed package than the one for which the doc is being generated”.

I was using `(require my-pkg/foo)` in my test files, and I was wondering why all my tests were passing despite an obviously-wrong code. It took me a while to realize that I had 2 versions of the package, and the one I was editing was not the one that referred to `my-pkg` as a collection. After a `raco pkg update --link ...` it all worked. I was thinking that some similar mistake could happen for package documentation, but actually maybe not.

@LiberalArtist Thanks for the detailed answer! It seems like using `make-base-eval-factory` is more complicated than I thought at first, so I'll just stick to repeated `make-base-eval` then.

---

<div class="post-metadata">

**Author:** ![benknoble](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/benknoble/32/16_2.png) [@benknoble](https://racket.discourse.group/u/benknoble)\
**Post date:** [July 2, 2025, 12:28am UTC](https://racket.discourse.group/t/proper-usage-of-make-base-eval-factory/3809/10 "2025-07-02T00:28:29Z")

</div>

Thanks; link fixed (the mail parser mangles Markdown-syntax links sometimes, I think)
