Skip to content

Indexes 2: Add new_unchecked() constructors to spk schema objects#1337

Open
dcookspi wants to merge 4 commits intomainfrom
index-2-new-unchecked-additions
Open

Indexes 2: Add new_unchecked() constructors to spk schema objects#1337
dcookspi wants to merge 4 commits intomainfrom
index-2-new-unchecked-additions

Conversation

@dcookspi
Copy link
Copy Markdown
Collaborator

@dcookspi dcookspi commented Mar 19, 2026

Note: for info on benefits of indexing for spk solves see #1340 (5 of 5). Maybe start there and work back down to this PR if you prefer to review PRs top down.

This adds new_unchecked() constructor methods to various spk schema objects. These allow direct creation of those objects from other existing object pieces, e.g. from other pieces of those objects in index data. This is one of the changes that supports adding indexes and index based packages to Spk repositories. It allows indexes to avoid reparsing data from text for some objects.

This is 2 of 5 chained PRs for adding indexes to spk solves:

  1. Indexes 1: Change Package and related traits to not return references to fields #1336
  2. this PR
  3. Indexes 3: Adds flatbuffers schema and SolverPackageSpec for indexes to spk #1338
  4. Indexes 4: Adds Indexes for SPK repositories #1339
  5. Indexes 5: Adds spk repo index subcommand for index generation and updates #1340
  6. Indexes 6: Changes version_filter field in index schema #1344
  7. Indexes 7: Adds a lock file around index generation and updates #1354
  8. Indexes 8: Fixes the spk build or mkb crash with indexes enabled #1355

@dcookspi dcookspi self-assigned this Mar 19, 2026
@dcookspi dcookspi added SPI AOI Area of interest for SPI pr-chain This PR doesn't target the main branch, don't merge! labels Mar 19, 2026
Comment thread crates/spk-schema/crates/foundation/src/version/compat/mod.rs
Comment thread crates/spk-schema/crates/foundation/src/version_range/mod.rs
}

// Allow tests to manufacture owned instances with known good values.
// Allow tests and indexes to manufacture owned instances with known good values.
Copy link
Copy Markdown
Collaborator Author

@dcookspi dcookspi Mar 19, 2026

Choose a reason for hiding this comment

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

No new_checked() methods were added for these String based types here. But in a later indexes PRs there are several uses of unsafe { ... } around an existing method on these types. It may be we want to fold those into a new_unchecked() method for non-test cases use.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Big picture I think we want to be able to say that values in the index were already validated before put into the index and therefore should be trusted without having to re-validate them when reading the index.

However we need a way to introduce changes to spk that may change validation rules. For example let's say we stop allowing '-' in package names. How do we want to put some kind of version number in the index metadata so at runtime the spk process can check that the index conforms to its expectations?

I'm interested in having a way to validate the whole index at once, in a sense, instead of having to validate every individual value found in the index.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I've added unsafe's to the new_checked methods and updated the callers to use unsafe blocks around them.

Validating the index will be addressed in PR3 (#1338).

Comment thread crates/spk-schema/crates/foundation/src/version/compat/mod.rs Outdated
@dcookspi dcookspi added the enhancement New feature or request label Mar 19, 2026
@dcookspi dcookspi requested review from jrray and rydrman March 19, 2026 22:53
Copy link
Copy Markdown
Collaborator

@jrray jrray left a comment

Choose a reason for hiding this comment

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

This is gonna hurt...

}

// Allow tests to manufacture owned instances with known good values.
// Allow tests and indexes to manufacture owned instances with known good values.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Big picture I think we want to be able to say that values in the index were already validated before put into the index and therefore should be trusted without having to re-validate them when reading the index.

However we need a way to introduce changes to spk that may change validation rules. For example let's say we stop allowing '-' in package names. How do we want to put some kind of version number in the index metadata so at runtime the spk process can check that the index conforms to its expectations?

I'm interested in having a way to validate the whole index at once, in a sense, instead of having to validate every individual value found in the index.

Comment thread crates/spk-schema/crates/foundation/src/ident_build/build_id.rs Outdated
Comment thread crates/spk-schema/crates/foundation/src/ident_ops/parsing/ident.rs Outdated
Comment thread crates/spk-schema/crates/foundation/src/version/compat/mod.rs
Comment thread crates/spk-schema/crates/foundation/src/version/compat/mod.rs Outdated
Comment thread crates/spk-schema/src/component_embedded_packages.rs Outdated
Comment thread crates/spk-schema/src/component_spec.rs Outdated
Comment thread crates/spk-schema/src/option.rs Outdated
Comment thread crates/spk-schema/src/option.rs Outdated
Comment thread crates/spk-schema/src/requirements_list.rs Outdated
Comment on lines +704 to +707
/// # Safety
///
/// The caller must ensure the string parses as a valid compat.
pub unsafe fn new_unchecked(compat: &str) -> Result<Self> {
Copy link
Copy Markdown
Collaborator

@jrray jrray Apr 2, 2026

Choose a reason for hiding this comment

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

This uses from_str and returns a Result. Is it really unsafe? It has to successfully parse so I don't think using this skips any validation.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I've removed the unsafe keyword for now. I'll will come back if/when the compat representations in the index change from string to an ordered list of compat rules entries - which is a possible future extension.

/// The caller must make sure the string can be parsed as a valid
/// Version.
pub unsafe fn new_unchecked(version_str: &str) -> Result<Self> {
Version::try_from(version_str)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Same thought here, really unsafe?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

To add to this, the followup question would be could the current users of Version::new_unchecked just switch to Version::try_from and then this constructor is not needed?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

It's not really unsafe here, I'll take it out.

For the followup question: Yes it could be replaced for this PR. But for one of the the later ones, or a coming improvement (I can't remember which, but one of them wull change the string in index storage to the component number pieces), the new_checked() implementation would change to directly set the fields in the Version struct. I suppose I could take it out and then put it back, but I wanted to put all the new_unchecked() methods in one PR.

Copy link
Copy Markdown
Collaborator Author

@dcookspi dcookspi Apr 7, 2026

Choose a reason for hiding this comment

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

I've taken the unsafe out.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

As above, remove the safety notice and rename to something that doesn't use "unchecked".

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I've updated this.

Comment on lines +112 to +113
/// The caller must make sure the pieces combine to make a valid
/// EmbeddedPackageSpec.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I feel the need to say that this safety message (and the others) amount to saying "you have responsibilities to use this correctly" but without giving any details on how to use it correctly. I get it that you don't necessarily know what those are. I couldn't tell you what they are right now on the spot either.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yes, the safety messages are a linting requirement. Looking up the specific details is very much left to the caller. I couldn't say what they are for the different cases either - other than make sure you pass valid x, y, z objects in. I suspect the actual details could be found in the parsing methods, maybe?

Comment thread crates/spk-schema/src/requirements_list.rs Outdated
Copy link
Copy Markdown
Collaborator

@jrray jrray left a comment

Choose a reason for hiding this comment

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

Typo, and maybe some things are unsafe that don't have to be.

I'm not expecting you to update the safety statements but can you create an issue for it to acknowledge it?

@dcookspi dcookspi requested a review from jrray April 7, 2026 20:20
@dcookspi dcookspi force-pushed the index-1-package-trait-changes branch from 9bd9685 to cebade9 Compare April 17, 2026 17:29
Base automatically changed from index-1-package-trait-changes to main April 17, 2026 18:14
@dcookspi dcookspi force-pushed the index-2-new-unchecked-additions branch from f2be50a to 54465be Compare April 17, 2026 19:20
Comment on lines +704 to +707
/// # Safety
///
/// The caller must ensure the string parses as a valid compat.
pub fn new_unchecked(compat: &str) -> Result<Self> {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

You can (should) remove the safety notice for a function that is not unsafe.

IMO new or from_compat would be a better name for this, if it is even needed at all, at least up until the point where this evolves into something that is unsafe and unchecked.

Copy link
Copy Markdown
Collaborator Author

@dcookspi dcookspi Apr 30, 2026

Choose a reason for hiding this comment

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

I've updated this.

/// The caller must make sure the string can be parsed as a valid
/// Version.
pub unsafe fn new_unchecked(version_str: &str) -> Result<Self> {
Version::try_from(version_str)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

As above, remove the safety notice and rename to something that doesn't use "unchecked".

These allow for directly creating those objects from other existing
object pieces, e.g. from index data objects.

Signed-off-by: David Gilligan-Cook <dcook@imageworks.com>
Signed-off-by: David Gilligan-Cook <dcook@imageworks.com>
Signed-off-by: David Gilligan-Cook <dcook@imageworks.com>
…s and compat.

Signed-off-by: David Gilligan-Cook <dcook@imageworks.com>
@dcookspi dcookspi force-pushed the index-2-new-unchecked-additions branch from 54465be to ae936f5 Compare April 30, 2026 19:29
@dcookspi dcookspi requested a review from jrray April 30, 2026 19:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request pr-chain This PR doesn't target the main branch, don't merge! SPI AOI Area of interest for SPI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants