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

cmd/go: CC variable misparsed when set to a path containing spaces #41400

Closed
XamarinPOC opened this issue Sep 15, 2020 · 42 comments
Closed

cmd/go: CC variable misparsed when set to a path containing spaces #41400

XamarinPOC opened this issue Sep 15, 2020 · 42 comments
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@XamarinPOC
Copy link

go build -buildmode=c-shared -o C:\Users\XAMARI~1\AppData\Local\Temp\gomobile-work-285666087\lib\armeabi-v7a\libbasic.so golang.org/x/mobile/example/basic failed: exit status 2

runtime/cgo

exec: "C:\Program": file does not exist

E:\GoWorkDir>go env

set GO111MODULE=auto
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\Xamarin Developer\AppData\Local\go-build
set GOENV=C:\Users\Xamarin Developer\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=E:\GoWorkDir\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=E:\GoWorkDir
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=c:\go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=c:\go\pkg\tool\windows_amd64
set GCCGO=gccgo
set AR=ar
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=C:\Users\XAMARI~1\AppData\Local\Temp\go-build823121506=/tmp/go-build -gno-record-gcc-switches

@davecheney
Copy link
Contributor

Your go installation is corrupt and/or the time stamps have been disrupted, possibly by unpacking one version of go over another

Please delete c:\go and reinstall from a fresh copy

@bcmills bcmills added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Sep 15, 2020
@AlexRouSg
Copy link
Contributor

AlexRouSg commented Sep 15, 2020

I am actually able to repro this.

exact command: CC="C:\\path with spaces\\bin\\gcc" go build or CC="C:/path with spaces/bin/gcc" go build
works with any source file that uses cgo

It's specific to the CC/CXX class of variables so looks like something is wrong with how go parses them.

cc @ianlancetaylor

@chaitanyaevoke

This comment has been minimized.

@bcmills bcmills removed the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Sep 15, 2020
@bcmills bcmills changed the title # runtime/cgo exec: "C:\\Program": file does not exist cmd/go: exec error with cgo when CC variable contains spaces on Windows Sep 15, 2020
@bcmills bcmills added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Sep 15, 2020
@bcmills bcmills added this to the Go1.16 milestone Sep 15, 2020
@bcmills
Copy link
Contributor

bcmills commented Sep 15, 2020

@AlexRouSg, thanks for the detail. I can even reproduce it on Linux:

example.com/bin with spaces$ echo "$CC"
/tmp/tmp.jTseVJjT2L/example.com/bin with spaces/gcc

example.com/bin with spaces$ "$CC" --version
gcc (Debian 9.3.0-13) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


example.com/bin with spaces$ go env CC
/tmp/tmp.jTseVJjT2L/example.com/bin

@bcmills bcmills changed the title cmd/go: exec error with cgo when CC variable contains spaces on Windows cmd/go: CC variable misparsed when set to a path containing spaces Sep 15, 2020
@AlexRouSg
Copy link
Contributor

So looks like there are multiple places that needs fixing ...

I've found

func (p *Package) gccBaseCmd() []string {

which only affects running the cgo binary direcly

go build and go tool cgo seems to have a different issue and I can't seem to figure out where that is. But likely it's related to calling strings.Fields or something along those lines.

@bcmills
Copy link
Contributor

bcmills commented Sep 15, 2020

Yes, there are several places where we're calling strings.Fields to split this sort of command, which seems clearly incorrect to me.

But I think we also try to follow the usual C toolchain behavior for the CC environment variable, so I'm trying to figure out if there is a standard format for escaping paths containing spaces. (Perhaps @ianlancetaylor or @jayconrod knows?)

@AlexRouSg
Copy link
Contributor

AlexRouSg commented Sep 15, 2020

@XamarinPOC @chaitanyaevoke In the meantime for gomobile, you'll have to install the SDK to a path without spaces

@XamarinPOC
Copy link
Author

I just uninstalled and installed fresh Go version 1.15.2 but no luck.

I'm trying to run build command => gomobile build -target=android golang.org/x/.............. to generate apk file

@XamarinPOC
Copy link
Author

@AlexRouSg Which Sdk?? Can you . please explain it .

@AlexRouSg
Copy link
Contributor

AlexRouSg commented Sep 15, 2020

@XamarinPOC
You need to install the android sdk/ndk to a path without spaces like C:\androidsdk
Make sure you changed the variable ANDROID_HOME too

@gopherbot
Copy link

Change https://golang.org/cl/255018 mentions this issue: cmd/go: allow the CC path to contain spaces

@XamarinPOC
Copy link
Author

Yup ,after removing spaces its working now. Can we get this fix in next release.

@ianlancetaylor
Copy link
Contributor

Wait. Right now I can run

CC="gcc -O3" go build

and all my cgo files will be built with -O3.

Now, maybe that is a mistake and maybe we should change it. But we definitely shouldn't change that behavior casually. We should discuss it on the mailing list, we should find out whether it will break anybody.

In particular there is no documented way to pass C compiler flags when running make.bash other than setting CC to include the compiler and the flags. And the value of the CC environment variable used when running make.bash because the default value of the C compiler used when building programs.

In other words, we can't just treat this a minor bug that we should just fix. We have to think about how this has worked in the past and how it should work going forward.

@bcmills
Copy link
Contributor

bcmills commented Sep 15, 2020

The CL that I mailed preserves the property that space-separated arguments following the executable name are treated as arguments: it uses exec.LookPath to identify the first space that could plausibly indicate a break between the command and its arguments, and falls back to the old strings.Fields behavior if none of the possible prefixes resolves to an executable.

I believe that preserves the existing behavior for all commands that would successfully invoke a compiler, and also fixes some number of cases that previously would have failed.

@bcmills bcmills self-assigned this Sep 15, 2020
@ianlancetaylor
Copy link
Contributor

OK, thanks. I'm a bit worried about the possibility of confusion. I guess it should work but it seems pretty complicated.

@bcmills
Copy link
Contributor

bcmills commented Sep 16, 2020

Agreed. I was kind of hoping that there would be some existing standard or extension for quoting within CC, but I couldn't find one.

As an alternative, I suppose that we could allow shell-style quoting within the CC variable. (Do you think that would be better or worse?)

@AlexRouSg
Copy link
Contributor

AlexRouSg commented Sep 16, 2020

Shell style quoting sounds better to me. I can imagine people having args like -I/path with spaces/ which wouldn't be covered by the current CL. Tho that makes me wonder if cgo handles args with spaces correctly for #cgo CFLAGS family of comments or pkgconfig provided flags ...

EDIT:
Looks like #cgo comments understands shell quoting while pkgconfig is kinda weird.
pkgconfig escapes spaces with \ so it looks like -L/path\ with\ spaces which then causes cgo to error

@ianlancetaylor
Copy link
Contributor

Shell quoting sounds a little better to me, I guess.

@jayconrod
Copy link
Contributor

I'm looking for a narrow fix of #43808 that can be backported, and it will probably affect the eventual solution to this issue.

What I'm thinking right now is that we should support shell quoting using the code in go/build.splitQuoted. That's what we use for #cgo directives today, so it seems reasonable to reuse it here (in the absence of any broader standard).

For #43808, we could quote the CC and GCCGO variables passed from cmd/go to cmd/cgo if needed, then unquote them in cmd/cgo. For 1.17, we could fix this issue by expanding that to cmd/go and anywhere else we deal with CC. We should fix #43078 at the same time.

@gopherbot
Copy link

Change https://golang.org/cl/334731 mentions this issue: [dev.cmdgo] cmd/internal/str: add functions for quoting and splitting args

@bcmills bcmills assigned jayconrod and unassigned bcmills Jul 15, 2021
@bcmills bcmills modified the milestones: Go1.17, Go1.18 Jul 15, 2021
gopherbot pushed a commit that referenced this issue Jul 30, 2021
This will let cmd/cgo and cmd/link use this package for argument parsing.

For #41400

Change-Id: I12ee21151bf3f00f3e8d427faaaab2453c823117
Reviewed-on: https://go-review.googlesource.com/c/go/+/334730
Trust: Jay Conrod <jayconrod@google.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
gopherbot pushed a commit that referenced this issue Jul 30, 2021
… args

JoinAndQuoteFields does the inverse of SplitQuotedFields: it joins a
list of arguments with spaces into one string, quoting arguments that
contain spaces or quotes.

QuotedStringListFlag uses SplitQuotedFields and JoinAndQuoteFields
together to define new flags that accept lists of arguments.

For #41400

Change-Id: I4986b753cb5e6fabb5b489bf26aedab889f853f5
Reviewed-on: https://go-review.googlesource.com/c/go/+/334731
Trust: Jay Conrod <jayconrod@google.com>
Trust: Michael Matloob <matloob@golang.org>
Run-TryBot: Jay Conrod <jayconrod@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
gopherbot pushed a commit that referenced this issue Jul 30, 2021
The CC and CXX environment variables now support spaces and quotes
(both double and single). This fixes two issues: first, if CC is a
single path that contains spaces (like 'c:\Program
Files\gcc\bin\gcc.exe'), that should now work if the space is quoted
or escaped (#41400). Second, if CC or CXX has multiple arguments (like
'gcc -O2'), they are now split correctly, and the arguments are passed
before other arguments when invoking the C compiler. Previously,
strings.Fields was used to split arguments, and the arguments were
placed later in the command line. (#43078).

Fixes #41400
Fixes #43078

Change-Id: I2d5d89ddb19c94adef65982a8137b01f037d5c11
Reviewed-on: https://go-review.googlesource.com/c/go/+/334732
Trust: Jay Conrod <jayconrod@google.com>
Trust: Michael Matloob <matloob@golang.org>
Run-TryBot: Jay Conrod <jayconrod@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
@gopherbot
Copy link

Change https://golang.org/cl/341934 mentions this issue: cmd/internal/str: move package from cmd/go/internal/str

@gopherbot
Copy link

Change https://golang.org/cl/341935 mentions this issue: cmd/internal/str: add utilities for quoting and splitting args

@gopherbot
Copy link

Change https://golang.org/cl/341936 mentions this issue: cmd: support space and quotes in CC and CXX

gopherbot pushed a commit that referenced this issue Aug 13, 2021
This will let cmd/cgo and cmd/link use this package for argument parsing.

For #41400

Change-Id: I12ee21151bf3f00f3e8d427faaaab2453c823117
Reviewed-on: https://go-review.googlesource.com/c/go/+/334730
Trust: Jay Conrod <jayconrod@google.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
Reviewed-on: https://go-review.googlesource.com/c/go/+/341934
Run-TryBot: Jay Conrod <jayconrod@google.com>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
gopherbot pushed a commit that referenced this issue Aug 16, 2021
JoinAndQuoteFields does the inverse of SplitQuotedFields: it joins a
list of arguments with spaces into one string, quoting arguments that
contain spaces or quotes.

QuotedStringListFlag uses SplitQuotedFields and JoinAndQuoteFields
together to define new flags that accept lists of arguments.

For #41400

Change-Id: I4986b753cb5e6fabb5b489bf26aedab889f853f5
Reviewed-on: https://go-review.googlesource.com/c/go/+/334731
Trust: Jay Conrod <jayconrod@google.com>
Trust: Michael Matloob <matloob@golang.org>
Run-TryBot: Jay Conrod <jayconrod@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
Reviewed-on: https://go-review.googlesource.com/c/go/+/341935
@gopherbot
Copy link

Change https://go.dev/cl/398058 mentions this issue: cmd/go: quote fragments in CGO_ env variables reported by 'go env'

gopherbot pushed a commit that referenced this issue Apr 5, 2022
These fields have been parsed as quoted fields since CL 334732,
but we missed the unparsing side in 'go env'.

Certain scripts (notably make.ba{sh,t}) expect to be able to set the
environment to exactly what 'go env' reports, so for round-trip
purposes it is important to match the marshaling and unmarshaling
functions.

(Noticed while debugging #52009.)
Updates #41400

Change-Id: I0ff39b7a6e1328111c285c97cd23f79b723f3c73
Reviewed-on: https://go-review.googlesource.com/c/go/+/398058
Trust: Bryan Mills <bcmills@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
@dmitshur dmitshur added NeedsFix The path to resolution is known, but the work has not been done. and removed NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels May 26, 2022
@gopherbot
Copy link

Change https://go.dev/cl/466875 mentions this issue: cmd/go: handle escapes in pkg-config ldflags output

gopherbot pushed a commit that referenced this issue Feb 10, 2023
#16455 handled escapes in pkg-config output but only for cflags. The fix
for #41400 left a note that we don't need to parse quotes and unescapes,
but it is still necessary to handle spaces in pkg-config output. As cflags
has already been processed correctly, we apply the same logic to ldflags
here.

Fixes #35262

Change-Id: Id01d422b103780f67f89e99ff1df0d8f51a7a137
GitHub-Last-Rev: c67e511
GitHub-Pull-Request: #58429
Reviewed-on: https://go-review.googlesource.com/c/go/+/466875
Run-TryBot: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Bryan Mills <bcmills@google.com>
Auto-Submit: Bryan Mills <bcmills@google.com>
Reviewed-by: David Chase <drchase@google.com>
johanbrandhorst pushed a commit to Pryz/go that referenced this issue Feb 12, 2023
golang#16455 handled escapes in pkg-config output but only for cflags. The fix
for golang#41400 left a note that we don't need to parse quotes and unescapes,
but it is still necessary to handle spaces in pkg-config output. As cflags
has already been processed correctly, we apply the same logic to ldflags
here.

Fixes golang#35262

Change-Id: Id01d422b103780f67f89e99ff1df0d8f51a7a137
GitHub-Last-Rev: c67e511
GitHub-Pull-Request: golang#58429
Reviewed-on: https://go-review.googlesource.com/c/go/+/466875
Run-TryBot: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Bryan Mills <bcmills@google.com>
Auto-Submit: Bryan Mills <bcmills@google.com>
Reviewed-by: David Chase <drchase@google.com>
@vault-thirteen
Copy link

Why was this issue closed ?
Was the problem fixed ?

@ericlagergren
Copy link
Contributor

Yes, see

@gopherbot gopherbot closed this as completed in 742dcba on Aug 16, 2021.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests