Subject: Variable not visible to compiler causes compilation failure when importing module

From: Jean Abou Samra <[email-masked]>
Date: Wed, 1 Dec 2021 22:53:56 +0100

In experiments with using guild for byte compilation
in LilyPond, I am hitting what seems like a bug. When
a variable is not defined declaratively in a way that
makes the compiler aware of it, the module it is
defined in can compile, but modules using it cannot.
Here is an example. fail.scm contains:

(define-module (fail))

variable-coming-from-somewhere-but-not-known-at-compile-time

import.scm is just:

(use-modules (fail))

Compiling fail.scm works:

$ guild compile fail.scm
fail.scm:3:0: warning: possibly unbound variable 
`variable-coming-from-somewhere-but-not-known-at-compile-time'
wrote `/home/jean/.cache/guile/ccache/3.0-LE-8-4.4/.../fail.scm.go'

Compiling import.scm gives this traceback:

$ guild compile -L . import.scm
Backtrace:
In system/base/compile.scm:
    327:39 19 (read-and-compile #<input: import.scm 13> #:from _ #:to …)
    261:27 18 (_ _ _)
In ice-9/boot-9.scm:
    2835:4 17 (save-module-excursion #<procedure 7f35bd197ea0 at lang…>)
In language/scheme/compile-tree-il.scm:
     31:15 16 (_)
In ice-9/psyntax.scm:
   1230:36 15 (expand-top-sequence (#<syntax:import.scm:1:0 (#<synt…>) …)
   1222:19 14 (parse _ (("placeholder" placeholder)) ((top) #(# # …)) …)
    259:10 13 (parse _ (("placeholder" placeholder)) (()) _ c&e (# #) #)
In ice-9/boot-9.scm:
   3927:20 12 (process-use-modules _)
    222:17 11 (map1 (((fail))))
   3928:31 10 (_ ((fail)))
   3326:17  9 (resolve-interface (fail) #:select _ #:hide _ #:prefix _ …)
In ice-9/threads.scm:
     390:8  8 (_ _)
In ice-9/boot-9.scm:
   3252:13  7 (_)
In ice-9/threads.scm:
     390:8  6 (_ _)
In ice-9/boot-9.scm:
   3536:20  5 (_)
    2835:4  4 (save-module-excursion #<procedure 7f35bd260960 at ice-…>)
   3556:26  3 (_)
In unknown file:
            2 (primitive-load-path "fail" #<procedure 7f35bd1a0860 at…>)
In fail.scm:
       3:0  1 (_)
In ice-9/boot-9.scm:
   1685:16  0 (raise-exception _ #:continuable? _)

ice-9/boot-9.scm:1685:16: In procedure raise-exception:
Unbound variable: 
variable-coming-from-somewhere-but-not-known-at-compile-time

I would appreciate any workarounds.