These are chat archives for rust-lang/rust

24th
Dec 2017
Michael Thomas
@Michaelt293
Dec 24 2017 01:18
Hello, I am using reqwest to download images from a site (with the permission of the site owner). If I don't view the image first in my browser, I get status: NotFound as a response. Also, afterwards, I will not be able to view the image in my browser. If I view the image first, I can download the image and there are no issues. What is the likely problem?
Julian Didier
@theredfish
Dec 24 2017 07:53
Hello everyone, i was playing with arguments when i seen a strange thing with the escaping character \ (Windows OS). If you set this argument '..\my\string\path\', it's parsed like that : ..\\my\\string\\path". Does someone has an explanation?
It seems that the last backslash escape the " character (because the argument is parsed like a string, see : println!("debug args : {:?}", env::args());), where the others backslashes are escaped.
Idk if it's a bug or a nominative behavior... but it's little bit weird
Jonas Platte
@jplatte
Dec 24 2017 07:57
@theredfish How are you executing your program? Sounds like whatever you are using to run the program is interpreting the \es
Julian Didier
@theredfish
Dec 24 2017 08:00
@jplatte i'm executing my program from a powershell console or cmd console (i tried both) and it's the same result. My steps : cargo build, then ./my_program.exe '..\my\string\path\'
(for example with Windows Powershell)
Jonas Platte
@jplatte
Dec 24 2017 08:00
Yeah, I'd assume the shell interprets the \es just like on Linux
Does echo \\ output one or two \es?
(pretty sure echo is a command in cmd.exe, not sure about PowerShell)
Julian Didier
@theredfish
Dec 24 2017 08:02
two \\ (cmd and powershell), and i tried under linux, i don't have this issue.
Since Linux use literal string with simple quotes
Jonas Platte
@jplatte
Dec 24 2017 08:02
On Linux is depends on the shell and how you quote the string
E.g. bash + single quotes => \ is just a literal; zsh + single quotes => \ is still an escape character
Julian Didier
@theredfish
Dec 24 2017 08:05
Ok :) . If I try to parse my arguments from a powershell script i get the expected result ..\my\string\path\ but when my arguments go through Rust program, I get an escaped quote at the end.
Jonas Platte
@jplatte
Dec 24 2017 08:08
That is really weird...
Julian Didier
@theredfish
Dec 24 2017 08:08
Oh! Sorry i simplified the example @jplatte but this behavior occurs only when i have a whitespace in my string! Like 'my\string with space\path\'
I just tried without spaces and i don't have any issue
Jonas Platte
@jplatte
Dec 24 2017 08:09
Uhm, did you have a typo in your message before actually? You seem to always quote your strings with ', but said there was a trailing " in your string.
Julian Didier
@theredfish
Dec 24 2017 08:10
Exactly, it's because std::env::args seems to parse the string with double quotes
Args { inner: ["C:\\my_program\\target\\debug\\my_program.exe", "my\\string with space\\path\""] } is the result returned by println!("{:?}", env::args())
What i understand is that env::args function will not escape the last backslash if it's followed by "
Jonas Platte
@jplatte
Dec 24 2017 08:15
I don't know what you mean. I'm pretty sure that env::args doesn't do any sort of escaping
Julian Didier
@theredfish
Dec 24 2017 08:20
So I don't know why i get this extra " in my argument and why my last \ isn't escaped :worried:
Jonas Platte
@jplatte
Dec 24 2017 08:21
It's not that your last \ isn't "escaped", there is not last \
In the {:?} debug format, a \ is represented as \\, and a " is represented as \"
Your \ somehow got replaced by "
If you want the first argument printed without escape sequences, you can do so like this:
println!("{}", std::env::args().nth(1).unwrap());
Julian Didier
@theredfish
Dec 24 2017 08:26
and I get my\string with space\path"
Jonas Platte
@jplatte
Dec 24 2017 08:27
Yeah, like I said. \ got replaced by ".
But I'm afraid I have no idea how that could happen.
Julian Didier
@theredfish
Dec 24 2017 08:28
Ok it's not only during the debug fn ... yes it's weird... i suspected a bug but i wasn't sure about that.
Jonas Platte
@jplatte
Dec 24 2017 08:28
It has to be a bug, the question is where is it a bug.
Since it happens with both PowerShell and cmd.exe, it seems unlikely to be the shell's fault..
But it also seems very unlikely that Rust's stdlib just replaces characters when there is a space in an argument on Windows.
I guess you could try running your rust program from somewhere else, e.g. Python, bash on Windows, or another Rust program
And check whether this happens with different versions of rustc.
Julian Didier
@theredfish
Dec 24 2017 08:36

yup ... in my case i don't use " in my string... it's added. I get the same result with "my\string with space\path\" and 'my\string with space\path\' ...

I didn't see any function from Rust's stdlib making characters replacement but maybe i missed it. I updated my rust version to the last stable and same result... but i didn't try with rustc itself. I will try your proposals and i'll let you know.

And yes like you said \ got replaced by " but it's weird with simple quoted string...
Jonas Platte
@jplatte
Dec 24 2017 08:40
I don't think you need to try rustc without cargo..
Julian Didier
@theredfish
Dec 24 2017 08:41
ok !
Jonas Platte
@jplatte
Dec 24 2017 08:41
By the way, is the println! statement the first thing that happens in your program?
Julian Didier
@theredfish
Dec 24 2017 08:41
yes
but same without println! as first statement
Jonas Platte
@jplatte
Dec 24 2017 08:45
Okay. I was just asking because theoretically some code outside of std::env could be fudging with the arguments before they are printed. But that's almost impossible when the println! happens right at the start of main.
Have you tried running the rust program from somewhere other than cmd.exe / PowerShell?
Julian Didier
@theredfish
Dec 24 2017 09:39
Sorry i was afk @jplatte . When i run my program on Linux i get : "\\my\\string path\\"
Denis Lisov
@tanriol
Dec 24 2017 09:43
@theredfish Does cmd.exe actually pass the single-quoted string with spaces as a single argument at all?
Julian Didier
@theredfish
Dec 24 2017 09:46
@tanriol no! I missed it with single quotes, you're right. With double quotes it's the same \my\super path" result but with single i got two arguments '\my\super and path\'
Denis Lisov
@tanriol
Dec 24 2017 09:50
With double quotes, the backslash escapes the closing double quote as a literal one... I'd expect cmd.exe to return an error about unclosed quoted string, but looks like it does not.
The single quotes have no special meaning for cmd.exe at all.
Don't know about the powershell yet.
Julian Didier
@theredfish
Dec 24 2017 09:50
Seems powershell will parse ' as "
and it explains why i got the same problem both with ' and "
It's an hypothesis... but seems to be that
... but echo "\"" the backslash doesn't escapes the closing double quote.
Julian Didier
@theredfish
Dec 24 2017 09:56
Weird...
Denis Lisov
@tanriol
Dec 24 2017 09:59
echo "\"" should echo ", IIUC - does it echo something else?
Julian Didier
@theredfish
Dec 24 2017 10:00
The double quote isn't escaped
Powershell expects that i close the string
but with cmd.exe it's ok
I got the echo...
Haha with cmd.exe I got "\""
Denis Lisov
@tanriol
Dec 24 2017 10:19
Looks like these are weird Windows shells, not anything wrong with Rust...
Peter Atashian
@retep998
Dec 24 2017 10:20
With powershell you use the backtick ` to escape
Julian Didier
@theredfish
Dec 24 2017 10:20
With powershell this issue appears only when i have a whitespace in my string
With cmd.exe both with and without whitespace but only with " (' is interpreted like a string but with a whitespace Rust will get two arguments)
With shell it's correctly parsed with a single quote and with double quotes the last one is escaped, so i need to add one more and i get \my\string repo"
Julian Didier
@theredfish
Dec 24 2017 10:27
@retep998 so it's not a normal behavior to have the double quote escaped...
(with powershell)
Peter Atashian
@retep998
Dec 24 2017 10:27
Backslashes should do absolutely nothing in powershell.
however they are interpreted by CommandLineToArgvW
Julian Didier
@theredfish
Dec 24 2017 10:28
arf ok
Peter Atashian
@retep998
Dec 24 2017 10:28
because the command line is a single massive string blob that rust passes to that function to split up
You can read about the rules for that here https://msdn.microsoft.com/en-us/library/windows/desktop/bb776391
  • 2n backslashes followed by a quotation mark produce n backslashes followed by begin/end quote. This does not become part of the parsed argument, but toggles the "in quotes" mode.
  • (2n) + 1 backslashes followed by a quotation mark again produce n backslashes followed by a quotation mark literal ("). This does not toggle the "in quotes" mode.
  • n backslashes not followed by a quotation mark simply produce n backslashes.
Julian Didier
@theredfish
Dec 24 2017 10:33
When i use Powershell Windows, this path is auto-completed : '..\..\..\fake folder\'
But std::env::args returns me ..\..\..\fake folder"
Peter Atashian
@retep998
Dec 24 2017 10:34
What happens if you specify '..\..\..\fake folder\\'?
Julian Didier
@theredfish
Dec 24 2017 10:35
I got the expected \ at the end. So ' is a quotation mark and the rule "2n backslashes followed by a quotation mark produce n backslashes" is valid
Peter Atashian
@retep998
Dec 24 2017 10:36
If you give a string quoted in single quotes, powershell will turn the single quotes into double quotes
Julian Didier
@theredfish
Dec 24 2017 10:39
That's what I understood... (very weird behavior... but ok)... but what about , 1 backslash followed by a quotation mark...
They say "This interpretation assumes that any preceding argument is a valid file system path" ... so the auto-completion is a valid file system path I guess...
Peter Atashian
@retep998
Dec 24 2017 10:44
Auto completion gives something that works in powershell, but it doesn't guarantee that it will be parsed correctly by native applications
because the command line is just a single string blob that applications are free to parse however they want
Julian Didier
@theredfish
Dec 24 2017 10:51
Ok, thank you for the explanation. I'm not very comfortable with Rust / OS commands, so for you it's an expected behavior? I seen that CommandLineToArgvWis used by Rust (https://github.com/rust-lang/rust/blob/9acc3331e187d329d29a70c788c44e25a79edf45/src/libstd/sys/windows/c.rs#L923) but from there i am a little lost :)
Julian Didier
@theredfish
Dec 24 2017 10:58
Anyway @retep998 @tanriol @jplatte thanks for the help. I learned interesting things too :)
Peter Atashian
@retep998
Dec 24 2017 11:01
It is indeed expected behavior and not a bug.
Julian Didier
@theredfish
Dec 24 2017 11:02
:+1: thanks!
Peter Atashian
@retep998
Dec 24 2017 11:02
Though powershell not automatically escaping backtraces before the ending quote of a string... that could be considered a bug in powershell
but there's nothing Rust can do about that
Julian Didier
@theredfish
Dec 24 2017 11:12
Ok, so for now I will remove the last backslash. Thanks, have a good day!