google / flatbuffers

FlatBuffers: Memory Efficient Serialization Library

Home Page:https://flatbuffers.dev/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Typescript exports for 3 namespace levels uses wrong relative paths

jens-totemic opened this issue · comments

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?

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.

not using

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.

Can this that issue #7828 be related to this one ? It has been closed without any review but it feels like it is close to the same problem