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

runtime: GOROOT() docs are confusing #22302

Closed
natefinch opened this issue Oct 17, 2017 · 5 comments

Comments

Projects
None yet
6 participants
@natefinch
Copy link
Contributor

commented Oct 17, 2017

The docs say:

GOROOT returns the root of the Go tree. It uses the GOROOT environment variable, if set, or else the root used during the Go build.

Thus I expect this would print "Hi!":

package main

import (
    "fmt"
    "log"
    "os"
    "runtime"
)

func main() {
    err := os.Setenv("GOROOT", "Hi!")
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(runtime.GOROOT())
}

However, this prints my real goroot, not "Hi!".

Either GOROOT() should actually check the environment variable when called, or the docs should be clarified to specify that it returns the GOROOT environment variable set at init() time.

@ianlancetaylor ianlancetaylor added this to the Go1.10 milestone Oct 17, 2017

@dmitshur

This comment has been minimized.

Copy link
Member

commented Oct 17, 2017

@gopherbot added the Documentation label automatically due to "docs" in issue title, but I think it needs to be decided/confirmed whether it's a bug in implementation or documentation.

If the current behavior is deemed correct, here's a take at fixing the documentation:

GOROOT returns the root of the Go tree. It uses the GOROOT environment variable, if set at process start, or else the root used during the Go build.

Also, https://godoc.org/runtime#NumCPU has some precedent:

The set of available CPUs is checked by querying the operating system at process startup. Changes to operating system CPU allocation after process startup are not reflected.

@mvdan

This comment has been minimized.

Copy link
Member

commented Oct 18, 2017

Oh well, I tried.

@dmitshur dmitshur removed the Documentation label Oct 18, 2017

@rsc

This comment has been minimized.

Copy link
Contributor

commented Oct 30, 2017

Decision: document current behavior. runtime.GOROOT is already too subtle without changing this detail.

@rsc rsc added the NeedsFix label Oct 30, 2017

@dmitshur

This comment has been minimized.

Copy link
Member

commented Oct 30, 2017

In that case, it'll be a good idea to add a test for this behavior (if one doesn't already exist).

@gopherbot

This comment has been minimized.

Copy link

commented Nov 3, 2017

Change https://golang.org/cl/75751 mentions this issue: runtime: clarify GOROOT return value in documentation

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.