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/user: doesn't work when CGO_ENABLED=0 #11797

Closed
mgilbir opened this issue Jul 20, 2015 · 2 comments
Closed

os/user: doesn't work when CGO_ENABLED=0 #11797

mgilbir opened this issue Jul 20, 2015 · 2 comments

Comments

@mgilbir
Copy link

mgilbir commented Jul 20, 2015

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

go version go1.5beta2 darwin/amd64

  1. What operating system and processor architecture are you using?

OSX Yosemite 10.10.4

  1. What did you do?

The problem can be reproduced by trying to cross compile the following code from darwin/amd64 to linux/amd64

package main

import (
        "fmt"
        "os/user"
)

func main() {
        fmt.Println(user.Current())
}

On my OSX machine I ran:
env GOOS=linux GOARCH=amd64 go build

Then I copied the binary to an Ubuntu machine and executed it:

ubuntu@machine:~$ uname -a
Linux machine 3.13.0-53-generic #89-Ubuntu SMP Wed May 20 10:34:39 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
ubuntu@machine:~$ ./golang_err
  1. What did you expect to see?
    &{1000 1000 ubuntu Ubuntu /home/ubuntu} <nil>
  2. What did you see instead?
    <nil> user: Current not implemented on linux/amd64

If I compile the code directly on the linux machine with the version go version go1.5beta2 linux/amd64, then it works correctly. This leads me to think that it is related to the cross compilation.

If the functionality is not to be implemented in 1.5 and it can be detected at compilation time, I would suggest that a warning/error is shown to the user in order to prevent deploying binaries that will break in production due to this issue.

It is also possible that I may have missed any reference to it in the documentation, but if this is a know or "works as designed" behaviour, I'd recommend making it more visible.

@crawshaw
Copy link
Member

The os/user package requires cgo to implement Current. Cross compiled binaries (like yours) are by default built with CGO_ENABLED=0.

If you have a C cross compiler, you can configure your Go installation to use it. But it is not common on OS X, hence the default to disable it.

@ianlancetaylor
Copy link
Contributor

The os/user package relies on cgo. To cross-compile it, you need to say

CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build

You will need to have a Darwin -> GNU/Linux cross-compiler installed. You probably also need to set the CC environment variable to point to that cross-compiler.

This is working as expected, so I'm going to close this issue.

@mikioh mikioh changed the title os/user 1.5 beta 2 os/user: doesn't work when CGO_ENABLED=0 Jul 21, 2015
@mikioh mikioh added this to the Unplanned milestone Jul 21, 2015
@golang golang locked and limited conversation to collaborators Jul 20, 2016
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

5 participants