Skip to content

Commit 39462cb

Browse files
authored
feat: change XDG taskrc naming (#2391)
1 parent 72dfec6 commit 39462cb

File tree

4 files changed

+47
-15
lines changed

4 files changed

+47
-15
lines changed

taskrc/node.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ type Node struct {
1111
func NewNode(
1212
entrypoint string,
1313
dir string,
14+
possibleFileNames []string,
1415
) (*Node, error) {
1516
dir = fsext.DefaultDir(entrypoint, dir)
16-
resolvedEntrypoint, err := fsext.SearchPath(dir, defaultTaskRCs)
17+
resolvedEntrypoint, err := fsext.SearchPath(dir, possibleFileNames)
1718
if err != nil {
1819
return nil, err
1920
}

taskrc/taskrc.go

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,22 @@ import (
44
"os"
55
"path/filepath"
66
"slices"
7+
"strings"
78

89
"github.com/go-task/task/v3/internal/fsext"
910
"github.com/go-task/task/v3/taskrc/ast"
1011
)
1112

12-
var defaultTaskRCs = []string{
13-
".taskrc.yml",
14-
".taskrc.yaml",
15-
}
13+
var (
14+
defaultXDGTaskRCs = []string{
15+
"taskrc.yml",
16+
"taskrc.yaml",
17+
}
18+
defaultTaskRCs = []string{
19+
".taskrc.yml",
20+
".taskrc.yaml",
21+
}
22+
)
1623

1724
// GetConfig loads and merges local and global Task configuration files
1825
func GetConfig(dir string) (*ast.TaskRC, error) {
@@ -21,12 +28,31 @@ func GetConfig(dir string) (*ast.TaskRC, error) {
2128

2229
// Read the XDG config file
2330
if xdgConfigHome := os.Getenv("XDG_CONFIG_HOME"); xdgConfigHome != "" {
24-
xdgConfigNode, err := NewNode("", filepath.Join(xdgConfigHome, "task"))
31+
xdgConfigNode, err := NewNode("", filepath.Join(xdgConfigHome, "task"), defaultXDGTaskRCs)
2532
if err == nil && xdgConfigNode != nil {
26-
config, err = reader.Read(xdgConfigNode)
33+
xdgConfig, err := reader.Read(xdgConfigNode)
2734
if err != nil {
2835
return nil, err
2936
}
37+
config = xdgConfig
38+
}
39+
}
40+
41+
// If the current path does not contain $HOME
42+
// If it does contain $HOME, then we will find this config later anyway
43+
home, err := os.UserHomeDir()
44+
if err == nil && !strings.Contains(home, dir) {
45+
homeNode, err := NewNode("", home, defaultTaskRCs)
46+
if err == nil && homeNode != nil {
47+
homeConfig, err := reader.Read(homeNode)
48+
if err != nil {
49+
return nil, err
50+
}
51+
if config == nil {
52+
config = homeConfig
53+
} else {
54+
config.Merge(homeConfig)
55+
}
3056
}
3157
}
3258

@@ -41,7 +67,7 @@ func GetConfig(dir string) (*ast.TaskRC, error) {
4167

4268
// Loop over the nodes, and merge them into the main config
4369
for _, entrypoint := range entrypoints {
44-
node, err := NewNode("", entrypoint)
70+
node, err := NewNode("", entrypoint, defaultTaskRCs)
4571
if err != nil {
4672
return nil, err
4773
}

taskrc/taskrc_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func TestGetConfig_NoConfigFiles(t *testing.T) { //nolint:paralleltest // cannot
6666
func TestGetConfig_OnlyXDG(t *testing.T) { //nolint:paralleltest // cannot run in parallel
6767
xdgDir, _, localDir := setupDirs(t)
6868

69-
writeFile(t, xdgDir, ".taskrc.yml", xdgConfigYAML)
69+
writeFile(t, xdgDir, "taskrc.yml", xdgConfigYAML)
7070

7171
cfg, err := GetConfig(localDir)
7272
assert.NoError(t, err)
@@ -121,7 +121,7 @@ func TestGetConfig_All(t *testing.T) { //nolint:paralleltest // cannot run in pa
121121
writeFile(t, homeDir, ".taskrc.yml", homeConfigYAML)
122122

123123
// Write XDG config
124-
writeFile(t, xdgConfigDir, ".taskrc.yml", xdgConfigYAML)
124+
writeFile(t, xdgConfigDir, "taskrc.yml", xdgConfigYAML)
125125

126126
cfg, err := GetConfig(localDir)
127127
assert.NoError(t, err)

website/src/docs/reference/config.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,29 @@ files.
1919

2020
## File Precedence
2121

22-
Task's configuration files are named `.taskrc.yml` or `.taskrc.yaml`. Task will
23-
automatically look for directories containing files with these names in the
24-
following order with the highest priority first:
22+
Task will automatically look for directories containing configuration files in
23+
the following order with the highest priority first:
2524

2625
- Current directory (or the one specified by the `--taskfile`/`--entrypoint`
2726
flags).
2827
- Each directory walking up the file tree from the current directory (or the one
2928
specified by the `--taskfile`/`--entrypoint` flags) until we reach the user's
3029
home directory or the root directory of that drive.
31-
- `$XDG_CONFIG_HOME/task`.
30+
- The users `$HOME` directory.
31+
- The `$XDG_CONFIG_HOME/task` directory.
32+
33+
Config files in the current directory, its parent folders or home directory
34+
should be called `.taskrc.yml` or `.taskrc.yaml`. Config files in the
35+
`$XDG_CONFIG_HOME/task` directory are named the same way, but should not contain
36+
the `.` prefix.
3237

3338
All config files will be merged together into a unified config, starting with
3439
the lowest priority file in `$XDG_CONFIG_HOME/task` with each subsequent file
3540
overwriting the previous one if values are set.
3641

3742
For example, given the following files:
3843

39-
```yaml [$XDG_CONFIG_HOME/task/.taskrc.yml]
44+
```yaml [$XDG_CONFIG_HOME/task/taskrc.yml]
4045
# lowest priority global config
4146
option_1: foo
4247
option_2: foo

0 commit comments

Comments
 (0)