Skip to content

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

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

Merged
merged 1 commit into from
Apr 9, 2025

Conversation

crazy-max
Copy link
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

@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
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

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
140 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