Skip to content

Commit 35108ee

Browse files
[8.17] (backport #8279) feat: introduce new mage tidy target (#8294)
* feat: introduce new mage tidy target (#8279) * fix: introduce new mage tidy target * fix: G204: Subprocess launched with a potential tainted input or cmd arguments * doc: add documentation for mage tidy (cherry picked from commit 5b7ee26) # Conflicts: # README.md # dev-tools/mage/target/common/notice.go # dev-tools/mage/target/update/update.go # wrapper/windows/archive-proxy/go.mod # wrapper/windows/archive-proxy/go.sum * fix: resolve conflicts --------- Co-authored-by: Panos Koutsovasilis <[email protected]>
1 parent afe7907 commit 35108ee

File tree

6 files changed

+53
-4
lines changed

6 files changed

+53
-4
lines changed

.ci/scripts/otel-update.sh

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ sed -i.bak "s/\(go\.opentelemetry\.io\/collector.*\) $current_stable_core/\1 $ne
3535
sed -i.bak "s/\(github\.com\/open-telemetry\/opentelemetry\-collector\-contrib\/.*\) $current_contrib/\1 $next_contrib/" go.mod
3636
rm go.mod.bak
3737

38-
echo "=> Running go mod tidy"
39-
go mod tidy
4038
echo "=> Running mage notice"
4139
mage notice
4240
echo "=> Running mage otel:readme"

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,12 @@ rules implemented on our `Makefile` as well as CI will use the
258258
locally before submitting any PRs to have a quicker feedback instead
259259
of waiting for a CI failure.
260260

261+
### Keeping Go module files tidy
262+
The Elastic Agent repository includes additional Go modules (e.g. `wrapper/windows/archive-proxy`) that import the main `elastic-agent` module. This requires keeping all `go.mod` files in sync whenever dependencies change. There is a dedicated `mage tidy` target that recursively runs `go mod tidy` across the entire repo. This is now handled automatically in the following targets:
263+
- `mage check`
264+
- `mage notice`
265+
- `mage update`
266+
261267
### Generating the `NOTICE.txt` when updating/adding dependencies
262268
To do so, just run `make notice`, this is also part of the `make
263269
check-ci` and is the same check our CI will do.

dev-tools/mage/target/common/check.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ func Check() {
2323
deps := make([]interface{}, 0, len(checkDeps)+2)
2424
deps = append(deps, devtools.Format)
2525
deps = append(deps, checkDeps...)
26+
deps = append(deps, Tidy)
2627
deps = append(deps, devtools.Check)
2728
mg.SerialDeps(deps...)
2829
}

dev-tools/mage/target/common/notice.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func runCommand(cmd string, args ...string) error {
2929
// Notice Generates NOTICE.txt.
3030
func Notice() (err error) {
3131
fmt.Println("Generating NOTICE")
32-
if err := runCommand("go", "mod", "tidy"); err != nil {
32+
if err := Tidy(); err != nil {
3333
return err
3434
}
3535
if err := runCommand("go", "mod", "download"); err != nil {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
2+
// or more contributor license agreements. Licensed under the Elastic License 2.0;
3+
// you may not use this file except in compliance with the Elastic License 2.0.
4+
5+
package common
6+
7+
import (
8+
"fmt"
9+
"os"
10+
"os/exec"
11+
"path/filepath"
12+
13+
"github.com/magefile/mage/mg"
14+
15+
devtools "github.com/elastic/elastic-agent/dev-tools/mage"
16+
)
17+
18+
// Tidy runs go mod tidy on all go.mod recursively inside the elastic-agent repository.
19+
func Tidy() error {
20+
goModFiles, err := devtools.FindFilesRecursive(func(path string, _ os.FileInfo) bool {
21+
return filepath.Base(path) == "go.mod"
22+
})
23+
if err != nil {
24+
return err
25+
}
26+
for _, file := range goModFiles {
27+
dir, err := filepath.Abs(filepath.Dir(file))
28+
if err != nil {
29+
return fmt.Errorf("tidy: error getting absolute dir: %w", err)
30+
}
31+
fmt.Printf(">> tidy: Running go mod tidy inside %s\n", dir)
32+
cmd := exec.Command(mg.GoCmd(), "mod", "tidy", "-v") // #nosec G204 -- Need to pass in name of package
33+
cmd.Dir = dir
34+
cmd.Stdout = os.Stdout
35+
cmd.Stderr = os.Stderr
36+
err = cmd.Run()
37+
if err != nil {
38+
return fmt.Errorf("tidy: error running go mod tidy: %w", err)
39+
}
40+
}
41+
return nil
42+
}

dev-tools/mage/target/update/update.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
package update
66

7-
import "github.com/magefile/mage/sh"
7+
import (
8+
"github.com/magefile/mage/sh"
9+
)
810

911
// Update updates the generated files (aka make update).
1012
func Update() error {

0 commit comments

Comments
 (0)