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

Difference between how Svelte and JS imports from node_modules are handled #1056

Open
dummdidumm opened this issue Jun 15, 2021 · 10 comments
Open

Comments

@dummdidumm
Copy link
Member

dummdidumm commented Jun 15, 2021

Through #1048, I noticed there is a difference between how JS and Svelte files imported from node_modules are handled:

  • Imports from node_modules that have no accompanying type definition (d.ts file) are marked as any which errors on strict mode (no implicit any). This means TS does not attempt to aquire the type structure from the node_modules package and also does not type-check it
  • Svelte imports from node_modules are read and treated as a regular file inside the project. This means they don't need a accompanying type definition and their types are aquired, which can be good or bad depending on the quality of the Svelte file. Some libraries like svelte-loading-spinners take advantage of this and provide TS-versions of Svelte files as types, which is dangerous.

I think in the long run it would be best to align with the behavior of TypeScript here. This would probably a breaking change to some users which do deep imports of Svelte files within libraries which don't have a .svelte.d.ts next to them. I'm also not sure how to even achieve this as I thought this stuff is handled inside ts.resolveModuleName. As an intermediate step, we definetly need to stop type-checking stuff inside node_modules (also see #1100)
@jasonlyu123 thoughts?

@cor
Copy link
Contributor

cor commented Jul 16, 2021

Is there a possible workaround for this in the meantime? It's hard to take advantage of svelte-check when the error list is polluted by dependencies.

@dummdidumm
Copy link
Member Author

dummdidumm commented Jul 16, 2021

If you don't use the --tsconfig setting it should only check Svelte files that you tell it to. But that won't check JS/TS files or files or transitive files anymore.

@cor
Copy link
Contributor

cor commented Jul 16, 2021

So I currently have two options:

  1. Check both .svelte and .ts files, with the error list polluted by dependencies
  2. Only check .svelte files, .ts files remain unchecked

Correct?

@dummdidumm
Copy link
Member Author

Yes. You could check .ts files with tsc --noEmit however. But that might produce some false positives, depending on whether or not you have named imports from Svelte files inside TS files.

dummdidumm pushed a commit to dummdidumm/language-tools that referenced this issue Jul 17, 2021
node_modules are considered read-only and unchangeable, so they should not be diagnosed. Make an exception for src/node_modules as this is a Sapper convention
sveltejs#1056, sveltejs#1100
dummdidumm added a commit that referenced this issue Jul 17, 2021
node_modules are considered read-only and unchangeable, so they should not be diagnosed. Make an exception for src/node_modules as this is a Sapper convention
#1056, #1100

Co-authored-by: cor <[email protected]>
@probablykasper
Copy link

Is there any workaround for this yet, or are the only two options what @cor mentioned?

@dummdidumm
Copy link
Member Author

Could you specify what your problem is? Files inside node_modules shouldn't get diagnosed anymore.

@probablykasper
Copy link

I'm running svelte-check --tsconfig ./tsconfig.json and it's showing errors/hints from node_modules:

Logs
> @ check /Users/kasper/dev/git/kryp
> svelte-check --tsconfig ./tsconfig.json


====================================
Loading svelte-check in workspace: /Users/kasper/dev/git/kryp
Getting Svelte diagnostics...

/Users/kasper/dev/git/kryp/node_modules/@tauri-apps/api/helpers/event.ts:7:1
Error: This import is never used as a value and must use 'import type' because 'importsNotUsedAsValues' is set to 'error'. 

import { WindowLabel } from '../window'
import { invokeTauriCommand } from './tauri'


/Users/kasper/dev/git/kryp/node_modules/@tauri-apps/api/event.ts:15:1
Error: This import is never used as a value and must use 'import type' because 'importsNotUsedAsValues' is set to 'error'. 
import { transformCallback } from './tauri'
import { LiteralUnion } from 'type-fest'



/Users/kasper/dev/git/kryp/node_modules/@tauri-apps/api/helpers/os-check.ts:8:20
Hint: 'appVersion' is deprecated. 
function isLinux(): boolean {
  return navigator.appVersion.includes('Linux')
}


/Users/kasper/dev/git/kryp/node_modules/@tauri-apps/api/helpers/os-check.ts:12:20
Hint: 'appVersion' is deprecated. 
function isWindows(): boolean {
  return navigator.appVersion.includes('Win')
}


/Users/kasper/dev/git/kryp/node_modules/@tauri-apps/api/helpers/os-check.ts:16:20
Hint: 'appVersion' is deprecated. 
function isMacOS(): boolean {
  return navigator.appVersion.includes('Mac')
}


/Users/kasper/dev/git/kryp/src/lib/NumericInput.svelte:36:9
Error: Object is possibly 'null'. (ts)
      } else {
        start -= 1
        end -= 1


/Users/kasper/dev/git/kryp/src/lib/NumericInput.svelte:37:9
Error: Object is possibly 'null'. (ts)
        start -= 1
        end -= 1
      }


====================================

@dummdidumm
Copy link
Member Author

The diagnostics you get from node_modules are from real TypeScript files. These files will be type-checked by TypeScript/tsc, too. You can try yourself by doing import { emit } from '@tauri-apps/api/event' in a .ts file and then running tsc --noEmit. There's also an issue about this in the TypeScript repo, with the current conclusion that this is "works as designed": microsoft/TypeScript#40426. This sounds more like a misconfiguration of the build step of the library (why are .ts files deployed?) or a misuse of the library (avoid importing the .ts files).

dummdidumm pushed a commit to dummdidumm/language-tools that referenced this issue Sep 13, 2021
Previously, only TS diagnostics were omitted
sveltejs#1056
dummdidumm added a commit that referenced this issue Sep 13, 2021
Previously, only TS diagnostics were omitted
#1056
@vpalos
Copy link

vpalos commented Dec 31, 2022

The diagnostics you get from node_modules are from real TypeScript files. These files will be type-checked by TypeScript/tsc, too. You can try yourself by doing import { emit } from '@tauri-apps/api/event' in a .ts file and then running tsc --noEmit. There's also an issue about this in the TypeScript repo, with the current conclusion that this is "works as designed": microsoft/TypeScript#40426. This sounds more like a misconfiguration of the build step of the library (why are .ts files deployed?) or a misuse of the library (avoid importing the .ts files).

It's not necessarily a misconfiguration to have *.ts files published in dependencies, right? Perhaps we're used to see .js+.d.ts files more often, but I see no problem in publishing libraries with *.ts files (even exclusively).

Also svelte-check is a tool to check for errors, while tsc is a compiler; therefore while it might be reasonable for tsc to go into all dependencies and show all errors by design, for svelte-check it seems more natural to have the option to exclude dependencies, since the goal is to validate my own codebase, and having the results polluted by tens, even hundreds of errors from dependencies can be very bad, especially since fixing all those errors is completely out of scope.

Just my 2 cents. Excellent work btw!

@thelinuxlich
Copy link

Any workaround?

dummdidumm added a commit that referenced this issue Aug 28, 2024
This change allows people to write export maps using only a `svelte` condition (and no `types` condition) and still have the types for their components resolved (i.e. the import is found) as long as they use TypeScript (i.e. have lang="ts" attribute) inside it. This should help people using monorepo setups with strong typings and not wanting to provide d.ts files alongside.

This is achieved doing three adjustments:
- add `customConditions: ['svelte']` to the compiler options, so that TypeScript's resolution algorithm takes it into account
- ensure that Svelte files have a module kind of ESM, so that TypeScript's resolution algorithm goes into the right branches
- deal with `.d.svelte.ts` files in the context of an exports map, because that's what TypeScript will try to resolve this to in the end

This is also related to #1056 insofar that we align with TypeScript for this new capability: We don't resolve the file if it's a component not using TypeScript (i.e. not having the lang="ts" tag), similar to how TypeScript does not resolve .js files within node_modules
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants