Skip to content

Complete documentation of Hpack's main field #609

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

mpilgrem
Copy link
Collaborator

@mpilgrem mpilgrem commented Mar 22, 2025

Motivation, @sol's currently undocumented explanation at:

Points of reference:

Unusually, for Hpack's approach to its README, I have added three short sentences of introductory context boiled down from the Haskell 2010 Language Report and the GHC User's Guide. That is because the scope for confusion is high.

@mpilgrem mpilgrem requested a review from sol March 22, 2025 13:57
Copy link
Owner

@sol sol left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://gitlab.haskell.org/ghc/ghc/-/issues/13801 limits the utility of this feature quite a bit.

Indeed, because of this limitations I myself rarely use this feature at all.

I am not sure if this feature is ready for mass consumption until the corresponding GHC issue is resolved.

@mpilgrem do you still think it is useful to document this, or should we keep this PR open until the corresponding GHC issue is resolved? If you want to get this merged now, do you think this needs a disclaimer of some sort?


Hpack's `main` field accepts a module name, a qualified name or a source file
name. Hpack assumes that anything that does not have the format of the first two
possibilities is a source file name. For the first two possibilities, Hpack
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not entirely accurate. Foo.hs is a valid qualified name. However, we interpreted as a file name.

It's roughly:

  1. If it looks like a Haskell source file (ends in .hs or .lhs), we interpret it as a file path.
  2. If it is a valid qualified name, we take it as a qualified name.
  3. If it is a valid module name, we take it as a module name.
  4. As a fallback we take it as a file path.

Or in other words, Hpack does not accept entry points named hs or lhs.

hpack/src/Hpack/Util.hs

Lines 55 to 61 in e11cf4f

parseMain :: String -> (FilePath, [GhcOption])
parseMain main = case reverse name of
x : _ | isQualifiedIdentifier name && x `notElem` ["hs", "lhs"] -> (intercalate "/" (init name) ++ ".hs", ["-main-is " ++ main])
_ | isModule name -> (intercalate "/" name ++ ".hs", ["-main-is " ++ main])
_ -> (main, [])
where
name = splitOn '.' main


By convention, a Haskell program must have a module called `Main` which exports
an IO action named `main`. When the program is executed, the action is
performed. However, GHC's `main-is` option can be used to change the name of the
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
performed. However, GHC's `main-is` option can be used to change the name of the
performed. However, GHC's `-main-is` option can be used to change the name of the


executable my-app
main-is: MyMainModule.hs
ghc-options: -main-is MyMainModule
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we don't frequently document things in terms of how they desugar to .cabal syntax. Rather we document how they can be used, so that a user does not need to be familiar with Cabal to be able to use Hpack.

I'm not saying this is necessarily bad, just want to bring it up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants