#Build a binary with a function | Dagger
1 messages · Page 1 of 1 (latest)
Also I'm on a M1 Mac so when I did dagger -m github.com/kpenfound/dagger-modules/golang@v0.1.5 call build --project https://github.com/dagger/dagger --args ./cmd/dagger export --path ./my-dagger-build and try to run cd my-dagger-build and ./dagger version like the guide said, I ran into this error: zsh: exec format error: ./dagger I guess it wasn't built for ARM by the module - should we add a note in the docs? Or if there is a way to build it for the OS the command is run in so that other people don't run into this?
@stray vapor I'm not a Go user but I can probably answer some of these
-
https://github.com/kpenfound/dagger-modules/blob/main/golang/main.go#L46 suggests
--argsare passed togo build -
The
buildmethod returns aDirectory(https://github.com/kpenfound/dagger-modules/blob/main/golang/main.go#L58) so you can use any validDirectorymethod there, includingentries. The directory returned is a constant,PROJ_MOUNT, which is set to/src(https://github.com/kpenfound/dagger-modules/blob/main/golang/main.go#L75) which might explain why you see more files -
exportis indeed a method available on theDirectorytype, and it requires apathargument (https://pkg.go.dev/dagger.io/dagger#Directory.Export), which you see above--path -
You being on an M1 Mac shouldn't matter, it's set using
runtime.GOARCHhere: https://github.com/kpenfound/dagger-modules/blob/main/golang/main.go#L60. You can override that by adding--archto the command however, might be worth testing
(I'm not a Dagger developer or a Go user, so I can't guarantee all the above is correct!)
It looks like there may be an issue with that example in the quickstart (\cc @thorn current). Should export just the binary, not the whole directory.
@stray vapor, --args are the arguments to go build (https://github.com/kpenfound/dagger-modules/blob/dc54c30fd8612a43742573a17416cf54c4e5bc0d/golang/main.go#L59) and if arch/os aren't provided it'll use the runtime from the container by default, so that chain is executing the equivalent of the following command in a clone of the --project directory, and returning it back (with the built binary):
$ GOARCH=arm64 GOOS=linux go build ./cmd/dagger
You're compiling to linux because that code is running in a container, so you need to set --os (also needs to be documented):
$ dagger -m github.com/kpenfound/dagger-modules/golang@v0.1.5 call build --project https://github.com/dagger/dagger --os darwin --args ./cmd/dagger entries
Notice that when selecting entries after go build ./cmd/dagger, you can actually see the dagger binary in that list. I actually expected it to be built to bin/ so you'd list that dir instead (\cc @graceful nova).
But, in the second command, rather than exporting the whole repo to the host, you should be selecting only the built file, so I think before export, get just that file:
$ dagger -m github.com/kpenfound/dagger-modules/golang@v0.1.5 call build --project https://github.com/dagger/dagger --args ./cmd/dagger --os darwin file --path dagger export --path my-dagger
Which will put that binary in your working dir, but nothing else:
$ ./my-dagger version
dagger (registry.dagger.io/engine) darwin/arm64
Note that I prefer using = to make it clearer what's the value to an argument vs what's a function, and use -o instead of export --path which shows the absolute path in your host where the file was saved, instead of just "true":
$ dagger -m github.com/kpenfound/dagger-modules/golang@v0.1.5 call build --project=https://github.com/dagger/dagger --args=./cmd/dagger --os=darwin file --path=dagger -o my-dagger
These were super helpful in understanding, thank you so much for the detailed replies!
$ dagger -m github.com/kpenfound/dagger-modules/golang@v0.1.5 call build --project=https://github.com/dagger/dagger --args=./cmd/dagger --os=darwin file --path=dagger -o my-dagger
Is file another function for Directory() which lets you select a particular file or folder from the returned directory?
Also so from what I understand to improve this example we can:
- Have the build function return only the binary instead of all the repo contents?
- Mention the
--osflag as a note in the getting started docs?
I'm new to this but want to learn so happy to raise a PR for both, just let me know!
@stiff epoch @graceful nova is there a way to make this more generic so it automatically detects the host platform (maybe an environment var passed to --os?) and builds accordingly?
In the meanwhile I'll add a note and make the changes suggested above
dagger -m github.com/kpenfound/dagger-modules/golang@v0.1.5 call build --project=https://github.com/dagger/dagger --args=./cmd/dagger --os=$(uname -s | awk '{print tolower($0)}') file --path=dagger -o my-dagger
PR at https://github.com/dagger/dagger/pull/6966 - thanks @graceful nova for the revised example!
@stray vapor yes file lets you select a file for further operations. fwiw I'm adding many more examples to the user manual in a separate PR, you may find it useful to look through it https://github.com/dagger/dagger/pull/6918