Skip to content

Commit 37d4faa

Browse files
authored
Makefile: trim gopath from panic logs (#1042) (#1043)
**Description of the change:** Trim the GOPATH from binaries built with `install` or `build`. **Motivation for the change:** Remove the home directory of the builder from the panic path.
1 parent 8b7d7c9 commit 37d4faa

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ clean:
4141
.PHONY: all test format dep clean
4242

4343
install:
44-
$(Q)go install $(BUILD_PATH)
44+
$(Q)go install -gcflags "all=-trimpath=${GOPATH}" -asmflags "all=-trimpath=${GOPATH}" $(BUILD_PATH)
4545

4646
release_x86_64 := \
4747
build/operator-sdk-$(VERSION)-x86_64-linux-gnu \
@@ -53,7 +53,7 @@ build/operator-sdk-%-x86_64-linux-gnu: GOARGS = GOOS=linux GOARCH=amd64
5353
build/operator-sdk-%-x86_64-apple-darwin: GOARGS = GOOS=darwin GOARCH=amd64
5454

5555
build/%:
56-
$(Q)$(GOARGS) go build -o $@ $(BUILD_PATH)
56+
$(Q)$(GOARGS) go build -gcflags "all=-trimpath=${GOPATH}" -asmflags "all=-trimpath=${GOPATH}" -o $@ $(BUILD_PATH)
5757

5858
build/%.asc:
5959
$(Q){ \

commands/operator-sdk/cmd/build.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,16 @@ func buildFunc(cmd *cobra.Command, args []string) {
144144

145145
projutil.MustInProjectRoot()
146146
goBuildEnv := append(os.Environ(), "GOOS=linux", "GOARCH=amd64", "CGO_ENABLED=0")
147+
goTrimFlags := []string{"-gcflags", "all=-trimpath=${GOPATH}", "-asmflags", "all=-trimpath=${GOPATH}"}
147148
absProjectPath := projutil.MustGetwd()
149+
projectName := filepath.Base(absProjectPath)
148150

149151
// Don't need to build go code if Ansible Operator
150152
if mainExists() {
151153
managerDir := filepath.Join(projutil.CheckAndGetProjectGoPkg(), scaffold.ManagerDir)
152-
outputBinName := filepath.Join(absProjectPath, scaffold.BuildBinDir, filepath.Base(absProjectPath))
153-
buildCmd := exec.Command("go", "build", "-o", outputBinName, managerDir)
154+
outputBinName := filepath.Join(absProjectPath, scaffold.BuildBinDir, projectName)
155+
goBuildArgs := append(append([]string{"build"}, goTrimFlags...), "-o", outputBinName, managerDir)
156+
buildCmd := exec.Command("go", goBuildArgs...)
154157
buildCmd.Env = goBuildEnv
155158
buildCmd.Stdout = os.Stdout
156159
buildCmd.Stderr = os.Stderr
@@ -181,9 +184,10 @@ func buildFunc(cmd *cobra.Command, args []string) {
181184
}
182185

183186
if enableTests {
184-
if mainExists() {
185-
testBinary := filepath.Join(absProjectPath, scaffold.BuildBinDir, filepath.Base(absProjectPath)+"-test")
186-
buildTestCmd := exec.Command("go", "test", "-c", "-o", testBinary, testLocationBuild+"/...")
187+
if projutil.GetOperatorType() == projutil.OperatorTypeGo {
188+
testBinary := filepath.Join(absProjectPath, scaffold.BuildBinDir, projectName+"-test")
189+
goTestBuildArgs := append(append([]string{"test"}, goTrimFlags...), "-c", "-o", testBinary, testLocationBuild+"/...")
190+
buildTestCmd := exec.Command("go", goTestBuildArgs...)
187191
buildTestCmd.Env = goBuildEnv
188192
buildTestCmd.Stdout = os.Stdout
189193
buildTestCmd.Stderr = os.Stderr

hack/image/build-scorecard-proxy-image.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
set -eux
44

55
# build operator binary and base image
6-
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o images/scorecard-proxy/scorecard-proxy images/scorecard-proxy/cmd/proxy/main.go
6+
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -gcflags "all=-trimpath=${GOPATH}" -asmflags "all=-trimpath=${GOPATH}" -o images/scorecard-proxy/scorecard-proxy images/scorecard-proxy/cmd/proxy/main.go
77
pushd images/scorecard-proxy
88
docker build -t "$1" .
99
popd

0 commit comments

Comments
 (0)