-
Notifications
You must be signed in to change notification settings - Fork 457
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
Fix issue with bad characters in file paths #3386
base: master
Are you sure you want to change the base?
Fix issue with bad characters in file paths #3386
Conversation
@@ -215,7 +215,7 @@ export class RemoteFile { | |||
} | |||
|
|||
get filename(): string { | |||
return this.path.replace(/^.*[\\/]/, ''); | |||
return this.path.replace(/^.*[/]/, ''); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that elsewhere in the code, to obtain the filename, the path is being split by "/", and the last item taken - this change is consistent with that - I haven't tested this on Windows.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As for the handling in the frontend, we should definitely be checking which platform we're running on and switching the regex based off that, not assuming. That's really the only 'proper' solution here since any character except for / and \0 is a valid pathname character on *nix.
@Qix- FYI :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, makes sense. Would it be possible if we go one step further and add a Tauri function that returns the path separator for the platform, and incorporate that into the path splitting function?
I take it you are referring to the change I introduced in this line (if not, please correct me 😄 ):
As far as I can tell from testing on Windows, \
is not used as a path separator in the UI. (If you check my screenshot from above, which was taken on a Windows VM, the file path that gets logged to the console is Sub/Folder/With space/hello world.txt
)
If I were to add a patch to split by \
on Windows and on /
on Linux, it would actually break things. Windows would start considering whole file paths a single file name.
Also note that there are more places already in the frontend codebase that use /
for path separator, which do not differentiate by OS:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Qix- (sorry, forgot to mention you)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right but they're not really correct; the app was written with only MacOS and Linux in mind, we're in the process of making it cross platform. We should be doing the right thing here and doing proper path parsing that is platform-aware, instead of standardizing on forward slashes.
The biggest reason why is that there might be paths coming from the frontend (user input, etc.) that we need to parse correctly regardless of the separator(s) supported. On windows it's [\\/]
and on *nix it's /
, I don't really see any other way around it than to do a platform check.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we're in the process of making it cross platform
Aaaah, right, I see now. I managed to run it on a Windows VM without WSL (I can't test with WSL because of virtualization), and it seemed to work pretty well. Occasionally I had to right-click and refresh, and it was a bit sluggish (I think that may be related to the VM, though, it took forever to compile).
that there might be paths coming from the frontend (user input, etc.)
Has anything like this already been implemented? I personally can't foresee having to input a path manually in a text box or something similar... (But then again, I don't know what the plan is for GitButler.)
On windows it's
[\\/]
and on *nix it's/
, I don't really see any other way around it than to do a platform check.
My recommendation in this case would be to stick to the /
separator - this is what is already working on Windows. Otherwise, you might have to rewrite a whole load of stuff which would be unneeded. So, when (if?) anyone enters a path manually in an input field, convert it to *nix format before passing it to the backend.
Anyway, if the need to use different path formats based on environment were required, I wouldn't add it in this PR, it's better to keep PRs small and related if possible (I only added that small change because it was actually fixing a visualization issue, it didn't break anything on Windows, and has small impact - it was only being used in two components). It would be better to change all the path handling logic in a separate PR all at once. However, I'm not 100% sure if this will be needed, TBH.
Adding support for detecting the OS from the frontend is trivial - the navigator.userAgent
string can be queried:
Please let me know if there is an issue anywhere which requires OS detection so I can have a look into it (I can test on Linux and Windows inside VM without WSL).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Has anything like this already been implemented?
Yes, namely when setting up a new project.
My recommendation in this case would be to stick to the / separator - this is what is already working on Windows.
I'm less inclined to say this really solves the issue as IMO we should be matching the platform paths as closely as possible, despite what works in some shells (cmd.exe notably does not accept the /
separator in all cases, and many Windows utilities do not either).
Adding support for detecting the OS from the frontend is trivial - the navigator.userAgent string can be queried:
Yes, that should be sufficient for now. If it ends up not being so then we can add a Tauri command for it later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, namely when setting up a new project.
That seems to be working fine in Windows and in Linux.
I'm less inclined to say this really solves the issue
What issue?
cmd.exe notably does not accept <...>
At any point in your code are you launching another process in cmd.exe
and passing in paths?
I'm not fully understanding what the rust portions of this PR are fixing. Could you perhaps explain a bit why a custom serializer that wraps JSON's default Path formatting is necessary here? As for the handling in the frontend, we should definitely be checking which platform we're running on and switching the regex based off that, not assuming. That's really the only 'proper' solution here since any character except for |
@Qix- Sure: The problem was that when the files in the repo contain
Which would then cause the UI to break: What this PR achieves is that the file path is escaped:
and so the ownership lines can be properly parsed back and understood by the UI (and backend). [edit] - lemme check how this behaves in Windows... |
Thanks, makes sense. Would it be possible if we go one step further and add a Tauri function that returns the path separator for the platform, and incorporate that into the path splitting function? |
By the way, changes should probably be rebased on top of #3405 which is going to be merged here shortly to unblock some of the other changes we want to make this week. It's just a rename with minimal other changes so it should rebase cleanly. Just wanted to give you a heads up. |
Updated my branch (again). |
@Qix- since the code moved quite a bit, would you mind doing the rebase / conflicts on this one? I can also attempt to do it tomorrow morning as I am catching up after my vacation |
These were 'too far gone' to bring back with merge-tools.
7450cdb
to
84959e8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And it works again :)!
Screen.Recording.2024-04-25.at.15.53.41.mov
Something that came to mind is where the escaping happens. When communicating between backend and frontend, I'd think that JSON is good enough to handle this, while the frontend would probably be the one responsible for assuring it can display paths properly. Another part of me thinks that core::serde::hash_to_hex
already knows that that binary hash digests should be in hex for the frontend, so it's not really an argument and it's probably OK for now that core
types *when serializingusing
serde` will have the frontend in mind.
Besides that, we'd only have to take care of the internal file formats which also are sensitive to newlines, so all file-paths need to be cleaned up. But there, we wouldn't necessarily have to use the json-based quoting, but for a lack of alternatives, it's probably totally fine.
Is this pr still relevant? |
This PR fixes an issue where the app would be left in an inconsistent state when a filename in the repo is present with strange characters in it (for example "\n").
To reproduce the issue, run the following command from a bash terminal inside a repo that is being watched by GitButler:
touch oh_no$'\n'.txt
Should file names contain
\n
in them? Probably not. Can they? It would seem so. If they can, it would be better that the app doesn't error out.Note that I haven't tested all the code that this PR modifies, and there may be some
path.display()
which needs replacing - the places I searched for were:Serialized
withserde
and contains a PathBuffile_path.display()
where being used were changed