google / flatbuffers

FlatBuffers: Memory Efficient Serialization Library
https://flatbuffers.dev/
Apache License 2.0
23.06k stars 3.22k forks source link

Typescript exports for 3 namespace levels uses wrong relative paths #7898

Open jens-ghc opened 1 year ago

jens-ghc commented 1 year ago

Using flatc version 23.3.3 and compiling to Typescript:

I'm running into an issue where using 3 levels of namespacing in the flatbuffer file (e.g. com.company.test) produces an export file named tests.ts which references other files using a wrong relative path.

For example, here's the content of person.fbs

namespace com.company.test;

table Person {
  name:string;
  age:short;
}
root_type Person;

when running

flatc --ts person.fbs

it produces this directory structure:

com
 |
 +company
     |
     +test
     |   |
     |   +person.ts
     | 
     +test.ts

The content of test.ts is

export { Person } from './company/test/person.js';

When compiling file test.ts in typescript, I get this error:

src/com/company/test.ts:3:24 - error TS2307: Cannot find module './company/test/person.js' or its corresponding type declarations.

3 export { Person } from './company/test/person.js';

This seems to be caused by test.ts using the wrong relative path. When I manually change the path in test.ts to ./test/person.js it compiles fine.

This issue only happens when I use 3 namespace levels. If I use 2 levels, then the relative path in test.ts are correct. E.g. this will produce a correct file that compiles just fine in typescript:

namespace com.test;

table Person {
  name:string;
  age:short;
}
root_type Person;

I tried any of the typescript related compiler flags for flatc but none of them made a difference. How can I get the relative paths to be produced correctly?

saurabhmj11 commented 1 year ago

This issue appears to be a bug in the current version of flatc that affects TypeScript output when using three or more levels of namespacing.

As a workaround, you can manually adjust the test.ts file to use the correct relative path for the Person module. In your example, you can change the export statement in test.ts from:

javascript Copy code export { Person } from './company/test/person.js'; to:

javascript Copy code export { Person } from './test/person.js'; Alternatively, you can use a different tool to generate TypeScript code from your FlatBuffers schema, such as flatbuffers-ts, which is a third-party TypeScript code generator for FlatBuffers that supports multiple levels of namespacing. To use flatbuffers-ts, you can install it via npm:

Copy code npm install flatbuffers-ts And then generate TypeScript code from your schema file using the flatbuffers-ts command:

Copy code flatbuffers-ts person.fbs This should generate TypeScript code with the correct relative paths for the Person module, even with three or more levels of namespacing.

rodry286MB commented 1 year ago

not using

urbenlegend commented 1 year ago

I am getting the same issue with flatc 23.5.26. I've resorted to using sed to remove the extra toplevel folder in the export paths, but it would be nice to see this issue get fixed.

MaxLeb commented 11 months ago

Can this that issue https://github.com/google/flatbuffers/issues/7828 be related to this one ? It has been closed without any review but it feels like it is close to the same problem

github-actions[bot] commented 5 months ago

This issue is stale because it has been open 6 months with no activity. Please comment or label not-stale, or this will be closed in 14 days.

urbenlegend commented 5 months ago

This bug is still present. I have to manually replace the paths with the correct relative paths each time.

jens-ghc commented 4 months ago

Yes, same for me. It's pretty disruptive as we have to do the adjustment every single time in a couple of files when we regenerate the flatbuffers. Would be great if this could be fixed.