Skip to content

Commit

Permalink
Ensure unique aliases (#12201)
Browse files Browse the repository at this point in the history
#### Description
This PR ensures that unique aliases are used for modules with same
suffix.

#### Link to tracking issue
Fixes #12162


<!--Describe what testing was performed and which tests were added.-->
#### Testing

<!--Describe the documentation added.-->
#### Documentation

<!--Please delete paragraphs that you did not use before submitting.-->
  • Loading branch information
mackjmr authored Feb 10, 2025
1 parent 76f44e1 commit aa333e9
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 12 deletions.
25 changes: 25 additions & 0 deletions .chloggen/unique-aliases.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: bug_fix

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: cmd/builder

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Ensure unique aliases for modules with same suffix

# One or more tracking issues or pull requests related to the change
issues: [12201]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
32 changes: 24 additions & 8 deletions cmd/builder/internal/builder/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,37 +170,38 @@ func (c *Config) SetGoPath() error {
// ParseModules will parse the Modules entries and populate the missing values
func (c *Config) ParseModules() error {
var err error
usedNames := make(map[string]int)

c.Extensions, err = parseModules(c.Extensions)
c.Extensions, err = parseModules(c.Extensions, usedNames)
if err != nil {
return err
}

c.Receivers, err = parseModules(c.Receivers)
c.Receivers, err = parseModules(c.Receivers, usedNames)
if err != nil {
return err
}

c.Exporters, err = parseModules(c.Exporters)
c.Exporters, err = parseModules(c.Exporters, usedNames)
if err != nil {
return err
}

c.Processors, err = parseModules(c.Processors)
c.Processors, err = parseModules(c.Processors, usedNames)
if err != nil {
return err
}

c.Connectors, err = parseModules(c.Connectors)
c.Connectors, err = parseModules(c.Connectors, usedNames)
if err != nil {
return err
}

c.ConfmapProviders, err = parseModules(c.ConfmapProviders)
c.ConfmapProviders, err = parseModules(c.ConfmapProviders, usedNames)
if err != nil {
return err
}
c.ConfmapConverters, err = parseModules(c.ConfmapConverters)
c.ConfmapConverters, err = parseModules(c.ConfmapConverters, usedNames)
if err != nil {
return err
}
Expand All @@ -220,7 +221,7 @@ func validateModules(name string, mods []Module) error {
return nil
}

func parseModules(mods []Module) ([]Module, error) {
func parseModules(mods []Module, usedNames map[string]int) ([]Module, error) {
var parsedModules []Module
for _, mod := range mods {
if mod.Import == "" {
Expand All @@ -232,6 +233,21 @@ func parseModules(mods []Module) ([]Module, error) {
mod.Name = parts[len(parts)-1]
}

originalModName := mod.Name
if count, exists := usedNames[mod.Name]; exists {
var newName string
for {
newName = fmt.Sprintf("%s%d", mod.Name, count+1)
if _, transformedExists := usedNames[newName]; !transformedExists {
break
}
count++
}
mod.Name = newName
usedNames[newName] = 1
}
usedNames[originalModName] = 1

// Check if path is empty, otherwise filepath.Abs replaces it with current path ".".
if mod.Path != "" {
var err error
Expand Down
112 changes: 112 additions & 0 deletions cmd/builder/internal/builder/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,118 @@ import (
"go.opentelemetry.io/collector/cmd/builder/internal/config"
)

func TestAliases(t *testing.T) {
// prepare
cfg := Config{
Extensions: []Module{
{
GoMod: "github.com/org/repo/impl v0.1.2",
},
{
GoMod: "github.com/org/repo2/impl v0.1.2",
},
{
GoMod: "github.com/org/repo3/impl v0.1.2",
},
},
Receivers: []Module{
{
GoMod: "github.com/org/repo v0.1.2",
},
{
GoMod: "github.com/org2/repo v0.1.2",
},
{
GoMod: "github.com/org/repo4/impl v0.1.2",
},
},
Exporters: []Module{
{
GoMod: "github.com/another/module v0.1.2",
},
{
GoMod: "github.com/org/repo5/impl v0.1.2",
},
},
Processors: []Module{
{
GoMod: "github.com/another/module2 v0.1.2",
},
{
GoMod: "github.com/another2/module v0.1.2",
},
},
Connectors: []Module{
{
GoMod: "github.com/another/module3 v0.1.2",
},
{
GoMod: "github.com/another2/module4 v0.1.2",
},
{
GoMod: "github.com/another3/module v0.1.2",
},
},
}

// test
err := cfg.ParseModules()
require.NoError(t, err)

// verify
assert.Equal(t, "github.com/org/repo/impl v0.1.2", cfg.Extensions[0].GoMod)
assert.Equal(t, "github.com/org/repo/impl", cfg.Extensions[0].Import)
assert.Equal(t, "impl", cfg.Extensions[0].Name)

assert.Equal(t, "github.com/org/repo2/impl v0.1.2", cfg.Extensions[1].GoMod)
assert.Equal(t, "github.com/org/repo2/impl", cfg.Extensions[1].Import)
assert.Equal(t, "impl2", cfg.Extensions[1].Name)

assert.Equal(t, "github.com/org/repo3/impl v0.1.2", cfg.Extensions[2].GoMod)
assert.Equal(t, "github.com/org/repo3/impl", cfg.Extensions[2].Import)
assert.Equal(t, "impl3", cfg.Extensions[2].Name)

assert.Equal(t, "github.com/org/repo v0.1.2", cfg.Receivers[0].GoMod)
assert.Equal(t, "github.com/org/repo", cfg.Receivers[0].Import)
assert.Equal(t, "repo", cfg.Receivers[0].Name)

assert.Equal(t, "github.com/org2/repo v0.1.2", cfg.Receivers[1].GoMod)
assert.Equal(t, "github.com/org2/repo", cfg.Receivers[1].Import)
assert.Equal(t, "repo2", cfg.Receivers[1].Name)

assert.Equal(t, "github.com/org/repo4/impl v0.1.2", cfg.Receivers[2].GoMod)
assert.Equal(t, "github.com/org/repo4/impl", cfg.Receivers[2].Import)
assert.Equal(t, "impl4", cfg.Receivers[2].Name)

assert.Equal(t, "github.com/another/module v0.1.2", cfg.Exporters[0].GoMod)
assert.Equal(t, "github.com/another/module", cfg.Exporters[0].Import)
assert.Equal(t, "module", cfg.Exporters[0].Name)

assert.Equal(t, "github.com/org/repo5/impl v0.1.2", cfg.Exporters[1].GoMod)
assert.Equal(t, "github.com/org/repo5/impl", cfg.Exporters[1].Import)
assert.Equal(t, "impl5", cfg.Exporters[1].Name)

assert.Equal(t, "github.com/another/module2 v0.1.2", cfg.Processors[0].GoMod)
assert.Equal(t, "github.com/another/module2", cfg.Processors[0].Import)
assert.Equal(t, "module2", cfg.Processors[0].Name)

assert.Equal(t, "github.com/another2/module v0.1.2", cfg.Processors[1].GoMod)
assert.Equal(t, "github.com/another2/module", cfg.Processors[1].Import)
assert.Equal(t, "module3", cfg.Processors[1].Name)

assert.Equal(t, "github.com/another/module3 v0.1.2", cfg.Connectors[0].GoMod)
assert.Equal(t, "github.com/another/module3", cfg.Connectors[0].Import)
assert.Equal(t, "module32", cfg.Connectors[0].Name)

assert.Equal(t, "github.com/another2/module4 v0.1.2", cfg.Connectors[1].GoMod)
assert.Equal(t, "github.com/another2/module4", cfg.Connectors[1].Import)
assert.Equal(t, "module4", cfg.Connectors[1].Name)

assert.Equal(t, "github.com/another3/module v0.1.2", cfg.Connectors[2].GoMod)
assert.Equal(t, "github.com/another3/module", cfg.Connectors[2].Import)
assert.Equal(t, "module5", cfg.Connectors[2].Name)
}

func TestParseModules(t *testing.T) {
// prepare
cfg := Config{
Expand Down
9 changes: 5 additions & 4 deletions cmd/builder/internal/builder/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ func TestReplaceStatementsAreComplete(t *testing.T) {
// Configure all components that we want to use elsewhere in these tests.
// This ensures the resulting go.mod file has maximum coverage of modules
// that exist in the Core repository.
usedNames := make(map[string]int)
cfg.Exporters, err = parseModules([]Module{
{
GoMod: "go.opentelemetry.io/collector/exporter/debugexporter v1.9999.9999",
Expand All @@ -376,7 +377,7 @@ func TestReplaceStatementsAreComplete(t *testing.T) {
{
GoMod: "go.opentelemetry.io/collector/exporter/otlphttpexporter v1.9999.9999",
},
})
}, usedNames)
require.NoError(t, err)
cfg.Receivers, err = parseModules([]Module{
{
Expand All @@ -385,13 +386,13 @@ func TestReplaceStatementsAreComplete(t *testing.T) {
{
GoMod: "go.opentelemetry.io/collector/receiver/otlpreceiver v1.9999.9999",
},
})
}, usedNames)
require.NoError(t, err)
cfg.Extensions, err = parseModules([]Module{
{
GoMod: "go.opentelemetry.io/collector/extension/zpagesextension v1.9999.9999",
},
})
}, usedNames)
require.NoError(t, err)
cfg.Processors, err = parseModules([]Module{
{
Expand All @@ -400,7 +401,7 @@ func TestReplaceStatementsAreComplete(t *testing.T) {
{
GoMod: "go.opentelemetry.io/collector/processor/memorylimiterprocessor v1.9999.9999",
},
})
}, usedNames)
require.NoError(t, err)

require.NoError(t, cfg.Validate())
Expand Down

0 comments on commit aa333e9

Please sign in to comment.