Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: change go module name to match dagger module name #6774

Merged
merged 3 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/unreleased/Fixed-20240304-100555.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Fixed
body: Dagger go modules default to the module name instead of "main"
time: 2024-03-04T10:05:55.625933343Z
custom:
Author: jedevc
PR: "6774"
5 changes: 2 additions & 3 deletions cmd/codegen/generator/go/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func (g *GoGenerator) bootstrapMod(ctx context.Context, mfs *memfs.FS) (*Package

// bootstrap go.mod using dependencies from the embedded Go SDK

newModName := "main" // use a safe default, not going to be a reserved word. User is free to modify
newModName := fmt.Sprintf("dagger/%s", strcase.ToKebab(g.Config.ModuleName))

newMod.AddModuleStmt(newModName)
newMod.AddGoStmt(goVersion.String())
Expand Down Expand Up @@ -249,7 +249,7 @@ func generateCode(
tmpls := templates.Templates(funcs)

for k, tmpl := range tmpls {
dt, err := renderFile(ctx, cfg, schema, pkgInfo, tmpl)
dt, err := renderFile(cfg, schema, pkgInfo, tmpl)
if err != nil {
return err
}
Expand All @@ -269,7 +269,6 @@ func generateCode(
}

func renderFile(
ctx context.Context,
cfg generator.Config,
schema *introspection.Schema,
pkgInfo *PackageInfo,
Expand Down
72 changes: 66 additions & 6 deletions core/integration/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func TestModuleGoInit(t *testing.T) {

generated, err := modGen.File("go.mod").Contents(ctx)
require.NoError(t, err)
require.Contains(t, generated, "module main")
require.Contains(t, generated, "module dagger/my-module")
})

t.Run("creates go.mod beneath an existing go.mod if context is beneath it", func(t *testing.T) {
Expand Down Expand Up @@ -112,7 +112,7 @@ func TestModuleGoInit(t *testing.T) {
t.Run("names Go module after Dagger module", func(t *testing.T) {
generated, err := modGen.Directory("dagger").File("go.mod").Contents(ctx)
require.NoError(t, err)
require.Contains(t, generated, "module main")
require.Contains(t, generated, "module dagger/beneath-go-mod")
})
})

Expand Down Expand Up @@ -192,6 +192,34 @@ func TestModuleGoInit(t *testing.T) {
})
})

t.Run("respects go.work for subdir if git dir", func(t *testing.T) {
t.Parallel()

c, ctx := connect(t)

modGen := goGitBase(t, c).
WithMountedFile(testCLIBinPath, daggerCliFile(t, c)).
WithWorkdir("/work").
WithExec([]string{"go", "mod", "init", "example.com/test"}).
WithExec([]string{"go", "work", "init"}).
WithExec([]string{"go", "work", "use", "."}).
With(daggerExec("init", "--name=hasGoMod", "--sdk=go", "subdir"))

out, err := modGen.
WithWorkdir("./subdir").
With(daggerQuery(`{hasGoMod{containerEcho(stringArg:"hello"){stdout}}}`)).
Stdout(ctx)
require.NoError(t, err)
require.JSONEq(t, `{"hasGoMod":{"containerEcho":{"stdout":"hello\n"}}}`, out)

t.Run("go.work is edited", func(t *testing.T) {
generated, err := modGen.File("go.work").Contents(ctx)
require.NoError(t, err)
require.Contains(t, generated, "\t.\n")
require.Contains(t, generated, "\t./subdir/dagger\n")
})
})

t.Run("ignores go.work for subdir", func(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -394,6 +422,38 @@ func (m *HasNotMainGo) Hello() string { return "Hello, world!" }
require.NoError(t, err)
require.NotContains(t, sourceRootEnts, "main.go")
})

t.Run("multiple modules in go.work", func(t *testing.T) {
t.Parallel()

c, ctx := connect(t)

modGen := goGitBase(t, c).
WithMountedFile(testCLIBinPath, daggerCliFile(t, c)).
WithWorkdir("/work").
WithExec([]string{"go", "work", "init"}).
With(daggerExec("init", "--sdk=go", "foo")).
With(daggerExec("init", "--sdk=go", "bar"))

generated, err := modGen.File("go.work").Contents(ctx)
require.NoError(t, err)
require.Contains(t, generated, "\t./foo/dagger\n")
require.Contains(t, generated, "\t./bar/dagger\n")

out, err := modGen.
WithWorkdir("./foo").
With(daggerQuery(`{foo{containerEcho(stringArg:"hello"){stdout}}}`)).
Stdout(ctx)
require.NoError(t, err)
require.JSONEq(t, `{"foo":{"containerEcho":{"stdout":"hello\n"}}}`, out)

out, err = modGen.
WithWorkdir("./bar").
With(daggerQuery(`{bar{containerEcho(stringArg:"hello"){stdout}}}`)).
Stdout(ctx)
require.NoError(t, err)
require.JSONEq(t, `{"bar":{"containerEcho":{"stdout":"hello\n"}}}`, out)
})
}

func TestModuleInitLICENSE(t *testing.T) {
Expand Down Expand Up @@ -2962,7 +3022,7 @@ type Test struct{}

func (m *Test) Fn(ctx context.Context) string {
resp := &graphql.Response{}
err := dag.Client.MakeRequest(ctx, &graphql.Request{
err := dag.GraphQLClient().MakeRequest(ctx, &graphql.Request{
Query: "{host{unixSocket(path:\"/some/sock\"){id}}}",
}, resp)
if err != nil {
Expand Down Expand Up @@ -3158,7 +3218,7 @@ func TestModuleGoUseDaggerTypesDirect(t *testing.T) {
WithNewFile("main.go", dagger.ContainerWithNewFileOpts{
Contents: `package main

import "main/internal/dagger"
import "dagger/minimal/internal/dagger"

type Minimal struct{}

Expand Down Expand Up @@ -3201,7 +3261,7 @@ func TestModuleGoUtilsPkg(t *testing.T) {

import (
"context"
"main/utils"
"dagger/minimal/utils"
)

type Minimal struct{}
Expand All @@ -3215,7 +3275,7 @@ func (m *Minimal) Hello(ctx context.Context) (string, error) {
WithNewFile("utils/util.go", dagger.ContainerWithNewFileOpts{
Contents: `package utils

import "main/internal/dagger"
import "dagger/minimal/internal/dagger"

func Foo() *dagger.Directory {
return dagger.Connect().Directory().WithNewFile("/foo", "hello world")
Expand Down