Skip to content

Commit 1de9214

Browse files
committed
fix(librarian/rust): Looks for sources in all roots.
Fixes googleapis#3561
1 parent 69c2cfb commit 1de9214

File tree

2 files changed

+54
-23
lines changed

2 files changed

+54
-23
lines changed

internal/librarian/rust/codec.go

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,45 @@ package rust
1616

1717
import (
1818
"fmt"
19+
"os"
20+
"path/filepath"
1921
"strings"
2022

2123
"github.com/googleapis/librarian/internal/config"
2224
"github.com/googleapis/librarian/internal/serviceconfig"
2325
sidekickconfig "github.com/googleapis/librarian/internal/sidekick/config"
2426
)
2527

28+
type rootMapEntry struct {
29+
path string
30+
key string
31+
}
32+
33+
func newRootMap(sources *Sources) map[string]rootMapEntry {
34+
return map[string]rootMapEntry{
35+
"googleapis": {path: sources.Googleapis, key: "googleapis-root"},
36+
"discovery": {path: sources.Discovery, key: "discovery-root"},
37+
"showcase": {path: sources.Showcase, key: "showcase-root"},
38+
"protobuf-src": {path: sources.ProtobufSrc, key: "protobuf-src-root"},
39+
"conformance": {path: sources.Conformance, key: "conformance-root"},
40+
}
41+
}
42+
43+
func determineSourceRoot(source string, sources *Sources, rootMap map[string]rootMapEntry) (string, string) {
44+
if r, ok := rootMap[source]; ok {
45+
return r.path, ""
46+
}
47+
// Check if the source directory exists in googleapisDir
48+
if _, err := os.Stat(filepath.Join(sources.Googleapis, source)); err == nil {
49+
return sources.Googleapis, source
50+
}
51+
// Check if the source directory exists locally
52+
if _, err := os.Stat(source); err == nil {
53+
return ".", source
54+
}
55+
return "", ""
56+
}
57+
2658
func toSidekickConfig(library *config.Library, ch *config.Channel, sources *Sources) (*sidekickconfig.Config, error) {
2759
source := map[string]string{}
2860
specFormat := "protobuf"
@@ -39,16 +71,7 @@ func toSidekickConfig(library *config.Library, ch *config.Channel, sources *Sour
3971
source["roots"] = "googleapis"
4072
} else {
4173
source["roots"] = strings.Join(library.Roots, ",")
42-
rootMap := map[string]struct {
43-
path string
44-
key string
45-
}{
46-
"googleapis": {path: sources.Googleapis, key: "googleapis-root"},
47-
"discovery": {path: sources.Discovery, key: "discovery-root"},
48-
"showcase": {path: sources.Showcase, key: "showcase-root"},
49-
"protobuf-src": {path: sources.ProtobufSrc, key: "protobuf-src-root"},
50-
"conformance": {path: sources.Conformance, key: "conformance-root"},
51-
}
74+
rootMap := newRootMap(sources)
5275
for _, root := range library.Roots {
5376
if r, ok := rootMap[root]; ok && r.path != "" {
5477
source[r.key] = r.path
@@ -233,13 +256,18 @@ func formatPackageDependency(dep *config.RustPackageDependency) string {
233256
return strings.Join(parts, ",")
234257
}
235258

236-
func moduleToSidekickConfig(library *config.Library, module *config.RustModule, googleapisDir, protobufSrcDir string) (*sidekickconfig.Config, error) {
259+
func moduleToSidekickConfig(library *config.Library, module *config.RustModule, sources *Sources) (*sidekickconfig.Config, error) {
237260
source := map[string]string{
238-
"googleapis-root": googleapisDir,
239-
"protobuf-src-root": protobufSrcDir,
261+
"googleapis-root": sources.Googleapis,
262+
"protobuf-src-root": sources.ProtobufSrc,
240263
}
264+
rootMap := newRootMap(sources)
241265
for root, dir := range module.ModuleRoots {
242-
source[root] = dir
266+
if r, ok := rootMap[dir]; ok {
267+
source[root] = r.path
268+
} else {
269+
source[root] = dir
270+
}
243271
}
244272
if len(module.IncludedIds) > 0 {
245273
source["included-ids"] = strings.Join(module.IncludedIds, ",")
@@ -251,12 +279,15 @@ func moduleToSidekickConfig(library *config.Library, module *config.RustModule,
251279
source["include-list"] = module.IncludeList
252280
}
253281
if module.Source != "" {
254-
api, err := serviceconfig.Find(googleapisDir, module.Source)
255-
if err != nil {
256-
return nil, fmt.Errorf("serviceconfig.Find(%s, %s): %w", googleapisDir, module.Source, err)
257-
}
258-
if api != nil && api.Title != "" {
259-
source["title-override"] = api.Title
282+
root, path := determineSourceRoot(module.Source, sources, rootMap)
283+
if root != "" {
284+
api, err := serviceconfig.Find(root, path)
285+
if err != nil {
286+
return nil, fmt.Errorf("serviceconfig.Find(%s, %s): %w", root, path, err)
287+
}
288+
if api != nil && api.Title != "" {
289+
source["title-override"] = api.Title
290+
}
260291
}
261292
}
262293

internal/librarian/rust/generate.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func generateVeneer(ctx context.Context, library *config.Library, sources *Sourc
7979
return nil
8080
}
8181
for _, module := range library.Rust.Modules {
82-
sidekickConfig, err := moduleToSidekickConfig(library, module, sources.Googleapis, sources.ProtobufSrc)
82+
sidekickConfig, err := moduleToSidekickConfig(library, module, sources)
8383
if err != nil {
8484
return fmt.Errorf("module %s: %w", module.Output, err)
8585
}
@@ -168,7 +168,7 @@ func generateRustStorage(ctx context.Context, library *config.Library, moduleOut
168168
if storageModule == nil {
169169
return fmt.Errorf("could not find module with output %s in library %s", output, library.Name)
170170
}
171-
storageConfig, err := moduleToSidekickConfig(library, storageModule, sources.Googleapis, sources.ProtobufSrc)
171+
storageConfig, err := moduleToSidekickConfig(library, storageModule, sources)
172172
if err != nil {
173173
return fmt.Errorf("failed to create storage sidekick config: %w", err)
174174
}
@@ -182,7 +182,7 @@ func generateRustStorage(ctx context.Context, library *config.Library, moduleOut
182182
if controlModule == nil {
183183
return fmt.Errorf("could not find module with output %s in library %s", output, library.Name)
184184
}
185-
controlConfig, err := moduleToSidekickConfig(library, controlModule, sources.Googleapis, sources.ProtobufSrc)
185+
controlConfig, err := moduleToSidekickConfig(library, controlModule, sources)
186186
if err != nil {
187187
return fmt.Errorf("failed to create control sidekick config: %w", err)
188188
}

0 commit comments

Comments
 (0)