flatbuffers icon indicating copy to clipboard operation
flatbuffers copied to clipboard

[TS] Generated Typescript code has `.js` extension

Open stephanemagnenat opened this issue 1 year ago • 4 comments

Using Flatbuffers 22.11.22, and doing:

flatc -o src/flatbuffers --ts --gen-all schemas/all.fbs

The generated .ts files have a .js extension, leading to imports like this:

export { Table } from './namespace/table.js';

while previously (tested with version 2.0.6) they properly correctly had no extension:

export { Table } from './namespace/table';

The generated files themselves properly end with .ts.

You'll find a minimal test case here: https://github.com/stephanemagnenat/flatbuffers-6739

Just type make and look in src/all_generated.ts.

stephanemagnenat avatar Nov 23 '22 13:11 stephanemagnenat

I'm afraid this is not so simple.

Imports with .js extensions in ts code are not "wrong" as far I know and some environments require it.

However, ts files with js extension are wrong.

Will need to investigate but there is a larger rework of TS/JS generation work in progress already in #7510 which might affect this.

bjornharrtell avatar Nov 23 '22 15:11 bjornharrtell

Sorry I was not very clear, the key problem is that flatc generates ts files with .ts extension but writes import of files ending with .js in the generated ts files, which obviously do not exist.

stephanemagnenat avatar Nov 23 '22 17:11 stephanemagnenat

@stephanemagnenat but that is common practice and required by many module loaders. I agree it's not an optimal situation but have a look at https://github.com/microsoft/TypeScript/issues/40878 for some history. If you use modules in a browser directly for example it will require the full path of the module, see https://stackoverflow.com/questions/55251956/how-does-javascript-import-find-the-module-without-an-extension.

bjornharrtell avatar Nov 23 '22 20:11 bjornharrtell

I see, thank you for the explanation!

stephanemagnenat avatar Nov 24 '22 07:11 stephanemagnenat

@bjornharrtell: Can this be made at least configurable? There are plenty of projects where this will start throwing errors because of the added extension.

Leandros avatar Nov 30 '22 16:11 Leandros

@Leandros mm, I'm open to add it as an option if really desired but I'm also curious of why/when it is cause of problem because I don't quite understand it. It's usually the other way round, no extension work "sometimes".

bjornharrtell avatar Nov 30 '22 20:11 bjornharrtell

I don't have a great example why, but it has something to do with the module resolution it could be that for example your local development is using TypeScript to discover it's files which will use a different methode of finding the right extension and a plugin like Eslint or Jest (running on Node.js) might use Node's module resolution (this I have not checked) and would fail to get the .js file to resolve properly to a TS file.

But I agree having an option to force an extension (or no extension) would be welcome to resolve some possible errors while working on a project.

mvdschee avatar Dec 01 '22 02:12 mvdschee

But I agree having an option to force an extension (or no extension) would be welcome to resolve some possible errors while working on a project.

I support this.

Leandros avatar Dec 18 '22 16:12 Leandros

@bjornharrtell I'm importing the generated TypeScript files inside a TypeScript backend written in NestJS. In my case, tsc can't cope with imports with a .js.

Generally, in the JavaScript/TypeScript world, it's common to avoid specifying any extensions for importing. More info here: https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/extensions.md

Leandros avatar Dec 18 '22 16:12 Leandros

I've been working with JavaScript and TypeScript in lots of different projects and contexts and haven't encountered any issue with specifying the full module path with extension (after transpilation), only the other way around. So my own interest in this is low. But I will not oppose a PR that adds an option to generate without extension, so feel free to contribute.

bjornharrtell avatar Dec 18 '22 16:12 bjornharrtell

Since upgrading to flatc-22.9.29 our TS builds have been breaking because of this change. Previous versions didn't specify an extension and worked fine.

maxburke avatar Dec 30 '22 16:12 maxburke

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 Jun 30 '23 20:06 github-actions[bot]

not stale

maxburke avatar Jul 01 '23 20:07 maxburke

In some development environment, there are not TS code file emitted. For example, ts-node has it disabled by default. The same default is used in React projects, at least if you use it with tools like react-scripts.

So yes, having the .js extension in the imports, breaks the code in environments like this.

PS: I would be curios in which situation, you have a TS module A, in which you want to import TS module B, yet you import module B using a .js extension. That mekes no sense to me ....

surdu avatar Oct 02 '23 09:10 surdu

Since https://github.com/google/flatbuffers/pull/7748 was implemented you can opt out of generating with js extension. Closing.

bjornharrtell avatar Oct 02 '23 11:10 bjornharrtell

Nice, but there is a bug in the implementation. When you have an import in one of your schema, the ts file will still contain an export with a .js extension:

If I have the following schemas:

// color.fbs

enum Color:byte { Red = 0, Green, Blue = 2 }
// monster.fbs

include "color.fbs";

namespace Sample;

table Monster {
  color:Color = Blue; // Enum.
}

root_type Monster;

After running flatc --ts --ts-no-import-ext -o monsters_generated monster.fbs we get a file monster.ts with the following content:

// automatically generated by the FlatBuffers compiler, do not modify

export { Color } from './color';
export * as Sample from './sample.js';

surdu avatar Oct 02 '23 13:10 surdu

@surdu can you please report that as a separate issue?

bjornharrtell avatar Oct 02 '23 13:10 bjornharrtell

Sure thing. Just created #8105

surdu avatar Oct 02 '23 15:10 surdu