Closed najoast closed 2 years ago
检查过了,你函数用错了,loadifle
的意思是“将这个文件的内容载入pb的内存数据库”,你这里的需求是“编译期间载入一个新的文件继续编译”,所以你应该用“parsefile”函数,下面的代码就没有报错了:
local protoc = require "protoc"
local p = protoc.new()
p.include_imports = true
p.unknown_import = function(self, name)
return self:parsefile("test.proto")
end
p:load [[
syntax = "proto3";
import "not_exist.proto";
]]
重现方法
创建
test.proto
文件,内容为空即可。创建
test2.lua
,把以下代码拷入:p:load [[ syntax = "proto3"; import "not_exist.proto"; ]]
lua test2.lua
lua: ./protoc.lua:1169: bad argument #2 to 'encode' (table expected at field 'file', got boolean) stack traceback: [C]: in function 'pb.encode' ./protoc.lua:1169: in function 'protoc.compile' ./protoc.lua:1180: in function 'protoc.load' test2.lua:11: in main chunk [C]: in ?
self:compilefile
最终会调用pb.encode('.google.protobuf.FileDescriptorSet', set)
, 这个 set 就是上面的 infos,里面有个 true, 就导致在 encode 时报错了为什么有个 true
在
Parser:parse(src, name)
里,为了防止循环 load,在开始加载前赋了个 true,加载完成后再把加载结果放进去。可能就是各种 callback 导致状态没同步好。