Skip to content

Commit b79115a

Browse files
committed
core(workspace): resolve as-sdk paths against the config directory
The as-sdk entries are the last paths in dagger.toml still measured from the workspace root while everything around them — [modules.<name>].source, an [[include]], a path typed at the CLI — is measured from the config that declares it. Give them the same resolver: a leading "/" anchors at the workspace root, anything else is relative to the directory holding the dagger.toml, and an escape is refused. As-sdk entries are always paths, so unlike a module source they are never classified as refs first. The helpers are unused until the engine adopts them in the next patch. Signed-off-by: Yves Brissaud <[email protected]>
1 parent 9861c8c commit b79115a

2 files changed

Lines changed: 97 additions & 0 deletions

File tree

‎core/workspace/config.go‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package workspace
22

33
import (
44
"fmt"
5+
"path"
56
"path/filepath"
67
"reflect"
78
"sort"
@@ -137,6 +138,47 @@ func ResolveModuleEntrySource(configDir, source string) string {
137138
return filepath.Clean(filepath.Join(configDir, source))
138139
}
139140

141+
// ResolveSDKManagedPath turns an as-sdk path into the workspace-relative path
142+
// the engine addresses modules and clients by, following the same rule as every
143+
// other path a workspace resolves: a leading "/" means the workspace root,
144+
// anything else is relative to the directory of the config that records it, and
145+
// escaping the root is refused. Unlike ResolveModuleEntrySource these entries
146+
// are always paths, never refs, so no ref classification happens here.
147+
func ResolveSDKManagedPath(configDir, p string) (string, error) {
148+
clean := path.Clean(filepath.ToSlash(p))
149+
var resolved string
150+
if path.IsAbs(clean) {
151+
resolved = strings.TrimPrefix(clean, "/")
152+
} else {
153+
resolved = path.Join(filepath.ToSlash(configDir), clean)
154+
}
155+
resolved = path.Clean(resolved)
156+
if resolved == "" {
157+
resolved = "."
158+
}
159+
if resolved != "." && !filepath.IsLocal(filepath.FromSlash(resolved)) {
160+
return "", fmt.Errorf("%q escapes the workspace root", p)
161+
}
162+
return resolved, nil
163+
}
164+
165+
// SDKManagedPathFor is the inverse of ResolveSDKManagedPath: it expresses a
166+
// workspace-relative path the way an as-sdk entry records it. A target outside
167+
// the config directory keeps a "../" prefix, as its install source would.
168+
func SDKManagedPathFor(configDir, workspacePath string) (string, error) {
169+
if configDir == "" {
170+
configDir = "."
171+
}
172+
if workspacePath == "" {
173+
workspacePath = "."
174+
}
175+
rel, err := filepath.Rel(configDir, filepath.Clean(workspacePath))
176+
if err != nil {
177+
return "", fmt.Errorf("resolve %q from %q: %w", workspacePath, configDir, err)
178+
}
179+
return filepath.ToSlash(rel), nil
180+
}
181+
140182
// ParseConfig parses dagger.toml bytes into a workspace config.
141183
func ParseConfig(data []byte) (*Config, error) {
142184
var cfg Config

‎core/workspace/config_test.go‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,61 @@ func TestResolveModuleEntrySource(t *testing.T) {
935935
})
936936
}
937937

938+
func TestSDKManagedPaths(t *testing.T) {
939+
t.Parallel()
940+
941+
mustResolve := func(t *testing.T, configDir, p string) string {
942+
t.Helper()
943+
resolved, err := ResolveSDKManagedPath(configDir, p)
944+
require.NoError(t, err)
945+
return resolved
946+
}
947+
948+
t.Run("resolves against the config directory", func(t *testing.T) {
949+
t.Parallel()
950+
require.Equal(t, "apps/demo/.dagger/modules/greeter", mustResolve(t, "apps/demo", ".dagger/modules/greeter"))
951+
require.Equal(t, "apps/demo", mustResolve(t, "apps/demo", "."))
952+
require.Equal(t, "apps/shared", mustResolve(t, "apps/demo", "../shared"))
953+
require.Equal(t, ".dagger/modules/greeter", mustResolve(t, ".", ".dagger/modules/greeter"))
954+
})
955+
956+
t.Run("a leading slash anchors at the workspace root", func(t *testing.T) {
957+
t.Parallel()
958+
require.Equal(t, "tools/greeter", mustResolve(t, "apps/demo", "/tools/greeter"))
959+
require.Equal(t, ".", mustResolve(t, "apps/demo", "/"))
960+
})
961+
962+
t.Run("refuses to escape the workspace root", func(t *testing.T) {
963+
t.Parallel()
964+
_, err := ResolveSDKManagedPath("apps/demo", "../../../outside")
965+
require.ErrorContains(t, err, "escapes the workspace root")
966+
})
967+
968+
t.Run("treats every entry as a path, never a ref", func(t *testing.T) {
969+
t.Parallel()
970+
// ResolveModuleEntrySource would hand this back untouched, reading the
971+
// dot as a domain; as-sdk entries are always paths.
972+
require.Equal(t, "apps/demo/modules/v1.2", mustResolve(t, "apps/demo", "modules/v1.2"))
973+
})
974+
975+
t.Run("records workspace paths relative to the config directory", func(t *testing.T) {
976+
t.Parallel()
977+
for _, tc := range []struct{ configDir, workspacePath, want string }{
978+
{"apps/demo", "apps/demo/.dagger/modules/greeter", ".dagger/modules/greeter"},
979+
{"apps/demo", "apps/demo", "."},
980+
{"apps/demo", "apps/shared", "../shared"},
981+
{".", "modules/greeter", "modules/greeter"},
982+
{"", "modules/greeter", "modules/greeter"},
983+
} {
984+
got, err := SDKManagedPathFor(tc.configDir, tc.workspacePath)
985+
require.NoError(t, err)
986+
require.Equal(t, tc.want, got)
987+
require.Equal(t, cleanRelPath(tc.workspacePath), mustResolve(t, tc.configDir, got),
988+
"round trip through %q", tc.configDir)
989+
}
990+
})
991+
}
992+
938993
func TestDeleteConfigValue(t *testing.T) {
939994
t.Parallel()
940995

0 commit comments

Comments
 (0)