Skip to content

bake: check condition and error_message are set during validation#3101

Merged
tonistiigi merged 1 commit intodocker:masterfrom
crazy-max:bake-validation-fixes
Apr 9, 2025
Merged

bake: check condition and error_message are set during validation#3101
tonistiigi merged 1 commit intodocker:masterfrom
crazy-max:bake-validation-fixes

Conversation

@crazy-max
Copy link
Copy Markdown
Member

fixes #3100

We forgot to check if condition and error_message are set and also check if result value has the expected type.

cc @rcjsuen

@crazy-max crazy-max added this to the v0.23.0 milestone Apr 9, 2025
@crazy-max crazy-max self-assigned this Apr 9, 2025
Copy link
Copy Markdown
Contributor

@rcjsuen rcjsuen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@crazy-max Could you try to add this test? What should be the expected case if condition = false? It seems like if the user puts that in they will get This check failed, but has an invalid error message?

func TestHello(t *testing.T) {
	fp := File{
		Name: "docker-bake.hcl",
		Data: []byte(`
variable "PORT" {
  default = 3000
  validation {
    condition = false
  }
}
`),
	}

	_, _, err := ReadTargets(context.TODO(), []File{fp}, []string{"app"}, nil, nil, &EntitlementConf{})
	require.Error(t, err)
	fmt.Println(">>>>>>")
	fmt.Println(err)
	fmt.Println(">>>>>>")
	require.Contains(t, err.Error(), "Condition expression must return either true or false, not null")
}

@crazy-max
Copy link
Copy Markdown
Member Author

crazy-max commented Apr 9, 2025

It seems like if the user puts that in they will get This check failed, but has an invalid error message?

Yes because the error message is required similar to how terraform does when evaluating conditions: https://github.com/hashicorp/terraform/blob/ea767aa7add3e14b7d292efead3ef23d25cd49e1/internal/terraform/eval_conditions.go#L190-L193

@rcjsuen
Copy link
Copy Markdown
Contributor

rcjsuen commented Apr 9, 2025

It seems like if the user puts that in they will get This check failed, but has an invalid error message?

Yes because the error message is required similar to how terraform does when evaluating conditions: https://github.com/hashicorp/terraform/blob/ea767aa7add3e14b7d292efead3ef23d25cd49e1/internal/terraform/eval_conditions.go#L190-L193

Gotcha, another Terraform thing, eh? Thanks for clarifying. 👍

@tonistiigi tonistiigi merged commit d69301d into docker:master Apr 9, 2025
139 checks passed
@crazy-max crazy-max deleted the bake-validation-fixes branch April 9, 2025 15:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Validation block with nothing will crash the HCL parser

4 participants