Fix generator error when output is in current directory (#553)
diff --git a/generator/nanopb_generator.py b/generator/nanopb_generator.py
index 400b8e7..f21e540 100755
--- a/generator/nanopb_generator.py
+++ b/generator/nanopb_generator.py
@@ -2006,8 +2006,8 @@
else:
data = open(filename, 'rb').read()
- for fdesc in descriptor.FileDescriptorSet.FromString(data).file:
- fdescs[fdesc.name] = fdesc
+ fdesc = descriptor.FileDescriptorSet.FromString(data).file[-1]
+ fdescs[fdesc.name] = fdesc
# Process any include files first, in order to have them
# available as dependencies
@@ -2031,7 +2031,10 @@
sys.stderr.write("Writing to %s\n" % paths)
for path, data in to_write:
- os.makedirs(os.path.dirname(path), exist_ok=True)
+ dirname = os.path.dirname(path)
+ if dirname and not os.path.exists(dirname):
+ os.makedirs(dirname, exist_ok=True)
+
with open(path, 'w') as f:
f.write(data)
diff --git a/tests/generator_relative_paths/SConscript b/tests/generator_relative_paths/SConscript
index a7c727a..3934e49 100644
--- a/tests/generator_relative_paths/SConscript
+++ b/tests/generator_relative_paths/SConscript
@@ -35,3 +35,10 @@
env.Match("simple_pb_c_ok", ["build/simple.pb.c", "simple.expected"])
env.Match("any_pb_h_ok", ["build/protobuf/any.pb.h", "any.expected"])
env.Match("any_pb_c_ok", ["build/protobuf/any.pb.c", "any.expected"])
+
+# Test when not using -D
+env.Command(["test.pb.c", "test.pb.h"], "test.proto",
+ env['NANOPB_GENERATOR'] + " test.proto",
+ chdir = True)
+env.Match("test_pb_h_ok", ["test.pb.h", "test.expected"])
+env.Match("test_pb_c_ok", ["test.pb.c", "test.expected"])