Skip to content

Add GHC-67120 main not defined #555

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

Merged
merged 3 commits into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions message-index/messages/GHC-67120/example1/after/Lib.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Lib where

factorial :: Int -> Int
factorial n = product [1..n]
2 changes: 2 additions & 0 deletions message-index/messages/GHC-67120/example1/before/Lib.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
factorial :: Int -> Int
factorial n = product [1..n]
9 changes: 9 additions & 0 deletions message-index/messages/GHC-67120/example1/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
title: Missing module header
---

In this example, the file `Lib.hs` is intended to contain a module called `Lib`
which is not supposed to be the main module. However, the file is missing a
module header, so GHC defaults to the module name `Main` and expects the `main`
function. Adding an explicit module header with the name `Lib` solves this
issue.
2 changes: 2 additions & 0 deletions message-index/messages/GHC-67120/example2/after/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
main :: IO ()
main = putStrLn "Hello, World!"
2 changes: 2 additions & 0 deletions message-index/messages/GHC-67120/example2/before/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
notMain :: IO ()
notMain = putStrLn "Hello, World!"
7 changes: 7 additions & 0 deletions message-index/messages/GHC-67120/example2/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
title: main has wrong name
---

In this example, the `notMain` value is the intended entry point of the program,
but it is not called `main` so GHC does not find it. Renaming `notMain` to
`main` solves the issue.
49 changes: 49 additions & 0 deletions message-index/messages/GHC-67120/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
title: Missing main
summary: The IO action ‘main’ is not defined in module ‘Main’.
introduced: 9.8.1
severity: error
---

GHC expects the `Main` module to define a function called `main` which it can
use as the entry point of your program.

This error can also occur in unnamed modules, because GHC will default to the
module name `Main` for such modules.

If you just want GHC to produce an object file without an entry point, then you
can give your module a name other than `Main` by putting a module header at the
top of your file (below language pragmas and compiler options), for example as
follows:

```
module Foo where
```

The conventions around `main` are defined in the second paragraph of Chapter 5
of [The Haskell 2010 Report](https://www.haskell.org/onlinereport/haskell2010/haskellch5.html#x11-980005),

> A Haskell program is a collection of modules, one of which, by convention,
> must be called `Main` and must export the value `main`. The value of the
> program is the value of the identifier `main` in module `Main`, which must be
> a computation of type `IO t` for some type `t` (see Chapter 7). When the
> program is executed, the computation `main` is performed, and its result (of
> type `t`) is discarded.

## Example error text

```
example1/before/Lib.hs:1:1: error: [GHC-67120]
The IO action ‘main’ is not defined in module ‘Main’
|
1 | factorial :: Int -> Int
| ^
```

```
example2/before/Main.hs:1:1: error: [GHC-67120]
The IO action ‘main’ is not defined in module ‘Main’
|
1 | notMain :: IO ()
| ^
```