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

throws an error if the extends resolves to a JS file #7

Closed
dylang opened this issue Jun 10, 2022 · 5 comments · Fixed by #9
Closed

throws an error if the extends resolves to a JS file #7

dylang opened this issue Jun 10, 2022 · 5 comments · Fixed by #9
Labels
bug Something isn't working

Comments

@dylang
Copy link
Contributor

dylang commented Jun 10, 2022

Hi!

This code looks for require.resolve result - and only if that's not found does it try path.join(extendsPath, 'tsconfig.json').

The problem is what if require.resolve resolves to an index.js file. Then we try using that instead of path.join(extendsPath, 'tsconfig.json').

Then we try to read the JSON file. But it's JS not JSON, so we throw an error.

Example:

"extends": "@company/tsconfig"

@company/tsconfig package.json:

"main": "./src/index.js"

@company/tsconfig tsconfig.json

{
  // normal tsconfig.json
}

To resolve this we should do the resolving closer to how TypeScript does it. In this case, it would never resolve a .JS file, only .json. Perhaps if the filename is not .json we then attempt path.join(extendsPath, 'tsconfig.json').

@privatenumber
Copy link
Owner

Is this a real issue you're encountering?

It currently throws if not a valid JSON:

test('invalid json', async () => {
const fixture = await createFixture({
'file.ts': '',
'tsconfig.empty.json': 'require("fs")',
'tsconfig.json': tsconfigJson({
extends: './tsconfig.empty.json',
}),
});
expect(() => getTsconfig(fixture.path)).toThrow('Failed to parse JSON');
await fixture.cleanup();
});

@dylang
Copy link
Contributor Author

dylang commented Jun 10, 2022

Yes - it throws if it's not valid JSON, but what I'm hoping for is that it prioritizes using the tsconfig.json in that path instead of the index.js.

I'm running into this when using tsx in our monorepo. We have a company-standard tsconfig package that has /tsconfig.json and a main pointing to /src/index.js.

TypeScript automatically uses the /tsconfig.json so we use extend: "company-tsconfig-package-name".

A potential workaround for us is to use extend: "company-tsconfig-package-name/tsconfig.json" but that would be a breaking change over many repos that I'm hoping to avoid.

@privatenumber
Copy link
Owner

Gotcha.

Do you mind opening a PR with a failing test case?

@dylang
Copy link
Contributor Author

dylang commented Jun 11, 2022

Failing test created.

BTW - the test framework throws an error but passes if the test JS doesn't compile. 😅

@privatenumber
Copy link
Owner

BTW - the test framework throws an error but passes if the test JS doesn't compile. 😅

What does this mean?

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

Successfully merging a pull request may close this issue.

2 participants