Skip to content

Commit

Permalink
pull-pylance-with-pyright-1.1.392-20250118-003823
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Jan 18, 2025
1 parent c87dd0b commit 987f159
Show file tree
Hide file tree
Showing 20 changed files with 815 additions and 733 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ export class BackgroundAnalysisProgram {
this._backgroundAnalysis?.setProgramView(this._program);
}

get serviceProvider() {
return this._serviceProvider;
}

get configOptions() {
return this._configOptions;
}
Expand Down
202 changes: 101 additions & 101 deletions packages/pyright-internal/src/analyzer/importResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,101 @@ export class ImportResolver {
return newImportResult;
}

protected findImplicitImports(
importingModuleName: string,
dirPath: Uri,
exclusions: Uri[]
): Map<string, ImplicitImport> {
const implicitImportMap = new Map<string, ImplicitImport>();

// Enumerate all of the files and directories in the path, expanding links.
const entries = getFileSystemEntriesFromDirEntries(
this.readdirEntriesCached(dirPath),
this.fileSystem,
dirPath
);

// Add implicit file-based modules.
for (const filePath of entries.files) {
const fileExt = filePath.lastExtension;
let strippedFileName: string;
let isNativeLib = false;

if (fileExt === '.py' || fileExt === '.pyi') {
strippedFileName = stripFileExtension(filePath.fileName);
} else if (
_isNativeModuleFileExtension(fileExt) &&
!this.fileExistsCached(filePath.packageUri) &&
!this.fileExistsCached(filePath.packageStubUri)
) {
// Native module.
strippedFileName = filePath.stripAllExtensions().fileName;
isNativeLib = true;
} else {
continue;
}

if (!exclusions.find((exclusion) => exclusion.equals(filePath))) {
const implicitImport: ImplicitImport = {
isStubFile: filePath.hasExtension('.pyi'),
isNativeLib,
name: strippedFileName,
uri: filePath,
};

// Always prefer stub files over non-stub files.
const entry = implicitImportMap.get(implicitImport.name);
if (!entry || !entry.isStubFile) {
// Try resolving resolving native lib to a custom stub.
if (isNativeLib) {
const nativeLibPath = filePath;
const nativeStubPath = this.resolveNativeImportEx(
nativeLibPath,
`${importingModuleName}.${strippedFileName}`,
[]
);
if (nativeStubPath) {
implicitImport.uri = nativeStubPath;
implicitImport.isNativeLib = false;
}
}
implicitImportMap.set(implicitImport.name, implicitImport);
}
}
}

// Add implicit directory-based modules.
for (const dirPath of entries.directories) {
const pyFilePath = dirPath.initPyUri;
const pyiFilePath = dirPath.initPyiUri;
let isStubFile = false;
let path: Uri | undefined;

if (this.fileExistsCached(pyiFilePath)) {
isStubFile = true;
path = pyiFilePath;
} else if (this.fileExistsCached(pyFilePath)) {
path = pyFilePath;
}

if (path) {
if (!exclusions.find((exclusion) => exclusion.equals(path))) {
const implicitImport: ImplicitImport = {
isStubFile,
isNativeLib: false,
name: dirPath.fileName,
uri: path,
pyTypedInfo: this._getPyTypedInfo(dirPath),
};

implicitImportMap.set(implicitImport.name, implicitImport);
}
}
}

return implicitImportMap;
}

private _resolveImportStrict(
importName: string,
sourceFileUri: Uri,
Expand Down Expand Up @@ -1279,7 +1374,7 @@ export class ImportResolver {
isNamespacePackage = true;
}

implicitImports = this._findImplicitImports(importName, dirPath, [pyFilePath, pyiFilePath]);
implicitImports = this.findImplicitImports(importName, dirPath, [pyFilePath, pyiFilePath]);
} else {
for (let i = 0; i < moduleDescriptor.nameParts.length; i++) {
const isFirstPart = i === 0;
Expand Down Expand Up @@ -1327,7 +1422,7 @@ export class ImportResolver {
continue;
}

implicitImports = this._findImplicitImports(moduleDescriptor.nameParts.join('.'), dirPath, [
implicitImports = this.findImplicitImports(moduleDescriptor.nameParts.join('.'), dirPath, [
pyFilePath,
pyiFilePath,
]);
Expand Down Expand Up @@ -1380,7 +1475,7 @@ export class ImportResolver {
resolvedPaths.push(Uri.empty());

if (isLastPart) {
implicitImports = this._findImplicitImports(importName, dirPath, [pyFilePath, pyiFilePath]);
implicitImports = this.findImplicitImports(importName, dirPath, [pyFilePath, pyiFilePath]);
isNamespacePackage = true;
}
}
Expand Down Expand Up @@ -2544,101 +2639,6 @@ export class ImportResolver {
return true;
}

private _findImplicitImports(
importingModuleName: string,
dirPath: Uri,
exclusions: Uri[]
): Map<string, ImplicitImport> {
const implicitImportMap = new Map<string, ImplicitImport>();

// Enumerate all of the files and directories in the path, expanding links.
const entries = getFileSystemEntriesFromDirEntries(
this.readdirEntriesCached(dirPath),
this.fileSystem,
dirPath
);

// Add implicit file-based modules.
for (const filePath of entries.files) {
const fileExt = filePath.lastExtension;
let strippedFileName: string;
let isNativeLib = false;

if (fileExt === '.py' || fileExt === '.pyi') {
strippedFileName = stripFileExtension(filePath.fileName);
} else if (
_isNativeModuleFileExtension(fileExt) &&
!this.fileExistsCached(filePath.packageUri) &&
!this.fileExistsCached(filePath.packageStubUri)
) {
// Native module.
strippedFileName = filePath.stripAllExtensions().fileName;
isNativeLib = true;
} else {
continue;
}

if (!exclusions.find((exclusion) => exclusion.equals(filePath))) {
const implicitImport: ImplicitImport = {
isStubFile: filePath.hasExtension('.pyi'),
isNativeLib,
name: strippedFileName,
uri: filePath,
};

// Always prefer stub files over non-stub files.
const entry = implicitImportMap.get(implicitImport.name);
if (!entry || !entry.isStubFile) {
// Try resolving resolving native lib to a custom stub.
if (isNativeLib) {
const nativeLibPath = filePath;
const nativeStubPath = this.resolveNativeImportEx(
nativeLibPath,
`${importingModuleName}.${strippedFileName}`,
[]
);
if (nativeStubPath) {
implicitImport.uri = nativeStubPath;
implicitImport.isNativeLib = false;
}
}
implicitImportMap.set(implicitImport.name, implicitImport);
}
}
}

// Add implicit directory-based modules.
for (const dirPath of entries.directories) {
const pyFilePath = dirPath.initPyUri;
const pyiFilePath = dirPath.initPyiUri;
let isStubFile = false;
let path: Uri | undefined;

if (this.fileExistsCached(pyiFilePath)) {
isStubFile = true;
path = pyiFilePath;
} else if (this.fileExistsCached(pyFilePath)) {
path = pyFilePath;
}

if (path) {
if (!exclusions.find((exclusion) => exclusion.equals(path))) {
const implicitImport: ImplicitImport = {
isStubFile,
isNativeLib: false,
name: dirPath.fileName,
uri: path,
pyTypedInfo: this._getPyTypedInfo(dirPath),
};

implicitImportMap.set(implicitImport.name, implicitImport);
}
}
}

return implicitImportMap;
}

// Retrieves the pytyped info for a directory if it exists. This is a small perf optimization
// that allows skipping the search when the pytyped file doesn't exist.
private _getPyTypedInfo(filePath: Uri): PyTypedInfo | undefined {
Expand Down Expand Up @@ -2741,7 +2741,7 @@ export class ImportResolver {
return (
current &&
!current.isEmpty() &&
(current.isChild(root) || (current.equals(root) && _isDefaultWorkspace(execEnv.root)))
(current.isChild(root) || (current.equals(root) && isDefaultWorkspace(execEnv.root)))
);
}
}
Expand All @@ -2757,7 +2757,7 @@ export function formatImportName(moduleDescriptor: ImportedModuleDescriptor) {
}

export function getParentImportResolutionRoot(sourceFileUri: Uri, executionRoot: Uri | undefined): Uri {
if (!_isDefaultWorkspace(executionRoot)) {
if (!isDefaultWorkspace(executionRoot)) {
return executionRoot!;
}

Expand Down Expand Up @@ -2830,6 +2830,6 @@ function _isNativeModuleFileExtension(fileExtension: string): boolean {
return supportedNativeLibExtensions.some((ext) => ext === fileExtension);
}

function _isDefaultWorkspace(uri: Uri | undefined) {
export function isDefaultWorkspace(uri: Uri | undefined) {
return !uri || uri.isEmpty() || Uri.isDefaultWorkspace(uri);
}
34 changes: 22 additions & 12 deletions packages/pyright-internal/src/analyzer/pythonPathUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,26 @@ function findSitePackagesPath(
return undefined;
}

export function readPthSearchPaths(pthFile: Uri, fs: FileSystem): Uri[] {
const searchPaths: Uri[] = [];

if (fs.existsSync(pthFile)) {
const data = fs.readFileSync(pthFile, 'utf8');
const lines = data.split(/\r?\n/);
lines.forEach((line) => {
const trimmedLine = line.trim();
if (trimmedLine.length > 0 && !trimmedLine.startsWith('#') && !trimmedLine.match(/^import\s/)) {
const pthPath = pthFile.getDirectory().combinePaths(trimmedLine);
if (fs.existsSync(pthPath) && isDirectory(fs, pthPath)) {
searchPaths.push(fs.realCasePath(pthPath));
}
}
});
}

return searchPaths;
}

export function getPathsFromPthFiles(fs: FileSystem, parentDir: Uri): Uri[] {
const searchPaths: Uri[] = [];

Expand All @@ -192,24 +212,14 @@ export function getPathsFromPthFiles(fs: FileSystem, parentDir: Uri): Uri[] {

// Skip all files that are much larger than expected.
if (fileStats?.isFile() && fileStats.size > 0 && fileStats.size < 64 * 1024) {
const data = fs.readFileSync(filePath, 'utf8');
const lines = data.split(/\r?\n/);
lines.forEach((line) => {
const trimmedLine = line.trim();
if (trimmedLine.length > 0 && !trimmedLine.startsWith('#') && !trimmedLine.match(/^import\s/)) {
const pthPath = parentDir.combinePaths(trimmedLine);
if (fs.existsSync(pthPath) && isDirectory(fs, pthPath)) {
searchPaths.push(fs.realCasePath(pthPath));
}
}
});
searchPaths.push(...readPthSearchPaths(filePath, fs));
}
});

return searchPaths;
}

function addPathIfUnique(pathList: Uri[], pathToAdd: Uri) {
export function addPathIfUnique(pathList: Uri[], pathToAdd: Uri) {
if (!pathList.some((path) => path.key === pathToAdd.key)) {
pathList.push(pathToAdd);
return true;
Expand Down
2 changes: 2 additions & 0 deletions packages/pyright-internal/src/backgroundAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { FullAccessHost } from './common/fullAccessHost';
import { Host } from './common/host';
import { ServiceProvider } from './common/serviceProvider';
import { getRootUri } from './common/uri/uriUtils';
import { ServiceKeys } from './common/serviceKeys';

export class BackgroundAnalysis extends BackgroundAnalysisBase {
private static _workerIndex = 0;
Expand All @@ -27,6 +28,7 @@ export class BackgroundAnalysis extends BackgroundAnalysisBase {
const index = ++BackgroundAnalysis._workerIndex;
const initialData: InitializationData = {
rootUri: getRootUri(serviceProvider)?.toString() ?? '',
tempFileName: serviceProvider.get(ServiceKeys.tempFile).tmpdir().getFilePath(),
serviceId: index.toString(),
cancellationFolderName: getCancellationFolderName(),
runner: undefined,
Expand Down
17 changes: 7 additions & 10 deletions packages/pyright-internal/src/backgroundThreadBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
} from './common/cancellationUtils';
import { ConfigOptions } from './common/configOptions';
import { ConsoleInterface, LogLevel } from './common/console';
import { Disposable, isThenable } from './common/core';
import { isThenable } from './common/core';
import * as debug from './common/debug';
import { createFromRealFileSystem, RealTempFile } from './common/realFileSystem';
import { ServiceKeys } from './common/serviceKeys';
Expand Down Expand Up @@ -70,14 +70,14 @@ export class BackgroundThreadBase {
this._serviceProvider.add(ServiceKeys.console, new BackgroundConsole());
}

let tempFile: RealTempFile | undefined = undefined;
if (!this._serviceProvider.tryGet(ServiceKeys.tempFile)) {
tempFile = new RealTempFile();
let tempFile = this._serviceProvider.tryGet(ServiceKeys.tempFile);
if (!tempFile) {
tempFile = new RealTempFile(data.tempFileName);
this._serviceProvider.add(ServiceKeys.tempFile, tempFile);
}

if (!this._serviceProvider.tryGet(ServiceKeys.caseSensitivityDetector)) {
this._serviceProvider.add(ServiceKeys.caseSensitivityDetector, tempFile ?? new RealTempFile());
this._serviceProvider.add(ServiceKeys.caseSensitivityDetector, tempFile as RealTempFile);
}

if (!this._serviceProvider.tryGet(ServiceKeys.fs)) {
Expand Down Expand Up @@ -114,11 +114,7 @@ export class BackgroundThreadBase {
}

protected handleShutdown() {
const tempFile = this._serviceProvider.tryGet(ServiceKeys.tempFile);
if (Disposable.is(tempFile)) {
tempFile.dispose();
}

this._serviceProvider.dispose();
parentPort?.close();
}
}
Expand Down Expand Up @@ -256,6 +252,7 @@ export function getBackgroundWaiter<T>(port: MessagePort, deserializer: (v: any)

export interface InitializationData {
rootUri: string;
tempFileName: string;
serviceId: string;
workerIndex: number;
cancellationFolderName: string | undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export interface LanguageServerBaseInterface {
readonly supportAdvancedEdits: boolean;
readonly serviceProvider: ServiceProvider;

createBackgroundAnalysis(serviceId: string): BackgroundAnalysisBase | undefined;
createBackgroundAnalysis(serviceId: string, workspaceRoot: Uri): BackgroundAnalysisBase | undefined;
reanalyze(): void;
restart(): void;

Expand Down
Loading

0 comments on commit 987f159

Please sign in to comment.