Skip to content

[Binary Provisioning] Change the default cache dir #4741

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

Closed
wants to merge 1 commit into from

Conversation

codebien
Copy link
Contributor

@codebien codebien commented Apr 24, 2025

What?

It changes the default folder to $HOME/.cache/k6/builds/*.

Why?

The previous k6provider might generate confusion for users, as it doesn't match any installed application.

Checklist

  • I have performed a self-review of my code.
  • I have commented on my code, particularly in hard-to-understand areas.
  • I have added tests for my changes.
  • I have run linter and tests locally (make check) and all pass.

Checklist: Documentation (only for k6 maintainers and if relevant)

Please do not merge this PR until the following items are filled out.

  • I have added the correct milestone and labels to the PR.
  • I have updated the release notes: link
  • I have updated or added an issue to the k6-documentation: grafana/k6-docs#NUMBER if applicable
  • I have updated or added an issue to the TypeScript definitions: grafana/k6-DefinitelyTyped#NUMBER if applicable

Related PR(s)/Issue(s)

@codebien codebien added this to the v1.0.0 milestone Apr 24, 2025
@codebien codebien self-assigned this Apr 24, 2025
@codebien codebien requested a review from a team as a code owner April 24, 2025 17:53
@codebien codebien requested review from mstoykov, inancgumus, pablochacin, joanlopez and a team and removed request for a team, mstoykov and inancgumus April 24, 2025 17:53
Comment on lines 237 to 243
// nolint:forbidigo
// TODO: init this as part of the GlobalState
// and remote it from here
userdir, err := os.UserCacheDir()
if err != nil {
return "", err
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I intend to address this having a field in GlobalState but I want to minimize the changes requires on this pull request.

The next week, I will open a separated pull request that I intend to merge only after we have merged Binary Provisioning.

Copy link
Contributor

Choose a reason for hiding this comment

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

FYI: Here is a draft with that approach #4743

// TODO: init this as part of the GlobalState
// and remote it from here
userdir, err := os.UserCacheDir()
if err != nil {
Copy link
Contributor

@pablochacin pablochacin Apr 25, 2025

Choose a reason for hiding this comment

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

I'm not sure we should return an error here. In the GlobalState when we fail to get the config dir, we default to .config. I prefer to be consistent in how we handle those errors here, mostly because when we move this logic to GlobalState we should do it anyway.

return "", err
}
cacheDir := filepath.Join(userdir, "k6", "builds")
if err := fs.MkdirAll(cacheDir, 0o700); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not necessary, the provider creates the directory if it doesn't exists.

Comment on lines +209 to +213
binaryCacheDir, err := getBinaryCacheDir(gs.FS)
if err != nil {
return nil, fmt.Errorf("failed to acquire the user's cache directory: %w", err)
}

Copy link
Contributor

@pablochacin pablochacin Apr 25, 2025

Choose a reason for hiding this comment

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

Based on my comments in getBinaryCacheDir I think this code could be simplified to this:

Suggested change
binaryCacheDir, err := getBinaryCacheDir(gs.FS)
if err != nil {
return nil, fmt.Errorf("failed to acquire the user's cache directory: %w", err)
}
userdir, err := os.UserCacheDir()
if err != nil {
userdir = ".cache"
gs.Logger.WithError(err).Debug("failed to acquire the user's cache directory)
}
binaryCacheDir := filepath.Join(userdir, "k6", "builds")

Copy link
Contributor

@pablochacin pablochacin Apr 25, 2025

Choose a reason for hiding this comment

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

FYI: I had implemented this approach here also considering overriding with the K6_BINARY_CACHE environment variable. #4742.

t.Run("directory already exists", func(t *testing.T) {
t.Parallel()
fs := testutils.MakeMemMapFs(t, map[string][]byte{
filepath.Join(userDir, "/k6/builds/marker.bin"): []byte(`example binary file`),
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this (and other similar usage in the test) be:

filepath.Join(userDir, "k6", "builds", "marker.bin")

@codebien
Copy link
Contributor Author

Superseded by #4743

@codebien codebien closed this Apr 30, 2025
@mstoykov mstoykov deleted the binpro/change-cache-path branch August 4, 2025 07:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants