mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-02-07 13:31:37 +02:00
feat: no main error page (#2709)
* feat: no main error page Signed-off-by: Carlos A Becker <caarlos0@gmail.com> * docs: improve seo Signed-off-by: Carlos A Becker <caarlos0@gmail.com>
This commit is contained in:
parent
af4a8642bd
commit
62da2dbe13
@ -250,7 +250,7 @@ func checkMain(build config.Build) error {
|
||||
}
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("build for %s does not contain a main function", build.Binary)
|
||||
return errNoMain{build.Binary}
|
||||
}
|
||||
file, err := parser.ParseFile(token.NewFileSet(), main, nil, 0)
|
||||
if err != nil {
|
||||
@ -259,7 +259,15 @@ func checkMain(build config.Build) error {
|
||||
if hasMain(file) {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("build for %s does not contain a main function", build.Binary)
|
||||
return errNoMain{build.Binary}
|
||||
}
|
||||
|
||||
type errNoMain struct {
|
||||
bin string
|
||||
}
|
||||
|
||||
func (e errNoMain) Error() string {
|
||||
return fmt.Sprintf("build for %s does not contain a main function\nLearn more at https://goreleaser.com/errors/no-main\n", e.bin)
|
||||
}
|
||||
|
||||
func hasMain(file *ast.File) bool {
|
||||
|
@ -581,7 +581,7 @@ func TestRunPipeWithoutMainFunc(t *testing.T) {
|
||||
ctx.Config.Builds[0].Main = ""
|
||||
require.EqualError(t, Default.Build(ctx, ctx.Config.Builds[0], api.Options{
|
||||
Target: runtimeTarget,
|
||||
}), `build for no-main does not contain a main function`)
|
||||
}), errNoMain{"no-main"}.Error())
|
||||
})
|
||||
t.Run("not main.go", func(t *testing.T) {
|
||||
ctx := newCtx(t)
|
||||
@ -595,14 +595,14 @@ func TestRunPipeWithoutMainFunc(t *testing.T) {
|
||||
ctx.Config.Builds[0].Main = "."
|
||||
require.EqualError(t, Default.Build(ctx, ctx.Config.Builds[0], api.Options{
|
||||
Target: runtimeTarget,
|
||||
}), `build for no-main does not contain a main function`)
|
||||
}), errNoMain{"no-main"}.Error())
|
||||
})
|
||||
t.Run("fixed main.go", func(t *testing.T) {
|
||||
ctx := newCtx(t)
|
||||
ctx.Config.Builds[0].Main = "main.go"
|
||||
require.EqualError(t, Default.Build(ctx, ctx.Config.Builds[0], api.Options{
|
||||
Target: runtimeTarget,
|
||||
}), `build for no-main does not contain a main function`)
|
||||
}), errNoMain{"no-main"}.Error())
|
||||
})
|
||||
t.Run("using gomod.proxy", func(t *testing.T) {
|
||||
ctx := newCtx(t)
|
||||
@ -613,7 +613,7 @@ func TestRunPipeWithoutMainFunc(t *testing.T) {
|
||||
ctx.Config.Builds[0].UnproxiedMain = "."
|
||||
require.EqualError(t, Default.Build(ctx, ctx.Config.Builds[0], api.Options{
|
||||
Target: runtimeTarget,
|
||||
}), `build for no-main does not contain a main function`)
|
||||
}), errNoMain{"no-main"}.Error())
|
||||
})
|
||||
t.Run("using gomod.proxy and template", func(t *testing.T) {
|
||||
ctx := newCtx(t)
|
||||
@ -625,7 +625,7 @@ func TestRunPipeWithoutMainFunc(t *testing.T) {
|
||||
ctx.Config.Builds[0].UnproxiedMain = "{{ .Env.Main }}"
|
||||
require.EqualError(t, Default.Build(ctx, ctx.Config.Builds[0], api.Options{
|
||||
Target: runtimeTarget,
|
||||
}), `build for no-main does not contain a main function`)
|
||||
}), errNoMain{"no-main"}.Error())
|
||||
})
|
||||
t.Run("using gomod.proxy and invalid template", func(t *testing.T) {
|
||||
ctx := newCtx(t)
|
||||
|
33
www/docs/errors/no-main.md
Normal file
33
www/docs/errors/no-main.md
Normal file
@ -0,0 +1,33 @@
|
||||
# Build does not contain a main function
|
||||
|
||||
This usually happens if you're trying to build a library or if you didn't setup the `builds.main` section in your `.goreleaser.yml` and you `main.go` is not in the root folder.
|
||||
|
||||
Here's an example error:
|
||||
|
||||
```sh
|
||||
⨯ build failed after 0.11s error=build for foo does not contain a main function
|
||||
|
||||
Learn more at https://goreleaser.com/errors/no-main
|
||||
```
|
||||
|
||||
## If you are building a library
|
||||
|
||||
Add something like this to your config:
|
||||
|
||||
```yaml
|
||||
# .goreleaser.yml
|
||||
builds:
|
||||
- skip: true
|
||||
```
|
||||
|
||||
## If your `main.go` is not in the root folder
|
||||
|
||||
Add something like this to your config:
|
||||
|
||||
```yaml
|
||||
# .goreleaser.yml
|
||||
builds:
|
||||
- main: ./path/to/your/main/pkg/
|
||||
```
|
||||
|
||||
For more info, check the [builds documentation](/customization/build/).
|
@ -131,6 +131,7 @@ nav:
|
||||
- goreleaser jsonschema: cmd/goreleaser_jsonschema.md
|
||||
- Common errors:
|
||||
- errors/dirty.md
|
||||
- errors/no-main.md
|
||||
- deprecations.md
|
||||
- Cookbooks:
|
||||
- About: cookbooks/index.md
|
||||
|
Loading…
x
Reference in New Issue
Block a user