Skip to content

Conversation

@gajeshbhat
Copy link
Contributor

This PR improves the error message for invalid slice names to provide clearer guidance to users about the validation requirements, as requested in #158.

Changes

  • Enhanced the error message in internal/setup/yaml.go to clarify that slice names must be at least 3 characters long
  • Added detailed explanation of all slice name validation requirements in the error message
  • Added unit test to verify that the improved error message is displayed correctly

Before

error: invalid slice name cc in slices/base-distroless.yaml

After

error: invalid slice name cc in slices/base-distroless.yaml (slice names must be at least 3 characters long, start with a lowercase letter, and contain only lowercase letters, digits, and hyphens)

Testing

  • All existing tests pass
  • Added a new unit test case for the improved error message
  • Manually verified that the error message appears correctly for various invalid slice names

Fixes #158

@github-actions
Copy link

github-actions bot commented Aug 16, 2025

Command Mean [s] Min [s] Max [s] Relative
BASE 8.829 ± 0.049 8.757 8.890 1.00
HEAD 8.833 ± 0.025 8.802 8.868 1.00 ± 0.01

@letFunny letFunny self-requested a review August 21, 2025 07:58
Copy link
Collaborator

@letFunny letFunny left a comment

Choose a reason for hiding this comment

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

Hi! Thank you for taking the time to work on this. I agree that we should improve the error messages to some extent to improve the UX. At the same time, writing prose to describe the regular expression might not be ideal, it will always be less precise and too verbose.

Let's see if we can create a message that is short and simple.

match := apacheutil.SnameExp.FindStringSubmatch(sliceName)
if match == nil {
return nil, fmt.Errorf("invalid slice name %q in %s", sliceName, pkgPath)
return nil, fmt.Errorf("invalid slice name %q in %s (slice names must be at least 3 characters long, start with a lowercase letter, and contain only lowercase letters, digits, and hyphens)", sliceName, pkgPath)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is not strictly true, the current regular expression doesn't match c-- for example, even though it is 3 characters long, starts with a lowercase letter and contains only lowercase letters and hyphens.

See regexr.com/8gm94.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you @letFunny for the regex link! It makes more sense now! Regex is always tricky. Looking at the regex ^([a-z](?:-?[a-z0-9]){2,})$ more carefully, I agree with you that "must be at least 3 characters" is still imprecise - strings like c-- or d- would fail for reasons beyond just length. I've updated the message to focus on the minimum length requirement since that was the specific confusion reported in issue #158.

However, we need to create a message that is concise and straightforward. What's the most common slice name validation error you've seen reported in community forums or support channels? Is it typically the length issue, or do users frequently encounter other validation failures?

Happy to work with you to update something more meaningful and useful to the user. Cheers.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Hey @gajeshbhat, thank you for making the changes. I agree with you the regex is very tricky and we should focus on the most intuitive requirements. At the same time, there is a balance to strike here, we shouldn't only mention one of the possible errors, let's try to mention all of them.

I had the chance of brainstorming about improving the UX today with @niemeyer and we landed on the following error message which is pretty sweet:

invalid slice name %q in %s (start with a-z, len >= 3, only a-z / 0-9 / -)

It captures all the common errors and, at the same time, it follows the style of your original PR of being easy to read. What do you think? Can we use this one?

Copy link
Contributor Author

@gajeshbhat gajeshbhat Aug 26, 2025

Choose a reason for hiding this comment

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

Thank you @letFunny, for brainstorming on this. I like the concise format you have proposed, but don't you think: "only a-z / 0-9 / -" would mislead the users since strings like config- or ab- contain only those characters but still fail because hyphens must be followed by a letter/digit? How about something like:

invalid slice name %q in %s (start with a-z, len >= 3, use a-z/0-9 (- allowed between chars) )

Copy link
Collaborator

Choose a reason for hiding this comment

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

I agree that this doesn't specify all the rules. At the same time it is hard to strike a balance between being precise and being helpful. If we wanted to be precise I would print the regex directly or put it in the documentation, that leaves no room for interpretation. By adding more rules you complicate it a bit because what does "between chars" mean, it is a bit ambiguous.

I would say let's keep the one I posted for now and we can always come back to this if there is a need for more descriptive message. If that is the case then documenting it more thoroughly in the reference documentation seems like an even better idea. Right now there is a paragraph there but it is not very precise: https://documentation.ubuntu.com/chisel/en/latest/reference/chisel-releases/slice-definitions/.

Copy link
Contributor Author

@gajeshbhat gajeshbhat Aug 27, 2025

Choose a reason for hiding this comment

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

I see your point about the precision vs. helpfulness trade-off. You're right that "between chars" is also ambiguous, and trying to be perfectly precise in an error message would make it unwieldy. I've updated the PR with your suggested message. Thank you for helping improve this PR. I have contributed to Canonical projects before, hence the CLA check should pass once approved. Please let me know if you have any questions. Cheers.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

On a side note, I went ahead and created an Issue #241 to update the documentation. I am happy to pick this up sometime later.

Copy link
Collaborator

Choose a reason for hiding this comment

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

We had the same idea about the documentation, hours before your issue I created a PR to fix it :)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thank you for contributing

@gajeshbhat gajeshbhat force-pushed the fix-slice-name-error-message-158 branch from ac9f29a to 62e6572 Compare August 23, 2025 08:54
@gajeshbhat gajeshbhat requested a review from letFunny August 23, 2025 08:59
- Add improved error message
- Add unit test to verify the improved error message

Fixes canonical#158
Copy link
Collaborator

@letFunny letFunny left a comment

Choose a reason for hiding this comment

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

Thank you for looking into improving the UX @gajeshbhat! LGTM

@letFunny letFunny added Simple Nice for a quick look on a minute or two Polish Refactorings, etc labels Aug 28, 2025
niemeyer

This comment was marked as outdated.

@gajeshbhat gajeshbhat changed the title fix: improve slice name validation error message clarity fix: improve invalid slice name error Aug 28, 2025
@gajeshbhat
Copy link
Contributor Author

gajeshbhat commented Aug 28, 2025

Looks good, thanks.

Thank you for the review @niemeyer . Appreciate you taking the time.

@niemeyer niemeyer merged commit 680d53c into canonical:main Aug 29, 2025
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Polish Refactorings, etc Simple Nice for a quick look on a minute or two

Projects

None yet

Development

Successfully merging this pull request may close these issues.

invalid slice name error should clarify min valid length is 3

3 participants