Skip to content

Commit

Permalink
fix: change go module name to match dagger module name
Browse files Browse the repository at this point in the history
This effectively reverts 7760080, since
with go.work support we can't have duplicate module names inside a
single namespace.

Thankfully though, we can avoid the original issue that this fixed
(where `go` is effectively a reserved module name), by prefixing all
module names with `dagger/`).

Signed-off-by: Justin Chadwell <me@jedevc.com>
  • Loading branch information
jedevc committed Feb 28, 2024
1 parent ddce621 commit 2523093
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 8 deletions.
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
70 changes: 65 additions & 5 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 @@ -3161,7 +3221,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 @@ -3204,7 +3264,7 @@ func TestModuleGoUtilsPkg(t *testing.T) {
import (
"context"
"main/utils"
"dagger/minimal/utils"
)
type Minimal struct{}
Expand All @@ -3218,7 +3278,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

0 comments on commit 2523093

Please sign in to comment.