TypeScript 中 `bufgenerate` 与 `google.type.Date` 时成为相对导入

问题描述 投票:0回答:1

当我在包含

buf generate
的文件上运行
google/type/date.proto
时,会生成一个包含相对路径的 pb 文件。但是,没有指定路径的文件。语言是 TypeScript。

// a part of the generated code
import type { BinaryReadOptions, FieldList, JsonReadOptions, JsonValue, PartialMessage, PlainMessage } from "@bufbuild/protobuf";
import { Message, proto3 } from "@bufbuild/protobuf";
import { Date } from "../google/type/date_pb";

我期待它引用文件输出到

node_modules
node_modules
中没有与google.type相关的文件)。如何摆脱相对路径?

使用的文件如下。

protos/sample/sample.proto
是:

syntax = "proto3";

package sample;

import "google/protobuf/empty.proto";
import "google/type/date.proto";

message Sample {
  google.type.Date start_date = 1;
}
service SampleService {
  rpc Initialize(google.protobuf.Empty) returns (Sample);
}

buf.yaml
是:

version: v2
modules:
  - path: protos
lint:
  use:
    - DEFAULT
breaking:
  use:
    - FILE
deps:
  - buf.build/googleapis/googleapis:f0e53af8f2fc4556b94f482688b57223

buf.gen.yaml
是:

version: v2
managed:
  enabled: true
  disable:
    - module: buf.build/googleapis/googleapis
plugins:
  - remote: buf.build/connectrpc/es:v1.4.0
    opt:
      - target=ts
      - import_extension=none
    out: src/gen
  - remote: buf.build/bufbuild/es
    opt:
      - target=ts
      - import_extension=none
    out: src/gen

文件结构和命令:

$ tree
.
├── protos
│   └── sample
│       └── sample.proto
├── buf.gen.yaml
└── buf.yaml

$ buf dep update
$ buf generate
typescript protocol-buffers buf
1个回答
0
投票

我遇到了同样的问题 - 对于依赖关系,Buf 在假设依赖代码位于同一顶级对象中的情况下生成相对路径。 buf.gen.yaml 允许配置“输入”(因此在您的情况下类似于

inputs: ["google/type"]
),但这只会导致所有导入的文件被复制到项目中,而不是引用外部依赖项。

你最终找到解决方案了吗?

© www.soinside.com 2019 - 2024. All rights reserved.