rspack icon indicating copy to clipboard operation
rspack copied to clipboard

[Bug]: Rspack does not rebuild when modifying pure type files

Open noshower opened this issue 1 year ago • 1 comments

System Info

  System:
    OS: macOS 14.5
    CPU: (16) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 125.49 MB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.12.0 - ~/.nvm/versions/node/v20.12.0/bin/node
    Yarn: 1.22.22 - ~/.nvm/versions/node/v20.12.0/bin/yarn
    npm: 10.5.0 - ~/.nvm/versions/node/v20.12.0/bin/npm
    pnpm: 9.5.0 - ~/.nvm/versions/node/v20.12.0/bin/pnpm
  Browsers:
    Chrome: 129.0.6668.59
    Safari: 17.5
  npmPackages:
    @rspack/cli: 0.7.5 => 0.7.5 
    @rspack/core: 0.7.5 => 0.7.5 
    @rspack/plugin-react-refresh: 0.7.5 => 0.7.5 

Details

修改纯类型的文件时,rspack 没有编译,导致 tsconfig-paths-webpack-plugin 插件反应。

Reproduce link

No response

Reproduce Steps

下面是可复现的demo: rspack-project.zip

复现步骤

  1. 启动项目 npm run dev
  2. 启动完项目后修改 src/type.ts , 将 "Android" 删除, 保存文件

预期

终端中显示错误

ERROR in ./src/App.tsx:9:51
TS2345: Argument of type '"Android"' is not assignable to parameter of type 'AppType | (() => AppType)'.
     7 |   const [count, setCount] = useState(0);
     8 |
  >  9 |   const [appType, setAppType] = useState<AppType>("Android");
       |                                                   ^^^^^^^^^
    10 |
    11 |   console.log(appType, setAppType);
    12 |

Found 1 error in NaN ms.

实际

没有任何反应。

其他说明

在 0.5.6 版本中,不管是纯类型的 .d.ts 还是 .ts 文件,在保存文件后,都会触发类型检查。升级到 0.7.5 之后,纯类型文件,只会在首次启动时才参与类型检查,后面再修改,仿佛就没有修改似得。

另外我们从 webpack 迁移到 rspack ,项目架构没有变化,webpack 和 [email protected] 都是可以在文件修改后,触发类型检查。

期望,尽快修复这个问题,我们项目中每个组件都可能存在一个 type.ts(会被其他文件引用) 这样的纯类型文件和全局的 .d.ts(不会被其他文件引用,全局的) 文件。目前类型检查不工作,毕竟影响工作效率。

辛苦研发大哥们,早点修复。☕️

noshower avatar Sep 24 '24 06:09 noshower

这个问题,在 1.0.x 中也是存在的

noshower avatar Sep 24 '24 06:09 noshower

嘿,这个问题什么时候安排修复

noshower avatar Oct 21 '24 03:10 noshower

You can try using this plugin code as a temporary fix for the issue.

class Plugin {
  apply(compiler) {
    compiler.hooks.afterCompile.tapPromise('delay', async () => {
      await new Promise((r) => {
        setTimeout(r, 10);
      });
    });
  }
}

inottn avatar Oct 21 '24 05:10 inottn

从提供的 demo 上看,因为 builtin:swc-loader 在转换代码时将 type.ts 的 import 给移除了导致 type.ts 没有被监听。目前可以直接把 type.ts 加入到 file_dependencies 中来解决。


From the provided demo, it seems that type.ts is not monitored because builtin:swc-loader removes the import of type.ts when converting the code. Currently, you can directly add type.ts to file_dependencies to solve this problem.

class MyPlugin {
    apply(compiler) {
        compiler.hooks.compilation.tap("MyPlugin", (compilation) => {
            compilation.fileDependencies.add(path.resolve(__dirname, "./src/type.ts"))
        })
    }
}

jerrykingxyz avatar Oct 22 '24 09:10 jerrykingxyz

Maybe we can add a swc plugin and a flag like https://rspack.dev/guide/features/builtin-swc-loader#rspackexperimentsimport to collect the import type before swc loader makes transformation and then add them to file dependencies. But there could also be problems such as path alias resolution.

CPunisher avatar Nov 10 '24 14:11 CPunisher

Maybe we can add a swc plugin and a flag like rspack.dev/guide/features/builtin-swc-loader#rspackexperimentsimport to collect the import type before swc loader makes transformation and then add them to file dependencies. But there could also be problems such as path alias resolution.

@CPunisher maybe swc can provide option like preserveValueImports to keep the import statement of type import, so the bundler can still track it

import type a from './lib';
transformed to 
import './lib';

hardfist avatar Nov 10 '24 14:11 hardfist

Maybe we can add a swc plugin and a flag like rspack.dev/guide/features/builtin-swc-loader#rspackexperimentsimport to collect the import type before swc loader makes transformation and then add them to file dependencies. But there could also be problems such as path alias resolution.

@CPunisher maybe swc can provide option like preserveValueImports to keep the import statement of type import, so the bundler can still track it

import type a from './lib'; transformed to import './lib';

But this transformation is not equivalent when ./lib introduces side effects. This is a corner case that the user can't import only types without introducing the side effects, which violates the initial aim of import type.

CPunisher avatar Nov 11 '24 05:11 CPunisher

This issue has been automatically marked as stale because it has not had recent activity. If this issue is still affecting you, please leave any comment (for example, "bump"). We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!

stale[bot] avatar Jan 10 '25 06:01 stale[bot]

Just tried v1.4.4 and found this is already fixed, the fix is released in v1.1.5 after bisect, but I'm not sure which commit fixed this.

So the bug reason is actually the type.ts is not successfully added to compilation.fileDependencies by ForkTsCheckerWebpackPlugin, it's not related to how swc-loader transform the ts code at all (preserve the type import or not), and not preserve the type import is also reasonable, types should not affect compilation, unless it has side effects, and swc has verbatimModuleSyntax for this case, and rspack builtin:swc-loader also support it

ahabhgk avatar Jul 07 '25 08:07 ahabhgk