Skip to content

Commit 81e1c30

Browse files
committed
core: keep explicit Git URL form when reloading module sources
A module loaded from an explicit Git URL such as https://host/team/project.git#main:ci can come back as the legacy form https://host/team/project.git/ci@main. The legacy form no longer says where the repository ends, so the parser falls back to import-path discovery. That discovery is unauthenticated and fails for private self-managed servers. Two places lose the form: - Workspace-backed Git modules never set their selector, so AsString, which nested clients reload, prints the legacy form. - A local dependency of a Git module always rebuilds its ref with GitRefString, whatever form the parent used. A version-shaped ref like v1.2 is then read as a version query. Mark workspace sources whose clone URL has a scheme as explicit Git URLs, and move the choice between the two forms into GitModuleSource.RefString, which AsString and dependency resolution now share. SCP-style refs (git@host:path) have no scheme and keep the legacy form. TestGitLocalDependencyPreservesSelector fails on main and passes here. TestGitWorkspaceModuleSourcePreservesKind now also checks that AsString parses back to the same clone URL, subpath and selector. Signed-off-by: Guillaume de Rouville <[email protected]>
1 parent ce6dc9f commit 81e1c30

4 files changed

Lines changed: 92 additions & 6 deletions

File tree

‎core/integration/workspace_api_test.go‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
"dagger.io/dagger"
2323
"dagger.io/dagger/core"
24+
"github.com/dagger/dagger/core/gitref"
2425
"github.com/dagger/dagger/internal/testutil"
2526
"github.com/dagger/testctx"
2627
"github.com/stretchr/testify/require"
@@ -1598,6 +1599,15 @@ engineVersion = "latest"
15981599
name, err := src.ModuleOriginalName(ctx)
15991600
require.NoError(t, err)
16001601
require.Equal(t, "git-module", name)
1602+
// Nested clients reload this string: it must parse back to the same
1603+
// repository and subpath without import-path discovery.
1604+
ref, err := src.AsString(ctx)
1605+
require.NoError(t, err)
1606+
parsed, err := gitref.Parse(ctx, ref)
1607+
require.NoError(t, err)
1608+
require.Equal(t, repoURL, parsed.CloneRef)
1609+
require.Equal(t, "module", parsed.RepoRootSubdir)
1610+
require.Equal(t, gitref.GitRefSelector, parsed.Selector)
16011611
}
16021612

16031613
func (WorkspaceAPISuite) TestGitWorkspaceModuleSourcePrivateSSH(ctx context.Context, t *testctx.T) {

‎core/modulesource.go‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,10 +1045,7 @@ func (src *ModuleSource) AsString() string {
10451045
if version == "" {
10461046
version = src.Git.Version
10471047
}
1048-
if src.Git.Selector == gitref.GitRefSelector {
1049-
return gitref.GitURLRefString(src.Git.CloneRef, src.SourceRootSubpath, version)
1050-
}
1051-
return GitRefString(src.Git.CloneRef, src.SourceRootSubpath, version)
1048+
return src.Git.RefString(src.SourceRootSubpath, version)
10521049

10531050
default:
10541051
return ""
@@ -2117,6 +2114,15 @@ func (src GitModuleSource) Link(filepath string, line int, column int) (string,
21172114
}
21182115
}
21192116

2117+
// RefString builds a ref string for a path in this repository, in the same
2118+
// syntax the source was loaded with.
2119+
func (src GitModuleSource) RefString(sourceRootSubpath, version string) string {
2120+
if src.Selector == gitref.GitRefSelector {
2121+
return gitref.GitURLRefString(src.CloneRef, sourceRootSubpath, version)
2122+
}
2123+
return GitRefString(src.CloneRef, sourceRootSubpath, version)
2124+
}
2125+
21202126
func (src GitModuleSource) Clone() *GitModuleSource {
21212127
return &src
21222128
}
@@ -2265,8 +2271,7 @@ func ResolveDepToSource(
22652271
case ModuleSourceKindGit:
22662272
// parent=git, dep=local
22672273
// load the dep relative to the parent's source root, from the parent source's git repo
2268-
refString := GitRefString(
2269-
parentSrc.Git.CloneRef,
2274+
refString := parentSrc.Git.RefString(
22702275
filepath.Join(parentSrc.SourceRootSubpath, depSrcRef),
22712276
parentSrc.Git.Version,
22722277
)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package core
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/dagger/dagger/core/gitref"
8+
"github.com/dagger/dagger/dagql"
9+
"github.com/dagger/dagger/engine"
10+
"github.com/stretchr/testify/require"
11+
)
12+
13+
// A local dependency of a Git module must be loaded in the same ref form as
14+
// its parent. Runs the real resolver and captures the ref it loads.
15+
func TestGitLocalDependencyPreservesSelector(t *testing.T) {
16+
type sourceArgs struct {
17+
RefString string
18+
RefPin string
19+
DisableFindUp bool
20+
}
21+
for _, tc := range []struct {
22+
name string
23+
selector gitref.SelectorType
24+
version string
25+
want string
26+
}{
27+
{"explicit Git URL", gitref.GitRefSelector, "main", "https://git.example.org/team/sub/project.git#main:ci/dep"},
28+
{"literal version-shaped Git ref", gitref.GitRefSelector, "v1.2", "https://git.example.org/team/sub/project.git#v1.2:ci/dep"},
29+
{"legacy module version", gitref.ModuleVersionSelector, "main", "https://git.example.org/team/sub/project.git/ci/dep@main"},
30+
} {
31+
t.Run(tc.name, func(t *testing.T) {
32+
root := &Query{}
33+
ctx := ContextWithQuery(t.Context(), root)
34+
ctx = engine.ContextWithClientMetadata(ctx, &engine.ClientMetadata{ClientID: "dependency-test", SessionID: "dependency-test"})
35+
cache, err := dagql.NewCache(ctx, "", nil, nil)
36+
require.NoError(t, err)
37+
t.Cleanup(func() { require.NoError(t, cache.Close(context.Background())) })
38+
ctx = dagql.ContextWithCache(ctx, cache)
39+
dag := newCoreDagqlServerForTest(t, root)
40+
dag.InstallObject(dagql.NewClass(dag, dagql.ClassOpts[*ModuleSource]{Typed: &ModuleSource{}}))
41+
var loadedArgs sourceArgs
42+
dagql.Fields[*Query]{
43+
dagql.Func("moduleSource", func(_ context.Context, _ *Query, args sourceArgs) (*ModuleSource, error) {
44+
loadedArgs = args
45+
return &ModuleSource{ModuleName: "dep"}, nil
46+
}),
47+
}.Install(dag)
48+
parent := &ModuleSource{
49+
Kind: ModuleSourceKindGit,
50+
ModuleName: "parent",
51+
SourceRootSubpath: "ci",
52+
Git: &GitModuleSource{
53+
CloneRef: "https://git.example.org/team/sub/project.git",
54+
Version: tc.version,
55+
Commit: "resolved-commit",
56+
Selector: tc.selector,
57+
},
58+
}
59+
_, err = ResolveDepToSource(ctx, nil, dag, parent, "./dep", "", "")
60+
require.NoError(t, err)
61+
require.Equal(t, tc.want, loadedArgs.RefString)
62+
require.Equal(t, "resolved-commit", loadedArgs.RefPin)
63+
})
64+
}
65+
}

‎core/schema/modulesource.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,6 +1051,12 @@ func (s *moduleSourceSchema) workspaceModuleSource(
10511051
Commit: ref.Ref.SHA,
10521052
Ref: ref.Ref.Name,
10531053
}
1054+
// Print this source back as repo#ref:subpath, so reloading it doesn't
1055+
// have to rediscover where the repository ends. SCP-style refs
1056+
// (git@host:path) have no scheme and can't use that form.
1057+
if gitref.Scheme(cloneRef).Prefix() != "" {
1058+
src.Git.Selector = gitref.GitRefSelector
1059+
}
10541060
src.Git.Symbolic = cloneRef
10551061
if sourceRootPath != "." {
10561062
src.Git.Symbolic += "/" + filepath.ToSlash(sourceRootPath)

0 commit comments

Comments
 (0)