Skip to content

Commit 9123599

Browse files
authored
Merge pull request #362 from gilramir/empty-deps-crash-issue-360
toSnippet shouldn't crash if the error has no source code
2 parents 65db372 + 284e97c commit 9123599

File tree

3 files changed

+69
-8
lines changed

3 files changed

+69
-8
lines changed

compiler/src/Reporting/Render/Code.hs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,22 @@ toSource source =
4242
-- CODE FORMATTING
4343

4444
toSnippet :: Source -> A.Region -> Maybe A.Region -> (D.Doc, D.Doc) -> D.Doc
45-
toSnippet source region highlight (preHint, postHint) =
46-
D.vcat
47-
[ preHint,
48-
"",
49-
render source region highlight,
50-
postHint
51-
]
45+
toSnippet source region@(A.Region (A.Position startLine _) (A.Position _ _)) highlight (preHint, postHint) =
46+
if startLine > 0
47+
then
48+
D.vcat
49+
[ preHint,
50+
"",
51+
render source region highlight,
52+
postHint
53+
]
54+
else
55+
-- The region doesn't point to actual source code. Don't render it.
56+
D.vcat
57+
[ preHint,
58+
"",
59+
postHint
60+
]
5261

5362
toPair :: Source -> A.Region -> A.Region -> (D.Doc, D.Doc) -> (D.Doc, D.Doc, D.Doc) -> D.Doc
5463
toPair source r1 r2 (oneStart, oneEnd) (twoStart, twoMiddle, twoEnd) =

gren.cabal

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,14 +255,17 @@ Test-Suite gren-tests
255255
Parse.UnderscorePatternSpec
256256
Parse.MultilineStringSpec
257257
Parse.DeclSpec
258+
Reporting.Error.ImportSpec
258259

259260
Build-Depends:
260261
gren:common,
261262
base >= 4.19 && <5,
262263
containers >= 0.6 && < 0.7,
263264
utf8-string,
264265
bytestring >= 0.11 && < 0.12,
265-
hspec >= 2.7.10 && < 3
266+
hspec >= 2.7.10 && < 3,
267+
prettyprinter >= 1.7.1 && < 2,
268+
text >= 2.1.2 && < 3
266269

267270
Build-Tool-Depends:
268271
hspec-discover:hspec-discover >= 2.7.10 && < 3
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
module Reporting.Error.ImportSpec (spec) where
2+
3+
import Data.Set qualified as Set
4+
import Data.Text as T
5+
import Data.Text.Encoding as T
6+
import Data.Utf8 qualified as Utf8
7+
import Prettyprinter qualified as P
8+
import Prettyprinter.Render.Text (renderStrict)
9+
import Reporting.Annotation qualified as A
10+
import Reporting.Error.Import qualified as Import
11+
import Reporting.Render.Code qualified as Code
12+
import Reporting.Report qualified as Report
13+
import Test.Hspec (Spec, describe, it)
14+
15+
spec :: Spec
16+
spec = do
17+
describe "Import error reporting" $ do
18+
{-
19+
When "gren-lang/core" module is not listed in gren.json, the
20+
default imports that the compiler inserts into the code
21+
fail, but there is no source code region to report.
22+
We want to ensure the reporting code doesn't crash, and
23+
actually reports that modules like Basics (and others) are reported
24+
as not found.
25+
-}
26+
it "Give proper error if default package dependencies are not found" $ do
27+
let -- Empty source code
28+
source = Code.toSource (T.encodeUtf8 (T.pack ""))
29+
30+
-- The "Module Not Found" Error
31+
err =
32+
Import.Error
33+
{ -- IMPORTANT: The region here is zero, not pointing to any
34+
-- possible lines of source code. This happens when
35+
-- "import Basics" is added by default by the compiler, without
36+
-- it appearing in the actual source code.
37+
Import._region = A.Region (A.Position 0 0) (A.Position 0 0),
38+
Import._import = Utf8.fromChars "foo",
39+
Import._unimported = Set.singleton (Utf8.fromChars "Basics"),
40+
Import._problem = Import.NotFound
41+
}
42+
43+
-- Create the report
44+
report = Import.toReport source err
45+
46+
-- Convert the Prettyprinter Doc to a Data.Text
47+
messageText = renderStrict (P.layoutCompact $ Report._message report)
48+
in -- Does it have one of the missing modules reported in it?
49+
T.isInfixOf (T.pack "Basics") messageText

0 commit comments

Comments
 (0)