Skip to content

Commit 3c8af0b

Browse files
authored
fix(internal/librarian): add cargo update --workspace to run after generate all (#3711)
Add `cargo update --workspace` to run inside of `librarian generate --all`. Add as a postGenerate step to generate, to perform repo level actions after generating individual libraries. For #3683
1 parent 211ad7b commit 3c8af0b

4 files changed

Lines changed: 39 additions & 8 deletions

File tree

internal/librarian/fake.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ func fakeFormat(library *config.Library) error {
6161
return os.WriteFile(readmePath, []byte(formatted), 0644)
6262
}
6363

64+
func fakePostGenerate() error {
65+
return os.WriteFile("POST_GENERATE_README.md", []byte("PostGenerated\n"), 0644)
66+
}
67+
6468
func fakePublish(libraries []string, execute bool) error {
6569
content := fmt.Sprintf("libraries=%s; execute=%v",
6670
strings.Join(libraries, ","), execute)

internal/librarian/generate.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,20 @@ func generateAll(ctx context.Context, cfg *config.Config) error {
151151
return err
152152
}
153153
}
154-
return nil
154+
return postGenerate(ctx, cfg.Language)
155+
}
156+
157+
// postGenerate performs repository-level actions after all individual
158+
// libraries have been generated.
159+
func postGenerate(ctx context.Context, language string) error {
160+
switch language {
161+
case languageRust:
162+
return rust.UpdateWorkspace(ctx)
163+
case languageFake:
164+
return fakePostGenerate()
165+
default:
166+
return nil
167+
}
155168
}
156169

157170
func defaultOutput(language, channel, defaultOut string) string {

internal/librarian/generate_test.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,11 @@ func TestGenerateCommand(t *testing.T) {
4848
}
4949

5050
for _, test := range []struct {
51-
name string
52-
args []string
53-
wantErr error
54-
want []string
51+
name string
52+
args []string
53+
wantErr error
54+
want []string
55+
wantPostGenerate bool
5556
}{
5657
{
5758
name: "no args",
@@ -69,9 +70,10 @@ func TestGenerateCommand(t *testing.T) {
6970
want: []string{lib1},
7071
},
7172
{
72-
name: "all flag",
73-
args: []string{"librarian", "generate", "--all"},
74-
want: []string{lib1, lib2},
73+
name: "all flag",
74+
args: []string{"librarian", "generate", "--all"},
75+
want: []string{lib1, lib2},
76+
wantPostGenerate: true,
7577
},
7678
{
7779
name: "skip generate",
@@ -161,6 +163,13 @@ libraries:
161163
t.Errorf("mismatch for STARTER.md for %q (-want +got):\n%s", libName, diff)
162164
}
163165
}
166+
167+
if test.wantPostGenerate {
168+
postGeneratePath := filepath.Join(tempDir, "POST_GENERATE_README.md")
169+
if _, err := os.Stat(postGeneratePath); err != nil {
170+
t.Errorf("expected POST_GENERATE_README.md to exist, but got error: %v", err)
171+
}
172+
}
164173
})
165174
}
166175
}

internal/librarian/rust/generate.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ func Generate(ctx context.Context, library *config.Library, sources *Sources) er
7777
return nil
7878
}
7979

80+
// UpdateWorkspace updates dependencies for the entire Rust workspace.
81+
func UpdateWorkspace(ctx context.Context) error {
82+
return command.Run(ctx, "cargo", "update", "--workspace")
83+
}
84+
8085
// Format formats a generated Rust library. Must be called sequentially;
8186
// parallel calls cause race conditions as cargo fmt runs cargo metadata,
8287
// which competes for locks on the workspace Cargo.toml and Cargo.lock.

0 commit comments

Comments
 (0)