Skip to content

Commit af88417

Browse files
authored
fix: use referenced project's compiler option to get resolution mode (#2676)
#2673 Fixing the actual problem that makes the OP want this option. The problem is that the referenced project is a common js project which allows extensionless import. But when we used the wrong compiler option to check for the resolution mode. TypeScript thus used the ESM strategy to resolve the import.
1 parent 9ee7e9c commit af88417

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

packages/language-server/src/plugins/typescript/module-loader.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,14 @@ export function createSvelteModuleLoader(
241241
redirectedReference: ts.ResolvedProjectReference | undefined,
242242
option: ts.CompilerOptions
243243
): ts.ResolvedModuleWithFailedLookupLocations {
244-
const mode = impliedNodeFormatResolver.resolve(name, index, containingSourceFile, option);
244+
const mode = impliedNodeFormatResolver.resolve(
245+
name,
246+
index,
247+
containingSourceFile,
248+
// use the same compiler options as resolveModuleName
249+
// otherwise it might not find the module because of inconsistent module resolution strategy
250+
redirectedReference?.commandLine.options ?? option
251+
);
245252
const resolvedModuleWithFailedLookup = tsModule.resolveModuleName(
246253
name,
247254
containingFile,

packages/language-server/test/plugins/typescript/service.test.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,54 @@ describe('service', () => {
599599
assert.deepStrictEqual(getSemanticDiagnosticsMessages(ls, importing), []);
600600
});
601601

602+
it('resolve module with source project reference redirect having different module resolution', async () => {
603+
const dirPath = getRandomVirtualDirPath(testDir);
604+
const { virtualSystem, lsDocumentContext, rootUris } = setup();
605+
606+
const package1 = path.join(dirPath, 'package1');
607+
608+
virtualSystem.writeFile(
609+
path.join(package1, 'tsconfig.json'),
610+
JSON.stringify({
611+
references: [{ path: '../package2' }],
612+
files: ['index.ts'],
613+
compilerOptions: {
614+
moduleResolution: 'Bundler',
615+
module: 'ESNext',
616+
paths: {
617+
package2: ['../package2']
618+
}
619+
}
620+
})
621+
);
622+
623+
const package2 = path.join(dirPath, 'package2');
624+
virtualSystem.writeFile(
625+
path.join(package2, 'tsconfig.json'),
626+
JSON.stringify({
627+
compilerOptions: {
628+
composite: true,
629+
strict: true,
630+
moduleResolution: 'NodeNext'
631+
},
632+
files: ['index.ts']
633+
})
634+
);
635+
636+
const importing = path.join(package1, 'index.ts');
637+
virtualSystem.writeFile(importing, 'import { hi } from "package2"; hi((a) => `${a}`);');
638+
639+
const reExport = path.join(package2, 'index.ts');
640+
virtualSystem.writeFile(reExport, 'export * from "./foo"');
641+
642+
const exportFile = path.join(package2, 'foo.ts');
643+
virtualSystem.writeFile(exportFile, 'export function hi(cb: (num: number) => string) {}');
644+
645+
const ls = await getService(importing, rootUris, lsDocumentContext);
646+
647+
assert.deepStrictEqual(getSemanticDiagnosticsMessages(ls, importing), []);
648+
});
649+
602650
it('skip directory watching if directory is root', async () => {
603651
const dirPath = getRandomVirtualDirPath(path.join(testDir, 'Test'));
604652
const { virtualSystem, lsDocumentContext } = setup();

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy