Skip to content

Comments

Drop remaining globals from mage settings#12856

Open
swiatekm wants to merge 7 commits intomainfrom
chore/mage/config-no-globals
Open

Drop remaining globals from mage settings#12856
swiatekm wants to merge 7 commits intomainfrom
chore/mage/config-no-globals

Conversation

@swiatekm
Copy link
Contributor

@swiatekm swiatekm commented Feb 19, 2026

What does this PR do?

It gets rid off a bunch of global variables containing configuration from the mage settings. These variables contained information about the repository and working directory. They were read on demand and stored to avoid recomputing them. An example is the agent version, which is read from the version/version.go file. They were all migrated to the Settings struct and loaded on construction. As a nice side effect, the functions no longer return errors, which saves us some boilerplate error checking.

We also had some leftover code from beats supporting community beats which we don't really need, so I deleted it.

The rest of the changes involve passing Settings to functions which now need it. I've also fixed some signatures in magefile.go, where some plain functions were reading the config from context for no good reason. Only actual mage targets should do this.

Why is it important?

Keeping configuration in global variables should be avoided. It makes it difficult to test code and under

Checklist

  • I have read and understood the pull request guidelines of this project.
  • My code follows the style guidelines of this project
  • I have commented my code, particularly in hard-to-understand areas
  • [ ] I have made corresponding changes to the documentation
  • [ ] I have made corresponding change to the default configuration files
  • [ ] I have added tests that prove my fix is effective or that my feature works
  • [ ] I have added an entry in ./changelog/fragments using the changelog tool
  • [ ] I have added an integration test or an E2E test

How to test this PR locally

Related issues

@swiatekm swiatekm added skip-changelog chore Tasks that just need to be done, they are neither bug, nor enhancements backport-active-all Automated backport with mergify to all the active branches labels Feb 19, 2026
Comment on lines -607 to -608
// Use mg.Deps() to ensure that the function will be called only once per mage invocation.
// devtools.Use*Packaging functions are not idempotent as they append in devtools.Packages
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can tell, this isn't true. Both the devtools.Use*Packaging functions override a global variable, so they're idempotent. In any case, I have a follow up which will get rid of the global and make the ambiguity go away.

@swiatekm swiatekm marked this pull request as ready for review February 20, 2026 20:10
@swiatekm swiatekm requested a review from a team as a code owner February 20, 2026 20:10
@elasticmachine
Copy link
Contributor

💛 Build succeeded, but was flaky

Failed CI Steps

History

cc @swiatekm

if err != nil {
panic(err)
}
func MustUsePackaging(specName, specFile string, cfg *Settings) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: AFAICT, cfg is being passed as the first or second argument (when ctx is the first argument). Would be good to maintain this pattern consistently.

actualPkgVersion, err := AgentPackageVersion(cfg)
require.NoError(t, err)
expectedPkgVersion := cfg.BeatQualifiedVersion()
actualPkgVersion := AgentPackageVersion(cfg)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: make this a method on cfg?

if err != nil {
panic(err)
}
func LoadLocalNamedSpec(name string, cfg *Settings) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this function being called from anywhere?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-active-all Automated backport with mergify to all the active branches chore Tasks that just need to be done, they are neither bug, nor enhancements skip-changelog

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants