Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix "module defined in multiple files" #37

Merged
merged 2 commits into from
Aug 22, 2023

Conversation

9999years
Copy link
Member

We hadn't been canonicalizing paths before checking if they were in the ModuleSet of loaded modules previously. This led to modules being :added when a simple :reload would've sufficed, leading to this error:

<no location info>: error: [GHC-29235]
    module 'mwb-0-inplace-test-dev:Foo' is defined in multiple files: Foo.hs
                                                                      Foo.hs

We hadn't been canonicalizing paths before checking if they were in the
`ModuleSet` of loaded modules previously. This led to modules being
`:add`ed when a simple `:reload` would've sufficed, leading to this
error:

```
<no location info>: error: [GHC-29235] module
'mwb-0-inplace-test-dev:Mercury.QuickForms.Models' is defined in
multiple files:
/Users/stephen/mercury/backend/src/Mercury/QuickForms/Models.hs
/Users/stephen/mercury/backend/src/Mercury/QuickForms/Models.hs
```
This prevents failed modules from being "lost".
@linear
Copy link

linear bot commented Aug 21, 2023

DUX-1256 Module defined in multiple files

ghci is failing with a rather odd error:


• Reloading `ghci` due to changed modules:
  • /home/matt/Projects/mercury-web-backend/src/A/MercuryPrelude.hs

• [ghci stderr]
• [ghci stderr] <no location info>: error: [GHC-29235]

• [ghci stderr]     module ‘mwb-0-inplace-test-dev:A.MercuryPrelude’ is defined in multiple files:
  /home/matt/Projects/mercury-web-backend/src/A/MercuryPrelude.hs

• [ghci stderr]
  /home/matt/Projects/mercury-web-backend/src/A/MercuryPrelude.hs

Failed, 78 modules loaded.

The file names are identical, so I suspect something weird is afoot.


Reproduction, courtesy of evanr:

  • Run make ghcid-ng
  • Mess up a leaf module's module name (e.g. change module TaskMain ... where to module TaskMai ... where)
  • Restore the correct module name (e.g. change module TaskMai ... where back to module TaskMain ... where)
  • Get the error

@github-actions github-actions bot added the patch Bug fixes or non-functional changes label Aug 21, 2023
@9999years 9999years merged commit 5ef6b9b into main Aug 22, 2023
@9999years 9999years deleted the rebeccat/dux-1256-module-defined-in-multiple-files branch August 22, 2023 15:47
@github-actions
Copy link
Contributor

9999years added a commit that referenced this pull request Dec 5, 2023
#168 introduced another bug with eval commands. The actual bugfix here
is relatively small, but I needed some architectural changes in the test
harness (to support restarting the `ghciwatch` session) and in the
`ghciwatch` codebase to make these changes.

- Removes the system of `Mode`s introduced in #11 and gutted in #78.
This grouped `ghci` output into buffers based on the "mode" `ghciwatch`
was in when the output was produced (modules being compiled, tests being
run, internal data being gathered, etc.). Over time this was used less
and less. Now we parse diagnostics out of all compiler output and it's
fine.
- Also removes the `Ghci::process_ghc_messages` method introduced in
#77. The idea was that methods like `Stdout::prompt` would return a list
of parsed GHC messages/diagnostics, and those would be processed by
`Ghci`. In practice, it was difficult to remember to pass the lists of
messages to `process_ghc_messages` consistently. Now, each method that
produces diagnostics takes a `&mut CompilationLog`. There's many places
where messages are produced, but only a few where messages need to be
written out (after reloads, after startup), so this is a lot more
convenient.

# Bug reproduction details

There's two bugs here: `module '...' is not interpreted` and `module
'...' defined in multiple files`. My attempted fix for the first bug in
#168 caused the second bug. This PR fixes both.

First, you load a `ghci` session:

```
$ ghci
[1 of 4] Compiling MyLib            ( src/MyLib.hs, interpreted )
[2 of 4] Compiling MyModule         ( src/MyModule.hs, interpreted )
[3 of 4] Compiling Paths_my_simple_package ( /Users/wiggles/ghciwatch/tests/data/simple/dist-newstyle/build/aarch64-osx/ghc-9.6.3/my-simple-package-0.1.0.0/l/test-dev/build/test-dev/autogen/Paths_my_simple_package.hs, interpreted )
[4 of 4] Compiling TestMain         ( test/TestMain.hs, interpreted )
Ok, four modules loaded.
ghci> :quit
```

This interprets each module. When the GHC option
[`-fwrite-if-simplified-core`](https://downloads.haskell.org/ghc/latest/docs/users_guide/phases.html#ghc-flag--fwrite-if-simplified-core)
(introduced in GHC 9.4) is used, loading the `ghci` session additionally
writes interface files for each module:

> The interface file will contain all the bindings for a module. From
this interface file we can restart code generation to produce byte-code.

Then, you can load a _new_ `ghci` session, which will load the interface
modules and use them to generate `ghci` byte-code directly. Critically,
this means that the modules are not considered to be "interpreted",
leading to this error message:

```
$ ghci
Ok, four modules loaded.
ghci> :module + *MyLib
module 'MyLib' is not interpreted; try ':add *MyLib' first
```

## How `ghciwatch` triggers the bug

When `ghciwatch`'s `--enable-eval` flag is used, an eval command
`runMyCommand` for a module `MyModule` is executed roughly like this:

```
> :module + *MyModule
> runMyCommand
> :module - *MyModule
```

`:module + *MyModule` brings the top-level definitions of `MyModule`
into scope, so that eval commands can refer to definitions in the module
they're defined in.

@parsonsmatt noticed that if `ghci` loaded a module from one of these
interface files, evaluating commands in that file would fail:

```
GHCi, version 9.6.2: https://www.haskell.org/ghc/  :? for help
Loaded GHCi configuration from .ghci
• ghci started in 5.52s

• Adding modules to ghci:
  • test/Foo.hs

Ok, 3140 modules loaded.
• test/Foo.hs:25:7: runMyTests
module 'Foo' is not interpreted; try ':add *Foo' first
```

I initially attempted to fix this in #168, which added an explicit `:add
*Foo` before running the eval command, which forces the module to be
interpreted.

Unfortunately, using the module name instead of the module path could
lead to the module being added twice, causing the dreaded `module
defined in multiple files` bug (see #37, #77, #125).
9999years added a commit that referenced this pull request May 16, 2024
In #214, we had a
situation where modules were loaded:

    ghci> :show targets
    Foo
    Bar
    Baz

And then an eval comment in (e.g.) `Foo` causes the module to be added
and explicitly interpreted by path:

    ghci> :add *src/Foo.hs

Then, we have `Foo` loaded by name (`Foo`) and by path (`src/Foo.hs`),
which triggers the dreaded bug.

At the time I proposed this fix, correctly:

> I think we can fix this by keeping track of how each module is added
to the session — as a path or as a module name — and then only using
that form going forward.

I threaded some extra information to the `:show targets` parser to track
if modules were listed as names or paths, **but then at the end of
`Ghci::interpret_module` I would always insert the module into the
target set as a `TargetKind::Path`,** meaning that the *next* time the
comment was evaluated, the module would be loaded as a path, causing the
error.


https://github.com/MercuryTechnologies/ghciwatch/blob/dbba61bbdec9a86f97051b12647e51b7be4fd484/src/ghci/mod.rs#L698-L699

This fixes the bug and adds an `assert!` to fail faster and more
obviously if it happens again.

## Prior art

- #37
- #77 
- #125 
- #214
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
patch Bug fixes or non-functional changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants