Skip to content

Commit fd8a746

Browse files
retlehsclaude
andauthored
Fix build changed count always showing total packages (#46)
* Restore build change detection lost during Composer v1 removal The p/-based content-addressed change detection was removed when Composer v1 support was dropped, leaving changedPkgs incrementing unconditionally. This caused the admin builds page to show all packages as changed every build. Restore detection using byte comparison of p2/ files against the previous build. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * Fix gofmt formatting Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent d384f40 commit fd8a746

File tree

2 files changed

+36
-15
lines changed

2 files changed

+36
-15
lines changed

cmd/wppackages/cmd/build.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"path/filepath"
66
"time"
77

8+
"github.com/roots/wp-packages/internal/deploy"
89
"github.com/roots/wp-packages/internal/repository"
910
"github.com/spf13/cobra"
1011
)
@@ -24,13 +25,21 @@ func runBuild(cmd *cobra.Command, args []string) error {
2425
output = filepath.Join("storage", "repository", "builds")
2526
}
2627

28+
// Resolve previous build for change detection
29+
repoDir := filepath.Dir(output) // storage/repository
30+
prevBuildDir := ""
31+
if id, err := deploy.CurrentBuildID(repoDir); err == nil && id != "" {
32+
prevBuildDir = deploy.BuildDirFromID(repoDir, id)
33+
}
34+
2735
result, err := repository.Build(cmd.Context(), application.DB, repository.BuildOpts{
28-
OutputDir: output,
29-
AppURL: application.Config.AppURL,
30-
Force: force,
31-
PackageName: pkg,
32-
BuildID: pipelineBuildID,
33-
Logger: application.Logger,
36+
OutputDir: output,
37+
AppURL: application.Config.AppURL,
38+
Force: force,
39+
PackageName: pkg,
40+
BuildID: pipelineBuildID,
41+
PreviousBuildDir: prevBuildDir,
42+
Logger: application.Logger,
3443
})
3544
if err != nil {
3645
return err

internal/repository/builder.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package repository
22

33
import (
4+
"bytes"
45
"context"
56
"database/sql"
67
"encoding/json"
@@ -16,13 +17,14 @@ import (
1617

1718
// BuildOpts configures a repository build.
1819
type BuildOpts struct {
19-
OutputDir string // base output dir (e.g. storage/repository/builds)
20-
AppURL string // absolute app URL for notify-batch
21-
Force bool
22-
PackageName string // optional: build single package
23-
PackageNames []string // optional: build only these slugs
24-
BuildID string // optional: pre-generated build ID (used by pipeline)
25-
Logger *slog.Logger
20+
OutputDir string // base output dir (e.g. storage/repository/builds)
21+
AppURL string // absolute app URL for notify-batch
22+
Force bool
23+
PackageName string // optional: build single package
24+
PackageNames []string // optional: build only these slugs
25+
BuildID string // optional: pre-generated build ID (used by pipeline)
26+
PreviousBuildDir string // optional: compare against previous build to count changes
27+
Logger *slog.Logger
2628
}
2729

2830
// BuildResult holds build metadata for manifest.json and the builds table.
@@ -175,13 +177,23 @@ func Build(ctx context.Context, db *sql.DB, opts BuildOpts) (*BuildResult, error
175177
return nil, fmt.Errorf("hashing %s: %w", composerName, err)
176178
}
177179

178-
p2File := filepath.Join(buildDir, "p2", composerName+".json")
180+
p2Rel := filepath.Join("p2", composerName+".json")
181+
p2File := filepath.Join(buildDir, p2Rel)
179182
if err := os.WriteFile(p2File, data, 0644); err != nil {
180183
return nil, fmt.Errorf("writing %s: %w", p2File, err)
181184
}
182-
changedPkgs++
183185
artifactCount++
184186

187+
// Compare against previous build to determine if this package changed
188+
if opts.PreviousBuildDir != "" {
189+
prevData, err := os.ReadFile(filepath.Join(opts.PreviousBuildDir, p2Rel))
190+
if err != nil || !bytes.Equal(prevData, data) {
191+
changedPkgs++
192+
}
193+
} else {
194+
changedPkgs++
195+
}
196+
185197
if totalPkgs%500 == 0 {
186198
opts.Logger.Info("build progress", "packages", totalPkgs)
187199
}

0 commit comments

Comments
 (0)