zen-fs / core

A filesystem, anywhere
https://zen-fs.github.io/core/
MIT License
103 stars 14 forks source link

`readFileSync` Not Working as Expected when using `Fetch` Backend #61

Closed hawav closed 4 months ago

hawav commented 4 months ago

@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'
james-pre commented 4 months ago

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.

james-pre commented 4 months ago

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

- JP

hawav commented 4 months ago

@james-pre 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.

james-pre commented 4 months ago

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

hawav commented 4 months ago

@james-pre 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)
james-pre commented 4 months ago

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 commented 4 months ago

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
james-pre commented 4 months ago

@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 commented 4 months ago

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!

james-pre commented 4 months ago

@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.