imp: include: improve cycle and read failure error messages

This commit is contained in:
Simon Michael 2025-07-11 13:32:06 -07:00
parent b71e001c51
commit 536589e2c2

View File

@ -366,27 +366,24 @@ includedirectivep = do
-- as if it was inlined in the current (parent) file. -- as if it was inlined in the current (parent) file.
-- The position in the parent file is provided for error messages. -- The position in the parent file is provided for error messages.
parseIncludedFile :: MonadIO m => Int -> SourcePos -> PrefixedFilePath -> ErroringJournalParser m () parseIncludedFile :: MonadIO m => Int -> SourcePos -> PrefixedFilePath -> ErroringJournalParser m ()
parseIncludedFile off pos prefixedpath = do parseIncludedFile off _pos prefixedpath = do
let (_mprefix,filepath) = splitReaderPrefix prefixedpath let (_mprefix,filepath) = splitReaderPrefix prefixedpath
-- Throw an error if a cycle is detected -- Throw an error if a cycle is detected
parentj <- get parentj <- get
let parentfilestack = jincludefilestack parentj let parentfilestack = jincludefilestack parentj
when (filepath `elem` parentfilestack) $ when (dbg7 "parseIncludedFile: reading" filepath `elem` parentfilestack) $
customFailure $ parseErrorAt off $ "Cyclic include: " ++ filepath customFailure $ parseErrorAt off $ "This included file forms a cycle: " ++ filepath
-- Read this file's content, or throw an error -- Read the file's content, or throw an error
childInput <- childInput <- lift $ readFilePortably filepath `orRethrowIOError` "reading a file failed"
dbg6Msg ("parseChild: "++takeFileName filepath) $
lift $ readFilePortably filepath
`orRethrowIOError` (show pos ++ " reading " ++ filepath)
let initChildj = newJournalWithParseStateFrom filepath parentj let initChildj = newJournalWithParseStateFrom filepath parentj
-- Choose a reader based on the file path prefix or file extension, -- Choose a reader based on the file path prefix or file extension,
-- defaulting to JournalReader. Duplicating readJournal a bit here. -- defaulting to JournalReader. Duplicating readJournal a bit here.
let r = fromMaybe reader $ findReader Nothing (Just prefixedpath) let r = fromMaybe reader $ findReader Nothing (Just prefixedpath)
parser = rParser r parser = rParser r
dbg6IO "parseChild: trying reader" (rFormat r) dbg6IO "parseIncludedFile: trying reader" (rFormat r)
-- Parse the file (and its own includes, if any) to a Journal -- Parse the file (and its own includes, if any) to a Journal
-- with file path and source text attached. Or throw an error. -- with file path and source text attached. Or throw an error.