flatbuffers icon indicating copy to clipboard operation
flatbuffers copied to clipboard

Typescript exports for 3 namespace levels uses wrong relative paths

Open jens-f opened this issue 2 years ago • 11 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?

jens-f avatar Apr 08 '23 17:04 jens-f

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.

saurabhmj11 avatar Apr 21 '23 12:04 saurabhmj11

not using

rodry286MB avatar May 14 '23 23:05 rodry286MB

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.

urbenlegend avatar Jun 30 '23 01:06 urbenlegend

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

MaxLeb avatar Oct 12 '23 09:10 MaxLeb

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.

github-actions[bot] avatar Apr 11 '24 20:04 github-actions[bot]

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

urbenlegend avatar Apr 11 '24 20:04 urbenlegend

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.

jens-f avatar Apr 12 '24 15:04 jens-f