Skip to content

Commit e2e9e6c

Browse files
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 <[email protected]>
1 parent ff31487 commit e2e9e6c

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

packages/vite/plugins/nx-tsconfig-paths.plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ There should at least be a tsconfig.base.json or tsconfig.json in the root of th
171171
resolvedFile = matchTsPathFallback?.(importPath);
172172
}
173173

174-
if (!resolvedFile) {
174+
if (!resolvedFile || !existsSync(resolvedFile)) {
175175
if (tsConfigPathsEsm || tsConfigPathsFallback) {
176176
logIt(
177177
`Unable to resolve ${importPath} with tsconfig paths. Using fallback file matching.`

0 commit comments

Comments
 (0)