Skip to content

Commit 50df4b3

Browse files
committed
cmd/go: document $GOPROXY, other module adjustments
Also document module use of GOPATH including GOPATH/src/mod and GOPATH/bin (unless GOBIN is set). Fixes #26399. Fixes #26406. Change-Id: I7be8eaf110f4fa6fc76ea4cd39aea3dd8addf0b0 Reviewed-on: https://go-review.googlesource.com/124707 Reviewed-by: Bryan C. Mills <[email protected]>
1 parent 9039c2c commit 50df4b3

File tree

3 files changed

+116
-9
lines changed

3 files changed

+116
-9
lines changed

src/cmd/go/internal/help/helpdoc.go

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ using the named version control system, and then the path inside
195195
that repository. The supported version control systems are:
196196
197197
Bazaar .bzr
198+
Fossil .fossil
198199
Git .git
199200
Mercurial .hg
200201
Subversion .svn
@@ -238,7 +239,7 @@ The meta tag should appear as early in the file as possible.
238239
In particular, it should appear before any raw JavaScript or CSS,
239240
to avoid confusing the go command's restricted parser.
240241
241-
The vcs is one of "git", "hg", "svn", etc,
242+
The vcs is one of "bzr", "fossil", "git", "hg", "svn".
242243
243244
The repo-root is the root of the version control system
244245
containing a scheme and not containing a .vcs qualifier.
@@ -260,12 +261,22 @@ the go tool will verify that https://example.org/?go-get=1 contains the
260261
same meta tag and then git clone https://code.org/r/p/exproj into
261262
GOPATH/src/example.org.
262263
263-
New downloaded packages are written to the first directory listed in the GOPATH
264-
environment variable (For more details see: 'go help gopath').
264+
When using GOPATH, downloaded packages are written to the first directory
265+
listed in the GOPATH environment variable.
266+
(See 'go help gopath-get' and 'go help gopath'.)
267+
268+
When using modules, downloaded packages are stored in the module cache.
269+
(See 'go help modules-get' and 'go help goproxy'.)
270+
271+
When using modules, an additional variant of the go-import meta tag is
272+
recognized and is preferred over those listing version control systems.
273+
That variant uses "mod" as the vcs in the content value, as in:
274+
275+
<meta name="go-import" content="example.org mod https://code.org/moduleproxy">
265276
266-
The go command attempts to download the version of the
267-
package appropriate for the Go release being used.
268-
Run 'go help get' for more.
277+
This tag means to fetch modules with paths beginning with example.org
278+
from the module proxy available at the URL https://code.org/moduleproxy.
279+
See 'go help goproxy' for details about the proxy protocol.
269280
270281
Import path checking
271282
@@ -288,6 +299,9 @@ Import path checking is disabled for code found within vendor trees.
288299
This makes it possible to copy code into alternate locations in vendor trees
289300
without needing to update import comments.
290301
302+
Import path checking is also disabled when using modules.
303+
Import path comments are obsoleted by the go.mod file's module statement.
304+
291305
See https://golang.org/s/go14customimport for details.
292306
`,
293307
}
@@ -360,6 +374,12 @@ in the list.
360374
361375
See https://golang.org/doc/code.html for an example.
362376
377+
GOPATH and Modules
378+
379+
When using modules, GOPATH is no longer used for resolving imports.
380+
However, it is still used to store downloaded source code (in GOPATH/src/mod)
381+
and compiled commands (in GOPATH/bin).
382+
363383
Internal Directories
364384
365385
Code in or below a directory named "internal" is importable only
@@ -471,6 +491,8 @@ General-purpose environment variables:
471491
Examples are linux, darwin, windows, netbsd.
472492
GOPATH
473493
For more details see: 'go help gopath'.
494+
GOPROXY
495+
URL of Go module proxy. See 'go help goproxy'.
474496
GORACE
475497
Options for the race detector.
476498
See https://golang.org/doc/articles/race_detector.html.

src/cmd/go/internal/modfetch/proxy.go

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,85 @@ import (
1414
"strings"
1515
"time"
1616

17+
"cmd/go/internal/base"
1718
"cmd/go/internal/modfetch/codehost"
1819
"cmd/go/internal/module"
1920
"cmd/go/internal/semver"
2021
)
2122

23+
var HelpGoproxy = &base.Command{
24+
UsageLine: "goproxy",
25+
Short: "module proxy protocol",
26+
Long: `
27+
The go command by default downloads modules from version control systems
28+
directly, just as 'go get' always has. If the GOPROXY environment variable
29+
is set to the URL of a module proxy, the go command will instead fetch
30+
all modules from that proxy. No matter the source of the modules, downloaded
31+
modules must match existing entries in go.sum (see 'go help modules' for
32+
discussion of verification).
33+
34+
A Go module proxy is any web server that can respond to GET requests for
35+
URLs of a specified form. The requests have no query parameters, so even
36+
a site serving from a fixed file system (including a file:/// URL)
37+
can be a module proxy.
38+
39+
The GET requests sent to a Go module proxy are:
40+
41+
GET $GOPROXY/<module>/@v/list returns a list of all known versions of the
42+
given module, one per line.
43+
44+
GET $GOPROXY/<module>/@v/<version>.info returns JSON-formatted metadata
45+
about that version of the given module.
46+
47+
GET $GOPROXY/<module>/@v/<version>.mod returns the go.mod file
48+
for that version of the given module.
49+
50+
GET $GOPROXY/<module>/@v/<version>.zip returns the zip archive
51+
for that version of the given module.
52+
53+
To avoid problems when serving from case-sensitive file systems,
54+
the <module> and <version> elements are case-encoded, replacing every
55+
uppercase letter with an exclamation mark followed by the correponding
56+
lower-case letter: github.com/Azure encodes as github.com/!azure.
57+
58+
The JSON-formatted metadata about a given module corresponds to
59+
this Go data structure, which may be expanded in the future:
60+
61+
type Info struct {
62+
Version string // version string
63+
Time time.Time // commit time
64+
}
65+
66+
The zip archive for a specific version of a given module is a
67+
standard zip file that contains the file tree corresponding
68+
to the module's source code and related files. The archive uses
69+
slash-separated paths, and every file path in the archive must
70+
begin with <module>@<version>/, where the module and version are
71+
substituted directly, not case-encoded. The root of the module
72+
file tree corresponds to the <module>@<version>/ prefix in the
73+
archive.
74+
75+
Even when downloading directly from version control systems,
76+
the go command synthesizes explicit info, mod, and zip files
77+
and stores them in its local cache, $GOPATH/src/mod/cache/download,
78+
the same as if it had downloaded them directly from a proxy.
79+
The cache layout is the same as the proxy URL space, so
80+
serving $GOPATH/src/mod/cache/download at (or copying it to)
81+
https://example.com/proxy would let other users access those
82+
cached module versions with GOPROXY=https://example.com/proxy.
83+
`,
84+
}
85+
2286
var proxyURL = os.Getenv("GOPROXY")
2387

2488
func lookupProxy(path string) (Repo, error) {
89+
if strings.Contains(proxyURL, ",") {
90+
return nil, fmt.Errorf("invalid $GOPROXY setting: cannot have comma")
91+
}
2592
u, err := url.Parse(proxyURL)
2693
if err != nil || u.Scheme != "http" && u.Scheme != "https" && u.Scheme != "file" {
2794
// Don't echo $GOPROXY back in case it has user:password in it (sigh).
28-
return nil, fmt.Errorf("invalid $GOPROXY setting")
95+
return nil, fmt.Errorf("invalid $GOPROXY setting: malformed URL or invalid scheme (must be http, https, file)")
2996
}
3097
return newProxyRepo(u.String(), path)
3198
}

src/cmd/go/internal/modload/help.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ Module support is enabled only when the current directory is outside
4949
GOPATH/src and itself contains a go.mod file or is below a directory
5050
containing a go.mod file.
5151
52+
In module-aware mode, GOPATH no longer defines the meaning of imports
53+
during a build, but it still stores downloaded dependencies (in GOPATH/src/mod)
54+
and installed commands (in GOPATH/bin, unless GOBIN is set).
55+
5256
Defining a module
5357
5458
A module is defined by a tree of Go source files with a go.mod file
@@ -245,7 +249,6 @@ For example, these commands are all valid:
245249
go get github.com/gorilla/mux@c856192 # records v0.0.0-20180517173623-c85619274f5d
246250
go get github.com/gorilla/mux@master # records current meaning of master
247251
248-
249252
Module compatibility and semantic versioning
250253
251254
The go command requires that modules use semantic versions and expects that
@@ -314,7 +317,15 @@ See https://research.swtch.com/vgo-import for more information about
314317
semantic import versioning, and see https://semver.org/ for more about
315318
semantic versioning.
316319
317-
Module verification
320+
Module code layout
321+
322+
For now, see https://research.swtch.com/vgo-module for information
323+
about how source code in version control systems is mapped to
324+
module file trees.
325+
326+
TODO: Add documentation to go command.
327+
328+
Module downloading and verification
318329
319330
The go command maintains, in the main module's root directory alongside
320331
go.mod, a file named go.sum containing the expected cryptographic checksums
@@ -330,6 +341,13 @@ each command invocation. The 'go mod -verify' command checks that
330341
the cached copies of module downloads still match both their recorded
331342
checksums and the entries in go.sum.
332343
344+
The go command can fetch modules from a proxy instead of connecting
345+
to source control systems directly, according to the setting of the GOPROXY
346+
environment variable.
347+
348+
See 'go help goproxy' for details about the proxy and also the format of
349+
the cached downloaded packages.
350+
333351
Modules and vendoring
334352
335353
When using modules, the go command completely ignores vendor directories.

0 commit comments

Comments
 (0)