Skip to content
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

[Traits] Change TraitConfiguration to be an enum instead of struct #8370

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

bripeticca
Copy link
Contributor

@bripeticca bripeticca commented Mar 13, 2025

Fixes #8355.

Since the way a TraitConfiguration is set up can affect how traits are calculated and whether default traits are overridden or not, it's more ergonomic to change this to an enum that lists the various possible cases of configuration:

  • No trait configuration, which defaults to using default traits if applicable
  • A configuration with an empty set of traits that overrides the default traits
  • A configuration that enables all traits
  • A configuration that specifies an exact set of traits that are enabled

This way we can avoid doing multiple checks on the properties of the struct and instead rely on the exact case that describes how to continue calculating enabled traits.

The refactoring done as a part of this change also fixes #8356.

To phase in the modification to TraitConfiguration, start with
the new enum and match the inits configured for the legacy
TraitConfiguration struct
…method

* enabledTraits will now instead default to using the new trait config
* comment out enabledTraits legacy method in favour of enabledTraits2;
  begin using this method instead. To rename to enabledTraits.
* remove optional TraitConfiguration parameters, since enum encapsulates
  a "no configuration" case.

TODO:
* Must still clean up commented out code
* Ensure behaviour still operates as expected
* Add tests for calculateAllEnabledTraits, as this is now public
* Ensure checks on TraitConfiguration aren't redundant i.e.
  make appropriate use of the new structure
* Pre-calculate enabled traits across the root with the TraitConfiguration
  and store where appropriate so we don't have to recalculate this constantly
- Since we will calculate all enabled traits for root manifests
  in the PackageGraphRoot init (which can throw depending on
  whether there is a non-allowable trait configuration passed),
  allow the init to throw this error as well.
- Clean up some commented code
- Replace redundant calculations of enabled traits since we now
  store the calculated traits in the PackageGraphRoot

TODO:
- Address disallow disabling traits test not catching error
- More cleanup of commented out code
Copy link
Contributor

@bkhouri bkhouri left a comment

Choose a reason for hiding this comment

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

I have initial question on the enum TraitConfiguration.

- Updated the test that asserts for an error thrown when disabling
  default traits for a package with no traits; prior, there was a
  chance to encounter a false positive if there was no error
  being thrown due to the do/catch block. Now, we assert for an
  error being thrown and check that it's the expected error message.
- Changed the case names for TraitConfiguration:
    - noConfiguration -> none
    - noEnabledTraits -> disableAllTraits
- Added optional parameter to calculateAllEnabledTraits entitled
  'parentPackage' that keeps a reference to the parent package
  (if any) when computing the enabled traits of a package; this
  way, we can meaningfully report an error if a parent package
  sets an unallowable set of traits to be enabled/disabled for
  a dependency.
- Move the trait helper and validation methods to a separate file
  (Manifest/Manifest+Traits.swift) and move the TraitConfiguration
  into the PackageModel instead of PackageGraph; updated the
  appropriate CMakeLists.txt
- Update TraitConfiguration computed variable for enabled traits;
  '.none' case should return '["default"]', as that is what its
   behaviour defaults to. Removed other computed variables in
  favor of checking the enum cases themselves where appropriate.
- To support further testing on traits and their possible configurations,
  set traits where appropriate in the MockDependency + make
  PackageDependency.Trait public to allow its use in the mocks.
- Fixed up the error messages displayed for traits
- Added more tests to cover various possible configurations of traits
@bripeticca
Copy link
Contributor Author

@swift-ci please test

@bripeticca
Copy link
Contributor Author

@swift-ci please test

@dschaefer2
Copy link
Member

Definitely cleans things up. Let me know when it's ready and I'll do a final run through.

@bripeticca
Copy link
Contributor Author

@swift-ci please test windows

@bripeticca
Copy link
Contributor Author

@swift-ci please test self hosted windows

@bripeticca
Copy link
Contributor Author

@swift-ci please test self hosted windows

- Cleaned up commented code
- Assure that enabledTraits on the PackageGraphRoot also considers
  the root's dependencies enabled traits, specified by the root itself
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants