on master help sign in
Show nicer error when module is not found in package

Fix https://github.com/elm/compiler/issues/1716

commit6f8ef997802081458efef580c11cff7f309a7ee0 authorEvan Czaplicki <evancz@users.noreply.github.com> dateTue 29 May 2018, 11:10 (8 years ago) parent5efc3d8
2 files changed+51−26
builder/src/File/Find.hs+11−6 builder/src/Reporting/Exit/Crawl.hs+40−20
builder/src/File/Find.hs+11−6
@@ -45,7 +45,7 @@ find (Summary.Summary root project exposed _ _) origin name =
4545 case project of
4646 Project.App info ->
4747 do let srcDirs = map toRoot (Project._app_source_dirs info)
48− findElm srcDirs exposed origin name
48+ findElm project srcDirs exposed origin name
4949
5050 Project.Pkg _ ->
5151 if N.startsWith "Elm.Kernel." name then
@@ -55,15 +55,15 @@ find (Summary.Summary root project exposed _ _) origin name =
5555 Task.throw $ E.ModuleNameReservedForKernel origin name
5656
5757 else
58− findElm [ toRoot "src" ] exposed origin name
58+ findElm project [ toRoot "src" ] exposed origin name
5959
6060
6161
6262-- FIND ELM
6363
6464
65−findElm :: [FilePath] -> Summary.ExposedModules -> E.Origin -> Module.Raw -> Task.Task_ E.Problem Asset
66−findElm srcDirs exposed origin name =
65+findElm :: Project.Project -> [FilePath] -> Summary.ExposedModules -> E.Origin -> Module.Raw -> Task.Task_ E.Problem Asset
66+findElm project srcDirs exposed origin name =
6767 do
6868 paths <- liftIO $ Maybe.catMaybes <$> mapM (elmExists name) srcDirs
6969
@@ -75,7 +75,12 @@ findElm srcDirs exposed origin name =
7575 return (Foreign pkg)
7676
7777 ([], Nothing) ->
78− Task.throw $ E.ModuleNotFound origin name srcDirs
78+ case project of
79+ Project.App _ ->
80+ Task.throw $ E.ModuleNotFound origin name (E.App srcDirs)
81+
82+ Project.Pkg _ ->
83+ Task.throw $ E.ModuleNotFound origin name E.Pkg
7984
8085 (_, maybePkgs) ->
8186 Task.throw $ E.ModuleAmbiguous origin name paths (maybe [] id maybePkgs)
@@ -106,4 +111,4 @@ findKernel srcDir exposed origin name =
106111 return ForeignKernel
107112
108113 _ ->
109− Task.throw $ E.ModuleNotFound origin name [srcDir]
114+ Task.throw $ E.ModuleNotFound origin name E.Pkg
builder/src/Reporting/Exit/Crawl.hs+40−20
@@ -3,6 +3,7 @@ module Reporting.Exit.Crawl
33 ( Exit(..)
44 , Problem(..)
55 , Origin(..)
6+ , SrcDirs(..)
67 , toReport
78 )
89 where
@@ -36,7 +37,7 @@ data Exit
3637
3738
3839data Problem
39− = ModuleNotFound Origin Module.Raw [FilePath]
40+ = ModuleNotFound Origin Module.Raw SrcDirs
4041 | ModuleAmbiguous Origin Module.Raw [FilePath] [Pkg.Package]
4142 | BadHeader FilePath Time.UTCTime BS.ByteString Compiler.Error
4243 | ModuleNameReservedForKernel Origin Module.Raw
@@ -55,6 +56,11 @@ data Origin
5556 | Module FilePath Module.Raw
5657
5758
59+data SrcDirs
60+ = App [FilePath]
61+ | Pkg
62+
63+
5864
5965-- TO REPORT
6066
@@ -113,8 +119,8 @@ toReport exit =
113119problemToReport :: Problem -> Help.Report
114120problemToReport problem =
115121 case problem of
116− ModuleNotFound origin name dirs ->
117− notFoundToDoc origin name dirs
122+ ModuleNotFound origin name srcDirs ->
123+ notFoundToDoc origin name srcDirs
118124
119125 ModuleAmbiguous origin child paths pkgs ->
120126 ambiguousToDoc origin child paths pkgs
@@ -173,7 +179,7 @@ badTagToDoc path name tag hintName summary =
173179
174180
175181
176−-- HELPERS
182+-- NAMELESS
177183
178184
179185namelessToDoc :: FilePath -> Module.Raw -> Help.Report
@@ -194,31 +200,34 @@ namelessToDoc path name =
194200 ]
195201
196202
197−notFoundToDoc :: Origin -> Module.Raw -> [FilePath] -> Help.Report
198−notFoundToDoc origin child dirs =
203+
204+-- NOT FOUND
205+
206+
207+notFoundToDoc :: Origin -> Module.Raw -> SrcDirs -> Help.Report
208+notFoundToDoc origin child srcDirs =
199209 case origin of
200210 ElmJson ->
201211 Help.report "MODULE NOT FOUND" (Just "elm.json")
202212 "Your elm.json says your project has the following module:"
203213 [ D.indent 4 $ D.red $ D.fromString $ Module.nameToString child
204214 , D.reflow $
205− "When creating packages, all modules must live in the\
206− \ src/ directory, but I cannot find it there."
215+ "When creating packages, all modules must live in the src/ directory."
207216 ]
208217
209218 File path ->
210219 Help.report "UNKNOWN IMPORT" (Just path)
211220 ("I cannot find a `" ++ Module.nameToString child ++ "` module to import.")
212− (notFoundDetails child dirs)
221+ (notFoundDetails child srcDirs)
213222
214223 Module path parent ->
215224 Help.report "UNKNOWN IMPORT" (Just path)
216225 ("The " ++ Module.nameToString parent ++ " module has a bad import:")
217− (notFoundDetails child dirs)
226+ (notFoundDetails child srcDirs)
218227
219228
220−notFoundDetails :: Module.Raw -> [FilePath] -> [D.Doc]
221−notFoundDetails child dirs =
229+notFoundDetails :: Module.Raw -> SrcDirs -> [D.Doc]
230+notFoundDetails child srcDirs =
222231 let
223232 simulatedCode =
224233 D.indent 4 $ D.red $ D.fromString $ "import " ++ Module.nameToString child
@@ -230,26 +239,37 @@ notFoundDetails child dirs =
230239 "Do you want the one from the " ++ Pkg.toString pkg
231240 ++ " package? If so, run this command to add that dependency to your elm.json file:"
232241 , D.indent 4 $ D.green $ D.fromString $ "elm install " ++ Pkg.toString pkg
233− , D.reflow $
234− "If you want a local file, make sure the directory that contains the `"
235− ++ Module.nameToString child
236− ++ "` module is listed in your elm.json \"source-directories\" field."
242+ , case srcDirs of
243+ Pkg ->
244+ D.reflow $
245+ "If you want a local file, make sure the `"
246+ ++ Module.nameToString child
247+ ++ "` module is in your src/ directory."
248+
249+ App _ ->
250+ D.reflow $
251+ "If you want a local file, make sure the `"
252+ ++ Module.nameToString child
253+ ++ "` module is in one of the \"source-directories\" listed in your elm.json file."
237254 ]
238255
239256 Nothing ->
240257 [ simulatedCode
241258 , "I cannot find that module! Is there a typo in the module name?"
242259 , D.reflow $
243− case dirs of
244− [] ->
260+ case srcDirs of
261+ Pkg ->
262+ "When creating a package, all modules must live in the src/ directory."
263+
264+ App [] ->
245265 "The \"source-directories\" field of your elm.json is empty, so I only\
246266 \ searched in packages. Maybe you need to add a \"src\" directory there?"
247267
248− [dir] ->
268+ App [dir] ->
249269 "The \"source-directories\" field of your elm.json tells me to only look in the "
250270 ++ dir ++ " directory, but it is not there. Maybe it is in a package that is not installed yet?"
251271
252− dir:_:_ ->
272+ App (dir:_:_) ->
253273 "The \"source-directories\" field of your elm.json tells me to look in directories like "
254274 ++ dir ++ ", but it is not in any of them. Maybe it is in a package that is not installed yet?"
255275 ]