Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ refactor ] Improve error handling in Core #3434

Open
wants to merge 14 commits into
base: main
Choose a base branch
from

Conversation

spcfox
Copy link
Contributor

@spcfox spcfox commented Dec 3, 2024

Description

  • Added utility functions to handle errors in Core.
  • Added new error types: NonZeroExitCode, SystemError, CodegenNotFound, UnsupportedOpertaion.
  • Added error handling in many places where they were ignored.
  • All coreLift_ calls replaced by explicit result processing.
  • Removed Maybe from compileExpr and incCompileFile signatures. Instead of returning Nothing, throw is used. Earlier both variants were found in the code. Using throw allows you to pass a more clear error.
  • fix Successful exit code after error in --exec #3427: the --exec command returns the program exit code. If several --exec options are passed, the result of the last execution will be returned.
  • Removed CompilationFailed from REPLResult. More specific errors using throw are returned instead.

Should this change go in the CHANGELOG?

  • If this is a fix, user-facing change, a compiler change, or a new paper
    implementation, I have updated CHANGELOG_NEXT.md (and potentially also
    CONTRIBUTORS.md).

@spcfox spcfox marked this pull request as draft December 3, 2024 13:41
src/Compiler/ES/Node.idr Outdated Show resolved Hide resolved
src/Compiler/RefC/RefC.idr Outdated Show resolved Hide resolved
ok <- coreLift $ system cmd
if ok == 0
then pure (Just execPath)
else pure Nothing
safeSystem cmd
pure (Just execPath)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not logically equivalent. Why is it legitimate to drop the Nothing branch?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

safeSystem throws an error if the exit code is not zero. This makes the errors more clear

Comment on lines 453 to 460
when mkexec $ logTime 1 "Build racket" $
safeSystem $ raco ++ ["-o", outBinAbs, outRktAbs]

-- TODO: add launcher script
let outShRel = outputDir </> outfile
makeShPlatform isWindows (if mkexec then "" else racket ++ " ") outShRel appDirRel outRktFile
handleFileError outShRel $ chmodRaw outShRel 0o755
pure (Just outShRel)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again we're also dropping the Nothing branch

Copy link
Contributor Author

@spcfox spcfox Dec 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

safeSystem throw NonZeroExitCode. I replaced all Nothing with throw. Currently Nothing just wraps around to CompilationFailed. A more specific cause of the error is passed to throw.

Idris2/src/Idris/REPL.idr

Lines 815 to 818 in ec74792

ok <- compile cg tm_erased outfile
maybe (pure CompilationFailed)
(pure . Compiled)
ok

node <- coreLift findNode
quoted_node <- pure $ "\"" ++ node ++ "\"" -- Windows often have a space in the path.
coreLift_ $ system (quoted_node ++ " " ++ outn)
system $ "\"" ++ node ++ "\" " ++ out -- Windows often have a space in the path.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does escapeArg not enclose in quotes in Windows?

escapeArg : String -> String
escapeArg cmd = let escapedCmdChars = pack $ unpack cmd >>= escapeArgChar in
if isWindows
then escapedCmdChars
else "\"" ++ escapedCmdChars ++ "\""

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, it escapes spaces, okay

c == ' ' || c == '\t' || c == '\n' || c == ';' || c == ',' || c == '=' || c == '\x0B' || c == '\x0C' || c == '\xFF' ||

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, it worth to add a comment nearby about this to make it clear for the future readers

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI on windows fails if I replace quoting with escapeArg. It's looks like problem with escapeArg

Copy link
Contributor Author

@spcfox spcfox Dec 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI on windows fails if I replace quoting with escapeArg. It's looks like problem with escapeArg

I think it's a Node.js problem nodejs/node#56146

@spcfox spcfox marked this pull request as ready for review December 4, 2024 12:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Successful exit code after error in --exec
3 participants