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

package import statement generated with well-defined type is not correct. #76

Closed
rhodee opened this issue Mar 26, 2018 · 6 comments
Closed

Comments

@rhodee
Copy link

rhodee commented Mar 26, 2018

When compiling using the following command, the import statement is not Dart code. Are there any examples for how to handle this - thank you!

<version of the grpc-dart packages used; see your pubspec.lock file>

  grpc:
    dependency: "direct main"
    description:
      name: grpc
      url: "https://pub.dartlang.org"
    source: hosted
    version: "0.4.0"

Repro steps

  1. Run: protoc -Iprotos -I $(GOPATH)src/github.com/golang/protobuf/ptypes/timestamp --dart_out=grpc:proto_out/service.proto

Expected result: Usable dart import package declaration for the well-defined types

Actual result: package import declaration causes an error and compilation is not possible.

Details

Below is a snipped of the .proto file. I am trying to create a client stub (the server is written in Go). I have confirmed the client stub exported from Go works.

syntax = "proto3";

package mypackage;

option go_package = "github.com/mypackage;

import "google/protobuf/timestamp.proto";

message Timestamp {
    google.protobuf.Timestamp created = 1;
    google.protobuf.Timestamp updated = 2;
    google.protobuf.Timestamp synced = 3;
}
... implementation details to follow...

Upon running the protoc command the output is:

/// service.pb.dart

///
//  Generated code. Do not modify.
///
// ignore_for_file: non_constant_identifier_names,library_prefixes
library my_service;

// ignore: UNUSED_SHOWN_NAME
import 'dart:core' show int, bool, double, String, List, override;

import 'package:protobuf/protobuf.dart';

import 'google/protobuf/timestamp.pb.dart' as $google$protobuf;

import 'service.pbenum.dart';

export 'service.pbenum.dart';

/// ...generated code continues...
@jakobr-google
Copy link
Contributor

jakobr-google commented Mar 27, 2018

Imports look correct to me. What error are you getting?

If I try your sample as is, I get this error when trying to analyze the project:

error • Target of URI hasn't been generated: 'google/protobuf/timestamp.pb.dart' at proto_out/service.pb.dart:12:8 • uri_has_not_been_generated

if that's the error you're seeing, then the reason is that you haven't generated Dart sources for google/protobuf/timestamp.proto.

Somewhat unfortunately, the protoc compiler only generates sources for the files you've listed on the command line, so you'll need to add timestamp there as well. On my machine, it's /usr/include/google/protobuf/timestamp.proto, YMMV.

If you're getting another error, please let me know what it is.

@rhodee
Copy link
Author

rhodee commented Mar 27, 2018

That is exactly the error I am getting. I tried following the example for what you suggested, using the only example I could find was in C#. Is there a Dart equivalent?

@rhodee
Copy link
Author

rhodee commented Mar 27, 2018

That did not solve the problem.

I have tried adding the following code on OSX:

PROTO_ROOT_DIR = $(shell brew --prefix)/Cellar/protobuf/3.5.1_1/include/google/protobuf

.PHONY: mobile-stub
mobile-stub:
	@mkdir -p lib/client
	@protoc -I. -I$(PROTO_ROOT_DIR)/timestamp.proto --dart_out=grpc:lib/client service.proto

I originally wrote the code in Go and tried to compile to Dart. I attempted to move the service.proto to the dart app - and it created the same output as when I originally opened the issue.

@jakobr-google
Copy link
Contributor

You need to have protoc generate the Dart stubs for google/protobuf/timestamp.proto as well, so you either have to add $PROTO_DIR/google/protobuf/timestamp.proto to the end of the command line, or add a separate call to protoc for it:

protoc -I$(PROTO_DIR) --dart_out=lib/client $(PROTO_DIR)/google/protobuf/timestamp.proto

On my macOS box, PROTO_DIR is /usr/local/include, but you can also use $(shell brew --prefix)/Cellar/protobuf/3.5.1_1/include.

The reason you don't need to do this for Go, is that the Go protobuf package includes prebuilt stubs for these types.

@rhodee
Copy link
Author

rhodee commented Mar 29, 2018

Very clear and thank you for your patience. This issue can be closed.

@rhodee rhodee closed this as completed Mar 29, 2018
@Ali-Mbacho
Copy link

I had the same problem dealing with money.proto and my solution was.

  1. I manually added from money.proto file to my /usr/incude/google/protobuf/....
  2. added money.proto to my project.
  3. I generated both myproto.proto and money.proto files separately with protobuf.
  4. Lastly I imported 'package:PATH TO/money.pb.dart' in myproto.pb.dart.

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

3 participants