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

resolve_latest_keg should return the latest HEAD keg when no stable kegs exist #11824

Merged
merged 4 commits into from Aug 8, 2021

Conversation

cnnrmnn
Copy link
Contributor

@cnnrmnn cnnrmnn commented Aug 4, 2021

  • Have you followed the guidelines in our Contributing document?
  • Have you checked to ensure there aren't other open Pull Requests for the same change?
  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your changes? Here's an example.
  • Have you successfully run brew style with your changes locally?
  • Have you successfully run brew typecheck with your changes locally?
  • Have you successfully run brew tests with your changes locally?

Closes #11741.

Currently, resolve_latest_keg has the following behavior:

  • If only one keg exists (HEAD or stable), return that keg.
  • Otherwise, return the latest stable keg.

This behavior doesn't handle the case where there are multiple HEAD kegs, but no stable kegs. This PR adds the following behavior to resolve_latest_keg:

  • If there are HEAD kegs but no stable kegs, return the latest HEAD keg.

@BrewTestBot
Copy link
Member

Review period will end on 2021-08-05 at 22:58:17 UTC.

@BrewTestBot BrewTestBot added the waiting for feedback Merging is blocked until sufficient time has passed for review label Aug 4, 2021
@cnnrmnn cnnrmnn requested a review from Rylan12 August 4, 2021 22:58
@cnnrmnn
Copy link
Contributor Author

cnnrmnn commented Aug 5, 2021

The test is failing on CI (but passing for me locally) because at the moment, comparing two HEAD versions returns 0. I suspect the best way to deal with this would be in resolve_latest_keg. We could use HeadVersion#commit to determine which version is newer. Alternatively, a less sophisticated approach would be picking the keg with the most recently modified directory. What do you think? @Rylan12

@Rylan12
Copy link
Member

Rylan12 commented Aug 5, 2021

I can take a closer look late tomorrow night (might not be until Friday—traveling today).

Formula#latest_head_version uses the modification time of the tab to determine which HEAD version is the latest:

def latest_head_version
head_versions = installed_prefixes.map do |pn|
pn_pkgversion = PkgVersion.parse(pn.basename.to_s)
pn_pkgversion if pn_pkgversion.head?
end.compact
head_versions.max_by do |pn_pkgversion|
[Tab.for_keg(prefix(pn_pkgversion)).source_modified_time, pn_pkgversion.revision]
end
end

I think it would be appropriate to use the same logic here (ideally this could be done without duplicating code but it's possible some refactoring would need to be done to support that)

@cnnrmnn
Copy link
Contributor Author

cnnrmnn commented Aug 5, 2021

I think it would be appropriate to use the same logic here (ideally this could be done without duplicating code but it's possible some refactoring would need to be done to support that)

Thanks for pointing me towards Formula#latest_head_version. Will use same logic for this issue.

@BrewTestBot BrewTestBot removed the waiting for feedback Merging is blocked until sufficient time has passed for review label Aug 6, 2021
@BrewTestBot
Copy link
Member

Review period ended.

kegs.reject { |k| k.version.head? }.max_by(&:version)
stable_kegs = kegs.reject { |k| k.version.head? }

return kegs.max_by { |keg| Tab.for_keg(keg).source_modified_time } if stable_kegs.blank?
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 included the source_modified_time code here directly since it is so brief. If you think this should be encapsulated elsewhere for use in resolve_latest_keg and latest_head_version, I'm happy to make changes. @Rylan12

Copy link
Member

Choose a reason for hiding this comment

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

@cnnrmnn I think this is fine. I'm not sure it really makes much sense to extract what's essentially #source_modified_time to its own method.

Just making sure, the revision secondary sort from Formula#latest_head_version isn't necessary here, right?

Copy link
Contributor Author

@cnnrmnn cnnrmnn Aug 10, 2021

Choose a reason for hiding this comment

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

I'm not sure that the revision secondary sort is needed anywhere we're dealing exclusively with HEAD versions. If you check out the parsing function for PkgVersion, you'll see that it parses the revision using a regex. HEAD paths (e.g., HEAD-6974ce8) matched to that regex will always set revision to nil. Any HEAD version should have a revision equal to 0 since to_i is called on the parsed nil revision.

Am I missing anything?

Copy link
Member

Choose a reason for hiding this comment

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

Gotcha makes sense. So it could probably be removed from latest_head_version too, right? Probably not worth doing but just curious if there's a difference.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So it could probably be removed from latest_head_version too, right?

Yep, should be fine to remove. I can do that in a quick PR as to not confuse anyone dealing with that code in the future.

Copy link
Member

Choose a reason for hiding this comment

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

You definitely don't need to but it probably wouldn't hurt. Plus it will expose more maintainers to it in case anyone knows something we're missing and it turns out it is necessary

Copy link
Contributor Author

@cnnrmnn cnnrmnn Aug 11, 2021

Choose a reason for hiding this comment

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

@Rylan12 Did some quick digging, and found a spec that tests HEAD versions with revisions. Do you know if there's any scenario where a HEAD version would actually have a revision?

I would think so given that the above test exists, but I'm still curious. If you don't know, I can do a little more digging and restore the secondary sort here if appropriate.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, good call. If you do brew install --HEAD on a formula that's specified a revision it will show up as HEAD-6974ce8_3 (which means there will be a revision), so I think it does need to be added here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Gotcha, will push a fix shortly.

Copy link
Member

@carlocab carlocab left a comment

Choose a reason for hiding this comment

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

Makes sense to me. Thanks @cnnrmnn!

@cnnrmnn cnnrmnn merged commit 5ea678b into Homebrew:master Aug 8, 2021
@cnnrmnn cnnrmnn deleted the latest-head-kegs branch August 8, 2021 16:05
@github-actions github-actions bot added the outdated PR was locked due to age label Sep 11, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated PR was locked due to age
Projects
None yet
Development

Successfully merging this pull request may close these issues.

brew link fails with multiple HEAD installations
4 participants