Skip to content

Commit

Permalink
Squashed 'server/pyright/' changes from 9521693..04a01c7 (microsoft#99)
Browse files Browse the repository at this point in the history
04a01c7 Merge pull request microsoft#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 (microsoft#514)
50b0d32 added a hook to ImportResolver for custom importing logic. (microsoft#515)
de41104 Merge pull request microsoft#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 microsoft#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. (microsoft#507)

git-subtree-dir: server/pyright
git-subtree-split: 04a01c7
  • Loading branch information
heejaechang authored Feb 13, 2020
1 parent 79a3eef commit 42a94f9
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 41 deletions.
2 changes: 1 addition & 1 deletion server/pyright/client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion server/pyright/server/src/analyzer/importResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
46 changes: 29 additions & 17 deletions server/pyright/server/src/analyzer/pythonPathUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string[]>();

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;
Expand All @@ -50,22 +62,22 @@ 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 = '';
}
}

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];
Expand All @@ -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];
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion server/pyright/server/src/analyzer/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
11 changes: 11 additions & 0 deletions server/pyright/server/src/common/pathConsts.ts
Original file line number Diff line number Diff line change
@@ -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';
4 changes: 2 additions & 2 deletions server/pyright/server/src/languageServerBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 {
Expand Down
16 changes: 1 addition & 15 deletions server/pyright/server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions server/pyright/server/src/tests/fourSlashRunner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
10 changes: 6 additions & 4 deletions server/pyright/server/src/tests/harness/vfs/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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');

/**
Expand Down Expand Up @@ -100,7 +101,8 @@ let localCSFSCache: FileSystem | undefined;
function getBuiltLocal(host: TestHost, ignoreCase: boolean, mountPaths: Map<string, string>): 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)) {
Expand Down

0 comments on commit 42a94f9

Please sign in to comment.