Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions internal/templater/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func init() {
"IsSH": IsSH, // Deprecated
"joinPath": filepath.Join,
"relPath": filepath.Rel,
"resolvePath": resolvePath,
Copy link
Contributor

Choose a reason for hiding this comment

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

Is more granular approach better?

absPath: filepath.Abs
evalSymlinks: filepathEvalSymlinks

Copy link
Author

@SergioChan SergioChan Mar 12, 2026

Choose a reason for hiding this comment

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

Great question! I intentionally kept this as a single resolvePath helper because issue #2681 is about resolving template paths to a final usable absolute path, and exposing one focused function keeps the template surface smaller.

Current behavior is effectively:

  1. filepath.Abs(...)
  2. best-effort filepath.EvalSymlinks(...)
  3. fallback to the absolute path if symlink evaluation fails

If you prefer explicit absPath and evalSymlinks helpers instead, I can split this PR that way.

Copy link
Contributor

Choose a reason for hiding this comment

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

The general approach has been to add individual functions. I think its more useful.

"merge": merge,
"spew": spew.Sdump,
"fromYaml": fromYaml,
Expand Down Expand Up @@ -89,6 +90,20 @@ func splitArgs(s string) ([]string, error) {
return shell.Fields(s, nil)
}

func resolvePath(path string) (string, error) {
absPath, err := filepath.Abs(path)
if err != nil {
return "", err
}

resolvedPath, err := filepath.EvalSymlinks(absPath)
if err != nil {
return absPath, nil
}

return resolvedPath, nil
}

// Deprecated: now always returns true
func IsSH() bool {
return true
Expand Down
57 changes: 57 additions & 0 deletions internal/templater/funcs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package templater

import (
"os"
"path/filepath"
"testing"
)

func TestResolvePathCleansRelativeSegments(t *testing.T) {
t.Parallel()

tmpDir := t.TempDir()
input := filepath.Join(tmpDir, "foo", "..", "bar")

resolved, err := resolvePath(input)
if err != nil {
t.Fatalf("resolvePath returned error: %v", err)
}

expected, err := filepath.Abs(filepath.Join(tmpDir, "bar"))
if err != nil {
t.Fatalf("filepath.Abs returned error: %v", err)
}

if resolved != expected {
t.Fatalf("expected %q, got %q", expected, resolved)
}
}

func TestResolvePathResolvesSymlink(t *testing.T) {
t.Parallel()

tmpDir := t.TempDir()
targetDir := filepath.Join(tmpDir, "target")
if err := os.Mkdir(targetDir, 0o755); err != nil {
t.Fatalf("mkdir target: %v", err)
}

link := filepath.Join(tmpDir, "link")
if err := os.Symlink(targetDir, link); err != nil {
t.Skipf("symlinks not supported in this environment: %v", err)
}

resolved, err := resolvePath(filepath.Join(link, "..", "link"))
if err != nil {
t.Fatalf("resolvePath returned error: %v", err)
}

expected, err := filepath.Abs(targetDir)
if err != nil {
t.Fatalf("filepath.Abs returned error: %v", err)
}

if resolved != expected {
t.Fatalf("expected symlink to resolve to %q, got %q", expected, resolved)
}
}
1 change: 1 addition & 0 deletions website/src/docs/reference/templating.md
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,7 @@ tasks:
- echo "{{.WIN_PATH | fromSlash}}" # Convert to OS-specific slashes
- echo "{{joinPath .OUTPUT_DIR .BINARY_NAME}}" # Join path elements
- echo "Relative {{relPath .ROOT_DIR .TASKFILE_DIR}}" # Get relative path
- echo "{{resolvePath "../dist/../build"}}" # Resolve to an absolute path (follows symlinks when available)
```

### Data Structure Functions
Expand Down