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: -trimpath option fails if it contains whitespace #33223

Closed
djdv opened this issue Jul 22, 2019 · 2 comments
Closed

cmd/go: -trimpath option fails if it contains whitespace #33223

djdv opened this issue Jul 22, 2019 · 2 comments

Comments

@djdv
Copy link
Contributor

djdv commented Jul 22, 2019

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

$ go version
go version go1.12.7 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=C:\Users\Dominic Della Valle\AppData\Local\go-build
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\Users\Dominic Della Valle\Projects\Go
set GOPROXY=
set GORACE=
set GOROOT=c:\go
set GOTMPDIR=
set GOTOOLDIR=c:\go\pkg\tool\windows_amd64
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 -fmessage-length=0 -fdebug-prefix-map=C:\Users\DOMINI~1\AppData\Local\Temp\go-build550192770=/tmp/go-build -gno-record-gcc-switches

What did you do?

Tried stripping path prefix during build with a path containing whitespace on Windows.
C:\Users\Dominic Della Valle\Projects\Go\src\local\hello>go build -asmflags=all=-trimpath="C:\Users\Dominic Della Valle\Projects\Go\src\local\hello" -gcflags=all=-trimpath="C:\Users\Dominic Della Valle\Projects\Go\src\local\hello"

What did you expect to see?

Successful build.

What did you see instead?

C:\Users\Dominic Della Valle\Projects\Go\src\local\hello>go build -asmflags=all=-trimpath="C:\Users\Dominic Della Valle\Projects\Go\src\local\hello" -gcflags=all=-trimpath="C:\Users\Dominic Della Valle\Projects\Go\src\local\hello"
# unicode/utf8
Della:0: open Della: The system cannot find the file specified.
# runtime/internal/sys
Della:0: open Della: The system cannot find the file specified.
# internal/race
Della:0: open Della: The system cannot find the file specified.
# errors
Della:0: open Della: The system cannot find the file specified.
# unicode/utf16
Della:0: open Della: The system cannot find the file specified.
# unicode
Della:0: open Della: The system cannot find the file specified.
# internal/syscall/windows/sysdll
Della:0: open Della: The system cannot find the file specified.
# math/bits
Della:0: open Della: The system cannot find the file specified.
# sync/atomic
usage: asm [options] file.s ...
Flags:
  -D value
        predefined symbol with optional simple value -D=identifier=value; can be set multiple times
  -I value
        include directory; can be set multiple times
  -S    print assembly and machine code
  -V    print version and exit
  -debug
        dump instructions as they are parsed
  -dynlink
        support references to Go symbols defined in other shared libraries
  -e    no limit on number of errors reported
  -gensymabis
        write symbol ABI information to output file, don't assemble
  -o string
        output file; default foo.o for /a/b/c/foo.s as first argument
  -shared
        generate code that can be linked into a shared library
  -trimpath string
        remove prefix from recorded source file paths
# internal/cpu
usage: asm [options] file.s ...
Flags:
  -D value
        predefined symbol with optional simple value -D=identifier=value; can be set multiple times
  -I value
        include directory; can be set multiple times
  -S    print assembly and machine code
  -V    print version and exit
  -debug
        dump instructions as they are parsed
  -dynlink
        support references to Go symbols defined in other shared libraries
  -e    no limit on number of errors reported
  -gensymabis
        write symbol ABI information to output file, don't assemble
  -o string
        output file; default foo.o for /a/b/c/foo.s as first argument
  -shared
        generate code that can be linked into a shared library
  -trimpath string
        remove prefix from recorded source file paths
# runtime/internal/atomic
usage: asm [options] file.s ...
Flags:
  -D value
        predefined symbol with optional simple value -D=identifier=value; can be set multiple times
  -I value
        include directory; can be set multiple times
  -S    print assembly and machine code
  -V    print version and exit
  -debug
        dump instructions as they are parsed
  -dynlink
        support references to Go symbols defined in other shared libraries
  -e    no limit on number of errors reported
  -gensymabis
        write symbol ABI information to output file, don't assemble
  -o string
        output file; default foo.o for /a/b/c/foo.s as first argument
  -shared
        generate code that can be linked into a shared library
  -trimpath string
        remove prefix from recorded source file paths

Moving the package to a path that doesn't contain whitespace, and changing the trimpath argument to match, results in a successful build with stripped paths.

It seems like it's trying to pass the words after the space, as arguments to the assembler/compiler.

contents of hello/main.go is just hello world:

package main

import "fmt"

func main() {
	fmt.Println("Hello")
}
@ianlancetaylor ianlancetaylor changed the title cmd/build: trimpath containing whitespace fails on Windows cmd/go: trimpath containing whitespace fails on Windows Jul 22, 2019
@ianlancetaylor ianlancetaylor changed the title cmd/go: trimpath containing whitespace fails on Windows cmd/go: -trimpath option fails if it contains whitespace Jul 22, 2019
@ianlancetaylor
Copy link
Member

The way to write this on a Unix system is

go build "-gcflags=all='-trimpath=a b'"

Here the Unix shell will remove the outer " characters, and pass

-gcflags=all='-trimpath=a b'

to cmd/go. That quoting style will work for cmd/go to do the right thing when invoking the other tools. Basically you have to quote the entire argument to -gcflags=all=, not just the -trimpath argument.

@djdv
Copy link
Contributor Author

djdv commented Jul 22, 2019

Thanks for clarifying @ianlancetaylor

I can properly pass the arguments to the assembler/compiler in the cmd interpreter if I quote paths as such (there may be a more concise manner):
go build -asmflags=all='-trimpath="C:\Users\Dominic Della Valle\Projects\Go\src\local\hello"' -gcflags=all='-trimpath="C:\Users\Dominic Della Valle\Projects\Go\src\local\hello"'

More generally
in cmd
-asmflags=all='-trimpath="%PREFIX%"' -gcflags=all='-trimpath="%PREFIX%"'
in make
GOFLAGS += "-asmflags=all='-trimpath=$(PREFIX)'" "-gcflags=all='-trimpath=$(PREFIX)'"

Problem was on my end, not Golang.

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

3 participants