Skip to content

Update symlink cache from AutoImportProvider resolution even if host project already contains the file via its realpath #46830

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

Merged
Merged
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
6 changes: 3 additions & 3 deletions src/server/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1944,15 +1944,15 @@ namespace ts.server {
for (const resolution of resolutions) {
if (!resolution.resolvedFileName) continue;
const { resolvedFileName, originalPath } = resolution;
if (originalPath) {
symlinkCache.setSymlinkedDirectoryFromSymlinkedFile(originalPath, resolvedFileName);
}
if (!program.getSourceFile(resolvedFileName) && (!originalPath || !program.getSourceFile(originalPath))) {
rootNames = append(rootNames, resolvedFileName);
// Avoid creating a large project that would significantly slow down time to editor interactivity
if (dependencySelection === PackageJsonAutoImportPreference.Auto && rootNames.length > this.maxDependencies) {
return ts.emptyArray;
}
if (originalPath) {
symlinkCache.setSymlinkedDirectoryFromSymlinkedFile(originalPath, resolvedFileName);
}
}
}
}
Expand Down
68 changes: 68 additions & 0 deletions tests/cases/fourslash/server/autoImportProvider7.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/// <reference path="../fourslash.ts" />

// @Filename: /tsconfig.json
//// { "compilerOptions": { "module": "commonjs" } }

// @Filename: /package.json
//// { "dependencies": { "mylib": "file:packages/mylib" } }

// @Filename: /packages/mylib/package.json
//// { "name": "mylib", "version": "1.0.0", "main": "index.js", "types": "index" }
Copy link
Member Author

Choose a reason for hiding this comment

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

The other part of the bug is this—for this test to work, this package.json needs "types": "index" exactly—leaving it off or specifying any file extension breaks the resolution attempted by the AutoImportProviderProject. My attempt to leverage resolveTypeReferenceDirective for this has hit its limit; there probably needs to be a much more permissive module resolution function written specifically for this. I’ll follow up with a separate PR for that.


// @Filename: /packages/mylib/index.ts
//// export * from "./mySubDir";

// @Filename: /packages/mylib/mySubDir/index.ts
//// export * from "./myClass";
//// export * from "./myClass2";

// @Filename: /packages/mylib/mySubDir/myClass.ts
//// export class MyClass {}

// @Filename: /packages/mylib/mySubDir/myClass2.ts
//// export class MyClass2 {}

// @link: /packages/mylib -> /node_modules/mylib

// @Filename: /src/index.ts
////
//// const a = new MyClass/*1*/();
//// const b = new MyClass2/*2*/();

goTo.marker("1");
format.setOption("newLineCharacter", "\n");

verify.completions({
marker: "1",
includes: [{
name: "MyClass",
source: "mylib",
sourceDisplay: "mylib",
hasAction: true,
sortText: completion.SortText.AutoImportSuggestions,
}],
preferences: {
includeCompletionsForModuleExports: true,
includeInsertTextCompletions: true,
allowIncompleteCompletions: true,
}
});

verify.applyCodeActionFromCompletion("1", {
name: "MyClass",
source: "mylib",
description: `Import 'MyClass' from module "mylib"`,
data: {
exportName: "MyClass",
fileName: "/packages/mylib/index.ts",
},
preferences: {
includeCompletionsForModuleExports: true,
includeCompletionsWithInsertText: true,
allowIncompleteCompletions: true,
},
newFileContent: `import { MyClass } from "mylib";

const a = new MyClass();
const b = new MyClass2();`,
});