Skip to content

Commit

Permalink
add explicit errors when an expected dependency isn't found in the gomod
Browse files Browse the repository at this point in the history
  • Loading branch information
Kristina Pathak committed Apr 16, 2024
1 parent 90b000f commit a2fdb95
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
27 changes: 19 additions & 8 deletions cmd/builder/internal/builder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ import (
var (
// ErrGoNotFound is returned when a Go binary hasn't been found
ErrGoNotFound = errors.New("go binary not found")
ErrStrictMode = errors.New("mismatch in go.mod and builder configuration versions")
ErrDepNotFound = errors.New("dependency not found in go mod file")
ErrVersionMismatch = errors.New("mismatch in go.mod and builder configuration versions")
errFailedToDownload = errors.New("failed to download go modules")
skipStrictMsg = "Use --skip-strict-versioning to temporarily disable this check. This flag will be removed in a future minor version"
)

func runGoCommand(cfg Config, args ...string) ([]byte, error) {
Expand Down Expand Up @@ -71,7 +73,7 @@ func Generate(cfg Config) error {
// create a warning message for non-aligned builder and collector base
if cfg.Distribution.OtelColVersion != defaultOtelColVersion {
if !cfg.SkipStrictVersioning {
return fmt.Errorf("builder version %q does not match build configuration version %q: %w", cfg.Distribution.OtelColVersion, defaultOtelColVersion, ErrStrictMode)
return fmt.Errorf("builder version %q does not match build configuration version %q: %w", cfg.Distribution.OtelColVersion, defaultOtelColVersion, ErrVersionMismatch)
}
cfg.Logger.Info("You're building a distribution with non-aligned version of the builder. Compilation may fail due to API changes. Please upgrade your builder or API", zap.String("builder-version", defaultOtelColVersion))
}
Expand Down Expand Up @@ -160,10 +162,14 @@ func GetModules(cfg Config) error {
}

corePath, coreVersion := cfg.coreModuleAndVersion()
if dependencyVersions[corePath] != coreVersion {
coreDepVersion, ok := dependencyVersions[corePath]
if !ok {

Check warning on line 166 in cmd/builder/internal/builder/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/builder/internal/builder/main.go#L166

Added line #L166 was not covered by tests
return fmt.Errorf("core collector %w: '%s'. %s", ErrDepNotFound, corePath, skipStrictMsg)
}
if coreDepVersion != coreVersion {
return fmt.Errorf(
"%w: core collector version calculated by component dependencies %q does not match configured version %q. Use --skip-strict-versioning to temporarily disable this check. This flag will be removed in a future minor version",
ErrStrictMode, dependencyVersions[corePath], coreVersion)
"%w: core collector version calculated by component dependencies %q does not match configured version %q. %s",

Check warning on line 171 in cmd/builder/internal/builder/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/builder/internal/builder/main.go#L169-L171

Added lines #L169 - L171 were not covered by tests
ErrVersionMismatch, coreDepVersion, coreVersion, skipStrictMsg)
}

for _, mod := range cfg.allComponents() {
Expand All @@ -174,10 +180,14 @@ func GetModules(cfg Config) error {
continue
}

if dependencyVersions[module] != version {
moduleDepVersion, ok := dependencyVersions[module]
if !ok {

Check warning on line 184 in cmd/builder/internal/builder/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/builder/internal/builder/main.go#L184

Added line #L184 was not covered by tests
return fmt.Errorf("component %w: '%s'. %s", ErrDepNotFound, module, skipStrictMsg)
}
if moduleDepVersion != version {
return fmt.Errorf(
"%w: component %q version calculated by dependencies %q does not match configured version %q. Use --skip-strict-versioning to temporarily disable this check. This flag will be removed in a future minor version",
ErrStrictMode, module, dependencyVersions[module], version)
"%w: component %q version calculated by dependencies %q does not match configured version %q. %s",
ErrVersionMismatch, module, moduleDepVersion, version, skipStrictMsg)
}
}

Expand Down Expand Up @@ -221,6 +231,7 @@ func (c *Config) coreModuleAndVersion() (string, string) {
}

func (c *Config) allComponents() []Module {
// TODO: Use slices.Concat when we drop support for Go 1.21
return append(c.Exporters,
append(c.Receivers,
append(c.Processors,
Expand Down
6 changes: 3 additions & 3 deletions cmd/builder/internal/builder/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestVersioning(t *testing.T) {
cfg.Distribution.OtelColVersion = "0.90.0"
return cfg
},
expectedErr: ErrStrictMode,
expectedErr: ErrVersionMismatch,
},
{
description: "old otel version without strict mode",
Expand All @@ -69,7 +69,7 @@ func TestVersioning(t *testing.T) {
cfg.Distribution.OtelColVersion = "invalid"
return cfg
},
expectedErr: ErrStrictMode,
expectedErr: ErrVersionMismatch,
},
{
description: "invalid version without strict mode, only generate",
Expand All @@ -96,7 +96,7 @@ func TestVersioning(t *testing.T) {
}
return cfg
},
expectedErr: ErrStrictMode,
expectedErr: ErrVersionMismatch,
},
}
for _, tc := range tests {
Expand Down

0 comments on commit a2fdb95

Please sign in to comment.