Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.guard.ts files are created in imported packages #146

Open
cauthmann opened this issue Jul 2, 2021 · 4 comments
Open

.guard.ts files are created in imported packages #146

cauthmann opened this issue Jul 2, 2021 · 4 comments
Labels
bug Something isn't working

Comments

@cauthmann
Copy link
Contributor

Hello,

Using v1.0.0-alpha.24.

I'm using a yarn monorepo, with two packages, in a directory structure similar to this:

./package-a/
./package-b/
./node_modules/

package-b has a dependency on package-a. On yarn/linux this creates a symlink to ../package-a in node_modules.

If I run ts-auto-guard on package-b, it will create .guard files in package-a.

Steps to reproduce:

git clone https://github.com/cauthmann/ts-auto-guard-monorepo.git
cd ts-auto-guard-monorepo
yarn install
cd package-b
yarn run build # ts-auto-guard --export-all --project tsconfig.json

observe the generated files:

package-a/index.guard.ts
package-b/index.guard.ts

The first file should not have been created. package-b doesn't even import any types from package-a.

The file in package-a is created with or without --export-all (it's just empty without).

There seems to be a check whether the file is inside node_modules, which package-a/index.ts is not (due to the symlink - if I copy the directory over, replacing the symlink, no files are generated).

As a possible solution, it could also check whether the file is in the current project's directory, e.g. below the relevant tsconfig.json.

@rhys-vdw
Copy link
Owner

rhys-vdw commented Jul 6, 2021

Hey, sorry for the slow response. I think I typed something then forgot to hit send.

Yes, this is obviously a bug. AFAIK this should only be looking at files in the current project. Are you sure you're not using any tsconfig options to include the other directory?

Either way an empty file should never be created.

I will accept a PR to fix this. Let me know if you need any help.

@rhys-vdw rhys-vdw added the bug Something isn't working label Jul 6, 2021
@cauthmann
Copy link
Contributor Author

cauthmann commented Jul 6, 2021

Oof. I made a local copy of ts-auto-guard and depended on it in my testcase via a file:.. url in package.json and.. the bug disappeared.

Using a file url will copy over the node_modules folder from ts-auto-guard into my testcase, and then the bug disappears. If I remove the node_modules folder, it's back.

The observable difference is that project.getSourceFiles() will sometimes include package-a/index.ts, and sometimes it won't.

I spent hours trying to figure out which difference in dependencies leads to the different results, but got nowhere.

Instead, I reproduced the bug in a minimal example with the latest ts-morph, and filed an issue with them: dsherret/ts-morph#1172

If that gets fixed upstream, ts-auto-guard would have to upgrade to ts-morph 11. Their breaking-changes don't list anything suspicious, and the upgrade seems to work fine without code changes, but some of the testcases break. Were you planning such an upgrade? Otherwise I doubt this bug is fixable.

Either way an empty file should never be created.

Then that's a separate bug. Even without the symlink shenanigans, running ts-auto-guard on a file without exported types will create empty files.

@rhys-vdw
Copy link
Owner

rhys-vdw commented Jul 7, 2021

@cauthmann thank you so much for your investigation and for filing the upstream error. I've invited you on as a collaborator as you're already contributing to the project IMO. I'm not actively working with TS at present (and am very busy elsewhere) but will keep an eye out and am happy to review PRs or answer questions.

@rhys-vdw
Copy link
Owner

rhys-vdw commented Dec 2, 2021

Uh, this issue might have been fixed along with #147. Will need to check before closing this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants