on f4401ac help sign in
162 lines 6.0 kB history raw
1{-# LANGUAGE OverloadedStrings #-}
2module Reporting.Error.Import
3 ( Error(..)
4 , Problem(..)
5 , toReport
6 )
7 where
10import qualified Data.Map as Map
11import qualified Data.Set as Set
13import qualified Elm.ModuleName as ModuleName
14import qualified Elm.Package as Pkg
15import qualified Reporting.Doc as D
16import qualified Reporting.Render.Code as Code
17import qualified Reporting.Report as Report
18import qualified Reporting.Suggest as Suggest
19import qualified Reporting.Annotation as A
23-- ERROR
26data Error =
27 Error
28 { _region :: A.Region
29 , _import :: ModuleName.Raw
30 , _unimported :: Set.Set ModuleName.Raw
31 , _problem :: Problem
32 }
35data Problem
36 = NotFound
37 | Ambiguous FilePath [FilePath] Pkg.Name [Pkg.Name]
38 | AmbiguousLocal FilePath FilePath [FilePath]
39 | AmbiguousForeign Pkg.Name Pkg.Name [Pkg.Name]
43-- TO REPORT
46toReport :: Code.Source -> Error -> Report.Report
47toReport source (Error region name unimportedModules problem) =
48 case problem of
49 NotFound ->
50 Report.Report "MODULE NOT FOUND" region [] $
51 Code.toSnippet source region Nothing
52 (
53 D.reflow $
54 "You are trying to import a `" ++ ModuleName.toChars name ++ "` module:"
55 ,
56 D.stack
57 [
58 D.reflow $
59 "I checked the \"dependencies\" and \"source-directories\" listend in your elm.json,\
60 \ but I cannot find it! Maybe it is a typo for one of these names?"
61 ,
62 D.dullyellow $ D.indent 4 $ D.vcat $
63 map D.fromName (toSuggestions name unimportedModules)
64 ,
65 case Map.lookup name Pkg.suggestions of
66 Nothing ->
67 D.toSimpleHint $
68 "If it is not a typo, check the \"dependencies\" and \"source-directories\"\
69 \ of your elm.json to make sure all the packages you need are listed there!"
71 Just dependency ->
72 D.toFancyHint
73 ["Maybe","you","want","the"
74 ,"`" <> D.fromName name <> "`"
75 ,"module","defined","in","the"
76 ,D.fromChars (Pkg.toChars dependency)
77 ,"package?","Running"
78 ,D.green (D.fromChars ("elm install " ++ Pkg.toChars dependency))
79 ,"should","make","it","available!"
80 ]
81 ]
82 )
84 Ambiguous path _ pkg _ ->
85 Report.Report "AMBIGUOUS IMPORT" region [] $
86 Code.toSnippet source region Nothing
87 (
88 D.reflow $
89 "You are trying to import a `" ++ ModuleName.toChars name ++ "` module:"
90 ,
91 D.stack
92 [
93 D.fillSep $
94 ["But","I","found","multiple","modules","with","that","name.","One","in","the"
95 ,D.dullyellow (D.fromChars (Pkg.toChars pkg))
96 ,"package,","and","another","defined","locally","in","the"
97 ,D.dullyellow (D.fromChars path)
98 ,"file.","I","do","not","have","a","way","to","choose","between","them."
99 ]
100 ,
101 D.reflow $
102 "Try changing the name of the locally defined module to clear up the ambiguity?"
103 ]
104 )
106 AmbiguousLocal path1 path2 paths ->
107 Report.Report "AMBIGUOUS IMPORT" region [] $
108 Code.toSnippet source region Nothing
109 (
110 D.reflow $
111 "You are trying to import a `" ++ ModuleName.toChars name ++ "` module:"
112 ,
113 D.stack
114 [
115 D.reflow $
116 "But I found multiple files in your \"source-directories\" with that name:"
117 ,
118 D.dullyellow $ D.indent 4 $ D.vcat $
119 map D.fromChars (path1:path2:paths)
120 ,
121 D.reflow $
122 "Change the module names to be distinct!"
123 ]
124 )
126 AmbiguousForeign pkg1 pkg2 pkgs ->
127 Report.Report "AMBIGUOUS IMPORT" region [] $
128 Code.toSnippet source region Nothing
129 (
130 D.reflow $
131 "You are trying to import a `" ++ ModuleName.toChars name ++ "` module:"
132 ,
133 D.stack
134 [
135 D.reflow $
136 "But multiple packages in your \"dependencies\" that expose a module that name:"
137 ,
138 D.dullyellow $ D.indent 4 $ D.vcat $
139 map (D.fromChars . Pkg.toChars) (pkg1:pkg2:pkgs)
140 ,
141 D.reflow $
142 "There is no way to disambiguate in cases like this right now. Of the known name\
143 \ clashes, they are usually for packages with similar purposes, so the current\
144 \ recommendation is to pick just one of them."
145 , D.toSimpleNote $
146 "It seems possible to resolve this with new syntax in imports, but that is\
147 \ more complicated than it sounds. Right now, our module names are tied to GitHub\
148 \ repos, but we may want to get rid of that dependency for a variety of reasons.\
149 \ That would in turn have implications for our package infrastructure, hosting\
150 \ costs, and possibly on how package names are specified. The particular syntax\
151 \ chosen seems like it would interact with all these factors in ways that are\
152 \ difficult to predict, potentially leading to harder problems later on. So more\
153 \ design work and planning is needed on these topics."
154 ]
155 )
159toSuggestions :: ModuleName.Raw -> Set.Set ModuleName.Raw -> [ModuleName.Raw]
160toSuggestions name unimportedModules =
161 take 4 $
162 Suggest.sort (ModuleName.toChars name) ModuleName.toChars (Set.toList unimportedModules)