Skip to content

Commit

Permalink
fix: prevent folder collisions in builds and universal binaries
Browse files Browse the repository at this point in the history
- on universal binaries, use the build id instead of the binary name to
  create the folder in the dist folder
- on builds, default the id the to the binary name instead of project
  name. The binary name already defaults to the project id if empty, so
  this should only prevent having to specify the id + binary name in
  some cases.

closes #3061

Signed-off-by: Carlos A Becker <caarlos0@gmail.com>
  • Loading branch information
caarlos0 committed Apr 25, 2022
1 parent 41f7c3a commit 8a51545
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion internal/pipe/build/build.go
Expand Up @@ -73,7 +73,7 @@ func buildWithDefaults(ctx *context.Context, build config.Build) (config.Build,
build.Binary = ctx.Config.ProjectName
}
if build.ID == "" {
build.ID = ctx.Config.ProjectName
build.ID = build.Binary
}
for k, v := range build.Env {
build.Env[k] = os.ExpandEnv(v)
Expand Down
9 changes: 6 additions & 3 deletions internal/pipe/build/build_test.go
Expand Up @@ -288,16 +288,19 @@ func TestDefaultBuildID(t *testing.T) {
Builds: []config.Build{
{
Binary: "{{.Env.FOO}}",
ID: "bar",
},
{
Binary: "bar",
},
},
},
}
require.EqualError(t, Pipe{}.Default(ctx), "found 2 builds with the ID 'foo', please fix your config")
build := ctx.Config.Builds[0]
require.Equal(t, ctx.Config.ProjectName, build.ID)
require.EqualError(t, Pipe{}.Default(ctx), "found 2 builds with the ID 'bar', please fix your config")
build1 := ctx.Config.Builds[0].ID
build2 := ctx.Config.Builds[1].ID
require.Equal(t, build1, build2)
require.Equal(t, "bar", build2)
}

func TestSeveralBuildsWithTheSameID(t *testing.T) {
Expand Down
6 changes: 4 additions & 2 deletions internal/pipe/universalbinary/universalbinary.go
Expand Up @@ -140,7 +140,7 @@ func makeUniversalBinary(ctx *context.Context, opts *build.Options, unibin confi
}
opts.Name = name

path := filepath.Join(ctx.Config.Dist, name+"_darwin_all", name)
path := filepath.Join(ctx.Config.Dist, unibin.ID+"_darwin_all", name)
opts.Path = path
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return err
Expand All @@ -151,7 +151,9 @@ func makeUniversalBinary(ctx *context.Context, opts *build.Options, unibin confi
return pipe.Skip(fmt.Sprintf("no darwin binaries found with id %q", unibin.ID))
}

log.WithField("binary", path).Infof("creating from %d binaries", len(binaries))
log.WithField("id", unibin.ID).
WithField("binary", path).
Infof("creating from %d binaries", len(binaries))

var inputs []input
offset := int64(align)
Expand Down
2 changes: 1 addition & 1 deletion internal/pipe/universalbinary/universalbinary_test.go
Expand Up @@ -353,7 +353,7 @@ func TestRun(t *testing.T) {
func checkUniversalBinary(tb testing.TB, unibin *artifact.Artifact) {
tb.Helper()

require.True(tb, strings.HasSuffix(unibin.Path, "foo_darwin_all/foo"))
require.True(tb, strings.HasSuffix(unibin.Path, unibin.ID()+"_darwin_all/foo"))
f, err := macho.OpenFat(unibin.Path)
require.NoError(tb, err)
require.Len(tb, f.Arches, 2)
Expand Down
2 changes: 1 addition & 1 deletion www/docs/customization/build.md
Expand Up @@ -13,7 +13,7 @@ builds:
# You can have multiple builds defined as a yaml list
-
# ID of the build.
# Defaults to the project name.
# Defaults to the binary name.
id: "my-build"

# Path to project's (sub)directory containing Go code.
Expand Down

0 comments on commit 8a51545

Please sign in to comment.