Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import errors when using google/api/annotations.proto #1055

Closed
paskozdilar opened this issue Jan 15, 2025 · 2 comments
Closed

Import errors when using google/api/annotations.proto #1055

paskozdilar opened this issue Jan 15, 2025 · 2 comments

Comments

@paskozdilar
Copy link

paskozdilar commented Jan 15, 2025

Here's a list of files and their contents required for minimal reproducible example:

buf.gen.yaml

version: v2
managed:
  enabled: true
plugins:
  - remote: buf.build/bufbuild/es:v2.2.3
    out: gen
    opt:
      - target=ts

buf.yaml

version: v2
modules:
  - path: proto
deps:
  - buf.build/googleapis/googleapis

proto/sample.proto

syntax = "proto3";

package sample;

import "google/api/annotations.proto";

service SampleService {
  rpc UnaryCall (SampleMessage) returns (SampleMessage) {
    option (google.api.http) = {
      post: "/unary";
      body: "*";
    };
  }
}

message SampleMessage {}

tsconfig.json

{
  "compilerOptions": {
    "target": "es2016",                                  
    "module": "commonjs",                                
    "esModuleInterop": true,                             
    "forceConsistentCasingInFileNames": true,            
    "strict": true,                                      
    "skipLibCheck": true                                 
  }
}

package.json

{
  "dependencies": {
    "@bufbuild/protobuf": "^2.2.3"
  },
  "devDependencies": {
    "@bufbuild/buf": "^1.49.0",
    "typescript": "^5.7.3"
  }
}

index.ts

import * as sample from './gen/sample_pb';

console.log(sample.SampleMessageSchema);

Commands to run:

npm install
npx buf dep update
npx buf generate
npx tsc --build

Output:

npx tsc --build
gen/sample_pb.ts:10:45 - error TS2307: Cannot find module './google/api/annotations_pb' or its corresponding type declarations.

10 import { file_google_api_annotations } from "./google/api/annotations_pb";
                                               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

For convenience, I've created a repository containing the reproducible example at https://github.com/paskozdilar/protobuf-es-bug. (deleted as it is not needed)

@timostamm
Copy link
Member

Thanks for the detailed report.

Because generated code provides access to the full schema, this also includes the google.api.http option. The Buf CLI does not generate dependencies by default. You can tell it to do so with an option, see the section The plugin generates missing imports in the manual for an example.

We'd like to support option filtering or an equivalent mechanism for the case you don't care about the google.api.http option in generated code. We're tracking this in #1020.

@paskozdilar
Copy link
Author

The option include_imports: true fixed this issue.

Thank you for the swift response. Since there is a workaround and a tracking issue, I'll close issue as duplicate.

@paskozdilar paskozdilar closed this as not planned Won't fix, can't repro, duplicate, stale Jan 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants