@@ -712,6 +712,11 @@ must match the module path it replaces.
712
712
and are ignored in other modules. See [ Minimal version
713
713
selection] ( #minimal-version-selection ) for details.
714
714
715
+ If there are multiple main modules, all main modules' ` go.mod `
716
+ files apply. Conflicting ` replace ` directives across main
717
+ modules are disallowed, and must be removed or overridden in
718
+ a [ replace in the ` go.work file ` ] ( #go-work-file-replace ) .
719
+
715
720
Note that a ` replace ` directive alone does not add a module to the [ module
716
721
graph] ( #glos-module-graph ) . A [ ` require ` directive] ( #go-mod-file-require ) that
717
722
refers to a replaced module version is also needed, either in the main module's
@@ -895,13 +900,14 @@ Conceptually, MVS operates on a directed graph of modules, specified with
895
900
[ ` go.mod ` files] ( #glos-go-mod-file ) . Each vertex in the graph represents a
896
901
module version. Each edge represents a minimum required version of a dependency,
897
902
specified using a [ ` require ` ] ( #go-mod-file-require )
898
- directive. [ ` replace ` ] ( #go-mod-file-replace ) and [ ` exclude ` ] ( #go-mod-file-exclude )
899
- directives in the main module's ` go.mod ` file modify the graph.
903
+ directive. The graph may be modified by [ ` exclude ` ] ( #go-mod-file-exclude )
904
+ and [ ` replace ` ] ( #go-mod-file-replace ) directives in the ` go.mod ` file(s) of the main
905
+ module(s) and by [ ` replace ` ] ( #go-work-file-replace ) directives in the ` go.work ` file.
900
906
901
907
MVS produces the [ build list] ( #glos-build-list ) as output, the list of module
902
908
versions used for a build.
903
909
904
- MVS starts at the main module (a special vertex in the graph that has no
910
+ MVS starts at the main modules ( special vertices in the graph that have no
905
911
version) and traverses the graph, tracking the highest required version of each
906
912
module. At the end of the traversal, the highest required versions comprise the
907
913
build list: they are the minimum versions that satisfy all requirements.
@@ -927,9 +933,9 @@ requires them.
927
933
### Replacement {#mvs-replace}
928
934
929
935
The content of a module (including its ` go.mod ` file) may be replaced using a
930
- [ ` replace ` directive] ( #go-mod-file-replace ) in the main module's ` go.mod ` file.
931
- A ` replace ` directive may apply to a specific version of a module or to all
932
- versions of a module.
936
+ [ ` replace ` directive] ( #go-mod-file-replace ) in a main module's ` go.mod ` file
937
+ or a workspace's ` go.work ` file. A ` replace ` directive may apply to a specific
938
+ version of a module or to all versions of a module.
933
939
934
940
Replacements change the module graph, since a replacement module may have
935
941
different dependencies than replaced versions.
@@ -1063,6 +1069,173 @@ the main module to ensure that they are locally consistent. (Inconsistencies can
1063
1069
arise due to version-control merges, hand-edits, and changes in modules that
1064
1070
have been [ replaced] ( #go-mod-file-replace ) using local filesystem paths.)
1065
1071
1072
+ ## Workspaces {#workspaces}
1073
+
1074
+ A <dfn >workspace</dfn > is a collection of modules on disk that are used as
1075
+ the root modules when running [ minimal version selection (MVS)] ( #minimal-version-selection ) .
1076
+
1077
+ A workspace can be declared in a [ ` go.work ` file] ( #go-work-file ) that specifies
1078
+ relative paths to the module directories of each of the modules in the workspace.
1079
+ When no ` go.work ` file exists, the workspace consists of the single module
1080
+ containing the current directory.
1081
+
1082
+ Most ` go ` subcommands that work with modules
1083
+ operate on the set of modules determined by the current workspace.
1084
+ ` go mod init ` , ` go mod why ` , ` go mod edit ` , ` go mod tidy ` , ` go mod vendor ` ,
1085
+ and ` go get ` always operate on a single main module.
1086
+
1087
+ A command determines whether it is in a workspace context by first examining
1088
+ the ` -workfile ` flag. If ` -workfile ` is set to ` off ` , the command will be
1089
+ in a single-module context. If it is empty or not provided, the command
1090
+ will search the current working directory, and then successive parent directories,
1091
+ for a file ` go.work ` . If a file is found, the command will operate in the
1092
+ workspace it defines; otherwise, the workspace will include only the module
1093
+ containing the working directory.
1094
+ If ` -workfile ` names a path to an existing file that ends in .work,
1095
+ workspace mode will be enabled. Any other value is an error.
1096
+
1097
+ ### ` go.work ` files {#go-work-file}
1098
+
1099
+ A workspace is defined by a UTF-8 encoded text file named ` go.work ` . The
1100
+ ` go.work ` file is line oriented. Each line holds a single directive, made up of
1101
+ a keyword followed by arguments. For example:
1102
+
1103
+ ```
1104
+ go 1.18
1105
+
1106
+ use ./my/first/thing
1107
+ use ./my/second/thing
1108
+
1109
+ replace example.com/bad/thing v1.4.5 => example.com/good/thing v1.4.5
1110
+ ```
1111
+
1112
+ As in ` go.mod ` files, a leading keyword can be factored out of adjacent lines
1113
+ to create a block.
1114
+
1115
+ ```
1116
+ use (
1117
+ ./my/first/thing
1118
+ ./my/second/thing
1119
+ ```
1120
+
1121
+ The ` go ` command provides several subcommands for manipulating ` go.work ` files.
1122
+ [ ` go work init ` ] ( #go-work-init ) creates new ` go.work ` files. [ ` go work use ` ] ( #go-work-use ) adds module directories to
1123
+ the ` go.work ` file. [ ` go work edit ` ] ( #go-work-edit ) performs low-level
1124
+ edits. The
1125
+ [ ` golang.org/x/mod/modfile ` ] ( https://pkg.go.dev/golang.org/x/mod/modfile?tab=doc )
1126
+ package can be used by Go programs to make the same changes programmatically.
1127
+
1128
+ ### Lexical elements {#go-work-file-lexical}
1129
+
1130
+ Lexical elements in ` go.work ` files are defined in exactly the same way [ as for
1131
+ ` go.mod files ` ] ( #go-mod-file-lexical ) .
1132
+
1133
+ ### Grammar {#go-work-file-grammar}
1134
+
1135
+ ` go.work ` syntax is specified below using Extended Backus-Naur Form (EBNF).
1136
+ See the [ Notation section in the Go Language Specification] ( /ref/spec#Notation )
1137
+ for details on EBNF syntax.
1138
+
1139
+ ```
1140
+ GoWork = { Directive } .
1141
+ Directive = GoDirective |
1142
+ UseDirective |
1143
+ ReplaceDirective .
1144
+ ```
1145
+
1146
+ Newlines, identifiers, and strings are denoted with ` newline ` , ` ident ` , and
1147
+ ` string ` , respectively.
1148
+
1149
+ Module paths and versions are denoted with ` ModulePath ` and ` Version ` .
1150
+ Module paths and versions are specified in exactly the same way [ as for
1151
+ ` go.mod files ` ] ( #go-mod-file-lexical ) .
1152
+
1153
+ ```
1154
+ ModulePath = ident | string . /* see restrictions above */
1155
+ Version = ident | string . /* see restrictions above */
1156
+ ```
1157
+
1158
+ ### ` go ` directive {#go-mod-file-go}
1159
+
1160
+ A ` go ` directive is required in a valid ` go.work ` file. The version must
1161
+ be a valid Go release version: a positive
1162
+ integer followed by a dot and a non-negative integer (for example, ` 1.18 ` ,
1163
+ ` 1.19 ` ).
1164
+
1165
+ The ` go ` directive indicates the go toolchain version with which the
1166
+ ` go.work ` file is intended to work. If changes are made to the ` go.work `
1167
+ file format, future versions of the toolchain will interpret the file
1168
+ according to its indicated version.
1169
+
1170
+ A ` go.work ` file may contain at most one ` go ` directive.
1171
+
1172
+ ```
1173
+ GoDirective = "go" GoVersion newline .
1174
+ GoVersion = string | ident . /* valid release version; see above */
1175
+ ```
1176
+
1177
+ Example:
1178
+
1179
+ ```
1180
+ go 1.18
1181
+ ```
1182
+
1183
+ ### ` use ` directive {#go-work-file-use}
1184
+
1185
+ A ` use ` adds a module on disk to the set of main modules in a workspace.
1186
+ Its argument is a relative path to the directory containing the module's
1187
+ ` go.mod ` file. A ` use ` directive does not add modules contained in
1188
+ subdirectories of its argument directory. Those modules may be added by
1189
+ the directory containing their ` go.mod ` file in separate ` use ` directives.
1190
+
1191
+ ```
1192
+ UseDirective = "use" ( UseSpec | "(" newline { UseSpec } ")" newline ) .
1193
+ UseSpec = FilePath newline .
1194
+ FilePath = /* platform-specific relative or absolute file path */
1195
+
1196
+ ```
1197
+
1198
+ Example:
1199
+
1200
+ ```
1201
+ use ./mymod // example.com/mymod
1202
+
1203
+ use (
1204
+ ../othermod
1205
+ ./subdir/thirdmod
1206
+ )
1207
+ ```
1208
+
1209
+ ### ` replace ` directive {#go-work-file-replace}
1210
+
1211
+ Similar to a ` replace ` directive in a ` go.mod ` file, a ` replace ` directive in
1212
+ a ` go.work ` file replaces the contents of a specific version of a module,
1213
+ or all versions of a module, with contents found elsewhere. A wildcard replace
1214
+ in ` go.work ` overrides a version-specific ` replace ` in a ` go.mod ` file.
1215
+
1216
+ ` replace ` directives in ` go.work ` files override any replaces of the same
1217
+ module or module version in workspace modules.
1218
+
1219
+ ```
1220
+ ReplaceDirective = "replace" ( ReplaceSpec | "(" newline { ReplaceSpec } ")" newline ) .
1221
+ ReplaceSpec = ModulePath [ Version ] "=>" FilePath newline
1222
+ | ModulePath [ Version ] "=>" ModulePath Version newline .
1223
+ FilePath = /* platform-specific relative or absolute file path */
1224
+ ```
1225
+
1226
+ Example:
1227
+
1228
+ ```
1229
+ replace golang.org/x/net v1.2.3 => example.com/fork/net v1.4.5
1230
+
1231
+ replace (
1232
+ golang.org/x/net v1.2.3 => example.com/fork/net v1.4.5
1233
+ golang.org/x/net => example.com/fork/net v1.4.5
1234
+ golang.org/x/net v1.2.3 => ./fork/net
1235
+ golang.org/x/net => ./fork/net
1236
+ )
1237
+ ```
1238
+
1066
1239
## Compatibility with non-module repositories {#non-module-compat}
1067
1240
1068
1241
To ensure a smooth transition from ` GOPATH ` to modules, the ` go ` command can
@@ -1270,6 +1443,11 @@ commands accept the following flags, common to all module commands.
1270
1443
` -modfile ` is specified, an alternate ` go.sum ` file is also used: its path is
1271
1444
derived from the ` -modfile ` flag by trimming the ` .mod ` extension and
1272
1445
appending ` .sum ` .
1446
+ * The ` -workfile ` flag instructs the ` go ` command to enter workspace mode using the provided
1447
+ [ ` go.work ` file] ( #go-work-file ) to define the workspace. If ` -workfile ` is
1448
+ set to ` off ` workspace mode is disabled. If ` -workfile ` is not provided the
1449
+ ` go ` command will search for a ` go.work ` file as described in the
1450
+ [ Workspaces] ( #workspaces ) section.
1273
1451
1274
1452
### Vendoring {#vendoring}
1275
1453
@@ -2325,6 +2503,135 @@ disabling module-aware mode.
2325
2503
</tbody >
2326
2504
</table >
2327
2505
2506
+ ### ` go work init ` {#go-work-init}
2507
+
2508
+ Usage:
2509
+
2510
+ ```
2511
+ go work init [moddirs]
2512
+ ```
2513
+
2514
+ Init initializes and writes a new go.work file in the
2515
+ current directory, in effect creating a new workspace at the current
2516
+ directory.
2517
+
2518
+ go work init optionally accepts paths to the workspace modules as
2519
+ arguments. If the argument is omitted, an empty workspace with no
2520
+ modules will be created.
2521
+
2522
+ Each argument path is added to a use directive in the go.work file. The
2523
+ current go version will also be listed in the go.work file.
2524
+
2525
+ ### ` go work edit ` {#go-work-edit}
2526
+
2527
+ Usage:
2528
+
2529
+ ```
2530
+ go work edit [editing flags] [go.work]
2531
+ ```
2532
+
2533
+ The ` go work edit ` command provides a command-line interface for editing ` go.work ` ,
2534
+ for use primarily by tools or scripts. It only reads ` go.work ` ;
2535
+ it does not look up information about the modules involved.
2536
+ If no file is specified, Edit looks for a ` go.work ` file in the current
2537
+ directory and its parent directories
2538
+
2539
+ The editing flags specify a sequence of editing operations.
2540
+ * The ` -fmt ` flag reformats the go.work file without making other changes.
2541
+ This reformatting is also implied by any other modifications that use or
2542
+ rewrite the ` go.work ` file. The only time this flag is needed is if no other
2543
+ flags are specified, as in 'go work edit ` -fmt ` '.
2544
+ * The ` -use=path ` and ` -dropuse=path ` flags
2545
+ add and drop a use directive from the ` go.work ` file's set of module directories.
2546
+ * The ` -replace=old[@v]=new[@v] ` flag adds a replacement of the given
2547
+ module path and version pair. If the ` @v ` in ` old@v ` is omitted, a
2548
+ replacement without a version on the left side is added, which applies
2549
+ to all versions of the old module path. If the ` @v ` in ` new@v ` is omitted,
2550
+ the new path should be a local module root directory, not a module
2551
+ path. Note that ` -replace ` overrides any redundant replacements for ` old[@v] ` ,
2552
+ so omitting ` @v ` will drop existing replacements for specific versions.
2553
+ * The ` -dropreplace=old[@v] ` flag drops a replacement of the given
2554
+ module path and version pair. If the ` @v ` is omitted, a replacement without
2555
+ a version on the left side is dropped.
2556
+ * The ` -go=version ` flag sets the expected Go language version.
2557
+
2558
+ The editing flags may be repeated. The changes are applied in the order given.
2559
+
2560
+ ` go work edit ` has additional flags that control its output
2561
+
2562
+ * The -print flag prints the final go.work in its text format instead of
2563
+ writing it back to go.mod.
2564
+ * The -json flag prints the final go.work file in JSON format instead of
2565
+ writing it back to go.mod. The JSON output corresponds to these Go types:
2566
+
2567
+ ```
2568
+ type Module struct {
2569
+ Path string
2570
+ Version string
2571
+ }
2572
+
2573
+ type GoWork struct {
2574
+ Go string
2575
+ Directory []Directory
2576
+ Replace []Replace
2577
+ }
2578
+
2579
+ type Use struct {
2580
+ Path string
2581
+ ModulePath string
2582
+ }
2583
+
2584
+ type Replace struct {
2585
+ Old Module
2586
+ New Module
2587
+ }
2588
+ ```
2589
+
2590
+ ### ` go work use ` {#go-work-use}
2591
+
2592
+ Usage:
2593
+
2594
+ ```
2595
+ go work use [-r] [moddirs]
2596
+ ```
2597
+
2598
+ The ` go work use ` command provides a command-line interface for adding
2599
+ directories, optionally recursively, to a ` go.work ` file.
2600
+
2601
+ A [ ` use ` directive] ( #go-work-file-use ) will be added to the ` go.work ` file for each argument
2602
+ directory listed on the command line ` go.work ` file, if it exists on disk,
2603
+ or removed from the ` go.work ` file if it does not exist on disk.
2604
+
2605
+ The ` -r ` flag searches recursively for modules in the argument
2606
+ directories, and the use command operates as if each of the directories
2607
+ were specified as arguments: namely, ` use ` directives will be added for
2608
+ directories that exist, and removed for directories that do not exist.
2609
+
2610
+ ### ` go work sync ` {#go-work-sync}
2611
+
2612
+ Usage:
2613
+
2614
+ ```
2615
+ go work sync
2616
+ ```
2617
+
2618
+ The ` go work sync ` command syncs the workspace's build list back to the
2619
+ workspace's modules.
2620
+
2621
+ The workspace's build list is the set of versions of all the
2622
+ (transitive) dependency modules used to do builds in the workspace. `go
2623
+ work sync` generates that build list using the [ Minimal Version Selection
2624
+ (MVS)] ( #glos-minimal-version-selection )
2625
+ algorithm, and then syncs those versions back to each of modules
2626
+ specified in the workspace (with ` use ` directives).
2627
+
2628
+ Once the workspace build list is computed, the ` go.mod ` file for each
2629
+ module in the workspace is rewritten with the dependencies relevant
2630
+ to that module upgraded to match the workspace build list.
2631
+ Note that [ Minimal Version Selection] ( #glos-minimal-version-selection )
2632
+ guarantees that the build list's version of each module is always
2633
+ the same or higher than that in each workspace module.
2634
+
2328
2635
## Module proxies {#module-proxy}
2329
2636
2330
2637
### ` GOPROXY ` protocol {#goproxy-protocol}
@@ -4070,6 +4377,11 @@ other metadata. Appears in the [module's root
4070
4377
directory] ( #glos-module-root-directory ) . See the section on [ ` go.mod `
4071
4378
files] ( #go-mod-file ) .
4072
4379
4380
+ <a id =" glos-go-work-file " ></a >
4381
+ ** ` go.work ` file** The file that defines the set of modules to be
4382
+ used in a [ workspace] ( #workspaces ) . See the section on
4383
+ [ ` go.work ` files] ( #go-work-file )
4384
+
4073
4385
<a id =" glos-import-path " ></a >
4074
4386
** import path:** A string used to import a package in a Go source file.
4075
4387
Synonymous with [ package path] ( #glos-package-path ) .
@@ -4232,3 +4544,8 @@ other modules needed to build packages in the main module. Maintained with
4232
4544
** version:** An identifier for an immutable snapshot of a module, written as the
4233
4545
letter ` v ` followed by a semantic version. See the section on
4234
4546
[ Versions] ( #versions ) .
4547
+
4548
+ <a id =" glos-workspace " ></a >
4549
+ ** workspace:** A collection of modules on disk that are used as
4550
+ the root modules when running [ minimal version selection (MVS)] ( #minimal-version-selection ) .
4551
+ See the section on [ Workspaces] ( #workspaces )
0 commit comments