Skip to content

Commit

Permalink
cmd/cue: use CUE_CACHE_DIR instead of CUE_MODCACHE
Browse files Browse the repository at this point in the history
We borrowed CUE_MODCACHE from Go's GOMODCACHE,
as a temporary place on disk to cache downloaded CUE modules.

CUE_MODCACHE works fine on its own, but there is a high probability
that we will want to cache other files in the future, much like Go
started with a build cache and later added test and modules caches.

Rather than needing to add more environment variables in the future
for other types of cached data we might want to support,
use CUE_CACHE_DIR as a parent directory for all CUE cache directories,
and place the module cache at ${CUE_CACHE_DIR}/mod.
We choose an abbreviated name for the directory as it is already
common elsewhere in CUE, and it keeps paths a little bit shorter.

This also mirrors CUE_CONFIG_DIR, which is also designed this way
as a parent directory to hold any number of future configuration files.

This is technically a breaking change, but CUE_MODCACHE was only just
introduced in v0.8.0-alpha.1, so we can change it for v0.8.0-alpha.2.

Centralize getting CUE_CONFIG_DIR and CUE_CACHE_DIR with their defaults
into common.go as well, for easier reuse, and use sync.OnceValues
since we only need to call Getenv and UserConfigDir/UserCacheDir once.

While here, I noticed that `cue help environment` had a tiny mistake
in the documentation of CUE_CONFIG_DIR, as on MacOS it lacked
the "cue" base directory at the end.

Fixes #2833.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: Ib1c908d6b6e0ec224931ddd9007e91963348e712
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1177047
Reviewed-by: Roger Peppe <rogpeppe@gmail.com>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@gmail.com>
  • Loading branch information
mvdan committed Feb 20, 2024
1 parent c1e1a52 commit 8bc8b1e
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 29 deletions.
24 changes: 24 additions & 0 deletions cmd/cue/cmd/common.go
Expand Up @@ -16,12 +16,14 @@ package cmd

import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"sync"

"github.com/spf13/pflag"
"golang.org/x/text/language"
Expand Down Expand Up @@ -846,3 +848,25 @@ func shortFile(root string, f *build.File) string {
}
return dir
}

var cueConfigDir = sync.OnceValues(func() (string, error) {
if dir := os.Getenv("CUE_CONFIG_DIR"); dir != "" {
return dir, nil
}
dir, err := os.UserConfigDir()
if err != nil {
return "", fmt.Errorf("cannot determine system config directory: %v", err)
}
return filepath.Join(dir, "cue"), nil
})

var cueCacheDir = sync.OnceValues(func() (string, error) {
if dir := os.Getenv("CUE_CACHE_DIR"); dir != "" {
return dir, nil
}
dir, err := os.UserCacheDir()
if err != nil {
return "", fmt.Errorf("cannot determine system cache directory: %v", err)
}
return filepath.Join(dir, "cue"), nil
})
16 changes: 9 additions & 7 deletions cmd/cue/cmd/help.go
Expand Up @@ -159,9 +159,17 @@ setting.
This defaults to a directory for user-specific configuration data, such as:
"$XDG_CONFIG_HOME/cue" or "$HOME/.config/cue" on Linux
"$HOME/Library/Application Support" on MacOS
"$HOME/Library/Application Support/cue" on MacOS
"%AppData%/cue" on Windows
CUE_CACHE_DIR
The directory where the cue command keeps a cache of files to be reused.
This defaults to a directory for user-specific temporary cache data, such as:
"$XDG_CACHE_HOME/cue" or "$HOME/.cache/cue" on Linux
"$HOME/Library/Caches/cue" on MacOS
"%LocalAppData%/cue" on Windows
CUE_REGISTRY
A comma-separated list specifying which registry to use for
downloading and publishing modules. A registry is specifed
Expand Down Expand Up @@ -190,12 +198,6 @@ setting.
Requires that CUE_EXPERIMENT=modules is enabled.
CUE_MODCACHE
The directory where the cue command will store downloaded
modules.
Requires that CUE_EXPERIMENT=modules is enabled.
CUE_EXPERIMENT
Comma-separated list of experiments to enable or disable.
The list of available experiments may change arbitrarily over
Expand Down
11 changes: 3 additions & 8 deletions cmd/cue/cmd/login.go
Expand Up @@ -142,14 +142,9 @@ func registryOAuthConfig(host string) oauth2.Config {
// changed between reading and writing the file.

func findLoginsPath() (string, error) {
configDir := os.Getenv("CUE_CONFIG_DIR")
if configDir == "" {
var err error
configDir, err = os.UserConfigDir()
if err != nil {
return "", err
}
configDir = filepath.Join(configDir, "cue")
configDir, err := cueConfigDir()
if err != nil {
return "", err
}
return filepath.Join(configDir, "logins.json"), nil
}
Expand Down
10 changes: 3 additions & 7 deletions cmd/cue/cmd/modtidy.go
Expand Up @@ -126,13 +126,9 @@ func findModuleRoot() (string, error) {
}

func modCacheDir() (string, error) {
if dir := os.Getenv("CUE_MODCACHE"); dir != "" {
return dir, nil
}
sysCacheDir, err := os.UserCacheDir()
cacheDir, err := cueCacheDir()
if err != nil {
return "", fmt.Errorf("cannot determine system cache directory: %v", err)
return "", err
}
// TODO rethink cache namespace as per comments in https://review.gerrithub.io/c/cue-lang/cue/+/1173535/18
return filepath.Join(sysCacheDir, "cue"), nil
return filepath.Join(cacheDir, "mod"), nil
}
2 changes: 1 addition & 1 deletion cmd/cue/cmd/script_test.go
Expand Up @@ -220,7 +220,7 @@ func TestScript(t *testing.T) {
// While os.UserCacheDir on Linux is derived from $HOME,
// under Windows it's not, so avoid polluting the system cache
// directory by setting up the cache specifically.
"CUE_MODCACHE="+filepath.Join(e.WorkDir, "tmp/cache"),
"CUE_CACHE_DIR="+filepath.Join(e.WorkDir, "tmp/cache"),
)
e.Defer(reg.Close)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/cue/cmd/testdata/script/registry_auth.txtar
Expand Up @@ -6,7 +6,7 @@ exec cue export .
cmp stdout expect-stdout

# Sanity-check that we get an error when using the wrong password.
env CUE_MODCACHE=$WORK/.tmp/different-cache
env CUE_CACHE_DIR=$WORK/.tmp/different-cache
env-fill dockerconfig/badpassword.json
cp dockerconfig/badpassword.json dockerconfig/config.json
! exec cue export .
Expand Down
2 changes: 1 addition & 1 deletion cmd/cue/cmd/testdata/script/registry_auth_logins.txtar
Expand Up @@ -7,7 +7,7 @@ exec cue export .
cmp stdout expect-stdout

# Sanity-check that we get an error when using the wrong token.
env CUE_MODCACHE=$WORK/.tmp/different-cache
env CUE_CACHE_DIR=$WORK/.tmp/different-cache
env-fill cueconfig/badtoken.json
cp cueconfig/badtoken.json cueconfig/logins.json
! exec cue export .
Expand Down
2 changes: 1 addition & 1 deletion cmd/cue/cmd/testdata/script/registry_publish.txtar
Expand Up @@ -11,7 +11,7 @@ cmp stdout ../expect-eval-stdout

# Sanity check that the module isn't present in the fallback registry.
env CUE_REGISTRY=$ORIG_CUE_REGISTRY
env CUE_MODCACHE=$WORK/.tmp/different-cache
env CUE_CACHE_DIR=$WORK/.tmp/different-cache
! exec cue eval
stderr 'repository name not known to registry'

Expand Down
2 changes: 1 addition & 1 deletion cmd/cue/cmd/testdata/script/registry_publish_auth.txtar
Expand Up @@ -3,7 +3,7 @@

memregistry -auth=foo:bar MEMREGISTRY
env CUE_EXPERIMENT=modules
env CUE_MODCACHE=$WORK/.tmp/cache
env CUE_CACHE_DIR=$WORK/.tmp/cache
env CUE_REGISTRY=$MEMREGISTRY+insecure
env DOCKER_CONFIG=$WORK/dockerconfig
env-fill $DOCKER_CONFIG/config.json
Expand Down
Expand Up @@ -3,7 +3,7 @@

memregistry MEMREGISTRY
env CUE_EXPERIMENT=modules
env CUE_MODCACHE=$WORK/.tmp/cache
env CUE_CACHE_DIR=$WORK/.tmp/cache
env CUE_REGISTRY=$MEMREGISTRY+insecure

cd example/foo
Expand Down
2 changes: 1 addition & 1 deletion internal/e2e/script_test.go
Expand Up @@ -98,7 +98,7 @@ func TestScript(t *testing.T) {
env.Setenv("CUE_REGISTRY_TOKEN", os.Getenv("CUE_REGISTRY_TOKEN"))

// Just like cmd/cue/cmd.TestScript, set up separate cache and config dirs per test.
env.Setenv("CUE_MODCACHE", filepath.Join(env.WorkDir, "tmp/modcache"))
env.Setenv("CUE_CACHE_DIR", filepath.Join(env.WorkDir, "tmp/cachedir"))
configDir := filepath.Join(env.WorkDir, "tmp/configdir")
env.Setenv("CUE_CONFIG_DIR", configDir)

Expand Down

0 comments on commit 8bc8b1e

Please sign in to comment.