Skip to content

Fix potential crashes: Better error handling for project names #201

@NickSeagull

Description

@NickSeagull

What's the problem?

When you run neo build or neo shell with an invalid project name, the CLI might crash instead of giving you a helpful error message.

We're using Maybe.getOrDie in our code, which is basically saying "this should work, and if it doesn't... 💥 crash!" That's fine for prototyping, but not great when someone's trying to build their project at 10 PM.

Where this happens

Both commands have this risky code:

let cabalFileName =
      [fmt|#{projectName}.cabal|]
        |> Path.fromText
        |> Maybe.getOrDie  -- 😬 This could crash!

Found in:

  • cli/src/Neo/Build.hs (line 36)
  • cli/src/Neo/Shell.hs (line 36)

Why we should fix this

Imagine you're Jess, our target developer:

  • It's late, you're tired
  • You accidentally type neo build "my project" (with a space)
  • Instead of a helpful message, the CLI crashes
  • You spend 20 minutes debugging instead of building

We can do better!

How to fix it

We have a few options - pick the one that feels right:

Option A: Quick fix (30 minutes)

Just handle the error gracefully:

-- Instead of crashing, show a helpful message
cabalFileName <- case Path.fromText [fmt|#{projectName}.cabal|] of
  Nothing -> Task.throw (Error.InvalidProjectName 
    "Project names can't contain spaces or special characters. Try: my-project")
  Just path -> Task.yield path

Option B: Validate early (1 hour)

Check the project name when we first read the configuration:

-- In project configuration parsing
validateProjectName :: Text -> Task Error ProjectName
validateProjectName name = 
  if hasValidChars name
    then Task.succeed (ProjectName name)
    else Task.fail "Project name must contain only letters, numbers, and hyphens"

Option C: Make it impossible to fail (2-3 hours)

Create a smart constructor that guarantees valid paths at compile time. This is more advanced but prevents the issue entirely.

Getting started

Time estimate: 30 minutes - 2 hours (depending on approach)

Perfect for: Someone who wants to improve error handling and make the CLI more user-friendly

Steps:

  1. Try to reproduce the crash (create a project with spaces in the name)
  2. Pick your approach (start with Option A if unsure!)
  3. Fix it in both files
  4. Test that invalid names now show helpful errors
  5. Make sure valid names still work!

Testing your fix

Before:

$ neo build "my cool project"
[CRASH: getOrDie called on Nothing]  # 😱

After:

$ neo build "my cool project"
Error: Invalid project name "my cool project"
Project names can only contain letters, numbers, and hyphens.
Try: my-cool-project

Need help?

This is about making NeoHaskell friendlier when things go wrong. If you're stuck for more than 15 minutes, jump in our [Discord](link-to-discord) - we've all been bitten by getOrDie before!

Context: Found during our haskell.nix migration (PR #199). The TODOs in the code are calling out to be fixed!

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issueGood for newcomerspackage: cliRelated to the Command Line Tooltype: chorework: obviousStraightforward tasks with known solutions; follow best practices

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions