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

fix: correctly detect JS projects with type checking #20944

Merged
merged 4 commits into from
Feb 7, 2023

Conversation

benmccann
Copy link
Contributor

Closes #20595

What I did

Detected if jsconfig.json exists

I also did some minor cleanup by removing an unnecessary cwd usage: https://nodejs.org/api/fs.html#string-paths

Checklist

  • Make sure your changes are tested (stories and/or unit, integration, or end-to-end tests)
  • Make sure to add/update documentation regarding your changes
  • If you are deprecating/removing a feature, make sure to update
    MIGRATION.MD

Maintainers

  • If this PR should be tested against many or all sandboxes,
    make sure to add the ci:merged or ci:daily GH label to it.
  • Make sure this PR contains one of the labels below.

["cleanup", "BREAKING CHANGE", "feature request", "bug", "documentation", "maintenance", "dependencies", "other"]

@benmccann benmccann added the bug label Feb 5, 2023
Copy link
Contributor

@JReinhold JReinhold left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Is the assumption here, that if there's a jsconfig.json then there isn't also a tsconfig.json, and we're certain it's not a TS project? It sounds reasonable to me, but I just want to make sure.

  2. This only works if the jsconfig.json is in the CWD right, is that always the case? What about monorepos, is it valid to have a jsconfig.json in a parent directory, that we would be missing here? Based on a quick glance at hasDependency it looks like we're already detecting package.json in parent directories, so maybe we should do the same for jsconfig.json

@benmccann
Copy link
Contributor Author

Is the assumption here, that if there's a jsconfig.json then there isn't also a tsconfig.json, and we're certain it's not a TS project? It sounds reasonable to me, but I just want to make sure.

Yeah, I don't know what TypeScript would do if you have both, but you really shouldn't have both.

This only works if the jsconfig.json is in the CWD right, is that always the case? What about monorepos, is it valid to have a jsconfig.json in a parent directory, that we would be missing here? Based on a quick glance at hasDependency it looks like we're already detecting package.json in parent directories, so maybe we should do the same for jsconfig.json

Yes. I think even in a monorepo you always need a tsconfig in each project directory. They can all essentially be empty except for an extends declaration extending from a common one in the root monorepo, but I don't think you can just leave it out

@JReinhold
Copy link
Contributor

Yes. I think even in a monorepo you always need a tsconfig in each project directory. They can all essentially be empty except for an extends declaration extending from a common one in the root monorepo, but I don't think you can just leave it out

I did some research and it looks like it's actually perfectly valid to not have a tsconfig.json in the project, as TypeScript will look up in parent paths until it finds one. I think we should support the same, and it looks pretty simple with the get-tsconfig package:

import { getTsconfig } from 'get-tsconfig'

const hasJsConfig = Boolean(getTsconfig('.', 'jsconfig.json'));

@benmccann
Copy link
Contributor Author

Thanks for finding that. I've made that change

@JReinhold
Copy link
Contributor

Tests are failing, it was of course too easy.

Basically our tests for the detection mocks out path and fs, which causes get-tsconfig to fail because they are incomplete mocks. The mocks were introduced in this commit by @yannbf.

I see two paths forward:

  1. Fix/remove the mocks so that the tests won't fail anymore.
  2. Go back to the simpler solution that just supports jsconfig.json in the current dir, with a TODO comment explaining the unsupported edge-case.

I'm okay with option 2, as some support for jsconfig.json is better than nothing. But either way I think we should add a test case for it in detect.test.ts.

@benmccann
Copy link
Contributor Author

I've gone back to the old way and added a TODO. I'm not sure how to add a test for this

@ndelangen
Copy link
Member

@JReinhold this seems fine to merge to me, WDYT?

@JReinhold JReinhold merged commit e5cded8 into storybookjs:next Feb 7, 2023
@benmccann benmccann deleted the create-svelte branch February 7, 2023 23:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SvelteKit setup only works on TypeScript projects
3 participants