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

[Bug] Couldn't find package "tslib" error in a nested project structure #4175

Closed
mcmxcdev opened this issue Dec 18, 2020 · 7 comments · Fixed by #4258
Closed

[Bug] Couldn't find package "tslib" error in a nested project structure #4175

mcmxcdev opened this issue Dec 18, 2020 · 7 comments · Fixed by #4258

Comments

@mcmxcdev
Copy link
Contributor

🐛 Bug report

Description

I am working on a frontend that had the expected structure with tsconfig.json at the root of the project, no error was reported.

Now the frontend was integrated as git submodule within a project with a nested folder structure and I always get the error:

Couldn't find package "tslib".
(typescript-config/import-helpers)

This error pops up even though tslib is installed (v2.0.3) and importHelpers: true is enabled in the tsconfig.json

Environment

  • hint version: 1.5.9 (VSCode)
  • Node.js version: v14.12.0
  • OS: Ubuntu
@mcmxcdev mcmxcdev changed the title [Bug] Couldn't find package "tslib" in a nested project structure [Bug] Couldn't find package "tslib" error in a nested project structure Dec 18, 2020
@antross
Copy link
Member

antross commented Dec 21, 2020

Thanks for reporting @mhatvan!

You mentioned tslib is installed. Is it part of the top-level project or part of the project being loaded as a submodule?

I ask because when I just took another look at the implementation for this hint it looks like it currently only checks the top-level node_modules for tslib which may be the source of the bug:

const pathToTslib = path.join(process.cwd(), 'node_modules', 'tslib');

If so, a fix here likely requires updating the hint to scope the check to opened/included tsconfig.json files. Then it can look for tslib relative to the location of the file as opposed to only at the top-level.

@mcmxcdev
Copy link
Contributor Author

It is part of the project being loaded as a sub module, so the checking for tslib can definitely be improved to also include subdirectories.

@antross
Copy link
Member

antross commented Jan 26, 2021

@mhatvan would you be interested in putting together a PR for this fix?

Looks like the hint is already triggered after parsing a tsconfig.json so we just need to update the logic to search for tslib relative to the path of the tsconfig.json we just parsed instead of the current working directory. I bet require.resolve with the paths option could do the trick.

@mcmxcdev
Copy link
Contributor Author

Sure thing, will provide a PR soonish ;)

@mcmxcdev
Copy link
Contributor Author

@antross After taking an initial look, I am wondering how I can receive the current full path where the tsconfig.json was found through webhint in import-helpers.ts ?

Then this would work: path.dirname(require.resolve("tslib", {paths: ["<path-to-tsconfig.json>"] }))

@antross
Copy link
Member

antross commented Jan 27, 2021

@mhatvan thanks for taking a look!

I think we already have the path to the tsconfig.json as the resource string pulled off the event that triggered the validation:

Does that help?

@mcmxcdev
Copy link
Contributor Author

Yep, that's it! I got confused because it is not an absolute path but is prefixed by file:///, but works nonetheless!

antross pushed a commit that referenced this issue Feb 9, 2021
Change pathToTslib to resolve the path to tslib based on
the tsconfig.json location rather than current working directory

- - - - - - - - - - - - - - - - - - - -

Fix #4175
Close #4258
sarvaje pushed a commit that referenced this issue May 17, 2021
Change pathToTslib to resolve the path to tslib based on
the tsconfig.json location rather than current working directory

- - - - - - - - - - - - - - - - - - - -

Fix #4175
Close #4258
sarvaje pushed a commit to sarvaje/hint that referenced this issue Jun 7, 2021
Change pathToTslib to resolve the path to tslib based on
the tsconfig.json location rather than current working directory

- - - - - - - - - - - - - - - - - - - -

Fix webhintio#4175
Close webhintio#4258
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants