version: convert to generated copy of golang.org/dl/internal/version

Package version has been copied from golang.org/x/build/version to
golang.org/dl/internal/version as part of golang/go#23223. The old
package was marked deprecated but kept for backwards compatibility
reasons. Fixes and changes have not been backported from the new
dl/internal/version package (to avoid spending time unproductively).

bundle has recently been updated to accept an empty prefix in CL 105515.

This allows one to use bundle to make a generated copy of a package.
A generated copied package is better than a manually-maintained copied
package for the following reasons:

1. Keeping the copied package up to date with upstream is low effort,
   just need to run go generate.

2. It's clear to contributors that the copied package isn't the
   canonical version, since there is a generated comment like
   "// Code generated by golang.org/x/tools/cmd/bundle. DO NOT EDIT."
   at the top of the file. It's easy to reject changes to the copied
   package, or redirect them to the upstream.

Overall, making the copied package generated rather than hand-written
reduces the maintenace cost, and makes it more viable to keep it more
up to date with upstream.

The diff to version.go in this CL is due to some dl/internal/version
CLs being effectively backported by bundle, including CL 134435,
CL 143545, and CL 144698.

Updates golang/go#23223
Fixes golang/go#23635

Change-Id: Id8cd63c52b660b6817e6fdba080373966789e1e8
Reviewed-on: https://go-review.googlesource.com/c/148881
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
diff --git a/version/doc.go b/version/doc.go
new file mode 100644
index 0000000..5cc72ad
--- /dev/null
+++ b/version/doc.go
@@ -0,0 +1,8 @@
+// Copyright 2016 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// The version package permits running a specific version of Go.
+//
+// Deprecated: Use https://godoc.org/golang.org/dl instead.
+package version
diff --git a/version/version.go b/version/version.go
index 01861ab..7f6dc5d 100644
--- a/version/version.go
+++ b/version/version.go
@@ -1,10 +1,9 @@
-// Copyright 2016 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
+// Code generated by golang.org/x/tools/cmd/bundle. DO NOT EDIT.
+//go:generate bundle -o version.go -prefix= golang.org/dl/internal/version
 
 // The version package permits running a specific version of Go.
 //
-// Deprecated: Use https://godoc.org/golang.org/dl instead.
+
 package version
 
 import (
@@ -59,7 +58,15 @@
 	cmd.Stdin = os.Stdin
 	cmd.Stdout = os.Stdout
 	cmd.Stderr = os.Stderr
-	cmd.Env = envutil.Dedup(caseInsensitiveEnv, append(os.Environ(), "GOROOT="+root))
+
+	newPath := filepath.Join(root, "bin")
+	if p := os.Getenv("PATH"); p != "" {
+		newPath += string(filepath.ListSeparator) + p
+	}
+	// This envutil.Dedup call is unnecessary when the binary is
+	// built with Go 1.9+, but keep it around for now until Go 1.8
+	// is no longer seen in the wild in common distros.
+	cmd.Env = envutil.Dedup(caseInsensitiveEnv, append(os.Environ(), "GOROOT="+root, "PATH="+newPath))
 	if err := cmd.Run(); err != nil {
 		// TODO: return the same exit status maybe.
 		os.Exit(1)
@@ -75,13 +82,10 @@
 		return nil
 	}
 
-	goURL, err := versionArchiveURL(version)
-	if err != nil {
-		return err
-	}
 	if err := os.MkdirAll(targetDir, 0755); err != nil {
 		return err
 	}
+	goURL := versionArchiveURL(version)
 	res, err := http.Head(goURL)
 	if err != nil {
 		return err
@@ -374,7 +378,7 @@
 }
 
 // versionArchiveURL returns the zip or tar.gz URL of the given Go version.
-func versionArchiveURL(version string) (string, error) {
+func versionArchiveURL(version string) string {
 	goos := getOS()
 
 	// TODO: Maybe we should parse
@@ -389,7 +393,7 @@
 	if goos == "linux" && runtime.GOARCH == "arm" {
 		arch = "armv6l"
 	}
-	return "https://storage.googleapis.com/golang/" + version + "." + goos + "-" + arch + ext, nil
+	return "https://storage.googleapis.com/golang/" + version + "." + goos + "-" + arch + ext
 }
 
 const caseInsensitiveEnv = runtime.GOOS == "windows"