Deal with non-existent files more gracefully

So now you can start idris2 with a non-existent file and use :e to start
editing it
This commit is contained in:
Edwin Brady 2019-07-07 13:10:14 +01:00
parent ccc53813ca
commit 51c406ff7d
17 changed files with 55 additions and 18 deletions

View File

@ -57,15 +57,15 @@ readDataFile fname
-- looking first in the build directory then in the extra_dirs -- looking first in the build directory then in the extra_dirs
export export
nsToPath : {auto c : Ref Ctxt Defs} -> nsToPath : {auto c : Ref Ctxt Defs} ->
FC -> List String -> Core String FC -> List String -> Core (Either Error String)
nsToPath loc ns nsToPath loc ns
= do d <- getDirs = do d <- getDirs
let fnameBase = showSep (cast sep) (reverse ns) let fnameBase = showSep (cast sep) (reverse ns)
let fs = map (\p => p ++ cast sep ++ fnameBase ++ ".ttc") let fs = map (\p => p ++ cast sep ++ fnameBase ++ ".ttc")
(build_dir d :: extra_dirs d) (build_dir d :: extra_dirs d)
Just f <- firstAvailable fs Just f <- firstAvailable fs
| Nothing => throw (ModuleNotFound loc ns) | Nothing => pure (Left (ModuleNotFound loc ns))
pure f pure (Right f)
-- Given a namespace, return the full path to the source module (if it -- Given a namespace, return the full path to the source module (if it
-- exists in the working directory) -- exists in the working directory)

View File

@ -217,7 +217,7 @@ perror (BadRunElab _ env script)
perror (GenericMsg _ str) = pure str perror (GenericMsg _ str) = pure str
perror (TTCError msg) = pure $ "Error in TTC file: " ++ show msg perror (TTCError msg) = pure $ "Error in TTC file: " ++ show msg
perror (FileErr fname err) perror (FileErr fname err)
= pure $ "File error in " ++ show fname ++ ": " ++ show err = pure $ "File error in " ++ fname ++ ": " ++ show err
perror (ParseFail _ err) perror (ParseFail _ err)
= pure $ show err = pure $ show err
perror (ModuleNotFound _ ns) perror (ModuleNotFound _ ns)

View File

@ -145,7 +145,10 @@ buildMod loc num len mod
= do clearCtxt; addPrimitives = do clearCtxt; addPrimitives
let src = buildFile mod let src = buildFile mod
mttc <- getTTCFileName src ".ttc" mttc <- getTTCFileName src ".ttc"
depFiles <- traverse (nsToPath loc) (imports mod) -- We'd expect any errors in nsToPath to have been caught by now
-- since the imports have been built! But we still have to check.
depFilesE <- traverse (nsToPath loc) (imports mod)
let (ferrs, depFiles) = getEithers depFilesE
ttcTime <- catch (do t <- fnameModified mttc ttcTime <- catch (do t <- fnameModified mttc
pure (Just t)) pure (Just t))
(\err => pure Nothing) (\err => pure Nothing)
@ -167,10 +170,22 @@ buildMod loc num len mod
": Building " ++ showMod ++ ": Building " ++ showMod ++
" (" ++ src ++ ")" " (" ++ src ++ ")"
[] <- process {u} {m} msg src [] <- process {u} {m} msg src
| errs => do traverse emitError errs | errs => do traverse emitError (ferrs ++ errs)
pure errs pure (ferrs ++ errs)
pure [] traverse_ emitError ferrs
else pure [] pure ferrs
else do traverse_ emitError ferrs
pure ferrs
where
getEithers : List (Either a b) -> (List a, List b)
getEithers [] = ([], [])
getEithers (Left x :: es)
= let (ls, rs) = getEithers es in
(x :: ls, rs)
getEithers (Right y :: es)
= let (ls, rs) = getEithers es in
(ls, y :: rs)
buildMods : {auto c : Ref Ctxt Defs} -> buildMods : {auto c : Ref Ctxt Defs} ->
{auto s : Ref Syn SyntaxInfo} -> {auto s : Ref Syn SyntaxInfo} ->

View File

@ -58,7 +58,8 @@ readModule : {auto c : Ref Ctxt Defs} ->
(as : List String) -> -- Namespace to import into (as : List String) -> -- Namespace to import into
Core () Core ()
readModule top loc vis reexp imp as readModule top loc vis reexp imp as
= do fname <- nsToPath loc imp = do Right fname <- nsToPath loc imp
| Left err => throw err
Just (syn, hash, more) <- logTime ("Reading " ++ show imp) $ Just (syn, hash, more) <- logTime ("Reading " ++ show imp) $
readFromTTC {extra = SyntaxInfo} readFromTTC {extra = SyntaxInfo}
loc vis fname imp as loc vis fname imp as
@ -89,7 +90,8 @@ readHash : {auto c : Ref Ctxt Defs} ->
{auto u : Ref UST UState} -> {auto u : Ref UST UState} ->
Import -> Core (List String, Int) Import -> Core (List String, Int)
readHash imp readHash imp
= do fname <- nsToPath (loc imp) (path imp) = do Right fname <- nsToPath (loc imp) (path imp)
| Left err => throw err
h <- readIFaceHash fname h <- readIFaceHash fname
-- If the import is a 'public' import, then it forms part of -- If the import is a 'public' import, then it forms part of
-- our own interface so add its hash to our hash -- our own interface so add its hash to our hash

View File

@ -373,9 +373,10 @@ loadMainFile : {auto c : Ref Ctxt Defs} ->
String -> Core () String -> Core ()
loadMainFile f loadMainFile f
= do resetContext = do resetContext
updateErrorLine !(buildDeps f)
Right res <- coreLift (readFile f) Right res <- coreLift (readFile f)
| Left err => setSource "" | Left err => do emitError (FileErr f err)
setSource ""
updateErrorLine !(buildDeps f)
setSource res setSource res
-- Returns 'True' if the REPL should continue -- Returns 'True' if the REPL should continue

View File

@ -30,7 +30,7 @@ idrisTests
"basic021", "basic022", "basic023", "basic024", "basic021", "basic022", "basic023", "basic024",
"coverage001", "coverage002", "coverage003", "coverage004", "coverage001", "coverage002", "coverage003", "coverage004",
"error001", "error002", "error003", "error004", "error005", "error001", "error002", "error003", "error004", "error005",
"error006", "error007", "error006", "error007", "error008", "error009",
"import001", "import002", "import001", "import002",
"interactive001", "interactive002", "interactive003", "interactive004", "interactive001", "interactive002", "interactive003", "interactive004",
"interactive005", "interactive006", "interactive007", "interactive008", "interactive005", "interactive006", "interactive007", "interactive008",

View File

@ -1,3 +1,3 @@
$1 --no-prelude Do.blod < input $1 --no-prelude Do.idr < input
rm -rf build rm -rf build

View File

@ -1,3 +1,3 @@
$1 --check VIndex.blod $1 --check VIndex.idr
rm -rf build rm -rf build

View File

@ -0,0 +1,3 @@
File error in DoesntExist.idr: File Not Found
Welcome to Idris 2 version 0.0. Enjoy yourself!
Main> Bye for now!

View File

@ -0,0 +1 @@
:q

3
tests/idris2/error008/run Executable file
View File

@ -0,0 +1,3 @@
$1 DoesntExist.idr < input
rm -rf build

View File

@ -0,0 +1,4 @@
import DoesntExist
foo : Int
foo = 94

View File

@ -0,0 +1,4 @@
(toplevel):1:1--1:1:DoesntExist not found
Exists.idr:1:1--3:1:DoesntExist not found
Welcome to Idris 2 version 0.0. Enjoy yourself!
Main> Bye for now!

View File

@ -0,0 +1 @@
:q

3
tests/idris2/error009/run Executable file
View File

@ -0,0 +1,3 @@
$1 Exists.idr < input
rm -rf build

View File

@ -1,3 +1,3 @@
$1 Do.blod < input $1 Do.idr < input
rm -rf build rm -rf build

View File

@ -1,3 +1,3 @@
$1 --check PError.run $1 --check PError.idr
rm -rf build rm -rf build