Skip to content

fix(swc): correct inverted ignored filter in watchFilesInSrcDir#3324

Open
yogeshwaran-c wants to merge 1 commit intonestjs:masterfrom
yogeshwaran-c:fix/swc-watch-src-ignored-filter
Open

fix(swc): correct inverted ignored filter in watchFilesInSrcDir#3324
yogeshwaran-c wants to merge 1 commit intonestjs:masterfrom
yogeshwaran-c:fix/swc-watch-src-ignored-filter

Conversation

@yogeshwaran-c
Copy link
Copy Markdown

What kind of change does this PR introduce?

Bug fix

What is the current behavior?

The watchFilesInSrcDir method in the SWC compiler has an inverted ignored filter in its chokidar watcher. The current logic ignores files whose extensions match the expected source extensions (e.g., .ts, .js), which is the opposite of the intended behavior. It should ignore files that do not match.

Additionally, there is an extension format inconsistency: swc-defaults.ts provides extensions with dot prefixes (['.js', '.ts']), but the ignored callback strips the dot via path.extname(file).slice(1), causing .ts to never match '.ts'. This mismatch accidentally prevents the inverted filter from causing harm with the default config, but the fallback default ['ts'] (without dots) would incorrectly ignore .ts files.

For comparison, the sibling method watchFilesInOutDir correctly uses ! negation in its ignored filter.

What is the new behavior?

  • The ignored callback now uses !extensions.includes(...) to ignore files that do not match the expected extensions (consistent with watchFilesInOutDir)
  • Extensions are normalized to always use dot-prefixed format (e.g., 'ts''.ts')
  • path.extname() is used directly without .slice(1), so comparisons are consistent

This ensures that in SWC watch mode, newly added source files (.ts, .js) are properly detected and compiled, while non-source files (.json, .md, etc.) are correctly ignored.

Additional context

Added 5 unit tests for watchFilesInSrcDir covering:

  • Extensions with dot prefix (.ts, .js)
  • Extensions without dot prefix normalization (ts, js)
  • Missing source directory handling
  • Empty filenames handling
  • Default extension fallback behavior

All existing tests continue to pass (20/20).

The chokidar ignored callback in watchFilesInSrcDir was incorrectly
filtering out files that matched the expected extensions (e.g., .ts)
instead of filtering out files that did not match. This meant that
newly added source files in watch mode could be missed by the watcher.

Additionally, the extension comparison was inconsistent: swc-defaults
provides extensions with dot prefixes (['.js', '.ts']) while the code
stripped the dot via path.extname().slice(1), causing a format mismatch.

This fix:
- Inverts the ignored logic (adds !) to match watchFilesInOutDir pattern
- Normalizes extensions to always use dot-prefixed format
- Uses path.extname() directly without stripping the dot
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

Successfully merging this pull request may close these issues.

1 participant