Skip to content

Return a wrapped fs.ErrNotExist if no spec directories are defined#309

Open
elezar wants to merge 1 commit intocncf-tags:mainfrom
elezar:return-fs-err-no-exits
Open

Return a wrapped fs.ErrNotExist if no spec directories are defined#309
elezar wants to merge 1 commit intocncf-tags:mainfrom
elezar:return-fs-err-no-exits

Conversation

@elezar
Copy link
Contributor

@elezar elezar commented Feb 19, 2026

See the discussion here: #308 (comment)

Here we return a wrapped fs.ErrNotExist error if there are no spec directories defined instead of a raw error that cannot be queried easily by the caller.

@elezar
Copy link
Contributor Author

elezar commented Feb 20, 2026

Thanks @klihub. While working on this, I was wondering whether we want to more strictly define (i.e. return an error from Configure) when no spec directories are defined? With that said, maybe this is a way for a user to instantiate an in-memory cache, but I would rather make that explicit rather than implicit.

@klihub
Copy link
Contributor

klihub commented Feb 20, 2026

Thanks @klihub. While working on this, I was wondering whether we want to more strictly define (i.e. return an error from Configure) when no spec directories are defined?

Yeah, at least my gut feeling is that it sounds like a reasonable idea.

Another option would be to update and document the cdi.WithSpecDirs option to ignore an empty director list and instead run with the defaults... since IFAICT using an explicit cdi.WithSpecDirs() is the only way how you can end up in this situation.

@elezar
Copy link
Contributor Author

elezar commented Feb 20, 2026

Another option would be to update and document the cdi.WithSpecDirs option to ignore an empty director list and instead run with the defaults... since IFAICT using an explicit cdi.WithSpecDirs() is the only way how you can end up in this situation.

I think this is a cleaner solution. Created #310 to do this.

@elezar elezar marked this pull request as draft February 20, 2026 13:32
@elezar elezar force-pushed the return-fs-err-no-exits branch from 2026e9b to bb2fc73 Compare February 20, 2026 13:32
@elezar
Copy link
Contributor Author

elezar commented Feb 20, 2026

rebased.

@elezar elezar marked this pull request as ready for review February 20, 2026 13:33
specDir, _ = c.highestPrioritySpecDir()
if specDir == "" {
return errors.New("no Spec directories to remove from")
return fmt.Errorf("no Spec directories defined: %w", fs.ErrNotExist)
Copy link
Contributor

Choose a reason for hiding this comment

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

the fix looks good to me, but how about adding test case for this?

err := cache.RemoveSpec("something")
assert.True(t, errors.Is(err, fs.ErrNotExist))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have added a basic test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@klihub as also pointed out by the test, we do have a slight disconnect here. In the case where the spec dirs are empty, we return ErrNotExist, but in the case where os.Remove fails for the spec we are trying to remove due to ErrNotExist we return nil. Should we always return nil (or ErrNotExist) for consistency?

Copy link
Contributor

Choose a reason for hiding this comment

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

@elezar Yes, probably would make sense. Returning ErrNotExist as such would provide transparency for RemoveSpec() while returnin nil for ErrNotExist would provide idempotency. From the top of my head I don't know which would be better.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think at this stage switching to returning ErrNotExists will end up breaking producers that use this code. I'm not sure if that's something we want to do. Let me add a comment to the code to this effect.

Also, as a follow-up, does this mean that we should rather return nil if there are no spec directories (especially given that this is now a case that we don't expect to ever occur)?

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, as a follow-up, does this mean that we should rather return nil if there are no spec directories (especially given that this is now a case that we don't expect to ever occur)?

On the RemoveSpec() code path I think we could return nil if we have no spec directories if we return nil when there is no spec file to remove, as the former implies the latter.

I think we still might get into the situation when there are no spec dirs, because someone might nuke them while we are running.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think at this stage switching to returning ErrNotExists will end up breaking producers that use this code. I'm not sure if that's something we want to do. Let me add a comment to the code to this effect.

Yes, I also think we should try to be extra conservative even when it feels like PITA. We're post 1.0, so we should not be making breaking changes. Even if it's not immediately obvious why and how someone could rely on existing functionality so that it would break if we start returning ErrNotExists, it's better not to risk especially if there is not much to gain by the change.

@klihub klihub self-requested a review February 23, 2026 16:47
@@ -321,7 +321,7 @@ func (c *Cache) RemoveSpec(name string) error {

specDir, _ = c.highestPrioritySpecDir()
if specDir == "" {
Copy link
Contributor

Choose a reason for hiding this comment

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

@elezar Can this still happen with #310 merged ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It shouldn't be possible, no.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Does that mean we want to update the highestPrioritySpecDir implementation to not return an error?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it does not return an error. It returns a directory and its priority.

Signed-off-by: Evan Lezar <elezar@nvidia.com>
@elezar elezar force-pushed the return-fs-err-no-exits branch from bb2fc73 to 092f1f9 Compare March 4, 2026 10:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants