Skip to content
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

[Bug] Needs Immediate Solution - Weird backslash handling #19

Open
YazeedAlKhalaf opened this issue Aug 10, 2020 · 11 comments
Open

[Bug] Needs Immediate Solution - Weird backslash handling #19

YazeedAlKhalaf opened this issue Aug 10, 2020 · 11 comments

Comments

@YazeedAlKhalaf
Copy link

The problem is that when I try to run a script using await shell.run(my_awesome_script).
If there is a \\ in my_awesome_script then it will take it as is, what I want is to escape it but it is not allowing me to do that. Specifically when I try to add a " after it which is frustrating.

Example:

print(
        "osascript -e \'do shell script \"cp -r \\"${await _localStorageService.getTempDiretoryPath()}/$tempDirName/$vscodeappName\\"  /Applications\" with administrator privileges\'");
await shell.run('''
osascript -e \'do shell script \"cp -r \\"${await _localStorageService.getTempDiretoryPath()}/$tempDirName/$vscodeappName\\"  /Applications\" with administrator privileges\'
''');

Output:

flutter: osascript -e 'do shell script "cp -r \"/Users/yazeed/Library/Caches/flutter_installer_nzs6j/Visual Studio Code.app\"  /Applications" with administrator privileges'
[ERROR:flutter/lib/ui/ui_dart_state.cc(171)] Unhandled Exception: ShellException(osascript -e 'do shell script "cp -r \\"/Users/yazeed/Library/Caches/flutter_installer_nzs6j/Visual Studio Code.app\\"  /Applications" with administrator privileges', exitCode 1, workingDirectory: /Users/yazeed/Library/Caches/flutter_installer_nzs6j)
@YazeedAlKhalaf
Copy link
Author

I have just made sure that when using in the script double backslash it is not escaped it is doubled, what I want to achieve is having \" in the final output of the script

@alextekartik
Copy link
Contributor

alextekartik commented Aug 10, 2020 via email

@YazeedAlKhalaf
Copy link
Author

Ah okay I will try it

@YazeedAlKhalaf
Copy link
Author

I have solved it by creating a bash file and putting the line of code inside it.

@therealsujitk
Copy link

therealsujitk commented Dec 31, 2023

@alextekartik isn't this a bug that should be resolved? I think this should be reopened since Process.run() doesn't have this bug.

@alextekartik
Copy link
Contributor

@therealsujitk As far as i know Process.run does not support parsing a full command line and requires you to pass each argument separately. I would gladly take a look at any parsing bug with an example that works on the command line but not with Shell.run if any. The initial example has \\ (in dart it should in fact expect escaping \ so would mean having \\\\. But since I had no way to properly test and the bug was closed before my investigation I did not take a deeper look.

I agree, any parsing bug should be fixed on my side but I don't have any good example to test yet. If you have a similar bug and a command that fails, I would gladly take a look

@therealsujitk
Copy link

therealsujitk commented Jan 2, 2024

@alextekartik sure! here are some basic examples to reproduce this bug. I initially encountered this issue while attempting to write a much larger awk command, which I eventually gave up on and decided to use File instead.

Example 1

const command = r"echo '\ \\ \\\ \\\\ \\\\\ \\\\\\'";
print(command);
run(command);

Output

flutter: echo '\ \\ \\\ \\\\ \\\\\ \\\\\\'
$ echo "\\ \\\\ \\\\\\ \\\\\\\\ \\\\\\\\\\ \\\\\\\\\\\\"
\\ \\\\ \\\\\\ \\\\\\\\ \\\\\\\\\\ \\\\\\\\\\\\

Expected

$ echo '\ \\ \\\ \\\\ \\\\\ \\\\\\'
\ \ \\ \\ \\\ \\\

Example 2

const command = "echo '\\ \\\\ \\\\\\ \\\\\\\\ \\\\\\\\\\ \\\\\\\\\\\\'";
print(command);
run(command);

Output

flutter: echo '\ \\ \\\ \\\\ \\\\\ \\\\\\'
$ echo "\\ \\\\ \\\\\\ \\\\\\\\ \\\\\\\\\\ \\\\\\\\\\\\"
\\ \\\\ \\\\\\ \\\\\\\\ \\\\\\\\\\ \\\\\\\\\\\\

Expected

$ echo '\ \\ \\\ \\\\ \\\\\ \\\\\\'
\ \ \\ \\ \\\ \\\

As far as i know Process.run does not support parsing a full command line and requires you to pass each argument separately.

You're right! I'm not sure what I did before, but for some reason, it worked properly once. I can't remember what I did. That aside, the above examples are definitely a bug. Somewhere along the way, the script is being escaped unnecessarily.

@alextekartik
Copy link
Contributor

Thanks @therealsujitk
Indeed I have a .replaceAll(r'\', r'\\') that I added on purpose I guess to fix the following unit test: https://github.com/tekartik/process_run.dart/blob/master/packages/process_run/test/shell_test.dart#L141-L153

I need to take a look again at why I did this (since the beginning) since this looks wrong to me now and would definitely not work in many cases. I'll look into it.

@alextekartik alextekartik reopened this Jan 2, 2024
@alextekartik alextekartik changed the title [Bug] Needs Immediate Solution [Bug] Needs Immediate Solution - Weird backslash handling Jan 2, 2024
@alextekartik
Copy link
Contributor

Ok indeed that looked wrong and not posix compliant.
I published v0.14.0 that should fix the issue. Thanks @therealsujitk for pointing out the mistake and sorry @YazeedAlKhalaf that I put your issue on the side. Let me know if that works for you.

I have a mixed feeling between a version fix and a breaking change so that is why I bumped the version to 0.14.0 (yes I know at some point I should make it 1.0.0...).

@alextekartik
Copy link
Contributor

Unfortunately that seems to break path parameters on windows so I might have a different behavior on Windows...

$ echo "\\"

prints \\ on windows but \ on Mac/Linux so any path parameters is broken on windows now. I'll publish a fix soon...

@alextekartik
Copy link
Contributor

Posix backslash handling has now been removed on windows (I guess that explained why I did this global replace before). Published in v0.14.0+1

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

No branches or pull requests

3 participants