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

defaultFlags is ignored #1450

Open
rh-hemartin opened this issue Nov 22, 2024 · 3 comments · May be fixed by #1464
Open

defaultFlags is ignored #1450

rh-hemartin opened this issue Nov 22, 2024 · 3 comments · May be fixed by #1464

Comments

@rh-hemartin
Copy link

rh-hemartin commented Nov 22, 2024

Hello! I'm trying to use defaultFlags but it looks like its ignored.

Here the reproducer:

mkdir ko-reproducer
cd ko-reproducer

cat <<EOF > main.go
package main

import "fmt"

func main() {
        fmt.Println("Hello World!")
}
EOF
go mod init example.com
go mod tidy
go build -cover .
GOCOVERDIR=hello ./example.com

This results in this error:

error: coverage meta-data emit failed: output directory "hello" inaccessible (err: stat hello: no such file or directory); no coverage data written
Hello World!
error: coverage counter data emit failed: output directory "hello" inaccessible (err: stat hello: no such file or directory); no coverage data written

Now with ko:

cat <<EOF > .ko.yaml
---
defaultFlags:
  - -cover
EOF

KOCACHE=kocache ko build . --push=false
GOCOVERDIR=hello ./kocache/bin/example.com/linux/amd64/out

Does not fail:

Hello World!

Now using "normal" flags:

cat <<EOF > .ko.yaml
---
builds:
- id: main
  main: ./
  flags:
    - -cover
EOF

rm -rf ./kocache
KOCACHE=kocache ko build . --push=false
GOCOVERDIR=hello ./kocache/bin/example.com/linux/amd64/out
error: coverage meta-data emit failed: output directory "hello" inaccessible (err: stat hello: no such file or directory); no coverage data written
Hello World!
error: coverage counter data emit failed: output directory "hello" inaccessible (err: stat hello: no such file or directory); no coverage data written

PD: I tested introducing a random string into defaultFlags and it does not fail, so its effectively ignored.
PD2: I can give it a go and try to fix it myself, but I want someone to confirm it first.

@jigar-joshi-nirmata
Copy link

I am facing something similar with the GOEXPERIMENT variable :(
Can you please help me out too, in case you found any solutions?

@rh-hemartin
Copy link
Author

I am facing something similar with the GOEXPERIMENT variable :( Can you please help me out too, in case you found any solutions?

Hey! The solution is already on this very same issue: you need to use the flags in each of your builds, instead of using defaultFlags as top level key. You could try to export the variable before using ko too, like:

export GOEXPERIMENT=<value>
ko build [...]

@rh-hemartin rh-hemartin linked a pull request Dec 12, 2024 that will close this issue
@rh-hemartin
Copy link
Author

I found that is likely that defaultFlags is never used.

By default ko uses -trimpath as an argument for go build and this is added to the config.Flags slice1. Then defaultFlags is used if config.Flags is not empty2, which contains -trimpath by default. So defaultFlags is not used unless you specifcy you want to preserve paths.

I think defaultFlags should be skipped if there are flags specified in the config file. The fact that the command line arguments interact here it is a little weird.

My idea is to move the defaultFlags usage to another function, and add it before trimpath gets injected, so defaultFlags is used if no flags where added to the configfile. After that, trimpath and other flags will be injected as normal. See the related PR #1464

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants