Skip to content

Fixed crashes when moving namespace imports to other files in refactorings #60302

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/services/codefixes/importFixes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ import {
insertImports,
InternalSymbolName,
isExternalModuleReference,
isExternalModuleSymbol,
isFullSourceFile,
isIdentifier,
isImportable,
Expand Down Expand Up @@ -276,7 +277,7 @@ function createImportAdderWorker(sourceFile: SourceFile | FutureSourceFile, prog
}

function addImportFromExportedSymbol(exportedSymbol: Symbol, isValidTypeOnlyUseSite?: boolean, referenceImport?: ImportOrRequireAliasDeclaration) {
const moduleSymbol = Debug.checkDefined(exportedSymbol.parent);
const moduleSymbol = Debug.checkDefined(isExternalModuleSymbol(exportedSymbol) ? exportedSymbol : exportedSymbol.parent);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the crash is happening here today because the external module symbol doesn't have a .parent

const symbolName = getNameForExportedSymbol(exportedSymbol, getEmitScriptTarget(compilerOptions));
const checker = program.getTypeChecker();
const symbol = checker.getMergedSymbol(skipAlias(exportedSymbol, checker));
Expand Down Expand Up @@ -1447,6 +1448,8 @@ export function getImportKind(importingFile: SourceFile | FutureSourceFile, expo
return ImportKind.Named;
case ExportKind.Default:
return ImportKind.Default;
case ExportKind.Module:
return ImportKind.Namespace;
case ExportKind.ExportEquals:
return getExportEqualsImportKind(importingFile, program.getCompilerOptions(), !!forceImportKeyword);
case ExportKind.UMD:
Expand Down
13 changes: 13 additions & 0 deletions src/services/exportInfoMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export const enum ExportKind {
Named,
Default,
ExportEquals,
Module,
UMD,
}

Expand Down Expand Up @@ -557,6 +558,18 @@ export function getExportInfoMap(importingFile: SourceFile | FutureSourceFile, h
if (++moduleCount % 100 === 0) cancellationToken?.throwIfCancellationRequested();
const seenExports = new Set<__String>();
const checker = program.getTypeChecker();
if (isImportableSymbol(moduleSymbol, checker)) {
cache.add(
importingFile.path,
moduleSymbol,
moduleSymbol.escapedName,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect this might be somewhat questionable solution here, I'm not fully sold on it myself either. cc @andrewbranch - you might have some opinions here (or suggestions for an improved fix? :P)

moduleSymbol,
moduleFile,
ExportKind.Module,
isFromPackageJson,
checker,
);
}
const defaultInfo = getDefaultLikeExportInfo(moduleSymbol, checker);
// Note: I think we shouldn't actually see resolved module symbols here, but weird merges
// can cause it to happen: see 'completionsImport_mergedReExport.ts'
Expand Down
5 changes: 4 additions & 1 deletion src/testRunner/unittests/tsserver/autoImportProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,9 +323,12 @@ describe("unittests:: tsserver:: autoImportProvider::", () => {
assert.lengthOf(info, 1);
seenSymbolNames.add(symbolName);
});
assert.equal(seenSymbolNames.size, 2);
assert.equal(seenSymbolNames.size, 5);
assert.ok(seenSymbolNames.has("Stats"));
assert.ok(seenSymbolNames.has("Volume"));
assert.ok(seenSymbolNames.has('"/user/username/projects/project/index"'));
assert.ok(seenSymbolNames.has('"/user/username/projects/project/node_modules/@types/node/index"'));
assert.ok(seenSymbolNames.has('"/user/username/projects/project/node_modules/memfs/lib/index"'));
baselineTsserverLogs("autoImportProvider", "Shared source files between AutoImportProvider and main program", session);
});
});
Expand Down
26 changes: 26 additions & 0 deletions tests/cases/fourslash/moveToFile_namespaceImport1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/// <reference path='fourslash.ts' />

// @Filename: /a.ts
//// export type ExecutionPoint = string;
////
// @Filename: /b.ts
//// import * as A from "./a";
////
//// [|async function pointToFrameExecutionPoint(point: A.ExecutionPoint) {}|]
////
// @Filename: /point.ts
////

verify.moveToFile({
newFileContents: {
"/b.ts": `
`,
"/point.ts":
`import * as A from "./a";


async function pointToFrameExecutionPoint(point: A.ExecutionPoint) { }
`,
},
interactiveRefactorArguments: { targetFile: "/point.ts" },
});
26 changes: 26 additions & 0 deletions tests/cases/fourslash/moveToFile_namespaceImport2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/// <reference path='fourslash.ts' />

// @Filename: /a.ts
//// export type ExecutionPoint = string;
////
// @Filename: /b.ts
//// import * as P from "./a";
////
//// [|async function fn(point: typeof P) {}|]
////
// @Filename: /c.ts
////

verify.moveToFile({
newFileContents: {
"/b.ts": `
`,
"/c.ts":
`import * as P from "./a";


async function fn(point: typeof P) { }
`,
},
interactiveRefactorArguments: { targetFile: "/c.ts" },
});
30 changes: 30 additions & 0 deletions tests/cases/fourslash/moveToFile_namespaceImport3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/// <reference path='fourslash.ts' />

// @Filename: /a.ts
//// export type ExecutionPoint = string;
////
// @Filename: /b.ts
//// import * as A from "./a";
////
//// [|async function fn1(point: A.ExecutionPoint) {}|]
////
//// async function fn2(point: A.ExecutionPoint) {}
////
// @Filename: /point.ts
////

verify.moveToFile({
newFileContents: {
"/b.ts": `import * as A from "./a";

async function fn2(point: A.ExecutionPoint) {}
`,
"/point.ts":
`import * as A from "./a";


async function fn1(point: A.ExecutionPoint) { }
`,
},
interactiveRefactorArguments: { targetFile: "/point.ts" },
});
29 changes: 29 additions & 0 deletions tests/cases/fourslash/moveToFile_namespaceImport4.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/// <reference path='fourslash.ts' />

// @Filename: /a.ts
//// export type ExecutionPoint = string;
////
// @Filename: /b.ts
//// import * as A from "./a";
////
//// [|async function fn1(point: A.ExecutionPoint) {}|]
////
// @Filename: /point.ts
//// import { ExecutionPoint } from "./a";
////
//// async function fn2(point: ExecutionPoint) {}

verify.moveToFile({
newFileContents: {
"/b.ts": `
`,
"/point.ts":
`import * as A from "./a";
import { ExecutionPoint } from "./a";

async function fn2(point: ExecutionPoint) {}
async function fn1(point: A.ExecutionPoint) { }
`,
},
interactiveRefactorArguments: { targetFile: "/point.ts" },
});
32 changes: 32 additions & 0 deletions tests/cases/fourslash/moveToFile_namespaceImport5.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/// <reference path='fourslash.ts' />

// @Filename: /a.ts
//// export type ExecutionPoint = string;
////
// @Filename: /b.ts
//// export type ExecutionPoint = { point: string };
////
// @Filename: /c.ts
//// import * as A from "./a";
////
//// [|async function fn1(point: A.ExecutionPoint) {}|]
////
// @Filename: /point.ts
//// import * as A from "./c";
////
//// async function fn2(point: A.ExecutionPoint) {}

verify.moveToFile({
newFileContents: {
"/c.ts": `
`,
"/point.ts":
`import * as A from "./a";
import * as A from "./c";
Comment on lines +24 to +25
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we can see here, those refactorings introduce issues into target files. This is not a new problem from what I can tell. This refactoring just never tried to rename inserted symbols to avoid name clashes etc.

At first, I thought those would be test cases that should be covered by this PR so I added them. I could just remove them now but I think they still have some value here, especially if this naming conflict is ever meant to be addressed in the future.


async function fn2(point: A.ExecutionPoint) {}
async function fn1(point: A.ExecutionPoint) { }
`,
},
interactiveRefactorArguments: { targetFile: "/point.ts" },
});
29 changes: 29 additions & 0 deletions tests/cases/fourslash/moveToFile_namespaceImport6.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/// <reference path='fourslash.ts' />

// @Filename: /a.ts
//// export type ExecutionPoint = string;
////
// @Filename: /b.ts
//// import * as A from "./a";
////
//// [|async function fn1(point: A.ExecutionPoint) {}|]
////
// @Filename: /point.ts
//// import * as A from "./a";
////
//// async function fn2(point: A.ExecutionPoint) {}

verify.moveToFile({
newFileContents: {
"/b.ts": `
`,
"/point.ts":
`import * as A from "./a";
import * as A from "./a";

async function fn2(point: A.ExecutionPoint) {}
async function fn1(point: A.ExecutionPoint) { }
`,
},
interactiveRefactorArguments: { targetFile: "/point.ts" },
});
29 changes: 29 additions & 0 deletions tests/cases/fourslash/moveToFile_namespaceImport7.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/// <reference path='fourslash.ts' />

// @Filename: /a.ts
//// export type ExecutionPoint = string;
////
// @Filename: /b.ts
//// import * as A from "./a";
////
//// [|async function fn1(point: A.ExecutionPoint) {}|]
////
// @Filename: /point.ts
//// import * as A1 from "./a";
////
//// async function fn2(point: A1.ExecutionPoint) {}

verify.moveToFile({
newFileContents: {
"/b.ts": `
`,
"/point.ts":
`import * as A from "./a";
import * as A1 from "./a";

async function fn2(point: A1.ExecutionPoint) {}
async function fn1(point: A.ExecutionPoint) { }
`,
},
interactiveRefactorArguments: { targetFile: "/point.ts" },
});
28 changes: 28 additions & 0 deletions tests/cases/fourslash/moveToFile_namespaceImport8.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/// <reference path='fourslash.ts' />

// @Filename: /a.ts
//// export type ExecutionPoint = string;
////
// @Filename: /b.ts
//// import * as A from "./a";
////
//// [|async function fn1(point: A.ExecutionPoint) {}|]
////
// @Filename: /point.ts
//// type A = {};
//// export {};

verify.moveToFile({
newFileContents: {
"/b.ts": `
`,
"/point.ts":
`import * as A from "./a";

type A = {};
export {};
async function fn1(point: A.ExecutionPoint) { }
`,
},
interactiveRefactorArguments: { targetFile: "/point.ts" },
});
23 changes: 23 additions & 0 deletions tests/cases/fourslash/moveToNewFile_namespaceImport2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/// <reference path='fourslash.ts' />

// @Filename: /a.ts
//// export type ExecutionPoint = string;
////
// @Filename: /b.ts
//// import * as A from "./a";
////
//// [|async function pointToFrameExecutionPoint(point: A.ExecutionPoint) {}|]
////

verify.moveToNewFile({
newFileContents: {
"/b.ts": `
`,
"/pointToFrameExecutionPoint.ts":
`import * as A from "./a";


async function pointToFrameExecutionPoint(point: A.ExecutionPoint) { }
`,
},
});
24 changes: 24 additions & 0 deletions tests/cases/fourslash/moveToNewFile_namespaceImport3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/// <reference path='fourslash.ts' />

// @Filename: /a.ts
//// export type ExecutionPoint = string;
////
// @Filename: /b.ts
//// import * as A from "./a";
////
//// [|async function fn(a: typeof A) {}|]
////

verify.moveToNewFile({
newFileContents: {
"/b.ts":
`
`,
"/fn.ts":
`import * as A from "./a";


async function fn(a: typeof A) { }
`,
},
});