[Haskell-cafe] possible bug in default module lookup scheme / or
quick at sparq.org
Sun Jul 18 03:22:20 EDT 2010
On Sat, 17 Jul 2010 22:45:57 -0700, Ivan Lazar Miljenovic <ivan.miljenovic at gmail.com> wrote:
> Carter Schonwald <carter.schonwald at gmail.com> writes:
>> Hello All, I'm not sure if this either a bug in how ghc does path/module
>> lookup or it simply is invalid haskell:
>> consider modules A, A.B and A.B.C
>> where A imports A.B, and A.B imports A.B.C
>> with the following file system layout
>> minimal file examples:
>> module A where
>> import A.B
>> testA = "will it really really work?
>> module A.B where
>> import A.B.C
>> testB = "will it work
>> module A.B.C where
>> testC = "will this work?"
>> if i run ghci A.hs everything's fine
>> but if in directory B i rune ghci B.hs, i get
>> Could not find module `A.B.C':
>> Use -v to see a list of the files searched for.
>> it seems to me that if the default search path for ghc(i) includes the
>> current directory (which according to docs it does), this shouldn't be
>> happening. (or is there some why this is good Behavior?)
> I think ghci is just not smart enough to know that it should change to
> the parent directory and run it from there. As such, it's trying to
> find "A.B.C" from the context of the current directory, and the file is
> not in A/A/B/C.hs so it can't find it.
> So it's just a limitation of ghci (I think).
I'm afraid I disagree and would view this as expected behavior.
"import A.B.C" translates internally to something like load_file_using_paths("A/B/C.hs").
When you are running this from the top level directory (e.g. "top"), ghci includes the current path "top" so the lookup is for "top/A/B/C.hs", which clearly exists.
When you are in directory B, ghci includes the current path "top/A/B" so the lookup is for "top/A/B/A/B/C.hs"... which does not exist, thus your error.
Your example would require ghci to try load_file_using_paths("B/C.hs") (and then load_file_using_paths("C.hs") to be complete), which discards the directory heirarchy specified by the module nomenclature. This is not adviseable because it introduces ambiguities. For example, if you also had a C.hs in A and another C.hs in A/B, which C.hs should it load when you say "import A.B.C"? Or "import C"? If ghc/ghci discarded paths, then the results would be either (1) a different C.hs depending on your current directory, (2) the bottom-most C.hs, (3) the C.hs in the current directory, (4) random?. Worse, any of the above results in a trojan-horse style security hole. Also, what if there was a C.hs in the directory above you (top/..)? A 1:1 mapping between module heirarchy specification and directory paths is the only dependable mechanism.
The better solution is to specifically set the paths you expect to form the roots of the (non-default) module heirarchy if you plan to work from within subdirectories of your source tree. If you invoked ghci as "$ ghci -i /path/to/top" then it would work regardless of your current directory. I believe that this is the proper solution to http://hackage.haskell.org/trac/ghc/ticket/3140 as well.
More information about the Haskell-Cafe