Skip to content
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

Ensure AutoImportProviderProject can share source files with main program #44274

Merged
Show file tree
Hide file tree
Changes from 2 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
18 changes: 7 additions & 11 deletions src/harness/fourslashImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,13 @@ namespace FourSlash {
nameToEntries.set(entry.name, [entry]);
}
else {
if (entries.some(e => e.source === entry.source && this.deepEqual(e.data, entry.data))) {
if (entries.some(e =>
e.source === entry.source &&
e.data?.exportName === entry.data?.exportName &&
e.data?.fileName === entry.data?.fileName &&
e.data?.moduleSpecifier === entry.data?.moduleSpecifier &&
e.data?.ambientModuleName === entry.data?.ambientModuleName
)) {
this.raiseError(`Duplicate completions for ${entry.name}`);
}
entries.push(entry);
Expand Down Expand Up @@ -1280,16 +1286,6 @@ namespace FourSlash {

}

private deepEqual(a: unknown, b: unknown) {
try {
this.assertObjectsEqual(a, b);
return true;
}
catch {
return false;
}
}

public verifyDisplayPartsOfReferencedSymbol(expected: ts.SymbolDisplayPart[]) {
const referencedSymbols = this.findReferencesAtCaret()!;

Expand Down
14 changes: 8 additions & 6 deletions src/server/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1928,6 +1928,13 @@ namespace ts.server {
}
}

/*@internal*/
static readonly compilerOptionsOverrides = {
andrewbranch marked this conversation as resolved.
Show resolved Hide resolved
diagnostics: false,
skipLibCheck: true,
sourceMap: false,
};

/*@internal*/
static create(dependencySelection: PackageJsonAutoImportPreference, hostProject: Project, moduleResolutionHost: ModuleResolutionHost, documentRegistry: DocumentRegistry): AutoImportProviderProject | undefined {
if (dependencySelection === PackageJsonAutoImportPreference.Off) {
Expand All @@ -1936,12 +1943,7 @@ namespace ts.server {

const compilerOptions: CompilerOptions = {
...hostProject.getCompilerOptions(),
noLib: true,
diagnostics: false,
skipLibCheck: true,
types: ts.emptyArray,
lib: ts.emptyArray,
sourceMap: false,
...this.compilerOptionsOverrides,
};

const rootNames = this.getRootFileNames(dependencySelection, hostProject, moduleResolutionHost, compilerOptions);
Expand Down
9 changes: 9 additions & 0 deletions src/testRunner/unittests/tsserver/autoImportProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,15 @@ namespace ts.projectSystem {
assert.isDefined(projectService.configuredProjects.get("/packages/a/tsconfig.json")!.getPackageJsonAutoImportProvider());
assert.isDefined(projectService.configuredProjects.get("/packages/a/tsconfig.json")!.getPackageJsonAutoImportProvider());
});

it("Can use the same document registry bucket key as main program", () => {
for (const option of sourceFileAffectingCompilerOptions) {
assert(
!hasProperty(server.AutoImportProviderProject.compilerOptionsOverrides, option.name),
`'${option.name}' may cause AutoImportProviderProject not to share source files with main program`
);
}
});
});

function setup(files: File[]) {
Expand Down
36 changes: 36 additions & 0 deletions tests/cases/fourslash/server/autoImportProvider6.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/// <reference path="../fourslash.ts" />

// @Filename: /tsconfig.json
//// { "compilerOptions": { "module": "commonjs", "lib": ["es2019"] } }

// @Filename: /package.json
//// { "dependencies": { "antd": "*", "react": "*" } }

// @Filename: /node_modules/@types/react/index.d.ts
//// export declare function Component(): void;

// @Filename: /node_modules/antd/index.d.ts
//// import "react";

// @Filename: /index.ts
//// Component/**/

// react/index.d.ts will be in both the auto import provider program
// and the main program, and will result in duplicate completions
// unless the two programs successfully share the same source file.
// This tests the configuration of the AutoImportProviderProject.
// See also the 'Can use the same document registry bucket key as main program'
// unit test in autoImportProvider.ts.
goTo.marker("");
verify.completions({
marker: "",
includes: [{
name: "Component",
hasAction: true,
source: "/node_modules/@types/react/index",
sortText: completion.SortText.AutoImportSuggestions,
}],
preferences: {
includeCompletionsForModuleExports: true,
},
});