Skip to content

Allow any module name #182

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

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ Response body on compilation failure:
}
```

Response body on other errors (eg, the name of the module in request body was not Main, or the request body was too large)
Response body on other errors (eg, the request body was too large)

```javascript
{
Expand Down
10 changes: 6 additions & 4 deletions server/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,14 @@ server externs initNamesEnv initEnv port = do
case CST.parseModuleFromFile "<file>" input >>= CST.resFull of
Left parseError ->
return . Left . CompilerErrors . P.toJSONErrors False P.Error $ CST.toMultipleErrors "<file>" parseError
Right m | P.getModuleName m == P.ModuleName "Main" -> do
Right m -> do
-- Rewrite the module name to "$Main" in order to ensure that the
-- module name doesn't clash with any existing module names in
-- the package set.
let rewriteModuleName (P.Module ss coms _ decls refs) = P.Module ss coms (P.moduleNameFromString "$Main") decls refs
Copy link
Contributor

Choose a reason for hiding this comment

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

If someone attempts to play with module export syntax this might be cause problems with:

module Foo (module Bar, module Foo) where ...

Copy link
Contributor

Choose a reason for hiding this comment

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

I'll wager this is by far an edge case, however 😆

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ah, good point. I think re-exporting main from elsewhere seems plausible to want to do, so perhaps this isn't the best approach. Maybe we should assemble a set of used module names from the externs files, and then instead of rewriting, just check that the given module name is not included in that set?

(resultMay, ws) <- runLogger' . runExceptT . flip runReaderT P.defaultOptions $ do
((P.Module ss coms moduleName elaborated exps, env), nextVar) <- P.runSupplyT 0 $ do
desugared <- P.desugar initNamesEnv externs [P.importPrim m] >>= \case
desugared <- P.desugar initNamesEnv externs [P.importPrim (rewriteModuleName m)] >>= \case
[d] -> pure d
_ -> error "desugaring did not produce one module"
P.runCheck' (P.emptyCheckState initEnv) $ P.typeCheckModule desugared
Expand All @@ -87,8 +91,6 @@ server externs initNamesEnv initEnv port = do
case resultMay of
Left errs -> (return . Left . CompilerErrors . P.toJSONErrors False P.Error) errs
Right js -> (return . Right) (P.toJSONErrors False P.Error ws, js)
Right _ ->
(return . Left . OtherError) "The name of the main module should be Main."

scottyOpts (getOpts port) $ do
get "/" $
Expand Down