1
0
mirror of https://github.com/ko-build/ko.git synced 2024-11-24 08:12:26 +02:00

Improve build config issue error scenario #483 (#487)

* Add build config usage log statement

There is currently no indication whether `ko` picks one of the configured
build configurations from the `.ko.yaml` configuration file for a build.

Add log statement to print the build config being picked for the build.

Introduce default entry for build config `ID` in case it is not specified.

* Add path check for build configuration settings

Add `os.Stat` to verify that the path that is configured in the build
configuration entry is valid. As a side effect, this will print out an error
message in case someone sets an import path like `github.com/google/ko` in
the `main` field of the build config.

* Fix trimpath command line flag in README

Fixed wrong command line flag `--trimpath` to `-trimpath`.
This commit is contained in:
Matthias Diester 2021-10-27 17:09:41 +02:00 committed by GitHub
parent 00d0a34583
commit b9f92681ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 1 deletions

View File

@ -127,7 +127,7 @@ baseImageOverrides:
### Overriding Go build settings
By default, `ko` builds the binary with no additional build flags other than
`--trimpath` (depending on the Go version). You can replace the default build
`-trimpath`. You can replace the default build
arguments by providing build flags and ldflags using a
[GoReleaser](https://github.com/goreleaser/goreleaser) influenced `builds`
configuration section in your `.ko.yaml`.

View File

@ -695,6 +695,10 @@ func (g *gobuild) configForImportPath(ip string) Config {
config.Flags = append(config.Flags, "-gcflags", "all=-N -l")
}
if config.ID != "" {
log.Printf("Using build config %s for %s", config.ID, ip)
}
return config
}

View File

@ -134,6 +134,12 @@ func (bo *BuildOptions) LoadConfig() error {
func createBuildConfigMap(workingDirectory string, configs []build.Config) (map[string]build.Config, error) {
buildConfigsByImportPath := make(map[string]build.Config)
for i, config := range configs {
// In case no ID is specified, use the index of the build config in
// the ko YAML file as a reference (debug help).
if config.ID == "" {
config.ID = fmt.Sprintf("#%d", i)
}
// Make sure to behave like GoReleaser by defaulting to the current
// directory in case the build or main field is not set, check
// https://goreleaser.com/customization/build/ for details
@ -154,6 +160,11 @@ func createBuildConfigMap(workingDirectory string, configs []build.Config) (map[
path = filepath.Dir(config.Main)
}
// Verify that the path actually leads to a local file (https://github.com/google/ko/issues/483)
if _, err := os.Stat(filepath.Join(baseDir, path)); err != nil {
return nil, err
}
// By default, paths configured in the builds section are considered
// local import paths, therefore add a "./" equivalent as a prefix to
// the constructured import path