Skip to content

fix: avoid infinite recursion while searching for path#585

Closed
yajo wants to merge 1 commit intonix-community:masterfrom
moduon:fix-root-path-detection
Closed

fix: avoid infinite recursion while searching for path#585
yajo wants to merge 1 commit intonix-community:masterfrom
moduon:fix-root-path-detection

Conversation

@yajo
Copy link
Copy Markdown
Contributor

@yajo yajo commented Apr 5, 2022

In findGitIgnores, to find the parent path, we do path + "/..". This means that, for each iteration, the path name grows:

  1. /nix/store/1234-example/a/b/
  2. /nix/store/1234-example/a/b/..
  3. /nix/store/1234-example/a/b/../..
  4. etc.

Thus, checking if path is not equal to / will always return true.

With this fix, it will instead check that the path exists. If the path goes higher than /, it will not exist, thus fixing the false positive.

Fixes the failure exercised in https://gitlab.com/moduon/mrchef/-/merge_requests/1.

You can reproduce it with:

nix flake check 'gitlab:moduon/mrchef?rev=7f6177504daaa74d1b8dc64b256fbf15866efb3a' --show-trace

The next commit includes the fix and won't fail:

nix flake check 'gitlab:moduon/mrchef?rev=6466f3e360d1e44f40dea04e376ec48745700007' --show-trace

@moduon MT-83

In `findGitIgnores`, to find the parent path, we do `path + "/.."`. This means that, for each iteration, the path name grows:

1. `/nix/store/1234-example/a/b/`
2. `/nix/store/1234-example/a/b/..`
3. `/nix/store/1234-example/a/b/../..`
4. etc.

Thus, checking if path is not equal to `/` will always return `true`.

With this fix, it will instead check that the path exists. If the path goes higher than `/`, it will not exist, thus fixing the false positive.

@moduon MT-83
@nixos-discourse
Copy link
Copy Markdown

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/how-to-debug-infinite-recursion/18470/4

@yajo yajo mentioned this pull request Apr 11, 2022
@adisbladis
Copy link
Copy Markdown
Member

In findGitIgnores, to find the parent path, we do path + "/..". This means that, for each iteration, the path name grows:

1. `/nix/store/1234-example/a/b/`

2. `/nix/store/1234-example/a/b/..`

3. `/nix/store/1234-example/a/b/../..`

4. etc.

Thus, checking if path is not equal to / will always return true.

This observation isn't entirely correct as we are dealing with a path type, not a string type.
Paths are resolved when concatenated like so:

nix-repl> ./.  
/home/adisbladis/sauce/github.com/nix-community/poetry2nix

nix-repl> ./. + "/.."
/home/adisbladis/sauce/github.com/nix-community

nix-repl> ./. + "/../.."
/home/adisbladis/sauce/github.com

Which eventually ends up at / as you can see from:

nix-repl> ./. + "/../../../../../../../../"
/

Hence, this fix introduces an infinite recursion when there is no gitignore available since / always exists.

So instead of your fix which is

lib.optionals (builtins.pathExists path && ! isGitRoot) (findGitIgnores parent) ++ gitIgnores;

We'll need to

lib.optionals (builtins.pathExists path && builtins.toString path != "/" && ! isGitRoot) (findGitIgnores parent) ++ gitIgnores;

adisbladis added a commit that referenced this pull request Apr 14, 2022
fix: avoid infinite recursion while searching for path #585
@yajo yajo deleted the fix-root-path-detection branch April 14, 2022 08:16
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 this pull request may close these issues.

3 participants