-
-
Notifications
You must be signed in to change notification settings - Fork 240
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
Incorrect webpack --watch
behavior for type-only imports when using yarn workspaces
#611
Comments
Actually, in a larger repo I still don't get some type errors when changing files, even when I've changed all |
Thanks for reporting that, I will take a look hopefully in this week :) |
Thanks for the response! Please let me know if you need any more information or help debugging 🙂 I noticed that my example repo demonstrates a partial issue, but the workaround doesn't work in our actual real-world repo, so the issue might be more complex than I initially guessed. I created a branch where I tried to remove all |
Thanks again for the reproduction repository. I wish everyone would report issues as you did 😄 |
Thanks! Is there a new build of fork-ts-checker that I can install to test out #615 in my repo? |
I just merged #615, should be released in 30 minutes |
I tried 6.2.9, and improved some of the settings you recommended: https://github.com/foxglove/studio/compare/90cdcd3 However, I am now getting some errors when building. Strangely, the Errors from
|
Here's an update to my example repo: jtbandes/ts-workspaces-repro@a4dde59 I enabled |
I'm still having issues in my main repo that I unfortunately can't reproduce in the small test repo. If you have some time to try it out, we would really appreciate the help:
Note that this doesn't use build mode / project references. We don't think it's necessary because we don't run tsc directly for the main packages under There is another branch (mentioned in a previous comment) where I tried using project references / build mode — but this is extremely slow and cpu intensive, and produces all the .js/.d.ts files on the filesystem, which we don't need for our webpack build. |
^ This PR contains a workaround for the issue, so we now get type errors when changing type-only files and files in other workspace packages. However, it also seems to slow down the type checking step (is fork-ts-checker re-checking every file mentioned in |
It seems that there is another "bug" in the plug-in. When you run project references with ts-loader, ts-loader writes files faster than plugin and typescript uses mtime of file to decided if re-check is needed. I have some ideas how to fix it. But I'm also thinking that maybe I could implement some exception for directories that are symlinked so project references would not be required here. I'm thinking about this, because I don't know what will be a performance impact of using project references :) |
cc @sheetalkamat and @andrewbranch in case they're interested |
Does this mean ts-loader and fork-ts-checker are both competing to write .js and .d.ts files if project references are used? I noticed that I did get both |
In the plugin, I use memfs to write files, so they are not written to the real file system. The problem is that when I read them, I use "passive" file system, which uses the newest one by comparing mtime. The #617 tries to fix that by using memfs always for output files :) |
Could you try 6.2.10? It should work, but I'm also interested if it's performant in your project :) |
I tried it briefly and updated this branch: https://github.com/foxglove/studio/compare/jacob/webpack-ts-fixes I started running into some new build errors but didn't have time to look into it yet (possibly I just missed one of the project dependencies). But I think overall, using project references is less performant for us, so we would prefer if there's a way to fix the incorrect watch behavior when project references are not used... |
Ok, I think the easiest fix for you would be to use https://github.com/pigcan/extra-watch-webpack-plugin: plugins: [
new ForkTsCheckerWebpackPlugin(),
new ExtraWatchWebpackPlugin({
files: ['packages/**/*.ts']
})
], I don't want to make this plugin too smart to detect workspaces because there are so many edge-cases. I think it makes more sense to let developers specify where they are using the extra-watch-webpack-plugin plugin. Let me know if it works in your project :) |
Or maybe you could even use built-in plugin functionality: plugins: [
new ForkTsCheckerWebpackPlugin({
typescript: {
configOverwrite: {
include: ["./src/**/*.ts", "./packages/**/*.ts"]
}
}
})
] |
Or maybe you can just update |
That's what I tried here: https://github.com/foxglove/studio/pull/967/files The problem is that these changes are only required because of our webpack/fork-ts-checker setup, and they are a bit fragile/hard to maintain... and it seems like it also adds extra rebuild time due to typechecking more files. From a TypeScript standpoint these files don't need to be listed in This is what I'm confused by: how does fork-ts-checker decide what files to watch? Is it purely based on the |
Yes. The list of files to watch cannot be provided asynchronously to webpack, so this operation is blocking. Because of that, we can't hook into TypeScript imports to get a list of files to watch. If we would do that, the
The plugin doesn't have its own watch system - it uses webpack's one. So on each rebuild, we get a list of changed and removed files and we send this to the plugin. Then, this list is propagated to the TypeScript instance. The situation you describe is when some files (type-only in this case) are not being watched. In this case, TypeScript will not be notified about this file change so it will keep the out-of-date version of the file in memory. |
I think adding Of course, the most correct way would be to use webpack API to build a proper dependency tree, but this is something that we intentionally sacrifice for performance :) |
@piotr-oles any update on this? |
Could you test with the latest version? I fixed some bugs, maybe this will help |
I tested the newest version with the reproduction repository and I'm no longer able to reproduce the issue. I think #712 may fix the bug :) I'm closing this issue, let me know if this isn't the case and it still doesn't work. |
Hi, thanks for the update! I tested 7.2.1 in https://github.com/foxglove/studio and I still experience the same issue: when I run |
Chiming in here to give another minimal repo where the issue happens. It's not necessarily related to yarn and/or workspaces. The only thing 'special' about this repo is that it is using Whenever you change something in the Using The repo: https://github.com/didii/fork-ts-checker-watch |
@didii In the example you posted, you could fix it by changing |
@piotr-oles Thanks, that indeed fixes the watch issue. It's not exactly how I'd want it since it will now check every file, even if it is not included (e.g. storybook files or some plain POC files), but it does suffice for dev purposes :) Maybe this is something that should be added as a remark in the readme since it deviates from the default TSC behavior? |
Current behavior
Type-only imports (
import type X
, orimport X
where X is only used as a type) result in incorrectwebpack --watch
behavior. When files residing in workspace packages are modified, the importing code does not always get recompiled.This is a similar issue to #36 and TypeStrong/ts-loader#1138, but that issue has been fixed and the recommendation to use
"importsNotUsedAsValues": "preserve"
has been removed in #516. However, issues still arise when using yarn workspaces.It seems like enabling
"importsNotUsedAsValues": "preserve"
and avoidingimport type
might be a viable workaround for this issue, but that's not great, becauseimport type
is a core TS feature that should ideally be supported properly.Expected behavior
Modifying a file in a workspace package should cause webpack to recompile the bundle, even if it's only used via type-only imports.
Steps to reproduce the issue
Check out https://github.com/jtbandes/ts-workspaces-repro. This repo uses Yarn workspaces and has two sub-packages under
packages/pkg1
andpackages/pkg2
. The main filesrc/index.ts
imports from these packages.Run
yarn install && yarn webpack --watch --progress --mode=development
Modify
type Example1
in packages/pkg1/index.ts. Observe that webpack does not recompilesrc/index.ts
nor display new type errors. ❌Modify
function Example2
in packages/pkg2/index.ts. Observe that webpack does recompilesrc/index.ts
and display new type errors (this is not a type-only import). ✅Add
"compilerOptions": { "importsNotUsedAsValues": "preserve" }
to tsconfig.json. Now, observe that changingpkg1/index.ts
does cause webpack to recompile. ✅import {Example1} from 'pkg1'
toimport type {Example1} from 'pkg1'
in src/index.ts. Observe that changingpkg1/index.ts
again does not result in a recompile. ❌ (This is because"importsNotUsedAsValues": "preserve"
does not affect explicitimport type
imports, which are always removed.)Issue reproduction repository
https://github.com/jtbandes/ts-workspaces-repro
Environment
The text was updated successfully, but these errors were encountered: