Skip to content

Commit

Permalink
Improve build config issue error scenario ko-build#483 (ko-build#487)
Browse files Browse the repository at this point in the history
* 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`.
  • Loading branch information
HeavyWombat authored Oct 27, 2021
1 parent 00d0a34 commit b9f9268
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
4 changes: 4 additions & 0 deletions pkg/build/gobuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
11 changes: 11 additions & 0 deletions pkg/commands/options/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit b9f9268

Please sign in to comment.