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

include filename in error: could not read file message #1530

Closed
jakobhellermann opened this issue Dec 8, 2024 · 4 comments · Fixed by #1532
Closed

include filename in error: could not read file message #1530

jakobhellermann opened this issue Dec 8, 2024 · 4 comments · Fixed by #1532

Comments

@jakobhellermann
Copy link

jakobhellermann commented Dec 8, 2024

I was building a project with janet build mistakenly instead of jpm build and got

error: could not read file
  in file/read [src/core/io.c] on line 193
  in chunks [boot.janet] on line 3009, column 24
  in run-context [boot.janet] on line 2653, column 12
  in dofile [boot.janet] on line 3032, column 5
  in cli-main [boot.janet] on line 4645, column 17

as a result.

This isn't particularly helpful and it would be neat if the message included the name of the file it tried to read.

Looks like the error comes from

janet_panic("could not read file");
which only has access to a JanetFile, which doesn't include the filename, only a FILE*:

janet/src/include/janet.h

Lines 1248 to 1251 in 83e8aab

struct JanetFile {
FILE *file;
int32_t flags;
};

So possible solutions could be

  1. store the filename the file was opened by in JanetFile
  2. attempt to find the filename associated to the file descriptor. (could be /proc/self/fd/NNN or F_GETPATH or F_GETPATH or GetFileInformationByHandleEx on windows? would need to investigate)
    if this is in scope for janet
@strangepete
Copy link
Contributor

strangepete commented Dec 13, 2024

Giving this the quick eyeball, I'm curious as to why 'build' is passing file/open in boot.janet and instead failing in fread; file/open at least returns the filename in the error.

Edit: oddly, when I type the same janet build I do in fact get a filename in the error:

error: could not find file build
  in dofile [boot.janet] on line 2966, column 5
  in cli-main [boot.janet] on line 4539, column 17

@sogaiu
Copy link
Contributor

sogaiu commented Dec 13, 2024

I think if one does jpm build at some point, then a build directory may get created. If subsequently one does janet build without removing the build directory, one may get:

$ janet build
error: could not read file
  in file/read [src/core/io.c] on line 193
  in chunks [boot.janet] on line 3009, column 24
  in run-context [boot.janet] on line 2653, column 12
  in dofile [boot.janet] on line 3032, column 5
  in cli-main [boot.janet] on line 4645, column 17

If the build directory doesn't exist:

$ rm -rf build && janet build
error: could not find file build
  in dofile [boot.janet] on line 3030, column 5
  in cli-main [boot.janet] on line 4645, column 17

@strangepete
Copy link
Contributor

strangepete commented Dec 13, 2024

That is odd because I get the full error message regardless of the directories existence; on Windows 🤨

Edit: can confirm what you see on my linux box too

@strangepete
Copy link
Contributor

strangepete commented Dec 13, 2024

https://stackoverflow.com/questions/42876210/c-fopen-opening-directories

Seems fopen has a quirk about detecting directories with write mode


The fopen() function will fail if:
[…]
[EISDIR] The named file is a directory and mode requires write access.

So if fopen is called with r instead of r+ it won't fail. I believe we just need a call to stat() on not-windows to confirm its a file

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 a pull request may close this issue.

3 participants