From 42a94f9c30e5743ad1acfb1507ea079b0fdc70a1 Mon Sep 17 00:00:00 2001 From: Heejae Chang Date: Thu, 13 Feb 2020 10:25:33 -0800 Subject: [PATCH] Squashed 'server/pyright/' changes from 9521693..04a01c7 (#99) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 04a01c7 Merge pull request #510 from microsoft/erictr/debugging f6bb03b Fixed debug builds. Recent changes broke debugging (setting breakpoints, etc.). 2aa319f 3 fourslash improvement to support custom lib/typeshed folders (#514) 50b0d32 added a hook to ImportResolver for custom importing logic. (#515) de41104 Merge pull request #511 from microsoft/erictr/clean c57fafd Fixed ordering of build script — npm install needs to happen before clean because the latter depends on a node module. 46bb90c Added "clean" command to build script. Fixed bug in build script for debug version of server. f3a4aed Revert "Added "clean" build command that deletes artifacts from previous build actions." e22c661 Added "clean" build command that deletes artifacts from previous build actions. a5e3b37 Merge pull request #509 from heejaechang/fixRootDirectoryIssue bdcd372 previously added assert was using wrong path (__dirname rather than __dirname/..) b1e0ff8 made reportMissingTypeStub diagnostics warning by default and some refactoring around code actions. (#507) git-subtree-dir: server/pyright git-subtree-split: 04a01c767f137c94a06cb2ba21b16a545555dea7 --- server/pyright/client/src/extension.ts | 2 +- .../server/src/analyzer/importResolver.ts | 2 +- .../server/src/analyzer/pythonPathUtils.ts | 46 ++++++++++++------- server/pyright/server/src/analyzer/service.ts | 2 +- .../pyright/server/src/common/pathConsts.ts | 11 +++++ .../pyright/server/src/languageServerBase.ts | 4 +- server/pyright/server/src/server.ts | 16 +------ .../server/src/tests/fourSlashRunner.test.ts | 1 + .../server/src/tests/harness/vfs/factory.ts | 10 ++-- 9 files changed, 53 insertions(+), 41 deletions(-) create mode 100644 server/pyright/server/src/common/pathConsts.ts diff --git a/server/pyright/client/src/extension.ts b/server/pyright/client/src/extension.ts index b52723b3c8a5..d83c2321b95b 100644 --- a/server/pyright/client/src/extension.ts +++ b/server/pyright/client/src/extension.ts @@ -20,7 +20,7 @@ import { Commands } from '../../server/src/commands/commands'; export function activate(context: ExtensionContext) { const bundlePath = context.asAbsolutePath(path.join('server', 'server.bundle.js')); - const nonBundlePath = context.asAbsolutePath(path.join('server', 'server.js')); + const nonBundlePath = context.asAbsolutePath(path.join('server', 'src', 'server.js')); const debugOptions = { execArgv: ["--nolazy", "--inspect=6600"] }; // If the extension is launched in debug mode, then the debug server options are used. diff --git a/server/pyright/server/src/analyzer/importResolver.ts b/server/pyright/server/src/analyzer/importResolver.ts index 22bc1e1d2135..6cac9bece9de 100644 --- a/server/pyright/server/src/analyzer/importResolver.ts +++ b/server/pyright/server/src/analyzer/importResolver.ts @@ -518,7 +518,7 @@ export class ImportResolver { // If typeshed directory wasn't found in other locations, use the fallback. if (!typeshedPath) { - typeshedPath = PythonPathUtils.getTypeShedFallbackPath(this.fileSystem.getModulePath()) || ''; + typeshedPath = PythonPathUtils.getTypeShedFallbackPath(this.fileSystem) || ''; } typeshedPath = PythonPathUtils.getTypeshedSubdirectory(typeshedPath, isStdLib); diff --git a/server/pyright/server/src/analyzer/pythonPathUtils.ts b/server/pyright/server/src/analyzer/pythonPathUtils.ts index d72492545dc4..e29eaa7ed791 100644 --- a/server/pyright/server/src/analyzer/pythonPathUtils.ts +++ b/server/pyright/server/src/analyzer/pythonPathUtils.ts @@ -8,22 +8,34 @@ */ import * as child_process from 'child_process'; + import { ConfigOptions } from '../common/configOptions'; -import * as consts from '../common/consts'; -import { - combinePaths, ensureTrailingDirectorySeparator, getDirectoryPath, - getFileSystemEntries, isDirectory, normalizePath -} from '../common/pathUtils'; +import * as pathConsts from '../common/pathConsts'; +import { combinePaths, ensureTrailingDirectorySeparator, getDirectoryPath, + getFileSystemEntries, isDirectory, normalizePath } from '../common/pathUtils'; import { VirtualFileSystem } from '../common/vfs'; const cachedSearchPaths = new Map(); -export function getTypeShedFallbackPath(moduleDirectory?: string) { - if (moduleDirectory) { - moduleDirectory = normalizePath(moduleDirectory); - return combinePaths(getDirectoryPath( - ensureTrailingDirectorySeparator(moduleDirectory)), - consts.TYPESHED_FALLBACK); +export function getTypeShedFallbackPath(fs: VirtualFileSystem) { + let moduleDirectory = fs.getModulePath(); + if (!moduleDirectory) { + return undefined; + } + + moduleDirectory = getDirectoryPath(ensureTrailingDirectorySeparator( + normalizePath(moduleDirectory))); + + const typeshedPath = combinePaths(moduleDirectory, pathConsts.typeshedFallback); + if (fs.existsSync(typeshedPath)) { + return typeshedPath; + } + + // In the debug version of Pyright, the code is one level + // deeper, so we need to look one level up for the typeshed fallback. + const debugTypeshedPath = combinePaths(moduleDirectory, '../' + pathConsts.typeshedFallback); + if (fs.existsSync(debugTypeshedPath)) { + return debugTypeshedPath; } return undefined; @@ -50,14 +62,14 @@ export function findPythonSearchPaths(fs: VirtualFileSystem, configOptions: Conf } if (venvPath) { - let libPath = combinePaths(venvPath, consts.LIB); + let libPath = combinePaths(venvPath, pathConsts.lib); if (fs.existsSync(libPath)) { - importFailureInfo.push(`Found path '${ libPath }'; looking for ${ consts.SITE_PACKAGES }`); + importFailureInfo.push(`Found path '${ libPath }'; looking for ${ pathConsts.sitePackages }`); } else { importFailureInfo.push(`Did not find '${ libPath }'; trying 'Lib' instead`); libPath = combinePaths(venvPath, 'Lib'); if (fs.existsSync(libPath)) { - importFailureInfo.push(`Found path '${ libPath }'; looking for ${ consts.SITE_PACKAGES }`); + importFailureInfo.push(`Found path '${ libPath }'; looking for ${ pathConsts.sitePackages }`); } else { importFailureInfo.push(`Did not find '${ libPath }'`); libPath = ''; @@ -65,7 +77,7 @@ export function findPythonSearchPaths(fs: VirtualFileSystem, configOptions: Conf } if (libPath) { - const sitePackagesPath = combinePaths(libPath, consts.SITE_PACKAGES); + const sitePackagesPath = combinePaths(libPath, pathConsts.sitePackages); if (fs.existsSync(sitePackagesPath)) { importFailureInfo.push(`Found path '${ sitePackagesPath }'`); return [sitePackagesPath]; @@ -79,7 +91,7 @@ export function findPythonSearchPaths(fs: VirtualFileSystem, configOptions: Conf for (let i = 0; i < entries.directories.length; i++) { const dirName = entries.directories[i]; if (dirName.startsWith('python')) { - const dirPath = combinePaths(libPath, dirName, consts.SITE_PACKAGES); + const dirPath = combinePaths(libPath, dirName, pathConsts.sitePackages); if (fs.existsSync(dirPath)) { importFailureInfo.push(`Found path '${ dirPath }'`); return [dirPath]; @@ -90,7 +102,7 @@ export function findPythonSearchPaths(fs: VirtualFileSystem, configOptions: Conf } } - importFailureInfo.push(`Did not find '${ consts.SITE_PACKAGES }'. Falling back on python interpreter.`); + importFailureInfo.push(`Did not find '${ pathConsts.sitePackages }'. Falling back on python interpreter.`); } // Fall back on the python interpreter. diff --git a/server/pyright/server/src/analyzer/service.ts b/server/pyright/server/src/analyzer/service.ts index c43910dcba90..49a6d47cdcfa 100644 --- a/server/pyright/server/src/analyzer/service.ts +++ b/server/pyright/server/src/analyzer/service.ts @@ -87,7 +87,7 @@ export class AnalyzerService { this._clearReanalysisTimer(); } - static createImportResolver(fs: VirtualFileSystem, options: ConfigOptions) : ImportResolver { + static createImportResolver(fs: VirtualFileSystem, options: ConfigOptions): ImportResolver { return new ImportResolver(fs, options); } diff --git a/server/pyright/server/src/common/pathConsts.ts b/server/pyright/server/src/common/pathConsts.ts new file mode 100644 index 000000000000..e1bb03eff88c --- /dev/null +++ b/server/pyright/server/src/common/pathConsts.ts @@ -0,0 +1,11 @@ +/* + * pathConsts.ts + * Copyright (c) Microsoft Corporation. + * Licensed under the MIT license. + * + * Defines path-related constants. + */ + +export const typeshedFallback = 'typeshed-fallback'; +export const lib = 'lib'; +export const sitePackages = 'site-packages'; diff --git a/server/pyright/server/src/languageServerBase.ts b/server/pyright/server/src/languageServerBase.ts index 4cd8eb17171a..158c4d501409 100644 --- a/server/pyright/server/src/languageServerBase.ts +++ b/server/pyright/server/src/languageServerBase.ts @@ -15,10 +15,10 @@ import { import { ImportResolver } from './analyzer/importResolver'; import { AnalyzerService } from './analyzer/service'; import { CommandLineOptions } from './common/commandLineOptions'; -import { ConfigOptions } from './common/configOptions'; import { Diagnostic as AnalyzerDiagnostic, DiagnosticCategory } from './common/diagnostic'; import './common/extensions'; import { combinePaths, convertPathToUri, convertUriToPath, normalizePath } from './common/pathUtils'; +import { ConfigOptions } from './common/configOptions'; import { Position } from './common/textRange'; import { createFromRealFileSystem, VirtualFileSystem } from './common/vfs'; import { CompletionItemData } from './languageService/completionProvider'; @@ -97,7 +97,7 @@ export abstract class LanguageServerBase { protected createImportResolver(fs: VirtualFileSystem, options: ConfigOptions): ImportResolver { return new ImportResolver(fs, options); } - + // Creates a service instance that's used for analyzing a // program within a workspace. createAnalyzerService(name: string): AnalyzerService { diff --git a/server/pyright/server/src/server.ts b/server/pyright/server/src/server.ts index 77eed84144f9..27616768b46a 100644 --- a/server/pyright/server/src/server.ts +++ b/server/pyright/server/src/server.ts @@ -4,13 +4,9 @@ * Implements pyright language server. */ -import * as fs from 'fs'; -import * as path from 'path'; import { isArray } from 'util'; import { CodeAction, CodeActionParams, Command, ExecuteCommandParams } from 'vscode-languageserver'; import { CommandController } from './commands/commandController'; -import * as consts from './common/consts'; -import * as debug from './common/debug'; import { convertUriToPath, getDirectoryPath, normalizeSlashes } from './common/pathUtils'; import { LanguageServerBase, ServerSettings, WorkspaceServiceInstance } from './languageServerBase'; import { CodeActionProvider } from './languageService/codeActionProvider'; @@ -19,17 +15,7 @@ class Server extends LanguageServerBase { private _controller: CommandController; constructor() { - // pyright has "typeshed-fallback" under "client" and __dirname points to "client/server" - // make sure root directory point to "client", one level up from "client/server" where we can discover - // "typeshed-fallback" folder. in release, root is "extension" instead of "client" but - // folder structure is same (extension/server). - // - // root directory will be used for 2 different purposes. - // 1. to find "typeshed-fallback" folder. - // 2. to set "cwd" to run python to find search path. - const rootDirectory = getDirectoryPath(__dirname); - debug.assert(fs.existsSync(path.join(rootDirectory, consts.TYPESHED_FALLBACK)), `Unable to locate typeshed fallback folder at '${ rootDirectory }'`); - super('Pyright', rootDirectory); + super('Pyright', getDirectoryPath(__dirname)); this._controller = new CommandController(this); } diff --git a/server/pyright/server/src/tests/fourSlashRunner.test.ts b/server/pyright/server/src/tests/fourSlashRunner.test.ts index 546347004a52..d5db3837e574 100644 --- a/server/pyright/server/src/tests/fourSlashRunner.test.ts +++ b/server/pyright/server/src/tests/fourSlashRunner.test.ts @@ -6,6 +6,7 @@ * Entry point that will read all *.fourslash.ts files and * register jest tests for them and run */ + import * as path from 'path'; import { normalizeSlashes } from '../common/pathUtils'; import { runFourSlashTest } from './harness/fourslash/runner'; diff --git a/server/pyright/server/src/tests/harness/vfs/factory.ts b/server/pyright/server/src/tests/harness/vfs/factory.ts index 047ccec4aa8c..936f49cfac9a 100644 --- a/server/pyright/server/src/tests/harness/vfs/factory.ts +++ b/server/pyright/server/src/tests/harness/vfs/factory.ts @@ -6,7 +6,7 @@ * Provides a factory to create virtual file system backed by a real file system with some path remapped */ -import * as consts from '../../../common/consts'; +import * as pathConsts from '../../../common/pathConsts'; import { combinePaths, getDirectoryPath, normalizeSlashes, resolvePaths } from '../../../common/pathUtils'; import { GlobalMetadataOptionNames } from '../fourslash/fourSlashTypes'; import { TestHost } from '../host'; @@ -30,8 +30,9 @@ export interface FileSystemCreateOptions extends FileSystemOptions { documents?: readonly TextDocument[]; } -export const libFolder = combinePaths(MODULE_PATH, normalizeSlashes(combinePaths(consts.LIB, consts.SITE_PACKAGES))); -export const typeshedFolder = combinePaths(MODULE_PATH, normalizeSlashes(consts.TYPESHED_FALLBACK)); +export const libFolder = combinePaths(MODULE_PATH, normalizeSlashes( + combinePaths(pathConsts.lib, pathConsts.sitePackages))); +export const typeshedFolder = combinePaths(MODULE_PATH, normalizeSlashes(pathConsts.typeshedFallback)); export const srcFolder = normalizeSlashes('/.src'); /** @@ -100,7 +101,8 @@ let localCSFSCache: FileSystem | undefined; function getBuiltLocal(host: TestHost, ignoreCase: boolean, mountPaths: Map): FileSystem { // Ensure typeshed folder if (!mountPaths.has(typeshedFolder)) { - mountPaths.set(typeshedFolder, resolvePaths(host.getWorkspaceRoot(), '../client/' + consts.TYPESHED_FALLBACK)); + mountPaths.set(typeshedFolder, resolvePaths(host.getWorkspaceRoot(), '../client/' + + pathConsts.typeshedFallback)); } if (!canReuseCache(host, mountPaths)) {