From e2e9e6cb36d363b348889e6fd2be1d967c9d1999 Mon Sep 17 00:00:00 2001 From: Esteban Date: Wed, 29 Jan 2025 06:01:50 -0500 Subject: [PATCH] fix(vite): fall back to file matching when resolved file does not exist in `nx-vite-ts-paths` plugin (#29472) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The fallback path resolution was only happening if the resolvedFile was a falsy value, so it was returning inexistent filepaths. This pull request includes a small but important change to the `packages/vite/plugins/nx-tsconfig-paths.plugin.ts` file. The change improves the reliability of resolving file paths by checking if the resolved path exists before proceeding with fallback file matching. * [`packages/vite/plugins/nx-tsconfig-paths.plugin.ts`](diffhunk://#diff-ad026b24ed45d9df484cabc7e277fc9b4d7759560af36bf357cbc4186725ae0bL174-R176): Added a check to verify if the resolved file path exists before using fallback file matching. * ## Current Behavior The resolver doesn't resolve the full file path, it ignores the file extension. For example, for a path like this: `import mergeClassNames from '@projects/global/utils/mergeClassNames'` It resolves to `absolutePath.../projects/libs/global/src/utils/mergeClassNames` When vite is building a project, it doesn't find the file and it errors. This happens because the resolved file path is not a falsy value, thus, it doesn't run the fallback file path matching. ## Expected Behavior What is expected is that it resolves to (notice the file extension) `absolutePath.../projects/libs/global/src/utils/mergeClassNames.ts` ## Related Issue(s) --------- Co-authored-by: Leosvel Pérez Espinosa --- packages/vite/plugins/nx-tsconfig-paths.plugin.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vite/plugins/nx-tsconfig-paths.plugin.ts b/packages/vite/plugins/nx-tsconfig-paths.plugin.ts index 5bb5e7ea84..5c21bae0ca 100644 --- a/packages/vite/plugins/nx-tsconfig-paths.plugin.ts +++ b/packages/vite/plugins/nx-tsconfig-paths.plugin.ts @@ -171,7 +171,7 @@ There should at least be a tsconfig.base.json or tsconfig.json in the root of th resolvedFile = matchTsPathFallback?.(importPath); } - if (!resolvedFile) { + if (!resolvedFile || !existsSync(resolvedFile)) { if (tsConfigPathsEsm || tsConfigPathsFallback) { logIt( `Unable to resolve ${importPath} with tsconfig paths. Using fallback file matching.`