1
2
3
4
5
6
7 package workcmd
8
9 import (
10 "cmd/go/internal/base"
11 "cmd/go/internal/gover"
12 "cmd/go/internal/imports"
13 "cmd/go/internal/modload"
14 "cmd/go/internal/toolchain"
15 "context"
16
17 "golang.org/x/mod/module"
18 )
19
20 var cmdSync = &base.Command{
21 UsageLine: "go work sync",
22 Short: "sync workspace build list to modules",
23 Long: `Sync syncs the workspace's build list back to the
24 workspace's modules
25
26 The workspace's build list is the set of versions of all the
27 (transitive) dependency modules used to do builds in the workspace. go
28 work sync generates that build list using the Minimal Version Selection
29 algorithm, and then syncs those versions back to each of modules
30 specified in the workspace (with use directives).
31
32 The syncing is done by sequentially upgrading each of the dependency
33 modules specified in a workspace module to the version in the build list
34 if the dependency module's version is not already the same as the build
35 list's version. Note that Minimal Version Selection guarantees that the
36 build list's version of each module is always the same or higher than
37 that in each workspace module.
38
39 See the workspaces reference at https://go.dev/ref/mod#workspaces
40 for more information.
41 `,
42 Run: runSync,
43 }
44
45 func init() {
46 base.AddChdirFlag(&cmdSync.Flag)
47 base.AddModCommonFlags(&cmdSync.Flag)
48 }
49
50 func runSync(ctx context.Context, cmd *base.Command, args []string) {
51 moduleLoaderState := modload.NewState()
52 moduleLoaderState.ForceUseModules = true
53 modload.InitWorkfile(moduleLoaderState)
54 if modload.WorkFilePath(moduleLoaderState) == "" {
55 base.Fatalf("go: no go.work file found\n\t(run 'go work init' first or specify path using GOWORK environment variable)")
56 }
57
58 _, err := modload.LoadModGraph(moduleLoaderState, ctx, "")
59 if err != nil {
60 toolchain.SwitchOrFatal(moduleLoaderState, ctx, err)
61 }
62 mustSelectFor := map[module.Version][]module.Version{}
63
64 mms := moduleLoaderState.MainModules
65
66 opts := modload.PackageOpts{
67 Tags: imports.AnyTags(),
68 VendorModulesInGOROOTSrc: true,
69 ResolveMissingImports: false,
70 LoadTests: true,
71 AllowErrors: true,
72 SilencePackageErrors: true,
73 SilenceUnmatchedWarnings: true,
74 }
75 for _, m := range mms.Versions() {
76 opts.MainModule = m
77 _, pkgs := modload.LoadPackages(moduleLoaderState, ctx, opts, "all")
78 opts.MainModule = module.Version{}
79
80 var (
81 mustSelect []module.Version
82 inMustSelect = map[module.Version]bool{}
83 )
84 for _, pkg := range pkgs {
85 if r := modload.PackageModule(pkg); r.Version != "" && !inMustSelect[r] {
86
87 mustSelect = append(mustSelect, r)
88 inMustSelect[r] = true
89 }
90 }
91 gover.ModSort(mustSelect)
92 mustSelectFor[m] = mustSelect
93 }
94
95 workFilePath := modload.WorkFilePath(moduleLoaderState)
96
97 var goV string
98 for _, m := range mms.Versions() {
99 if mms.ModRoot(m) == "" && m.Path == "command-line-arguments" {
100
101
102
103 continue
104 }
105
106
107
108 modload.EnterModule(moduleLoaderState, ctx, mms.ModRoot(m))
109
110
111
112
113
114
115
116
117
118 changed, err := modload.EditBuildList(moduleLoaderState, ctx, nil, mustSelectFor[m])
119 if err != nil {
120 continue
121 }
122 if changed {
123 modload.LoadPackages(moduleLoaderState, ctx, modload.PackageOpts{
124 Tags: imports.AnyTags(),
125 Tidy: true,
126 VendorModulesInGOROOTSrc: true,
127 ResolveMissingImports: false,
128 LoadTests: true,
129 AllowErrors: true,
130 SilenceMissingStdImports: true,
131 SilencePackageErrors: true,
132 }, "all")
133 modload.WriteGoMod(moduleLoaderState, ctx, modload.WriteOpts{})
134 }
135 goV = gover.Max(goV, moduleLoaderState.MainModules.GoVersion(moduleLoaderState))
136 }
137
138 wf, err := modload.ReadWorkFile(workFilePath)
139 if err != nil {
140 base.Fatal(err)
141 }
142 modload.UpdateWorkGoVersion(wf, goV)
143 modload.UpdateWorkFile(wf)
144 if err := modload.WriteWorkFile(workFilePath, wf); err != nil {
145 base.Fatal(err)
146 }
147 }
148
View as plain text