grammars-v4
grammars-v4 copied to clipboard
[build] TypeScript broken
Testing of the TypeScript target is broken.
Tests fail with import module problem.
$ bash run.sh ../hw-examples/alter_operator.sql
Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'C:\msys64\home\Kenne\issues\g4-3890\sql\plsql\Generated-TypeScript-hw\PlSqlLexerBase' imported from C:\msys64\home\Kenne\issues\g4-3890\sql\plsql\Generated-TypeScript-hw\PlSqlLexer.js
Did you mean to import ../PlSqlLexerBase.js?
at finalizeResolution (node:internal/modules/esm/resolve:255:11)
at moduleResolve (node:internal/modules/esm/resolve:908:10)
at defaultResolve (node:internal/modules/esm/resolve:1121:11)
at nextResolve (node:internal/modules/esm/hooks:865:28)
at resolve (C:\Users\kenne\AppData\Roaming\npm\node_modules\ts-node\dist\child\child-loader.js:15:125)
at nextResolve (node:internal/modules/esm/hooks:865:28)
at Hooks.resolve (node:internal/modules/esm/hooks:303:30)
at handleMessage (node:internal/modules/esm/worker:196:24)
at Immediate.checkForMessages [as _onImmediate] (node:internal/modules/esm/worker:138:28)
at process.processImmediate (node:internal/timers:478:21) {
code: 'ERR_MODULE_NOT_FOUND',
url: 'file:///C:/msys64/home/Kenne/issues/g4-3890/sql/plsql/Generated-TypeScript-hw/PlSqlLexerBase'
}
6 days ago, MS released a new package for typescript and things haven't worked well since. Unfortunately I can't find a rollback state that does work.
I am able to get the target to work if I add a ".js" suffix for the file import in the generated .ts file. But that is not a good solution. Filed an Issue in the Antlr4 repo as to why some import have ".js" and others do not. I'm not sure whether there are settings in the tsconfig.json to fix the problem.
https://github.com/antlr/antlr4/issues/4491
Apparently, this can be fixed if the entire code is placed in a package. The inconsistency in the tool has been fixed. https://github.com/antlr/antlr4/pull/4492
Note here that this is the result of going ESM. ES modules are used in browsers, which have no search strategy like Node.js (where CommonJS was invented before browsers had module support). Browsers always required to have the full file name, including the extension to be specified for a module. Now that Node.js also supports ESM a new setting was introduced ("Node16") by TS that enforces an explicit extension in TS source code for imports.
In the mid run I expect ESM to completely take over as this is supported by both major parties (browsers + Node.js), so it's a good idea to get used to the fact that the extension is necessary in imports. At least if you don't use a bundler (like esbuild or webpack), which would take care for that (and for which a different TS setting exists).