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

os.Args is missing/eating/interpreting double quote on Windows #30952

Closed
u1735067 opened this issue Mar 20, 2019 · 3 comments
Closed

os.Args is missing/eating/interpreting double quote on Windows #30952

u1735067 opened this issue Mar 20, 2019 · 3 comments

Comments

@u1735067
Copy link

What version of Go are you using (go version)?

$ go version
go version go1.12.1 windows/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
set GOARCH=amd64
set GOBIN=
set GOCACHE=(private)
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=(private)
set GOPROXY=
set GORACE=
set GOROOT=(private)
set GOTMPDIR=
set GOTOOLDIR=(private)
set GCCGO=gccgo
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=(private)=/tmp/go-build -gno-record-gcc-switches

What did you do?

When passing quotes (properly escaped) as arguments, Go doesn't restitue them in os.Args.
Go seems to be interpreting the quotes in lieu of the shell (PowerShell here).

Example:

package main

import (
	"fmt"
	"os"
)

func main() {
	fmt.Println(os.Args[1])
}
PS A:\Dev\go> echo "`"`"`""
"""
PS A:\Dev\go> .\test.exe "`"`"`""
"

(Or with a single quote:

PS A:\Dev\go> echo "`""
"
PS A:\Dev\go> .\test.exe "`""

)

What did you expect to see?

"""

What did you see instead?

"
@kardianos
Copy link
Contributor

I'm fairly sure Go on windows goes to extensive lengths to mimic how windows internally parses arguments. Doing a test right now even windows isn't consistent. On powershell I got similar results as above, but on windows cmd I get different results.

I believe most of windows uses the same argument parser the cmd terminal demonstrates.

What you are finding is a limitation of the design in windows itself. Yes, Go is interpreting the arguments. That's how windows works.

@u1735067
Copy link
Author

u1735067 commented Mar 20, 2019

Hum, ok, I just tested again using Python:

#!/usr/bin/env python

import sys, subprocess

subprocess.run(['test.exe', '"'], stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr)

Also I found https://stackoverflow.com/questions/6714165/powershell-stripping-double-quotes-from-command-line-arguments

PS A:\Dev\go> py .\call.py
"
PS A:\Dev\go> .\test.exe \`"
"

-_- ... Sorry for the noise.

Edit: more references
https://github.com/PowerShell/PowerShell/issues/3049
https://github.com/PowerShell/PowerShell/issues/1995

@alexbrainman
Copy link
Member

http://daviddeley.com/autohotkey/parameters/parameters.htm#WIN

for people who get exited about things like that.

Alex

@golang golang locked and limited conversation to collaborators Mar 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants