Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XCADDY_GO_BUILD_FLAGS is not behaving well with windows builds #222

Open
YoungMind1 opened this issue Jan 9, 2025 · 3 comments
Open

XCADDY_GO_BUILD_FLAGS is not behaving well with windows builds #222

YoungMind1 opened this issue Jan 9, 2025 · 3 comments
Labels
bug Something isn't working

Comments

@YoungMind1
Copy link

YoungMind1 commented Jan 9, 2025

This command works: XCADDY_GO_BUILD_FLAGS="-ldflags '-w -s -extldflags=-static'" xcaddy build
But this is not working: GOOS='windows' GOARCH='amd64' XCADDY_GO_BUILD_FLAGS="-ldflags '-w -s'" xcaddy build

second command's output:

2025/01/09 13:31:01 [INFO] absolute output file path: /tmp/tmp.FgBTzEnSTP/caddy.exe
2025/01/09 13:31:01 [INFO] Temporary folder: /tmp/buildenv_2025-01-09-1331.2596660125
2025/01/09 13:31:01 [INFO] Writing main module: /tmp/buildenv_2025-01-09-1331.2596660125/main.go
package main

import (
	caddycmd "github.com/caddyserver/caddy/v2/cmd"

	// plug in Caddy modules here
	_ "github.com/caddyserver/caddy/v2/modules/standard"
)

func main() {
	caddycmd.Main()
}
2025/01/09 13:31:01 [INFO] Initializing Go module
2025/01/09 13:31:01 [INFO] exec (timeout=0s): /usr/bin/go mod init caddy 
go: creating new go.mod: module caddy
go: to add module requirements and sums:
	go mod tidy
2025/01/09 13:31:01 [INFO] Pinning versions
2025/01/09 13:31:01 [INFO] exec (timeout=0s): /usr/bin/go get -v -ldflags -w -s -extldflags=-static github.com/caddyserver/caddy/v2 
go: github.com/caddyserver/caddy/[email protected] requires go >= 1.22.3; switching to go1.22.10
go: accepting indirect upgrade from [email protected] to 1.22.3
go: upgraded go 1.22.2 => 1.22.3
go: added toolchain go1.22.10
go: added github.com/beorn7/perks v1.0.1
go: added github.com/caddyserver/caddy/v2 v2.9.1
go: added github.com/caddyserver/certmagic v0.21.6
go: added github.com/caddyserver/zerossl v0.1.3
go: added github.com/cespare/xxhash/v2 v2.3.0
go: added github.com/francoispqt/gojay v1.2.13
go: added github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572
go: added github.com/google/pprof v0.0.0-20231212022811-ec68065c825e
go: added github.com/google/uuid v1.6.0
go: added github.com/klauspost/cpuid/v2 v2.2.9
go: added github.com/libdns/libdns v0.2.2
go: added github.com/mholt/acmez/v3 v3.0.0
go: added github.com/miekg/dns v1.1.62
go: added github.com/onsi/ginkgo/v2 v2.13.2
go: added github.com/prometheus/client_golang v1.19.1
go: added github.com/prometheus/client_model v0.5.0
go: added github.com/prometheus/common v0.48.0
go: added github.com/prometheus/procfs v0.12.0
go: added github.com/quic-go/qpack v0.5.1
go: added github.com/quic-go/quic-go v0.48.2
go: added github.com/zeebo/blake3 v0.2.4
go: added go.uber.org/mock v0.4.0
go: added go.uber.org/multierr v1.11.0
go: added go.uber.org/zap v1.27.0
go: added go.uber.org/zap/exp v0.3.0
go: added golang.org/x/crypto v0.31.0
go: added golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842
go: added golang.org/x/mod v0.18.0
go: added golang.org/x/net v0.33.0
go: added golang.org/x/sync v0.10.0
go: added golang.org/x/sys v0.28.0
go: added golang.org/x/term v0.27.0
go: added golang.org/x/text v0.21.0
go: added golang.org/x/time v0.7.0
go: added golang.org/x/tools v0.22.0
go: added google.golang.org/protobuf v1.35.1
2025/01/09 13:31:03 [INFO] exec (timeout=0s): /usr/bin/go get -v -ldflags -w -s -extldflags=-static  
2025/01/09 13:31:06 [INFO] Build environment ready
2025/01/09 13:31:06 [INFO] exec (timeout=0s): /usr/bin/go list -m github.com/caddyserver/caddy/v2 -ldflags -w -s -extldflags=-static 
go: module -ldflags: not a known dependency
go: module -w -s -extldflags=-static: not a known dependency
2025/01/09 13:31:06 [INFO] Cleaning up temporary folder: /tmp/buildenv_2025-01-09-1331.2596660125
2025/01/09 13:31:06 [FATAL] exit status 1

@mohammed90
Copy link
Member

What do you mean "not working"? What happens? What's printed?

@YoungMind1
Copy link
Author

@mohammed90 Oh, I totally forgot to add context :). description is updated.

@mohammed90 mohammed90 added the bug Something isn't working label Jan 9, 2025
@mohammed90
Copy link
Member

Found it. It's because the flags are appended to the end as argument instead of right after the sub-command of go, so the go command assumes it's a Go module in that position. The relevant code paths are here, if anyone wants to beat us to the fix:

xcaddy/builder.go

Lines 98 to 100 in eb894be

if b.OS == "windows" {
// get version string, we need to parse the output to get the exact version instead tag, branch or commit
cmd := buildEnv.newGoBuildCommand(ctx, "list", "-m", buildEnv.caddyModulePath)

xcaddy/environment.go

Lines 249 to 252 in eb894be

func (env environment) newGoBuildCommand(ctx context.Context, args ...string) *exec.Cmd {
cmd := env.newCommand(ctx, utils.GetGo(), args...)
return parseAndAppendFlags(cmd, env.buildFlags)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants