Skip to content

Commit c9b14a2

Browse files
mattlewis92FrozenPandaz
authored andcommitted
fix(vite): tsconfig paths plugin should not partially match paths (#29501)
<!-- 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 --> When there is a path mapping like: ``` "match-lib-deep/": ["libs/lib1/src/*"], "match-lib-top-level": ["libs/lib2/src/*"], "match-lib/*": ["libs/lib3/src/*"], ``` Imports to `match-lib-deep` or `match-lib-top-level` will try and use the last `match-lib` path mapping as the trailing `/` is not accounted for in the `startsWith` check. ## Expected Behavior <!-- This is the behavior we should expect with the changes in this PR --> The correct path mapping is matched. ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> N/A (cherry picked from commit 6948249)
1 parent 0838095 commit c9b14a2

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

e2e/vite/src/vite.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,47 @@ describe('@nx/vite/plugin', () => {
206206

207207
expect(() => runCLI(`test ${mylib}`)).not.toThrow();
208208
});
209+
210+
it('should not partially match a path mapping', () => {
211+
const lib1 = uniq('lib1');
212+
const lib2 = uniq('lib2');
213+
const lib3 = uniq('lib3');
214+
runCLI(
215+
`generate @nx/react:library libs/${lib1} --bundler=none --unitTestRunner=vitest`
216+
);
217+
runCLI(
218+
`generate @nx/react:library libs/${lib2} --bundler=none --unitTestRunner=vitest`
219+
);
220+
runCLI(
221+
`generate @nx/react:library libs/${lib3} --bundler=none --unitTestRunner=vitest`
222+
);
223+
updateFile(`libs/${lib1}/src/foo.enum.ts`, `export const foo = 'foo';`);
224+
updateFile(`libs/${lib2}/src/bar.enum.ts`, `export const bar = 'bar';`);
225+
updateFile(`libs/${lib3}/src/bam.enum.ts`, `export const bam = 'bam';`);
226+
updateFile(
227+
`libs/${lib1}/src/foo.spec.ts`,
228+
`
229+
import { foo } from 'match-lib-deep/foo.enum';
230+
import { bar } from 'match-lib-top-level';
231+
import { bam } from 'match-lib/bam.enum';
232+
test('should work', () => {
233+
expect(foo).toBeDefined();
234+
expect(bar).toBeDefined();
235+
expect(bam).toBeDefined();
236+
});
237+
`
238+
);
239+
updateJson('tsconfig.base.json', (json) => {
240+
json.compilerOptions.paths['match-lib-deep/*'] = [`libs/${lib1}/src/*`];
241+
json.compilerOptions.paths['match-lib-top-level'] = [
242+
`libs/${lib2}/src/bar.enum.ts`,
243+
];
244+
json.compilerOptions.paths['match-lib/*'] = [`libs/${lib3}/src/*`];
245+
return json;
246+
});
247+
248+
expect(() => runCLI(`test ${lib1}`)).not.toThrow();
249+
});
209250
});
210251

211252
describe('react with vitest only', () => {

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,10 @@ There should at least be a tsconfig.base.json or tsconfig.json in the root of th
237237

238238
const normalizedImport = alias.replace(/\/\*$/, '');
239239

240-
if (importPath.startsWith(normalizedImport)) {
240+
if (
241+
importPath === normalizedImport ||
242+
importPath.startsWith(normalizedImport + '/')
243+
) {
241244
const joinedPath = joinPathFragments(
242245
tsconfig.absoluteBaseUrl,
243246
paths[0].replace(/\/\*$/, '')

0 commit comments

Comments
 (0)