microsoft / microsoft/typespec
[protobuf] Support setting fileName in TypeSpec.Protobuf.PackageDetails
- Dominant language
- Java
- Stars
- 5.9k
- Forks
- 394
- Avg merge
- 1d 23h
- Merged PRs (30d)
- 104
Description
### Clear and concise description of the problem
The [TypeSpec.Protobuf.package decorator](https://typespec.io/docs/emitters/protobuf/guide/reference/decorators#@TypeSpec.Protobuf.package) `name` property results in a generated file rather than a directory matching the package name.
## Current State
Given the following:
```
import "@typespec/protobuf";
using TypeSpec.Protobuf;
@package({
name: "my.example.v1",
options: {
go_package: "github.com/org/repo/go/my/example/v1",
},
})
namespace Example;
```
The protobuf emitter emits a file `/my/example/v1.proto`, see [playground](https://typespec.io/playground/?c=aW1wb3J0ICJAdHlwZXNwZWMvcHJvdG9idWYiOw0KDQp1c2luZyBUeXBlU3BlYy5Qxx3FHEBwYWNrYWdlKHsNCiAgICBuYW1lOiAibXkuZXhhbXBsZS52MSIsxhxvcHRpb25zOiDHLMQBZ29fx0I6ICJnaXRodWIuY29tL29yZy9yZXBvL2dvL215L8dNL8pNfSwNCn0pDQrEcXNwYWNlIEXGJDs%3D&e=%40typespec%2Fprotobuf&options=%7B%7D).
I would expect `/my/example/v1/generated.proto`
## Problem
This prevents the ability to add multiple manual `.proto` files along side the generated proto under the same package ` "my.example.v1"`.
- Given `/my/example/v1.proto`, I cannot add `/my/example/v1/non-generated.proto`
This also breaks the linter for the popular [bufbuild tool](https://buf.build/docs/best-practices/style-guide/#files-and-packages) which expects the package name to match a directory path minus a filename - `"my.example.v1"` -> `/my/example/v1/`.
## Solution (non-breaking)
Add a property `fileName` to the `TypeSpec.Protobuf.PackageDetails` to allow generating proto files with a specific filename.
If no `fileName` is provided, maintain backwards compatibility by emitting the current state.
Example with `fileName` would result in emitted file: `/my/example/v1/cool.proto`
```
@package({
name: "my.example.v1",
fileName: "cool"
options: {
go_package: "github.com/org/repo/go/my/example/v1",
},
})
namespace Example;
```
## Other considerations
I imagine it is better to add a new property `fileName` to `TypeSpec.Protobuf.PackageDetails` rather than a new protobuf emitter `--option="@typespec/protobuf.file-name=xxx"` as the former would persist the file name to support `.proto` import directives.
### Checklist
- [x] Follow our [Code of Conduct](https://github.com/microsoft/typespec/blob/main/CODE_OF_CONDUCT.md)
- [x] Read the [docs](https://typespec.io/docs/).
- [x] Check that there isn't already an issue that request the same feature to avoid creating a duplicate.
Contributor guide
Assessment
This issue has not been assessed yet.