Skip to content

Commit 7ed2862

Browse files
authored
fix(internal/librarian): generate should skip formatting if library is skipped (#3325)
Skip formatting if library generate code is skipped per config, otherwise it panics trying to format on a nil library passed in. Local test to generate a skipped module now completes with no err: ``` ╰─$ librarian generate common ⊘ Skipping common (skip_generate is set) ``` Fix #3323
1 parent ddb3a29 commit 7ed2862

File tree

2 files changed

+47
-22
lines changed

2 files changed

+47
-22
lines changed

internal/librarian/generate.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ func runGenerate(ctx context.Context, all bool, libraryName string) error {
9494
if err != nil {
9595
return err
9696
}
97+
if lib == nil {
98+
// Skip formatting if generation skipped.
99+
return nil
100+
}
97101
return formatLibrary(ctx, cfg.Language, lib)
98102
}
99103

@@ -113,6 +117,10 @@ func generateAll(ctx context.Context, cfg *config.Config) error {
113117
if err != nil {
114118
return err
115119
}
120+
if lib == nil {
121+
// Skip formatting if generation skipped.
122+
continue
123+
}
116124
if err := formatLibrary(ctx, cfg.Language, lib); err != nil {
117125
return err
118126
}
@@ -315,6 +323,9 @@ func generate(ctx context.Context, language string, library *config.Library, sou
315323
func formatLibrary(ctx context.Context, language string, library *config.Library) error {
316324
switch language {
317325
case "testhelper":
326+
// Access library.Name to simulate a real formatter and trigger
327+
// a panic if library is nil.
328+
_ = library.Name
318329
return nil
319330
case "rust":
320331
return rust.Format(ctx, library)

internal/librarian/generate_test.go

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,37 +32,20 @@ func TestGenerateCommand(t *testing.T) {
3232
lib1Output = "output1"
3333
lib2 = "library-two"
3434
lib2Output = "output2"
35+
lib3 = "library-three"
36+
lib3Output = "output3"
3537
)
36-
tempDir := t.TempDir()
37-
t.Chdir(tempDir)
38-
googleapisDir := createGoogleapisServiceConfigs(t, tempDir, map[string]string{
38+
baseTempDir := t.TempDir()
39+
googleapisDir := createGoogleapisServiceConfigs(t, baseTempDir, map[string]string{
3940
"google/cloud/speech/v1": "speech_v1.yaml",
4041
"grafeas/v1": "grafeas_v1.yaml",
4142
"google/cloud/texttospeech/v1": "texttospeech_v1.yaml",
4243
})
43-
configPath := filepath.Join(tempDir, librarianConfigPath)
44-
configContent := fmt.Sprintf(`language: testhelper
45-
sources:
46-
googleapis:
47-
dir: %s
48-
libraries:
49-
- name: %s
50-
output: %s
51-
channels:
52-
- path: google/cloud/speech/v1
53-
- path: grafeas/v1
54-
- name: %s
55-
output: %s
56-
channels:
57-
- path: google/cloud/texttospeech/v1
58-
`, googleapisDir, lib1, lib1Output, lib2, lib2Output)
59-
if err := os.WriteFile(configPath, []byte(configContent), 0644); err != nil {
60-
t.Fatal(err)
61-
}
6244

6345
allLibraries := map[string]string{
6446
lib1: lib1Output,
6547
lib2: lib2Output,
48+
lib3: lib3Output,
6649
}
6750

6851
for _, test := range []struct {
@@ -91,8 +74,39 @@ libraries:
9174
args: []string{"librarian", "generate", "--all"},
9275
want: []string{lib1, lib2},
9376
},
77+
{
78+
name: "skip generate",
79+
args: []string{"librarian", "generate", lib3},
80+
want: []string{},
81+
},
9482
} {
9583
t.Run(test.name, func(t *testing.T) {
84+
tempDir := t.TempDir()
85+
t.Chdir(tempDir)
86+
configContent := fmt.Sprintf(`language: testhelper
87+
sources:
88+
googleapis:
89+
dir: %s
90+
libraries:
91+
- name: %s
92+
output: %s
93+
channels:
94+
- path: google/cloud/speech/v1
95+
- path: grafeas/v1
96+
- name: %s
97+
output: %s
98+
channels:
99+
- path: google/cloud/texttospeech/v1
100+
- name: %s
101+
output: %s
102+
skip_generate: true
103+
channels:
104+
- path: google/cloud/speech/v1
105+
`, googleapisDir, lib1, lib1Output, lib2, lib2Output, lib3, lib3Output)
106+
if err := os.WriteFile(filepath.Join(tempDir, librarianConfigPath), []byte(configContent), 0644); err != nil {
107+
t.Fatal(err)
108+
}
109+
96110
err := Run(t.Context(), test.args...)
97111
if test.wantErr != nil {
98112
if !errors.Is(err, test.wantErr) {

0 commit comments

Comments
 (0)