protobuf.js icon indicating copy to clipboard operation
protobuf.js copied to clipboard

Error: illegal token in proto file which can be used normally

Open antonilol opened this issue 3 years ago • 2 comments

protobuf.js version: 7.1.0

i get an error when generating typescript types with a proto file that works normally with loadSync and older grpc.load. is this the right place to post this? the stack trace says the error occurred in protobufjs

# not js, this is shell script
# download LND proto file
wget -O lightning.proto https://raw.githubusercontent.com/lightningnetwork/lnd/b208642caf96b5753e9e295450a26917e1b0bb0b/lnrpc/lightning.proto
# try generate typescript types
npx proto-loader-gen-types --keepCase --longs=String --enums=String --defaults --oneofs --grpcLib=@grpc/grpc-js --outDir=types lightning.proto

(proto-loader-gen-types is from @grpc/proto-loader)

Error: illegal token 'PendingChannels' (lightning.proto, line 153)
    at illegal (/tmp/node_modules/protobufjs/src/parse.js:96:16)
    at parseService_block (/tmp/node_modules/protobufjs/src/parse.js:699:23)
    at ifBlock (/tmp/node_modules/protobufjs/src/parse.js:290:17)
    at parseService (/tmp/node_modules/protobufjs/src/parse.js:691:9)
    at parseCommon (/tmp/node_modules/protobufjs/src/parse.js:269:17)
    at parse (/tmp/node_modules/protobufjs/src/parse.js:829:21)
    at process (/tmp/node_modules/protobufjs/src/root.js:127:30)
    at /tmp/node_modules/protobufjs/src/root.js:194:17
    at fetchReadFileCallback (/tmp/node_modules/@protobufjs/fetch/index.js:51:19)
    at FSReqCallback.readFileAfterClose [as oncomplete] (node:internal/fs/read_file_context:68:3)

i noticed (and use this as a workaround now) that removing all // comments makes it work:

sed -Ei '/^ +\/\/.*/d' lightning.proto

antonilol avatar Aug 31 '22 19:08 antonilol

Repro:

> root.loadSync('lightning.proto', {alternateCommentMode: true})
Uncaught Error: illegal token 'PendingChannels' (lightning.proto, line 153)
    at illegal (/private/tmp/lightning/node_modules/protobufjs/src/parse.js:96:16)
    at parseService_block (/private/tmp/lightning/node_modules/protobufjs/src/parse.js:699:23)
    at ifBlock (/private/tmp/lightning/node_modules/protobufjs/src/parse.js:290:17)
    at parseService (/private/tmp/lightning/node_modules/protobufjs/src/parse.js:691:9)
    at parseCommon (/private/tmp/lightning/node_modules/protobufjs/src/parse.js:269:17)
    at parse (/private/tmp/lightning/node_modules/protobufjs/src/parse.js:829:21)
    at process (/private/tmp/lightning/node_modules/protobufjs/src/root.js:127:30)
    at fetch (/private/tmp/lightning/node_modules/protobufjs/src/root.js:178:13)
    at Root.load (/private/tmp/lightning/node_modules/protobufjs/src/root.js:206:13)
    at Root.loadSync (/private/tmp/lightning/node_modules/protobufjs/src/root.js:247:17)

alexander-fenster avatar Sep 09 '22 01:09 alexander-fenster

So this is caused by alternateCommentMode being enabled by @grpc/proto-loader. Your proto file has a mix of // and /* ... */ comments, which apparently confuses the parser. I won't be able to look into this deeper soon enough, but should you have a fix, please feel free to contribute :)

Cc: @murgatroid99 - FYI.

alexander-fenster avatar Sep 09 '22 01:09 alexander-fenster