jiacai2050 / zigcli

A toolkit for building command lines programs in Zig.
https://zigcli.liujiacai.net/
MIT License
57 stars 4 forks source link

error: FileNotFound on large `tree` runs #15

Closed moderation closed 11 months ago

moderation commented 1 year ago

Describe this problem

When running tree on large directories I'm occasionally getting error: FileNotFound errors. Not sure what triggers but am including output from standard Linux tree and the zigcli tree. This is being run on the https://github.com/Wilfred/difftastic/tree/master/vendored_parsers/vendor

difftastic_vendor_tree_linux.txt difftastic_vendor_tree_zigcli.txt

Is there a way to run with debug output logging to try and find the issue? Thanks for a great tool.

Version

tree --version
Git commit: f1561de
Build date: 2023-09-30T15:06:29-0700
Zig version: 0.12.0-dev.670+19a82ffdb
Zig backend: builtin.CompilerBackend.stage2_llvm
jiacai2050 commented 11 months ago

https://github.com/jiacai2050/zigcli/blob/f1561defb0c05c3e94529bc478e53b0e9c7dbc6a/src/tree.zig#L219

You can replace L219 with following code to debug which file cause the trouble:

@@ -216,7 +216,10 @@ fn walk(
             .directory => {
                 _ = try writer.write("\n");
                 ret.directories += 1;
-                var sub_iter_dir = try iter_dir.dir.openIterableDir(entry.name, .{});
+                var sub_iter_dir = iter_dir.dir.openIterableDir(entry.name, .{}) catch |e| {
+                    std.debug.print("e: {any}, name:{s}\n", .{ e, entry.name });
+                    return e;
+                };
                 defer sub_iter_dir.close();
moderation commented 11 months ago

Not seeing any difference in output. Do I need to change how I invoke?

diff --git a/src/tree.zig b/src/tree.zig
index b408387..5859ec0 100644
--- a/src/tree.zig
+++ b/src/tree.zig
@@ -216,7 +216,10 @@ fn walk(
             .directory => {
                 _ = try writer.write("\n");
                 ret.directories += 1;
-                var sub_iter_dir = try iter_dir.dir.openIterableDir(entry.name, .{});
+                var sub_iter_dir = iter_dir.dir.openIterableDir(entry.name, .{}) catch |e| {
+                    std.debug.print("e: {any}, name:{s}\n", .{ e, entry.name });
+                    return e;
+                };
                 defer sub_iter_dir.close();

                 const new_prefix =
├──tree-sitter-python
│  ├──Cargo.toml
│  ├──LICENSE
│  ├──README.md
│  ├──binding.gyp
│  ├──grammar.js
│  ├──package.json
│  ├──bindings
│  │  ├──node
│  │  │  ├──binding.cc
│  │  │  └──index.js
│  │  └──rust
│  │     ├──README.md
│  │     ├──build.rs
│  │     └──lib.rs
│  ├──examples
│  │  ├──compound-statement-without-trailing-newline.py
│  │  ├──crlf-line-endings.py
│  │  ├──mixed-spaces-tabs.py
│  │  ├──multiple-newlines.py
│  │  ├──python2-grammar-crlf.py
│  │  ├──python2-grammar.py
│  │  ├──python3-grammar-crlf.py
│  │  ├──python3-grammar.py
│  │  ├──python3.8_grammar.py
│  │  ├──error: FileNotFound
jiacai2050 commented 11 months ago

I guess we can apply same changes to all io operations, the following two are most important.

jiacai2050 commented 11 months ago

I found the not-found file, when we first clone the repo, node_modules is not installed, so I guess Linux's tree just ignore this error...

~/difftastic/vendored_parsers/tree-sitter-qmljs/queries 
 (master)$ ll
total 8.0K
lrwxr-xr-x 1 jiacai   61 Oct 12 07:34 highlights-javascript.scm -> ../node_modules/tree-sitter-javascript/queries/highlights.scm
lrwxr-xr-x 1 jiacai   61 Oct 12 07:34 highlights-typescript.scm -> ../node_modules/tree-sitter-typescript/queries/highlights.scm
-rw-r--r-- 1 jiacai 1000 Oct 12 07:34 highlights.scm
-rw-r--r-- 1 jiacai  148 Oct 12 07:34 locals.scm

~/difftastic/vendored_parsers/tree-sitter-qmljs/queries 
 (master)$ ls ../node_modules/tree-sitter-javascript/queries/highlights.scm
ls: cannot access '../node_modules/tree-sitter-javascript/queries/highlights.scm': No such file or directory
moderation commented 11 months ago

Yes - that is the issue. It would be good to implement the pattern matching options in Linux tree so you could exclude node_modules and bypass the error. Or just keep going without returning an error to match Linux tree

  -P pattern    List only those files that match the pattern given.
  -I pattern    Do not list files that match the given pattern.
jiacai2050 commented 11 months ago

Fixed in main, please try again.

PS: it turns out that readLink won't return the not-found error.

moderation commented 11 months ago

This works. Thanks!