File.read unexpected error

I’ve got a C header file (testfile.h) that only has a comment line "/* comment /". When I execute “puts File.read(testfile.h)” an error is generated:
1 | /
comment */
^
Error: invalid regex: quantifier does not follow a repeatable item at 0

Using Crystal 1.12.1 on x86_64-pc-windows-msvc

This is not expected behavior, shouldn’t it simply read the characters and return the string? If I wanted regex processing, I’d tell it to.

Any explanation or work around appreciated.

Just to be sure, you have a file testfile.h with

/* comment /

And another file, say test.cr, with

puts File.read("testfile.h")

and you execute crystal test.cr and it fails?

Hmm, that case didn’t work earlier but now it does.

I’ve recreated the issue using an ARGV[0] input. The test.cr file is one line:

puts File.read(ARGV[0])

and is in the same directory as the testfile.h.

When executed using

crystal run test.cr testfile.h

it responds with the invalid regex error.

When you pass multiple paths to crystal run they are all treated as source files. So the compiler tries to read testfile.h as Crystal code (which it is not). If you want to pass options to the compiled program, you need to separate them with --: crystal run test.cr -- testfile.h

1 Like