roc icon indicating copy to clipboard operation
roc copied to clipboard

[WIP] Add some basic file I/O to CLI example

Open rtfeldman opened this issue 3 years ago • 2 comments

(this was blocked on https://github.com/rtfeldman/roc/issues/2365, but now that that's fixed, this is unblocked! I'll probably get back to it in mid-May, but have 3 talks and other things that are likely higher priority between now and then.)

rtfeldman avatar Nov 30 '21 03:11 rtfeldman

Using File.readBytes causes a segfault on my system (macOS 12/ARM):

Read this list of bytes: RocList { storage: Some(Refcounted(-9223372036854775808)), elements: [49, 56, 56, 49, 57, 10] }
[1]    94454 segmentation fault  ./roc code.roc

Here's a minimal program that fails:

main =
    task = File.readBytes inputPath # This path should exist.
    Task.attempt task \result ->
        when result is
            Ok _ -> Stdout.line "Read file."
            Err _ -> Stdout.line "Failed to read input."

I threw a few debug statements into the platform code:

readBytes = \path ->
    x = await (Stdout.line "Here") \a -> Stdout.line "oo"
    Effect.map (Effect.readAllBytes path) \answer ->
        z = await (Stdout.line "Here, again.") \a -> Stdout.line ""
        # ...

This prints Here and the output from the rust code, but segfaults before the Here, again.

The issue seems to be in converting the tuple returned by roc_fx_readAllBytes ((RocList<u8>, i32)) to the record Roc expects in the File module ({ bytes : List U8, errno : I32 }). Changing the declaration to just be a RocList<u8> and (List U8) works:

main =
    _ <- await (Stdout.line "Trying to read file...")
    task = File.readBytes inputPath
    Task.attempt task \result ->
        when result is
            Ok _ -> Stdout.line "Read the file!"
            Err _ -> Stdout.line "Failed to read input."

Outputs:

Trying to read file...
Read this list of bytes: RocList { storage: Some(Refcounted(-9223372036854775808)), elements: [49, 56, 56, 49, 57, 10] }
Read the file!

shrik450 avatar Dec 02 '21 02:12 shrik450

Yeah, that's a known problem without a known fix yet! 😄

rtfeldman avatar Dec 02 '21 02:12 rtfeldman

@rtfeldman Is this superceded by #3597?

JanCVanB avatar Sep 07 '22 07:09 JanCVanB

I'm gonna clean all of these up and get things merged this week!

rtfeldman avatar Sep 07 '22 18:09 rtfeldman

#4030 is merged, so this is now outdated!

JanCVanB avatar Sep 13 '22 02:09 JanCVanB