flatbuffers icon indicating copy to clipboard operation
flatbuffers copied to clipboard

[TS] Regression in generating from simple schema

Open FallingSnow opened this issue 3 years ago • 7 comments

When upgrading from flatc version 2.0.0 to 2.0.5 it no longer correctly generates proper typescript code. Using the schema below (datapoint.fbs) data.ts and datapoint.ts files are generated. data.ts contains usable code but imports datapoint.ts. datapoint.ts only contains a single line, a reference to itself.

datapoint.fbs

struct Datapoint {
  t: int64;
  o: float64;
  h: float64;
  l: float64;
  c: float64;
  v: float64;
  w: float64;
}

table Data {
  datapoints: [Datapoint];
}

root_type Data;
$ flatc --ts datapoint.fbs

Generated datapoint.ts

export { Datapoint } from './datapoint';

FallingSnow avatar Mar 28 '22 00:03 FallingSnow

@krojew for ts support

I tried to reproduce it myself and it seems to me that the problem is a name collision. The generated files include${your_schema}.ts and ${your_type[i]}.ts for each of your types. The type Datapoint and the schema file name datapoint.fbs both try to generate datapoint.ts and collide.

A simple workaround would be to rename your schema file. My 2c for a fix would be that the file with the export should be called datapoint.fbs.ts or datapoint_generated.ts or something that's harder to collide with.

CasperN avatar Mar 28 '22 02:03 CasperN

I think I am seeing the same issue when trying to create the ts code for Apache Arrow.

domoritz avatar Mar 28 '22 03:03 domoritz

FWIW, the single-file ts codegen flag I created for #7161 would allow you to work around this, although it does actually generate self-imports (although they are at least correct self-imports), but I imagine that swapping to that would be more work than getting the relevant codegen fixed for this case.

Seems like making the top-level generated file for typescript have a _generated (well, whatever the contents are of --filename-suffix are, more precisely) suffix would be desirable, though.

jkuszmaul avatar Mar 28 '22 18:03 jkuszmaul

#7098

dbaileychess avatar Mar 31 '22 00:03 dbaileychess

@krojew @CasperN Can you take a look at this regression?

domoritz avatar Apr 16 '22 16:04 domoritz

@bjornharrtell could you take a look at this. It's preventing me from upgrading flatbuffers in Apache Arrow.

domoritz avatar Jun 23 '22 02:06 domoritz

@domoritz would like to but unlikely to be able to prioritize this in near future. You are welcome to work on it yourself.

bjornharrtell avatar Jun 23 '22 12:06 bjornharrtell

@domoritz I have a fix up.

dbaileychess avatar Aug 13 '22 05:08 dbaileychess