From 55e10b428a3d2f1b102b6878594d038b6632ecbf Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Wed, 26 May 2021 14:11:53 -0700 Subject: [PATCH 1/3] Ensure AutoImportProviderProject can share source files with main program --- package-lock.json | 6 ++-- src/harness/fourslashImpl.ts | 18 ++++------ src/server/project.ts | 14 ++++---- .../unittests/tsserver/autoImportProvider.ts | 9 +++++ .../fourslash/server/autoImportProvider6.ts | 36 +++++++++++++++++++ 5 files changed, 63 insertions(+), 20 deletions(-) create mode 100644 tests/cases/fourslash/server/autoImportProvider6.ts diff --git a/package-lock.json b/package-lock.json index 86e72bc43a3e5..0c8081bcf47b2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7957,9 +7957,9 @@ "dev": true }, "uglify-js": { - "version": "3.13.7", - "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.13.7.tgz", - "integrity": "sha512-1Psi2MmnZJbnEsgJJIlfnd7tFlJfitusmR7zDI8lXlFI0ACD4/Rm/xdrU8bh6zF0i74aiVoBtkRiFulkrmh3AA==", + "version": "3.13.8", + "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.13.8.tgz", + "integrity": "sha512-PvFLMFIQHfIjFFlvAch69U2IvIxK9TNzNWt1SxZGp9JZ/v70yvqIQuiJeVPPtUMOzoNt+aNRDk4wgxb34wvEqA==", "dev": true, "optional": true }, diff --git a/src/harness/fourslashImpl.ts b/src/harness/fourslashImpl.ts index c0b7053f93b73..592fe8817541a 100644 --- a/src/harness/fourslashImpl.ts +++ b/src/harness/fourslashImpl.ts @@ -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); @@ -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()!; diff --git a/src/server/project.ts b/src/server/project.ts index ef48ebf530bc2..069a6e2f5fe68 100644 --- a/src/server/project.ts +++ b/src/server/project.ts @@ -1928,6 +1928,13 @@ namespace ts.server { } } + /*@internal*/ + static readonly compilerOptionsOverrides = { + diagnostics: false, + skipLibCheck: true, + sourceMap: false, + }; + /*@internal*/ static create(dependencySelection: PackageJsonAutoImportPreference, hostProject: Project, moduleResolutionHost: ModuleResolutionHost, documentRegistry: DocumentRegistry): AutoImportProviderProject | undefined { if (dependencySelection === PackageJsonAutoImportPreference.Off) { @@ -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); diff --git a/src/testRunner/unittests/tsserver/autoImportProvider.ts b/src/testRunner/unittests/tsserver/autoImportProvider.ts index 36d17d59d06a2..9d1978849295b 100644 --- a/src/testRunner/unittests/tsserver/autoImportProvider.ts +++ b/src/testRunner/unittests/tsserver/autoImportProvider.ts @@ -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[]) { diff --git a/tests/cases/fourslash/server/autoImportProvider6.ts b/tests/cases/fourslash/server/autoImportProvider6.ts new file mode 100644 index 0000000000000..b06d1557e45cd --- /dev/null +++ b/tests/cases/fourslash/server/autoImportProvider6.ts @@ -0,0 +1,36 @@ +/// + +// @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, + }, +}); From f9c02d661736db498be0117d474f86b837aa9f32 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Wed, 26 May 2021 14:59:06 -0700 Subject: [PATCH 2/3] Revert package-lock change --- package-lock.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 0c8081bcf47b2..86e72bc43a3e5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7957,9 +7957,9 @@ "dev": true }, "uglify-js": { - "version": "3.13.8", - "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.13.8.tgz", - "integrity": "sha512-PvFLMFIQHfIjFFlvAch69U2IvIxK9TNzNWt1SxZGp9JZ/v70yvqIQuiJeVPPtUMOzoNt+aNRDk4wgxb34wvEqA==", + "version": "3.13.7", + "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.13.7.tgz", + "integrity": "sha512-1Psi2MmnZJbnEsgJJIlfnd7tFlJfitusmR7zDI8lXlFI0ACD4/Rm/xdrU8bh6zF0i74aiVoBtkRiFulkrmh3AA==", "dev": true, "optional": true }, From abb898c210386df2a76e44908ae752cdb28fe5fd Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Wed, 26 May 2021 15:54:04 -0700 Subject: [PATCH 3/3] Add back reclassified options --- src/server/project.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/server/project.ts b/src/server/project.ts index 069a6e2f5fe68..e490851c0e6e5 100644 --- a/src/server/project.ts +++ b/src/server/project.ts @@ -1929,10 +1929,13 @@ namespace ts.server { } /*@internal*/ - static readonly compilerOptionsOverrides = { + static readonly compilerOptionsOverrides: CompilerOptions = { diagnostics: false, skipLibCheck: true, sourceMap: false, + types: ts.emptyArray, + lib: ts.emptyArray, + noLib: true, }; /*@internal*/ @@ -1941,7 +1944,7 @@ namespace ts.server { return undefined; } - const compilerOptions: CompilerOptions = { + const compilerOptions = { ...hostProject.getCompilerOptions(), ...this.compilerOptionsOverrides, };