#Debugging bats module
1 messages ยท Page 1 of 1 (latest)
Starting a thread ๐
I don't see anything immediately wrong with your code
Note, you can drop the context + error in dagger functions if you don't need them
ie. you can do func (dir *Directory) Bats (args []string) *Container
I even added an extra Foo method to test if the Directory type needs to appear in return types, not in the argument list
neat
It's neat-o, which rhymes with vit-o
Btw, lately I've found that I rely on extending core types less than before. Instead I create custom types that wrap/embed core types as needed. Feels more powerful in the end
I used my stack overflow keyboard to create this module from your make and Kyle's golang module, so ๐คทโโ๏ธ
So I stay in control of my chaining that way
That's what I wanted to do, just couldn't figure out at first how to pass a directory to a function.
so bats(dir: ???) { stdout }
Honestly, I haven't really used the graphql API directly so far, so it doesn't feel that natural yet, but I can see how it makes debugging easier than writing test code for the module.
@sly dawn you can just take a *Directory as argument
Yeah, but how do I query that? What do I put in the query?
and you can keep that *Directory as a public field of your custom struct
huh, field?
I have this function already:
func (m *Bats) Bats(ctx context.Context, dir *Directory, args []string) (*Container, error) {
return run(dir, args), nil
}
I would do something like:
// Your module top-level type
type Bats struct {}
func (b *Bats) Project(dir *Directory) *Project {
return &Project{Dir: dir}
}
// A BATS project
type Project struct {
Dir *Directory
}
// Do something in the project
func (p *Project) Foo(args []string) *Container {
return dag.Container().From("alpine").WithMountedDirectory("/bats", p.Dir).WithExec([]string{"bats", "foo"})
}
Then from there you can add "with"-style chaining:
func (p *Project) WithFoo(args []string) *Project {
return &Project{
Dir: dag.Container().From("alpine").WithDirectory("/bats", p.Dir).WithExec([]string{"bats", "foo"}).Directory("/bats"),
}
}
As long as the fields in Project (your custom struct type) are public, the Go SDK will json-marshal them and preserve their state in between function calls, so chaining will work as you expect
My helloWorld is a simple implementation of that pattern: https://daggerverse.fly.dev/mod/github.com/shykes/daggerverse/helloWorld
Hm, okay. I guess that works. Now, the only thing I'm curious about is how you call that with graphql. ๐
There is not a single example for that in any of the existing modules.
How do you pass a directory to this module?
Then a query would look like:
query test {
bats {
project(dir: <DIRECTORY_ID_GOES_HERE>) {
Foo(args: ["hello", "world"]) {
...
}
}
}
}
Of course we're missing convenience to pass that ID ๐ You can copy paste it from another gql query for now
OR you can call it from another module
That's the piece I've been missing ๐
OR you can wait for dagger CLI to add convenience to pass local directories as input
And probably that's why I ended up trying to extend the directory type
Yeah, makes sense
btw extending core type should still work - I don't know why it doesn't
but you're right that it does make it much easier to bootstrap from a graphql query
yeah
Flagging @heady halo and @latent quest ๐
We went on a tangent, but the core issue is that extending core type fails in @sly dawn 's bats module
Yep, I couldn't get it to work in a minimal case either
package main
import (
"context"
)
type Bats struct{}
func (m *Bats) Foo(ctx context.Context) (*Directory, error) {
return nil, nil
}
func (dir *Directory) Bats(ctx context.Context) (*Container, error) {
return dag.Container().
From("bats/bats:v1.10.0").
WithEntrypoint([]string{""}).
WithMountedDirectory("/src", dir).
WithExec([]string{"/usr/local/bin/bats", "-r", "/src"}).Sync(ctx)
}
dagger query <<EOF
query test {
git(url: "https://github.com/sagikazarmark/distribution") {
branch(name: "main") {
tree {
bats {
stdout
}
}
}
}
}
EOF
distribution โค docker run -it --rm --workdir /src -v $PWD:/src --entrypoint "" bats/bats:v1.10.0 bats -r /src
0 tests, 0 failures
Interesting errors when run from Zenith:
init
โ โ [0.49s] connect
โ โฃ [0.27s] starting engine
โ โฃ [0.21s] starting session
โ โ Failed to connect; retrying... name:"error" value:"make request: Post \"http://dagger
โ โ /query\": rpc error: code = Unknown desc = server \"h1sj3zf6rtmd6i2fzqlmdifh0\" not fo
โ โ und"
โ โ OK!
โโโโโดโโฏ CACHED exec dagger mod sync bust-hack:1
โ CACHED exec go build -o /runtime -ldflags -s -d -w .
โ CACHED exec /runtime
โ [1.27s] ERROR dagger query
โฃ [1.27s] loading module
โ [0.00s] ERROR query
โป
WARNING: Using development engine; skipping version compatibility check.
โข Cloud URL: https://dagger.cloud/runs/30c5383a-83ef-4088-9597-5a70631a687b
โข Engine: 55e92bcabacc (version devel ())
โง 1.77s โ 15 โ
5 โ 2
Error: make request: input:5: Cannot query field "bats" on type "Directory".
Is this only a problem with my module? Or something broke and type extensions don't work at all at the moment?
Yes, they're broken for me at the moment too with latest build.
I just tested with a previous one that @lyric hound had created a CLI for and it worked.
Will take a look soon, trying to wrap up my bootstrapping adventure before I fly out (https://discord.com/channels/707636530424053791/1156327297796800573)
@sly dawn Fixed! Sorry for the wait. https://daggerverse.fly.dev/mod/github.com/sagikazarmark/daggerverse/bats@6ac2eba8e4d4854e432154da50a926e485d161c8
This was a bug in the codegen: https://github.com/shykes/dagger/commit/fffa34b5223c041d42747706f58a5041f6f1ceae