Skip to content

Commit

Permalink
Limit the number of warnings shown when using COMMAND keyword (#3586)
Browse files Browse the repository at this point in the history
The previous version was too noisy.
This changes it to show 3 times max and only once per file.

---------

Co-authored-by: Vlad A. Ionescu <[email protected]>
  • Loading branch information
idodod and vladaionescu authored Dec 11, 2023
1 parent 23edc7e commit ea16f40
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ All notable changes to [Earthly](https://github.com/earthly/earthly) will be doc
- A warning when a `COPY` destination includes a tilde (~). Related to [#1789](https://github.com/earthly/earthly/issues/1789).
- A hint message to suggest the usage of `-i` flag to debug the build when a RUN command fails.

### Fixed
- Limit the number of deprecation warnings when using `COMMAND` instead of `FUNCTION` keyword.

### Changed
- Changed the color used to print metadata values (such as ARGs values) in the build log to Faint Blue.
- Updated default alpine/git image to v2.40.1.
Expand Down
1 change: 1 addition & 0 deletions builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ func (b *Builder) convertAndBuild(ctx context.Context, target domain.Target, opt
MainTargetDetailsFunc: opt.MainTargetDetailsFunc,
Runner: opt.Runner,
ProjectAdder: opt.ProjectAdder,
FilesWithCommandRenameWarning: make(map[string]bool),
}
mts, err = earthfile2llb.Earthfile2LLB(childCtx, target, opt, true)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions earthfile2llb/earthfile2llb.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ type ConvertOpt struct {

// ProjectAdder is a callback that is used to discover PROJECT <org>/<project> values
ProjectAdder ProjectAdder

// FilesWithCommandRenameWarning keeps track of the files for which the COMMAND => FUNCTION warning was displayed
// this can be removed in VERSION 0.8
FilesWithCommandRenameWarning map[string]bool
}

// TargetDetails contains details about the target being built.
Expand Down
16 changes: 10 additions & 6 deletions earthfile2llb/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import (
"github.com/pkg/errors"
)

const maxCommandRenameWarnings = 3

var errCannotAsync = errors.New("cannot run async operation")

// Interpreter interprets Earthly AST's into calls to the converter.
Expand Down Expand Up @@ -1794,7 +1796,8 @@ func (i *Interpreter) handleDo(ctx context.Context, cmd spec.Command) error {

for _, uc := range bc.Earthfile.Functions {
if uc.Name == command.Command {
return i.handleDoFunction(ctx, command, relCommand, uc, cmd, parsedFlagArgs, allowPrivileged, opts.PassArgs, bc.Features.UseFunctionKeyword)
sourceFilePath := bc.Ref.ProjectCanonical() + "/Earthfile"
return i.handleDoFunction(ctx, command, relCommand, uc, cmd, parsedFlagArgs, allowPrivileged, opts.PassArgs, sourceFilePath, bc.Features.UseFunctionKeyword)
}
}
return i.errorf(cmd.SourceLocation, "user command %s not found", ucName)
Expand Down Expand Up @@ -1983,7 +1986,7 @@ func (i *Interpreter) handleHost(ctx context.Context, cmd spec.Command) error {

// ----------------------------------------------------------------------------

func (i *Interpreter) handleDoFunction(ctx context.Context, command domain.Command, relCommand domain.Command, uc spec.Function, do spec.Command, buildArgs []string, allowPrivileged, passArgs bool, useFunctionCmd bool) error {
func (i *Interpreter) handleDoFunction(ctx context.Context, command domain.Command, relCommand domain.Command, uc spec.Function, do spec.Command, buildArgs []string, allowPrivileged, passArgs bool, sourceLocationFile string, useFunctionCmd bool) error {
cmdName := "FUNCTION"
if !useFunctionCmd {
cmdName = "COMMAND"
Expand All @@ -1994,11 +1997,12 @@ func (i *Interpreter) handleDoFunction(ctx context.Context, command domain.Comma
if len(uc.Recipe) == 0 || uc.Recipe[0].Command == nil || uc.Recipe[0].Command.Name != cmdName {
return i.errorf(uc.SourceLocation, "%s recipes must start with %s", strings.ToLower(cmdName), cmdName)
}
if !useFunctionCmd {
i.console.Warnf(
if !useFunctionCmd && len(i.converter.opt.FilesWithCommandRenameWarning) < maxCommandRenameWarnings && !i.converter.opt.FilesWithCommandRenameWarning[sourceLocationFile] {
i.console.Printf(
`Note that the COMMAND keyword will be replaced by FUNCTION starting with VERSION 0.8.
To start using the FUNCTION keyword now (experimental) please use VERSION --use-function-keyword 0.7. Note that switching now may cause breakages for your colleagues if they are using older Earthly versions.
`)
To start using the FUNCTION keyword now (experimental) please use VERSION --use-function-keyword 0.7 in %s. Note that switching now may cause breakages for your colleagues if they are using older Earthly versions.
`, sourceLocationFile)
i.converter.opt.FilesWithCommandRenameWarning[sourceLocationFile] = true
}
if len(uc.Recipe[0].Command.Args) > 0 {
return i.errorf(uc.Recipe[0].SourceLocation, "%s takes no arguments", cmdName)
Expand Down

0 comments on commit ea16f40

Please sign in to comment.