-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[builder] Support for --skip-new-go-module #10098
Merged
mx-psi
merged 4 commits into
open-telemetry:main
from
kristinapathak:builder-skip-go-mod
Aug 22, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: enhancement | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver) | ||
component: builder | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Add a --skip-new-go-module flag to skip creating a module in the output directory. | ||
|
||
# One or more tracking issues or pull requests related to the change | ||
issues: [9252] | ||
|
||
# (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: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,6 @@ | |
"slices" | ||
"strings" | ||
"text/template" | ||
"time" | ||
|
||
"go.uber.org/zap" | ||
"golang.org/x/mod/modfile" | ||
|
@@ -25,7 +24,6 @@ | |
ErrGoNotFound = errors.New("go binary not found") | ||
ErrDepNotFound = errors.New("dependency not found in go mod file") | ||
ErrVersionMismatch = errors.New("mismatch in go.mod and builder configuration versions") | ||
errDownloadFailed = errors.New("failed to download go modules") | ||
errCompileFailed = errors.New("failed to compile the OpenTelemetry Collector distribution") | ||
skipStrictMsg = "Use --skip-strict-versioning to temporarily disable this check. This flag will be removed in a future minor version" | ||
) | ||
|
@@ -86,18 +84,30 @@ | |
return fmt.Errorf("failed to create output path: %w", err) | ||
} | ||
|
||
for _, tmpl := range []*template.Template{ | ||
allTemplates := []*template.Template{ | ||
mainTemplate, | ||
mainOthersTemplate, | ||
mainWindowsTemplate, | ||
componentsTemplate, | ||
goModTemplate, | ||
} { | ||
} | ||
|
||
// Add the go.mod template unless that file is skipped. | ||
if !cfg.SkipNewGoModule { | ||
allTemplates = append(allTemplates, goModTemplate) | ||
} | ||
|
||
for _, tmpl := range allTemplates { | ||
if err := processAndWrite(cfg, tmpl, tmpl.Name(), cfg); err != nil { | ||
return fmt.Errorf("failed to generate source file %q: %w", tmpl.Name(), err) | ||
} | ||
} | ||
|
||
// when not creating a new go.mod file, update modules one-by-one in the | ||
// enclosing go module. | ||
if err := cfg.updateModules(); err != nil { | ||
return err | ||
} | ||
|
||
cfg.Logger.Info("Sources created", zap.String("path", cfg.Distribution.OutputPath)) | ||
return nil | ||
} | ||
|
@@ -145,7 +155,7 @@ | |
} | ||
|
||
if cfg.SkipStrictVersioning { | ||
return downloadModules(cfg) | ||
return nil | ||
} | ||
|
||
// Perform strict version checking. For each component listed and the | ||
|
@@ -185,22 +195,7 @@ | |
} | ||
} | ||
|
||
return downloadModules(cfg) | ||
} | ||
|
||
func downloadModules(cfg Config) error { | ||
cfg.Logger.Info("Getting go modules") | ||
failReason := "unknown" | ||
for i := 1; i <= cfg.downloadModules.numRetries; i++ { | ||
if _, err := runGoCommand(cfg, "mod", "download"); err != nil { | ||
failReason = err.Error() | ||
cfg.Logger.Info("Failed modules download", zap.String("retry", fmt.Sprintf("%d/%d", i, cfg.downloadModules.numRetries))) | ||
time.Sleep(cfg.downloadModules.wait) | ||
continue | ||
} | ||
return nil | ||
} | ||
return fmt.Errorf("%w: %s", errDownloadFailed, failReason) | ||
return nil | ||
} | ||
|
||
func processAndWrite(cfg Config, tmpl *template.Template, outFile string, tmplParams any) error { | ||
|
@@ -226,6 +221,68 @@ | |
c.Extensions, c.Connectors, *c.Providers) | ||
} | ||
|
||
func (c *Config) updateModules() error { | ||
if !c.SkipNewGoModule { | ||
return nil | ||
} | ||
|
||
// Build the main service dependency | ||
coremod, corever := c.coreModuleAndVersion() | ||
corespec := coremod + " " + corever | ||
|
||
if err := c.updateGoModule(corespec); err != nil { | ||
return err | ||
} | ||
|
||
for _, comp := range c.allComponents() { | ||
if err := c.updateGoModule(comp.GoMod); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (c *Config) updateGoModule(modspec string) error { | ||
mod, ver, found := strings.Cut(modspec, " ") | ||
if !found { | ||
return fmt.Errorf("ill-formatted modspec %q: missing space separator", modspec) | ||
} | ||
|
||
// Re-parse the go.mod file on each iteration, since it can | ||
// change each time. | ||
modulePath, dependencyVersions, err := c.readGoModFile() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if mod == modulePath { | ||
// this component is part of the same module, nothing to update. | ||
return nil | ||
} | ||
|
||
// check for exact match | ||
hasVer, ok := dependencyVersions[mod] | ||
if ok && hasVer == ver { | ||
c.Logger.Info("Component version match", zap.String("module", mod), zap.String("version", ver)) | ||
return nil | ||
} | ||
|
||
scomp := semver.Compare(hasVer, ver) | ||
if scomp > 0 { | ||
// version in enclosing module is newer, do not change | ||
c.Logger.Info("Not upgrading component, enclosing module is newer.", zap.String("module", mod), zap.String("existing", hasVer), zap.String("config_version", ver)) | ||
return nil | ||
} | ||
|
||
// upgrading or changing version | ||
updatespec := "-require=" + mod + "@" + ver | ||
|
||
if _, err := runGoCommand(*c, "mod", "edit", updatespec); err != nil { | ||
return err | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I couldn't figure out how to get |
||
} | ||
return nil | ||
} | ||
|
||
func (c *Config) readGoModFile() (string, map[string]string, error) { | ||
var modPath string | ||
stdout, err := runGoCommand(*c, "mod", "edit", "-print") | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
go mod tidy
is run a few lines above and downloads the needed dependencies, sogo mod download
isn't needed. No dependencies are updated after the tidy call.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, this comment was useful!