protobuf-javascript icon indicating copy to clipboard operation
protobuf-javascript copied to clipboard

Commonjs pb exports do not carry intellisense

Open madaster97 opened this issue 4 years ago • 4 comments

What version of protobuf and what language are you using? Version: v3.6.1 Language: Javascript

What operating system (Linux, Windows, ...) and version? Ubuntu 20.04, x86_64

What runtime / compiler are you using (e.g., python version or gcc version) protoc pre-built compiler

What did you do? Steps to reproduce the behavior:

  1. Create a sample protoc file, here simple.proto. I took it from this example, modified to "proto3"
  2. Use the compiler to autogen JavaScript code: protoc --js_out=import_style=commonjs,binary:. simple.proto
  3. Use the default export as an object with expected Schema: const Schema = require('./simple_pb');
  4. Attempt to use the expected Class(es) on that object: const test = new Schema.Test1();
  5. See that you do not receive intellisense for the Test1() class, nor for expected operations it's instances

What did you expect to see Intellisense for the simple_pb operations on the default export.

What did you see instead? No Intellisense, but the operations themselves do execute (setting values, serialzing to Binary).

I was able to "fix" the issue by changing the last line of the simple_pb.js file. The goog export extension did not work, so I just commented it out and spread the goog and proto objects into module.exports:

module.exports = {...goog, ...proto};
// goog.object.extend(exports, proto);

madaster97 avatar Mar 06 '21 20:03 madaster97

it is because it uses google module system instead of commonjs or esm and not likely to be fixed soon.

thesayyn avatar Apr 24 '21 15:04 thesayyn

I am running into this issue and it is very frustrating

piekstra avatar Jun 07 '21 19:06 piekstra

@piekstra you might wanna try https://github.com/thesayyn/protoc-gen-ts

thesayyn avatar Sep 14 '21 20:09 thesayyn

It sounds like the issue is the way we manage exports in the code generator. We need to do a more typical assignment to exports here: https://github.com/protocolbuffers/protobuf-javascript/blob/a428c58273abad07c66071d9753bc4d1289de426/generator/js_generator.cc#L3696

it might work to simply delete that code and change GetNamespace to return "exports" in common js mode.

dibenede avatar Sep 02 '22 22:09 dibenede