Skip to content

Commit 119f962

Browse files
authored
fix: Set GOOS and GOARCH in package command (#1246)
This ensures that different binaries are actually built as expected. I also added a test assertion to check that the SHA256 of the resulting binaries are at least different.
1 parent ff074f4 commit 119f962

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

serve/package.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ func (s *PluginServe) build(pluginDirectory, goos, goarch, distPath, pluginVersi
113113
cmd.Stdout = os.Stdout
114114
cmd.Stderr = os.Stderr
115115
cmd.Env = os.Environ()
116+
cmd.Env = append(cmd.Env, fmt.Sprintf("GOOS=%s", goos))
117+
cmd.Env = append(cmd.Env, fmt.Sprintf("GOARCH=%s", goarch))
118+
cmd.Env = append(cmd.Env, fmt.Sprintf("CGO_ENABLED=%v", getEnvOrDefault("CGO_ENABLED", "0"))) // default to CGO_ENABLED=0
116119
if err := cmd.Run(); err != nil {
117120
return nil, fmt.Errorf("failed to build plugin with `go %v`: %w", args, err)
118121
}

serve/package_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ with multiple lines and **markdown**`
5050
}
5151
for _, tc := range testCases {
5252
t.Run(tc.name, func(t *testing.T) {
53+
t.Setenv("CGO_ENABLED", "0") // disable CGO to ensure we environmental differences don't interfere with the test
5354
srv := Plugin(p)
5455
cmd := srv.newCmdPluginRoot()
5556
distDir := t.TempDir()
@@ -74,6 +75,12 @@ with multiple lines and **markdown**`
7475
if diff := cmp.Diff(expect, fileNames(files)); diff != "" {
7576
t.Fatalf("unexpected files in dist directory (-want +got):\n%s", diff)
7677
}
78+
// expect SHA-256 for the zip files to differ
79+
sha1 := sha256sum(filepath.Join(distDir, "plugin-testPlugin-v1.2.3-linux-amd64.zip"))
80+
sha2 := sha256sum(filepath.Join(distDir, "plugin-testPlugin-v1.2.3-windows-amd64.zip"))
81+
if sha1 == sha2 {
82+
t.Fatalf("expected SHA-256 for linux and windows zip files to differ, but they are the same: %s", sha1)
83+
}
7784

7885
expectPackage := PackageJSON{
7986
SchemaVersion: 1,
@@ -135,6 +142,7 @@ with multiple lines and **markdown**`
135142
}
136143
for _, tc := range testCases {
137144
t.Run(tc.name, func(t *testing.T) {
145+
t.Setenv("CGO_ENABLED", "0") // disable CGO to ensure we environmental differences don't interfere with the test
138146
srv := Plugin(p)
139147
cmd := srv.newCmdPluginRoot()
140148
distDir := t.TempDir()
@@ -158,6 +166,12 @@ with multiple lines and **markdown**`
158166
if diff := cmp.Diff(expect, fileNames(files)); diff != "" {
159167
t.Fatalf("unexpected files in dist directory (-want +got):\n%s", diff)
160168
}
169+
// expect SHA-256 for the zip files to differ
170+
sha1 := sha256sum(filepath.Join(distDir, "plugin-testPlugin-v1.2.3-windows-amd64.zip"))
171+
sha2 := sha256sum(filepath.Join(distDir, "plugin-testPlugin-v1.2.3-darwin-amd64.zip"))
172+
if sha1 == sha2 {
173+
t.Fatalf("expected SHA-256 for windows and darwin zip files to differ, but they are the same: %s", sha1)
174+
}
161175

162176
expectPackage := PackageJSON{
163177
SchemaVersion: 1,

0 commit comments

Comments
 (0)