Skip to content

Commit

Permalink
feat: no main error page (#2709)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
caarlos0 committed Nov 30, 2021
1 parent af4a864 commit 62da2db
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 7 deletions.
12 changes: 10 additions & 2 deletions internal/builders/golang/build.go
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
10 changes: 5 additions & 5 deletions internal/builders/golang/build_test.go
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
33 changes: 33 additions & 0 deletions www/docs/errors/no-main.md
@@ -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/).
1 change: 1 addition & 0 deletions www/mkdocs.yml
Expand Up @@ -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
Expand Down

0 comments on commit 62da2db

Please sign in to comment.