Skip to content

Commit 79bef21

Browse files
authored
Revert "[builder] Support for --skip-new-go-module (open-telemetry#10098)" (open-telemetry#10978)
This reverts commit a0331ed.
1 parent 5963d44 commit 79bef21

File tree

12 files changed

+71
-651
lines changed

12 files changed

+71
-651
lines changed

.chloggen/builder-skip-go-mod.yaml

Lines changed: 0 additions & 25 deletions
This file was deleted.

cmd/builder/Makefile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
include ../../Makefile.Common
22

3-
GOTEST_TIMEOUT=360s
4-
53
.PHONY: ocb
64
ocb:
75
CGO_ENABLED=0 $(GOCMD) build -trimpath -o ../../bin/ocb_$(GOOS)_$(GOARCH) .

cmd/builder/README.md

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -157,24 +157,6 @@ ocb --skip-generate --skip-get-modules --config=config.yaml
157157
```
158158
to only execute the compilation step.
159159

160-
### Avoiding the use of a new go.mod file
161-
162-
You can optionally skip creating a new `go.mod` file. This is helpful when
163-
using a monorepo setup with a shared go.mod file. When the `--skip-new-go-module`
164-
command-line flag is supplied, the build process issues a `go get` command for
165-
each component, relying on the Go toolchain to update the enclosing Go module
166-
appropriately.
167-
168-
This command will avoid downgrading a dependency in the enclosing
169-
module, according to
170-
[`semver.Compare()`](https://pkg.go.dev/golang.org/x/mod/semver#Compare),
171-
and will instead issue a log indicating that the component was not
172-
upgraded.
173-
174-
`--skip-new-go-module` is incompatible with `replaces`, `excludes`,
175-
and the component `path` override. For each of these features, users
176-
are expected to modify the enclosing `go.mod` directly.
177-
178160
### Strict versioning checks
179161

180162
The builder checks the relevant `go.mod`

cmd/builder/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ require (
3131
github.com/mitchellh/copystructure v1.2.0 // indirect
3232
github.com/mitchellh/reflectwalk v1.0.2 // indirect
3333
github.com/pmezard/go-difflib v1.0.0 // indirect
34-
github.com/rogpeppe/go-internal v1.12.0 // indirect
34+
github.com/rogpeppe/go-internal v1.10.0 // indirect
3535
golang.org/x/sys v0.21.0 // indirect
3636
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
3737
gopkg.in/yaml.v3 v3.0.1 // indirect

cmd/builder/go.sum

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/builder/internal/builder/config.go

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,8 @@ import (
1919

2020
const defaultOtelColVersion = "0.107.0"
2121

22-
var (
23-
// ErrMissingGoMod indicates an empty gomod field
24-
ErrMissingGoMod = errors.New("missing gomod specification for module")
25-
// ErrIncompatibleConfigurationValues indicates that there is configuration that cannot be combined
26-
ErrIncompatibleConfigurationValues = errors.New("cannot combine configuration values")
27-
)
22+
// ErrMissingGoMod indicates an empty gomod field
23+
var ErrMissingGoMod = errors.New("missing gomod specification for module")
2824

2925
// Config holds the builder's configuration
3026
type Config struct {
@@ -33,7 +29,6 @@ type Config struct {
3329
SkipGenerate bool `mapstructure:"-"`
3430
SkipCompilation bool `mapstructure:"-"`
3531
SkipGetModules bool `mapstructure:"-"`
36-
SkipNewGoModule bool `mapstructure:"-"`
3732
SkipStrictVersioning bool `mapstructure:"-"`
3833
LDFlags string `mapstructure:"-"`
3934
Verbose bool `mapstructure:"-"`
@@ -121,15 +116,14 @@ func NewDefaultConfig() Config {
121116
func (c *Config) Validate() error {
122117
var providersError error
123118
if c.Providers != nil {
124-
providersError = c.validateModules("provider", *c.Providers)
119+
providersError = validateModules("provider", *c.Providers)
125120
}
126121
return multierr.Combine(
127-
c.validateModules("extension", c.Extensions),
128-
c.validateModules("receiver", c.Receivers),
129-
c.validateModules("exporter", c.Exporters),
130-
c.validateModules("processor", c.Processors),
131-
c.validateModules("connector", c.Connectors),
132-
c.validateFlags(),
122+
validateModules("extension", c.Extensions),
123+
validateModules("receiver", c.Receivers),
124+
validateModules("exporter", c.Exporters),
125+
validateModules("processor", c.Processors),
126+
validateModules("connector", c.Connectors),
133127
providersError,
134128
)
135129
}
@@ -246,21 +240,11 @@ func (c *Config) ParseModules() error {
246240
return nil
247241
}
248242

249-
func (c *Config) validateFlags() error {
250-
if c.SkipNewGoModule && (len(c.Replaces) != 0 || len(c.Excludes) != 0) {
251-
return fmt.Errorf("%w excludes or replaces with --skip-new-go-module; please modify the enclosing go.mod file directly", ErrIncompatibleConfigurationValues)
252-
}
253-
return nil
254-
}
255-
256-
func (c *Config) validateModules(name string, mods []Module) error {
243+
func validateModules(name string, mods []Module) error {
257244
for i, mod := range mods {
258245
if mod.GoMod == "" {
259246
return fmt.Errorf("%s module at index %v: %w", name, i, ErrMissingGoMod)
260247
}
261-
if mod.Path != "" && c.SkipNewGoModule {
262-
return fmt.Errorf("%w cannot modify mod.path %q combined with --skip-new-go-module; please modify the enclosing go.mod file directly", ErrIncompatibleConfigurationValues, mod.Path)
263-
}
264248
}
265249
return nil
266250
}

cmd/builder/internal/builder/config_test.go

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package builder
55

66
import (
7+
"errors"
78
"os"
89
"strings"
910
"testing"
@@ -139,37 +140,10 @@ func TestMissingModule(t *testing.T) {
139140
},
140141
err: ErrMissingGoMod,
141142
},
142-
{
143-
cfg: Config{
144-
Logger: zap.NewNop(),
145-
SkipNewGoModule: true,
146-
Extensions: []Module{{
147-
GoMod: "some-module",
148-
Path: "invalid",
149-
}},
150-
},
151-
err: ErrIncompatibleConfigurationValues,
152-
},
153-
{
154-
cfg: Config{
155-
Logger: zap.NewNop(),
156-
SkipNewGoModule: true,
157-
Replaces: []string{"", ""},
158-
},
159-
err: ErrIncompatibleConfigurationValues,
160-
},
161-
{
162-
cfg: Config{
163-
Logger: zap.NewNop(),
164-
SkipNewGoModule: true,
165-
Excludes: []string{"", ""},
166-
},
167-
err: ErrIncompatibleConfigurationValues,
168-
},
169143
}
170144

171145
for _, test := range configurations {
172-
assert.ErrorIs(t, test.cfg.Validate(), test.err)
146+
assert.True(t, errors.Is(test.cfg.Validate(), test.err))
173147
}
174148
}
175149

cmd/builder/internal/builder/main.go

Lines changed: 22 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"slices"
1414
"strings"
1515
"text/template"
16+
"time"
1617

1718
"go.uber.org/zap"
1819
"golang.org/x/mod/modfile"
@@ -24,6 +25,7 @@ var (
2425
ErrGoNotFound = errors.New("go binary not found")
2526
ErrDepNotFound = errors.New("dependency not found in go mod file")
2627
ErrVersionMismatch = errors.New("mismatch in go.mod and builder configuration versions")
28+
errDownloadFailed = errors.New("failed to download go modules")
2729
errCompileFailed = errors.New("failed to compile the OpenTelemetry Collector distribution")
2830
skipStrictMsg = "Use --skip-strict-versioning to temporarily disable this check. This flag will be removed in a future minor version"
2931
)
@@ -84,30 +86,18 @@ func Generate(cfg Config) error {
8486
return fmt.Errorf("failed to create output path: %w", err)
8587
}
8688

87-
allTemplates := []*template.Template{
89+
for _, tmpl := range []*template.Template{
8890
mainTemplate,
8991
mainOthersTemplate,
9092
mainWindowsTemplate,
9193
componentsTemplate,
92-
}
93-
94-
// Add the go.mod template unless that file is skipped.
95-
if !cfg.SkipNewGoModule {
96-
allTemplates = append(allTemplates, goModTemplate)
97-
}
98-
99-
for _, tmpl := range allTemplates {
94+
goModTemplate,
95+
} {
10096
if err := processAndWrite(cfg, tmpl, tmpl.Name(), cfg); err != nil {
10197
return fmt.Errorf("failed to generate source file %q: %w", tmpl.Name(), err)
10298
}
10399
}
104100

105-
// when not creating a new go.mod file, update modules one-by-one in the
106-
// enclosing go module.
107-
if err := cfg.updateModules(); err != nil {
108-
return err
109-
}
110-
111101
cfg.Logger.Info("Sources created", zap.String("path", cfg.Distribution.OutputPath))
112102
return nil
113103
}
@@ -155,7 +145,7 @@ func GetModules(cfg Config) error {
155145
}
156146

157147
if cfg.SkipStrictVersioning {
158-
return nil
148+
return downloadModules(cfg)
159149
}
160150

161151
// Perform strict version checking. For each component listed and the
@@ -195,7 +185,22 @@ func GetModules(cfg Config) error {
195185
}
196186
}
197187

198-
return nil
188+
return downloadModules(cfg)
189+
}
190+
191+
func downloadModules(cfg Config) error {
192+
cfg.Logger.Info("Getting go modules")
193+
failReason := "unknown"
194+
for i := 1; i <= cfg.downloadModules.numRetries; i++ {
195+
if _, err := runGoCommand(cfg, "mod", "download"); err != nil {
196+
failReason = err.Error()
197+
cfg.Logger.Info("Failed modules download", zap.String("retry", fmt.Sprintf("%d/%d", i, cfg.downloadModules.numRetries)))
198+
time.Sleep(cfg.downloadModules.wait)
199+
continue
200+
}
201+
return nil
202+
}
203+
return fmt.Errorf("%w: %s", errDownloadFailed, failReason)
199204
}
200205

201206
func processAndWrite(cfg Config, tmpl *template.Template, outFile string, tmplParams any) error {
@@ -221,68 +226,6 @@ func (c *Config) allComponents() []Module {
221226
c.Extensions, c.Connectors, *c.Providers)
222227
}
223228

224-
func (c *Config) updateModules() error {
225-
if !c.SkipNewGoModule {
226-
return nil
227-
}
228-
229-
// Build the main service dependency
230-
coremod, corever := c.coreModuleAndVersion()
231-
corespec := coremod + " " + corever
232-
233-
if err := c.updateGoModule(corespec); err != nil {
234-
return err
235-
}
236-
237-
for _, comp := range c.allComponents() {
238-
if err := c.updateGoModule(comp.GoMod); err != nil {
239-
return err
240-
}
241-
}
242-
return nil
243-
}
244-
245-
func (c *Config) updateGoModule(modspec string) error {
246-
mod, ver, found := strings.Cut(modspec, " ")
247-
if !found {
248-
return fmt.Errorf("ill-formatted modspec %q: missing space separator", modspec)
249-
}
250-
251-
// Re-parse the go.mod file on each iteration, since it can
252-
// change each time.
253-
modulePath, dependencyVersions, err := c.readGoModFile()
254-
if err != nil {
255-
return err
256-
}
257-
258-
if mod == modulePath {
259-
// this component is part of the same module, nothing to update.
260-
return nil
261-
}
262-
263-
// check for exact match
264-
hasVer, ok := dependencyVersions[mod]
265-
if ok && hasVer == ver {
266-
c.Logger.Info("Component version match", zap.String("module", mod), zap.String("version", ver))
267-
return nil
268-
}
269-
270-
scomp := semver.Compare(hasVer, ver)
271-
if scomp > 0 {
272-
// version in enclosing module is newer, do not change
273-
c.Logger.Info("Not upgrading component, enclosing module is newer.", zap.String("module", mod), zap.String("existing", hasVer), zap.String("config_version", ver))
274-
return nil
275-
}
276-
277-
// upgrading or changing version
278-
updatespec := "-require=" + mod + "@" + ver
279-
280-
if _, err := runGoCommand(*c, "mod", "edit", updatespec); err != nil {
281-
return err
282-
}
283-
return nil
284-
}
285-
286229
func (c *Config) readGoModFile() (string, map[string]string, error) {
287230
var modPath string
288231
stdout, err := runGoCommand(*c, "mod", "edit", "-print")

0 commit comments

Comments
 (0)