Skip to content

Conversation

@jskeet
Copy link
Contributor

@jskeet jskeet commented Jan 21, 2026

First instance of a pattern we may need elsewhere: if we need to write out a new version of librarian.yaml at the end of a command, and the command uses code which needs defaults filling in (as per prepareLibrary) or might make other changes we don't want to persist, clone the config first and only copy the desired changes at the end.

Fixes #3668

First instance of a pattern we may need elsewhere: if we need to write
out a new version of librarian.yaml at the end of a command, and the
command uses code which needs defaults filling in (as per
prepareLibrary) or might make other changes we don't want to persist,
clone the config first and only copy the desired changes at the end.

Fixes googleapis#3668
@jskeet jskeet requested a review from julieqiu January 21, 2026 17:24
@jskeet jskeet requested a review from a team as a code owner January 21, 2026 17:24
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @jskeet, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses an issue where the librarian tool's bump command could inadvertently write unrelated configuration changes back to librarian.yaml. To resolve this, a robust mechanism has been implemented: the configuration is now deep-cloned at the start of the bump operation. After all version updates are processed, only the specific library version changes are selectively copied back to this pristine configuration clone, which is then used for saving. This ensures that the librarian.yaml file remains clean and only reflects the intended version modifications.

Highlights

  • Configuration Management: The bump command now ensures that only library version changes are persisted to librarian.yaml, preventing unintended modifications from other parts of the configuration.
  • Deep Cloning: A new cloneConfig utility function has been introduced, utilizing YAML marshaling and unmarshaling, to create a deep, independent copy of the configuration object.
  • Selective Version Update: The copyLibraryVersions function was added to precisely transfer only the updated library versions from a working configuration to a pristine copy, maintaining other configuration fields.
  • Test Coverage: Comprehensive unit tests were added for cloneConfig and copyLibraryVersions to validate their correctness and handle various scenarios, including error conditions for mismatched libraries.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a robust pattern for making isolated changes to the librarian.yaml configuration during commands like bump. By cloning the configuration, operating on the copy, and then selectively merging back only the intended changes (library versions), it prevents unintended modifications from being persisted. The implementation is sound and well-tested. I've provided a few suggestions to improve the implementation of copyLibraryVersions for better performance and to align more closely with idiomatic Go practices.

Comment on lines +162 to +165
pristineCfg, err = copyLibraryVersions(cfg, pristineCfg)
if err != nil {
return err
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This updates the call to copyLibraryVersions to align with the suggested refactoring of that function in a separate comment.

if err := copyLibraryVersions(cfg, pristineCfg); err != nil {
	return err
}

Comment on lines 407 to 425
// copyLibraryVersions copies the version (and only the version) of
// every library from dirtyCfg to pristineCfg, so that we can save just
// the version number changes. The function returns an error if the two
// configurations do not have the same set of libraries (by name).
func copyLibraryVersions(dirtyCfg, pristineCfg *config.Config) (*config.Config, error) {
if len(dirtyCfg.Libraries) != len(pristineCfg.Libraries) {
return nil, fmt.Errorf("mismatched library count after bump: %d != %d", len(dirtyCfg.Libraries), len(pristineCfg.Libraries))
}
// We don't care about whether or not the libraries are in the same order,
// so we just find them by name.
for _, dirtyLibrary := range dirtyCfg.Libraries {
pristineLibrary, err := libraryByName(pristineCfg, dirtyLibrary.Name)
if err != nil {
return nil, err
}
pristineLibrary.Version = dirtyLibrary.Version
}
return pristineCfg, nil
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

I have two suggestions for improving copyLibraryVersions:

  1. Idiomatic Signature: The function modifies its pristineCfg parameter in-place but also returns it. In Go, it's more idiomatic for functions that modify an argument to not return it. I've changed the signature to func copyLibraryVersions(from, to *config.Config) error and renamed parameters for clarity.
  2. Performance: The current implementation has a time complexity of O(N^2) due to calling libraryByName (which does a linear scan) inside a loop. This can be optimized to O(N) by building a map of the libraries from the destination config first.

The suggested code below incorporates both of these improvements. Note that this change will require updating the call sites in runBump and TestCopyLibraryVersions, for which I have left separate comments.

// copyLibraryVersions copies the version (and only the version) of
// every library from `from` to `to`, so that we can save just
// the version number changes. The function returns an error if the two
// configurations do not have the same set of libraries (by name).
func copyLibraryVersions(from, to *config.Config) error {
	if len(from.Libraries) != len(to.Libraries) {
		return fmt.Errorf("mismatched library count after bump: %d != %d", len(from.Libraries), len(to.Libraries))
	}
	toLibraries := make(map[string]*config.Library, len(to.Libraries))
	for _, lib := range to.Libraries {
		toLibraries[lib.Name] = lib
	}

	for _, fromLibrary := range from.Libraries {
		toLibrary, ok := toLibraries[fromLibrary.Name]
		if !ok {
			return fmt.Errorf("library %q not found in destination config", fromLibrary.Name)
		}
		toLibrary.Version = fromLibrary.Version
	}
	return nil
}
References
  1. The repository style guide (line 14) recommends following idiomatic Go patterns from the Go Code Review Comments wiki. The suggestion to change the function signature to not return a modified input parameter aligns with these idiomatic practices, improving code clarity. (link)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@julieqiu: the "idiomatic signature" part was actually what I originally had, before remembering https://github.com/googleapis/librarian/blob/main/doc/howwewritego.md#make-mutations-explicit - please check that my understanding is correct.

I'll apply the "use a map" idea now though.

Comment on lines +966 to +969
pristine, err := copyLibraryVersions(dirty, pristine)
if err != nil {
t.Fatal(err)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This updates the call to copyLibraryVersions to align with the suggested refactoring of that function in a separate comment.

if err := copyLibraryVersions(dirty, pristine); err != nil {
	t.Fatal(err)
}

@codecov
Copy link

codecov bot commented Jan 21, 2026

Codecov Report

❌ Patch coverage is 75.00000% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.37%. Comparing base (6ec8ac6) to head (97704b0).
⚠️ Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
internal/librarian/bump.go 75.00% 3 Missing and 3 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3676      +/-   ##
==========================================
- Coverage   82.43%   82.37%   -0.06%     
==========================================
  Files         138      138              
  Lines       12665    12690      +25     
==========================================
+ Hits        10440    10454      +14     
- Misses       1735     1741       +6     
- Partials      490      495       +5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jskeet
Copy link
Contributor Author

jskeet commented Jan 21, 2026

(The coverage is tricky to improve as I'd need to figure out a way of making configuration formatting/parsing fail...)

@zhumin8
Copy link
Contributor

zhumin8 commented Jan 21, 2026

Ah, this config clone was deleted in #3548. I thought as long as we run tidy afterwards, it should not matter if the defaults are filled in? (I agree with #3668, just wondering how this was causing bump --all to fail?)

@jskeet
Copy link
Contributor Author

jskeet commented Jan 21, 2026

Ah, this config clone was deleted in #3548. I thought as long as we run tidy afterwards, it should not matter if the defaults are filled in? (I agree with #3668, just wondering how this was causing bump --all to fail?)

This was causing bump --all to fail because the tidy code only spots conflicts (one channel being used in multiple libraries) if the channel is listed explicitly in those libraries. It's possible that the tidy validation should clone the config, fill in all the defaults, and then validate... but that's a different matter :)

Two reasons to avoid filling in the defaults in (which aren't removed by tidy):

  • It makes the release commit much more chatty and harder to review
  • It makes the config file itself much bigger - I believe the intention is to keep it as concise as reasonably possible

If tidy were changed to remove anything that could be inferred, that's another alternative - it might be hard to achieve though.

@julieqiu
Copy link
Member

@zhumin8 and I took at look at this. It looks like #3678 might be the actual fix. Let us know if you still see issues

@jskeet
Copy link
Contributor Author

jskeet commented Jan 22, 2026

@julieqiu As I commented on #3678, I suspect we will want prepareLibrary inside bump.

Basically I suspect that everywhere that language-specific code will need to do anything non-trivial (e.g. modify a file), it will benefit from being passed a "normalized" config, where it can know immediately where to find the source code etc. I don't think we want to bake "apply defaults to figure stuff out" code in each language's functions.

In fact, even the existing Rust code for bump assumes that library.Output has been populated, so I'm not sure how that would work right now for libraries which don't specify the output in librarian.yaml. It's possible that I'm missing something - or it's possible that #3678 broke bump.

@jskeet
Copy link
Contributor Author

jskeet commented Jan 22, 2026

@julieqiu Created #3693 to capture this so it wasn't buried in comments.

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.

librarian: config mutation is reflected in librarian.yaml

3 participants