Skip to content

Commit a01359f

Browse files
committed
fix(build): run xgo from project root so its package arg resolves cleanly
xgo treats its last positional arg as a Go package path. Running it from build/ with `../` confused Go's module resolution — it tried to find a package inside the build module instead of switching to the project's own go.mod, failing with: package code.vikunja.io/build: build constraints exclude all Go files in /go/src/code.vikunja.io/api/build Use exec.CommandContext with cmd.Dir set to the project's absolute root, and pass p.BuildPath ("." or "./cmd/veans") as the package arg — the exact pattern the per-project magefiles used before centralization. -dest is absolutized so it still ends up under <project>/dist/binaries regardless of cwd.
1 parent 4e73694 commit a01359f

1 file changed

Lines changed: 22 additions & 7 deletions

File tree

build/magefile.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -469,23 +469,38 @@ func xgoOutName(p *project, version string) string {
469469
return p.Executable + "-" + versionTagOrUnstable(version)
470470
}
471471

472-
func runXgo(_ context.Context, p *project, version, targets string) error {
472+
func runXgo(ctx context.Context, p *project, version, targets string) error {
473473
extraLdflags := `-linkmode external -extldflags "-static" `
474474
// xgo's darwin builds can't use the static external linker.
475475
if strings.HasPrefix(targets, "darwin") {
476476
extraLdflags = ""
477477
}
478-
// xgo's last arg is the path to the Go main package. From build/, the
479-
// project's main is at Root/BuildPath.
480-
buildTarget := filepath.Join(p.Root, p.BuildPath)
481-
return sh.RunV("xgo",
482-
"-dest", projectDist(p, subBin),
478+
// xgo resolves its last arg as a Go package path. Running it from build/
479+
// with `../` confuses the module resolution (it tries to find a package
480+
// inside this build module). Invoke xgo from the project root so we can
481+
// pass p.BuildPath ("." or "./cmd/veans") just like the original
482+
// per-project magefiles did.
483+
absRoot, err := filepath.Abs(p.Root)
484+
if err != nil {
485+
return fmt.Errorf("resolve project root: %w", err)
486+
}
487+
absDest, err := filepath.Abs(projectDist(p, subBin))
488+
if err != nil {
489+
return fmt.Errorf("resolve dest dir: %w", err)
490+
}
491+
//nolint:gosec // mage helper; args are derived from the static project table above.
492+
cmd := exec.CommandContext(ctx, "xgo",
493+
"-dest", absDest,
483494
"-tags", p.BuildTags,
484495
"-ldflags", extraLdflags+p.Ldflags(version),
485496
"-targets", targets,
486497
"-out", xgoOutName(p, version),
487-
buildTarget,
498+
p.BuildPath,
488499
)
500+
cmd.Dir = absRoot
501+
cmd.Stdout = os.Stdout
502+
cmd.Stderr = os.Stderr
503+
return cmd.Run()
489504
}
490505

491506
func xgoAllOS(ctx context.Context, p *project, version string) error {

0 commit comments

Comments
 (0)