fix(vite): fall back to file matching when resolved file does not exist in nx-vite-ts-paths plugin (#29472)

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.
* 
<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->
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
<!-- This is the behavior we should expect with the changes in this PR
-->
What is expected is that it resolves to (notice the file extension)
`absolutePath.../projects/libs/global/src/utils/mergeClassNames.ts`

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

---------

Co-authored-by: Leosvel Pérez Espinosa <leosvel.perez.espinosa@gmail.com>
This commit is contained in:
Esteban 2025-01-29 06:01:50 -05:00 committed by GitHub
parent ff31487cf6
commit e2e9e6cb36
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -171,7 +171,7 @@ There should at least be a tsconfig.base.json or tsconfig.json in the root of th
resolvedFile = matchTsPathFallback?.(importPath); resolvedFile = matchTsPathFallback?.(importPath);
} }
if (!resolvedFile) { if (!resolvedFile || !existsSync(resolvedFile)) {
if (tsConfigPathsEsm || tsConfigPathsFallback) { if (tsConfigPathsEsm || tsConfigPathsFallback) {
logIt( logIt(
`Unable to resolve ${importPath} with tsconfig paths. Using fallback file matching.` `Unable to resolve ${importPath} with tsconfig paths. Using fallback file matching.`