Skip to content

Fix search file feature unnecessarily prepending ./ #119

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

Merged
merged 8 commits into from
Feb 6, 2021

Conversation

ovv
Copy link
Contributor

@ovv ovv commented Feb 6, 2021

If the file path already starts with . or /, which can happen if such a path was used as the base directory, then don't prepend ./.
Without such logic, the path becomes invalid. For example:

  • if the current token is /etc/ and you select systemd, you end up with './/etc/systemd'
  • if the current token is ~/ and you select Desktop, you end up with './~/desktop'

Additionally, since the output paths are quoted, we need to use the expanded token when outputting paths because ~ doesn't get expanded inside quotes.

else
set --append fzf_arguments --query=$token --preview='__fzf_preview_file {}'
set --append fzf_arguments --query=$expanded_token --preview='__fzf_preview_file {}'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see the bug. We used token intentionally because in all of these instances, ~ does expand to $HOME

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without those if I do a search with ~/ as token and select desktop I end up with './~/desktop'.

The result doesn't work because it's escaped.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did some more tests, the expanded_token is only needed there

set current_token (commandline --current-token)
if test "$commandline_tokens" = "$current_token"
set file_paths_selected ./$file_paths_selected
end
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain the bug here, please?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you do a query with /etc/ as token and select the systemd directory you end up with .//etc/systemd

@PatrickF1
Copy link
Owner

Oh boy I'm starting to regret introducing the ./ feature. Thanks Quentin!

@ovv ovv force-pushed the ovv/search_current_dir_fix branch from 0fa4a54 to ddee6ff Compare February 6, 2021 15:23
ovv added 2 commits February 6, 2021 16:24
Doing a query on a token starting with '~' lead to an escaped
result. Using $extended_token makes the result be the full path
which doesn't need escaping and can be used as is.
A path starting from the root directory doesn't need './' to cd
additionally this conflict with the starting / of the path
@ovv ovv force-pushed the ovv/search_current_dir_fix branch from ddee6ff to e8ae9e6 Compare February 6, 2021 15:24
@PatrickF1
Copy link
Owner

Hey Quentin, let's back up. The bug you're trying to fix is basically when the current token is used as the base path and it starts with ~ or / right? If so, I think there's a simpler and more straightforward way to fix it. Hope you don't mind if I redo your PR a bit.

@PatrickF1 PatrickF1 changed the title search current dir edge cases Fix search files messing up path by appending ./ when it doesn't need to Feb 6, 2021
@ovv
Copy link
Contributor Author

ovv commented Feb 6, 2021

Hope you don't mind if I redo your PR a bit.

Sure no problem, you have a lot more experience with this code than me. Take this PR as an extended bug report :)

Hey Quentin, let's back up. The bug you're trying to fix is basically when the current token is used as the base path and it starts with ~ or / right? If so, I think there's a simpler and more straightforward way to fix it.

Exactly, I just tested your changes. They do fix the / case but it doesn't fix ~.

Any result with a ~ will be escaped by

commandline --current-token --replace -- (string escape -- $file_paths_selected | string join ' ')

and then fish will not expand ~ to $HOME. We do need to keep the escaping (to handle path with a for example). The only solution I see is to replace ~ by $HOME ourselves.

@PatrickF1
Copy link
Owner

Exactly, I just tested your changes. They do fix the / case but it doesn't fix ~.

You're right. I caught that and was looking for a way to avoid having to always expand ~ because it makes the resulting command line kinda ugly. But it looks like I don't have a choice without terribly complicating the code. Darn

@PatrickF1 PatrickF1 changed the title Fix search files messing up path by appending ./ when it doesn't need to Fix two bugs in search file feature Feb 6, 2021
@PatrickF1 PatrickF1 changed the title Fix two bugs in search file feature Fix search file feature unnecessarily prepending ./ if there is already a . or / Feb 6, 2021
@PatrickF1
Copy link
Owner

@ovv updated. Can you help me test one more time, please?

@ovv
Copy link
Contributor Author

ovv commented Feb 6, 2021

everything looks good :shipit:

@PatrickF1 PatrickF1 force-pushed the ovv/search_current_dir_fix branch from 217abcb to c6c0928 Compare February 6, 2021 21:43
@PatrickF1 PatrickF1 changed the title Fix search file feature unnecessarily prepending ./ if there is already a . or / Fix search file feature unnecessarily prepending ./ Feb 6, 2021
@PatrickF1 PatrickF1 merged commit b19e3f8 into PatrickF1:main Feb 6, 2021
@@ -23,6 +23,6 @@ function __fzf_preview_file --argument-names file_path --description "Print a pr
else if test -p "$file_path"
__fzf_report_file_type "$file_path" "named pipe"
else
echo "File doesn't exist." >&2
echo "File doesn't exist or is empty." >&2
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turns out this case is hit also when the file is empty.

@kidonng
Copy link
Contributor

kidonng commented Feb 8, 2021

Thank you @ovv! Yes, the tilde should be expanded because it may be quoted.

Oh boy I'm starting to regret introducing the ./ feature.

It's certainly my oversight. I didn't notice this when making #75 and #109.

hrshtst pushed a commit to hrshtst/fzf.fish that referenced this pull request Apr 22, 2021
If the file path already starts with . or /, which can happen if such a path was used as the base directory, then don't prepend ./. Without such logic, the path becomes invalid. For example:
- if the current token is /etc/ and you select systemd, you end up with './/etc/systemd'
- if the current token is ~/ and you select Desktop, you end up with './~/desktop'

Additionally, since the output paths are quoted, we need to use the expanded token when outputting paths because ~ doesn't get expanded inside quotes.
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