Repository navigation
fix(librarian/rust): detect inconsistent repos #6766
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
Merged
coryan
merged 4 commits into
googleapis:main
from
coryan:fix-rust-existing-lib-detection
Jul 13, 2026
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // https://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package rust | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "errors" | ||
| "fmt" | ||
| "io/fs" | ||
| "os" | ||
| ) | ||
|
|
||
| // crateExists determines if a crate (the Rust mapping for "library") exists. | ||
| // | ||
| // Once a crate is generated the following things are true: | ||
| // - The output directory exists | ||
| // - The output directory is listed in the top-level Cargo.toml file | ||
| // | ||
| // If none of those things are true, it is safe to assume the crate is a new | ||
| // crate and to execute the commands to add the crate to the repository. If | ||
| // both are true, then we can skip the commands to add the crate. | ||
| // | ||
| // If one of them is true, but not the other, then there is an error in the | ||
| // repository configuration that requires human intervention. | ||
| func crateExists(output string) (bool, error) { | ||
| hasDirectory := true | ||
| if _, err := os.Stat(output); err != nil { | ||
| hasDirectory = false | ||
| if !errors.Is(err, fs.ErrNotExist) { | ||
| return false, fmt.Errorf("cannot access output directory %q: %w", output, err) | ||
| } | ||
| } | ||
| // The librarian commands are always executed at the top-level directory of | ||
| // the monorepo. | ||
| contents, err := os.ReadFile("Cargo.toml") | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
| // The production version of `Cargo.toml` is always formatted with `taplo | ||
| // fmt`. That guarantees the entries will follow this strict format. | ||
| hasCargoEntry := bytes.Contains(contents, fmt.Appendf(nil, "\n \"%s\",\n", output)) | ||
| if !hasCargoEntry && hasDirectory { | ||
| return false, fmt.Errorf("inconsistent repository state, crate missing in Cargo.toml, but the directory exists: %s", output) | ||
|
coryan marked this conversation as resolved.
Resolved
|
||
| } | ||
| if hasCargoEntry && !hasDirectory { | ||
| return false, fmt.Errorf("inconsistent repository state, crate listed in Cargo.toml, but the directory is missing: %s", output) | ||
| } | ||
| return hasDirectory && hasCargoEntry, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // https://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package rust | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "testing" | ||
| ) | ||
|
|
||
| const formatTestCargoToml = ` | ||
| [workspace] | ||
| resolver = "3" | ||
|
|
||
| members = [ | ||
| "src/auth", | ||
| %s | ||
| ] | ||
| ` | ||
|
|
||
| func TestCrateExists(t *testing.T) { | ||
|
coryan marked this conversation as resolved.
Resolved
|
||
| const testDir = "src/generated/test/service/v1" | ||
| for _, test := range []struct { | ||
| name string | ||
| cargoText string | ||
| makeDir string | ||
| wantErr bool | ||
| wantResult bool | ||
| }{ | ||
| { | ||
| name: "exists", | ||
| cargoText: fmt.Sprintf(` "%s",`, testDir), | ||
| makeDir: testDir, | ||
| wantErr: false, | ||
| wantResult: true, | ||
| }, | ||
| { | ||
| name: "not exists", | ||
| cargoText: ` "src/generated/test/service/v2",`, | ||
| makeDir: "src/generated/test/service/v2", | ||
| wantErr: false, | ||
| wantResult: false, | ||
| }, | ||
| { | ||
| name: "only directory", | ||
| cargoText: ` "bad-bad-bad",`, | ||
| makeDir: testDir, | ||
| wantErr: true, | ||
| wantResult: false, | ||
| }, | ||
| { | ||
| name: "only entry", | ||
| cargoText: fmt.Sprintf(` "%s",`, testDir), | ||
| makeDir: "src/generated", | ||
| wantErr: true, | ||
| wantResult: false, | ||
| }, | ||
| { | ||
| name: "entry is substring", | ||
| cargoText: ` "src/generated/test/service/v1/weird/but/should/test",`, | ||
| makeDir: testDir, | ||
| wantErr: true, | ||
| wantResult: false, | ||
| }, | ||
| } { | ||
| t.Run(test.name, func(t *testing.T) { | ||
| d := t.TempDir() | ||
| t.Chdir(d) | ||
| if err := os.MkdirAll(test.makeDir, 0775); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if err := os.WriteFile("Cargo.toml", fmt.Appendf(nil, formatTestCargoToml, test.cargoText), 0644); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| got, err := crateExists(testDir) | ||
| if test.wantErr && err == nil { | ||
| t.Fatalf("crateExists() succeed when an error was expected: %v", got) | ||
| } | ||
| if !test.wantErr && err != nil { | ||
| t.Fatalf("crateExists() failed unexpectedly: %v", err) | ||
| } | ||
| if test.wantResult != got { | ||
| t.Errorf("crateExists() mismatch, want=%v, got=%v", test.wantResult, got) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestCrateExistsDirectoryError(t *testing.T) { | ||
| d := t.TempDir() | ||
| t.Chdir(d) | ||
| // Create part of the path, but make it unreadable. | ||
| if err := os.MkdirAll("src", 0000); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if err := os.WriteFile("Cargo.toml", []byte("# test only"), 0644); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if got, err := crateExists("src/generated/unreachable"); err == nil { | ||
| t.Errorf("expected an error from crateExists(), got=%v", got) | ||
|
coryan marked this conversation as resolved.
Resolved
|
||
| } | ||
| } | ||
|
|
||
| func TestCrateExistsCargoTomlError(t *testing.T) { | ||
| d := t.TempDir() | ||
| t.Chdir(d) | ||
| // Create part of the path, but make it unreadable. | ||
| if err := os.MkdirAll("src/generated/test", 0755); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if got, err := crateExists("src/generated/test"); err == nil { | ||
| t.Errorf("expected an error from crateExists(), got=%v", got) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.