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

readFileSync Not Working as Expected when using Fetch Backend #61

Closed
hawav opened this issue May 17, 2024 · 10 comments
Closed

readFileSync Not Working as Expected when using Fetch Backend #61

hawav opened this issue May 17, 2024 · 10 comments
Assignees
Labels
bug Something isn't working
Milestone

Comments

@hawav
Copy link

hawav commented May 17, 2024

@zenfs/core versions: 0.11.2, 0.12.0

Expected Behavior:

The readFileSync function should be able to fetch the content of test.txt from the remote server, just like the readFile function does.

Actual Behavior:

The readFileSync function throws an ENOENT error, indicating that it cannot find the file, which is not true.

Steps to Reproduce:

  1. Created a file test.txt in the directory with the content "Hello ZenFS!"
echo "Hello ZenFS!" > test.txt
  1. Generated the index file using the make-index script from ZenFS.
npx -p @zenfs/core make-index . -o index.json
  1. Started an HTTP file server using serve on port 8080 with CORS enabled.
serve . -p 8080 --cors
  1. Configured ZenFS to use the Fetch Backend, pointing to the local server.
import * as zenfs from '@zenfs/core'

await zenfs.configure({
    backend: zenfs.Fetch,
    baseUrl: '//localhost:8080',
    index: '//localhost:8080/index.json'
});
  1. Used the readFile function to successfully fetch test.txt from the remote server.
zenfs.readFile('test.txt', (e, content) => console.log(content?.toString()));
  1. Attempted to use the readFileSync function, but encountered an error.
console.log(zenfs.readFileSync('test.txt'));  
// Error: ENOENT: No such file or directory, '/test.txt'
@hawav hawav changed the title readFileSync Not Working as Expected when using Fetch Backend readFileSync Not Working as Expected when using Fetch Backend May 17, 2024
@dr-vortex dr-vortex self-assigned this May 17, 2024
@dr-vortex dr-vortex added the bug Something isn't working label May 17, 2024
@dr-vortex dr-vortex added this to the 1.0 milestone May 17, 2024
@dr-vortex
Copy link
Member

dr-vortex commented May 17, 2024

Okay, I've successfully reproduced the issue. I can confirm AsyncFS.crossCopy is not even called, so I believe that is the issue.

Edit: FetchFS does not mixin Async so it will not have caching, which means that it won't even have a sync cache fs.

@dr-vortex
Copy link
Member

dr-vortex commented May 17, 2024

@hawav This should be fixed in v0.12.0. Can you please test it with your use case, to make sure? Thanks!

- JP

@hawav
Copy link
Author

hawav commented May 17, 2024

@dr-vortex Unfortunately, the issue still persists in v0.12.0. I've verified that I'm using the correct version, and after reproducing the issue again, I can confirm that the readFileSync function is not working as expected.

Could you please investigate further to identify the root cause and provide a fix? Thanks for your help and attention to this issue.

@dr-vortex
Copy link
Member

@hawav Could you provide a complete stack trace of the error message? (Please copy and paste into a code block).

@hawav
Copy link
Author

hawav commented May 17, 2024

@dr-vortex Here is the complete stack trace of the error message:

Uncaught Error: ENOENT: No such file or directory, '/test.txt'
    ErrnoError error.js:261
    With error.js:249
    _openSync sync.js:113
    _readFileSync sync.js:165
    readFileSync sync.js:184
    EditorWindow page.tsx:27
    ... (no more zenfs related stack)

@dr-vortex
Copy link
Member

I hope you don't mind, I truncated the stack trace you provided to the part relevant to ZenFS.

In any case, could you please make this change to the dist/emulation/sync.js file:

@@ -100,6 +100,7 @@ function _openSync(_path, _flag, _mode, resolveSymlinks = true) {
         stats = wrap('statSync', resolveSymlinks, path, cred);
     }
     catch (e) {
+		debugger;
         // File does not exist.
         switch (pathNotExistsAction(flag)) {
             case ActionType.CREATE:

Then comment with e (the error being caught)?

@hawav
Copy link
Author

hawav commented May 19, 2024

Of course, here is the content of e:

Error: ENOENT: No such file or directory, '/test.txt'
    ErrnoError error.js:261
    With error.js:249
    _findINodeSync fs.js:454
    findINodeSync fs.js:474
    statSync fs.js:204
    wrap sync.js:15
    _openSync sync.js:100
    _readFileSync sync.js:165
    readFileSync sync.js:184

@dr-vortex
Copy link
Member

dr-vortex commented May 19, 2024

@hawav 0.12.1 made some changes. I doubt it fixed the issue, but could you check anyway? Also, could you provide an updated stack trace? You won't need to make any changes or open the debugger since _open and _openSync correctly throw errors now.

@hawav
Copy link
Author

hawav commented May 20, 2024

After upgrading to version 0.12.1 of @zenfs/core, I encountered an error with the message EINVAL: Index version mismatch. This error suggests that there might be a mismatch in the index version between the older and newer versions of the library.

To resolve this issue, I regenerated the index.json file using the latest version of @zenfs/core with the following command:

npx -p @zenfs/core@0.12.1 make-index . -o index.json

After executing this command, the error was resolved, and I was able to successfully retrieve the content of the file, which reads "Hello ZenFS!".

Since I've confirmed that the issue has been resolved, I'm going to close this issue.

If this issue persists for other users, it might be worth investigating the version compatibility between the older and newer versions of the index.json format in @zenfs/core.

Thank you for your attention to this matter!

@hawav hawav closed this as completed May 20, 2024
@dr-vortex
Copy link
Member

@hawav In hindsight, I believe your index.json may have been invalid for 0.11.*. This would explain why the issue was fixed on my side but not on yours.

0.12.0 did change the index.json format to use versioning, and to store the entries in a different way which actually keeps track of stats.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants