Hookyns / tst-reflect

Advanced TypeScript runtime reflection system
MIT License
338 stars 11 forks source link

const _ßr = require("tst-reflect"); // SyntaxError: Identifier '_ßr' has already been declared #44

Closed mribbons closed 2 years ago

mribbons commented 2 years ago

I am using yarn workspaces, project structure is like this @megarepo/backend @megarepo/shared

backend imports from shared.

I have added the tst-reflect setup to both packages, and I'm using ts-patch.

My use case is reflecting the type names from shared when they are passed as generic arguments to calls in backend. Things seemed to be working OK, I can compile the shared project and use gettype(), however now it looks like require("tst-reflect") is being injected into all of the .js files in dist, the error below occurs when I try to run build backend:

\rootpath\packages\shared\dist\SharedClass.js:6
const _ßr = require("tst-reflect");
      ^

SyntaxError: Identifier '_ßr' has already been declared

Initially I was able to build and run both packages and reflect some type names, but I noticed generic type arguments didn't work so I omitted that code, and the next build failed. The line number referred to in the error doesn't contain _ßr = which may suggest an issue with the transformer.

There's a lot going on, so generating a repro may be a challenge, I am hoping this is a known issue with a fix, but I will do my best if required.

Hookyns commented 2 years ago

Identifier '_ßr' has already been declared

How is that even possible? It means there are multiple declarations in the same scope. Which build tool You use and what's the target, module?

I am hoping this is a known issue with a fix, but I will do my best if required.

Nope. I see this for the first time. There is an opposite issue that _ßr is not declared after hot reloads in some build tools. If You can create simple repro, it would be nice. 🙂

mribbons commented 2 years ago

How is that even possible? It means there are multiple declarations in the same scope.

Actually your question raises an interesting point. Most of the files where the duplicates are being flagged don't import or require tst-reflect, in fact nothing in the shared package does at present. At one point when I was testing reflection to see it working within a single package, I saw the _Identifier 'ßr' has already been declared error, and it looked like an additional require had been inserted at line 0, while I had explicitly imported at line 2. Removing the comment on line 0 resolved the issue.

I have created a gist of the shared package tsconfig: https://gist.github.com/mribbons/3ad4731ba2b1ce81b5e05e0555c9c4c8

shared package build command: tsc -p tsconfig.json --watch --preserveWatchOutput

backend package run command: ts-node index.tsx

Repro won't appear for 18 hours or so ;)

mribbons commented 2 years ago

The problem goes away when I run rimraf tsconfig.tsbuildinfo on the shared package.