diff --git a/agent/ls.go b/agent/ls.go index 9e65e26fdd..5c90e5e602 100644 --- a/agent/ls.go +++ b/agent/ls.go @@ -7,6 +7,7 @@ import ( "path/filepath" "regexp" "runtime" + "slices" "strings" "github.com/shirou/gopsutil/v4/disk" @@ -103,6 +104,17 @@ func listFiles(query LSRequest) (LSResponse, error) { }) } + // Sort alphabetically: directories then files + slices.SortFunc(respContents, func(a, b LSFile) int { + if a.IsDir && !b.IsDir { + return -1 + } + if !a.IsDir && b.IsDir { + return 1 + } + return strings.Compare(a.Name, b.Name) + }) + absolutePath := pathToArray(absolutePathString) return LSResponse{ diff --git a/agent/ls_internal_test.go b/agent/ls_internal_test.go index acc4ea2929..0c4e42f2d0 100644 --- a/agent/ls_internal_test.go +++ b/agent/ls_internal_test.go @@ -137,17 +137,18 @@ func TestListFilesSuccess(t *testing.T) { require.NoError(t, err) require.Equal(t, tmpDir, resp.AbsolutePathString) - require.ElementsMatch(t, []LSFile{ - { - Name: "repos", - AbsolutePathString: reposDir, - IsDir: true, - }, + // Output is sorted + require.Equal(t, []LSFile{ { Name: "Downloads", AbsolutePathString: downloadsDir, IsDir: true, }, + { + Name: "repos", + AbsolutePathString: reposDir, + IsDir: true, + }, { Name: "file.txt", AbsolutePathString: textFile,