Skip to content

cmd/go: CGO_CFLAGS variable with spaces is parsed incorrectly on windows #45637

@justinfx

Description

@justinfx

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

$ go version
go version go1.16.2 windows/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using?

windows10, amd64, intel

go env Output
$ go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\justin\AppData\Local\go-build
set GOENV=C:\Users\justin\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\justin\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\justin\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.16.2
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
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 -fmessage-length=0 -fdebug-prefix-map=C:\Users\justin\AppData\Local\Temp\go-build995163971=/tmp/go-build -gno-record-gcc-switches

What did you do?

While attempting to build a cgo project on windows, I am finding that the value CGO_CFLAGS is parsed incorrectly if an include path contains spaces.

.
├── go.mod
├── main.c
└── main.go
// main.go
package main

import "C"

func main() {}
// main.c
// <empty>
> set CGO_CFLAGS='-IC:/Program Files/Foo'
> go env CGO_CFLAGS
'-IC:/Program Files/Foo'

> go build -x
WORK=C:\Users\justin\AppData\Local\Temp\go-build1942282733
mkdir -p $WORK\b003\
cd C:\Program Files\Go\src\runtime\cgo
TERM='dumb' CGO_LDFLAGS='"-g" "-O2"' "C:\\Program Files\\Go\\pkg\\tool\\windows_amd64\\cgo.exe" -objdir "$WORK\\b003\\" -importpath runtime/cgo -import_runtime_cgo=false -import_syscall=false -- -I "$WORK\\b003\\" '-IC:/Program Files/Foo' -Wall -Werror "C:\\Program Files\\Go\\src\\runtime\\cgo\\cgo.go"
# runtime/cgo
gcc: error: '-IC:/Program: Invalid argument
gcc: error: Files/Foo': No such file or directory

I've also tried various combinations of quoting and escaping with no change in result:

set CGO_CFLAGS=-I'C:/Program Files/Foo'
set CGO_CFLAGS='-IC:/Program\ Files/Foo'
set CGO_CFLAGS='-IC:/Program\\ Files/Foo'

What did you expect to see?

I would expect the full single string value of "C:/Program Files/Foo" to be passed to the "-I" include flag when compiling.

What did you see instead?

The path is split on spaces, acting like multiple arguments to gcc

# runtime/cgo
gcc: error: '-IC:/Program: Invalid argument
gcc: error: Files/Foo': No such file or directory

Metadata

Metadata

Assignees

No one assigned

    Labels

    GoCommandcmd/goNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.OS-Windowshelp wanted

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions