From d4084bee425bc4d5dc029cf38312404f431b02cd Mon Sep 17 00:00:00 2001 From: Austin Riedhammer Date: Tue, 31 Jan 2023 11:33:49 +0900 Subject: [PATCH 1/2] fix paths not using mapping when scanning project --- server/src/lib/phpstan/configManager.ts | 36 ++++++++++++++++--------- server/src/lib/phpstan/runner.ts | 5 +++- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/server/src/lib/phpstan/configManager.ts b/server/src/lib/phpstan/configManager.ts index 330fde2..f8333bf 100644 --- a/server/src/lib/phpstan/configManager.ts +++ b/server/src/lib/phpstan/configManager.ts @@ -26,6 +26,13 @@ export class ConfigurationManager { config: ClassConfig, filePath: string ): Promise { + const pathMapper = await this.getPathMapper(config); + return pathMapper(filePath); + } + + public static async getPathMapper( + config: ClassConfig + ): Promise<(filePath: string) => string> { const pathMapping = ( await getConfiguration( @@ -33,20 +40,23 @@ export class ConfigurationManager { config.getWorkspaceFolder ) ).paths ?? {}; - if (Object.keys(pathMapping).length === 0) { - return filePath; - } - const expandedFilePath = filePath.replace(/^~/, os.homedir()); - for (const [from, to] of Object.entries(pathMapping)) { - const expandedFromPath = from.replace(/^~/, os.homedir()); - if (expandedFilePath.startsWith(expandedFromPath)) { - return expandedFilePath.replace( - expandedFromPath, - to.replace(/^~/, os.homedir()) - ); + + return (filePath: string) => { + if (Object.keys(pathMapping).length === 0) { + return filePath; } - } - return filePath; + const expandedFilePath = filePath.replace(/^~/, os.homedir()); + for (const [from, to] of Object.entries(pathMapping)) { + const expandedFromPath = from.replace(/^~/, os.homedir()); + if (expandedFilePath.startsWith(expandedFromPath)) { + return expandedFilePath.replace( + expandedFromPath, + to.replace(/^~/, os.homedir()) + ); + } + } + return filePath; + }; } private async _fileIfExists(filePath: string): Promise { diff --git a/server/src/lib/phpstan/runner.ts b/server/src/lib/phpstan/runner.ts index 3da551a..dcb80c6 100644 --- a/server/src/lib/phpstan/runner.ts +++ b/server/src/lib/phpstan/runner.ts @@ -390,6 +390,9 @@ export class PHPStanRunner implements Disposable { if (this._cancelled) { return ReturnResult.canceled(); } + const pathMapper = await ConfigurationManager.getPathMapper( + this._config + ); // Get args const args = await this._getArgs(config, { @@ -411,7 +414,7 @@ export class PHPStanRunner implements Disposable { normalized[ URI.from({ scheme: 'file', - path: filePath, + path: pathMapper(filePath), }).toString() ] = parsed[filePath]; } From 6780b51b01828a0b13db2b75da36888221658894 Mon Sep 17 00:00:00 2001 From: Austin Riedhammer Date: Wed, 1 Feb 2023 14:35:05 +0900 Subject: [PATCH 2/2] add inverse param used in project scan map case --- server/src/lib/phpstan/configManager.ts | 9 ++++++--- server/src/lib/phpstan/runner.ts | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/server/src/lib/phpstan/configManager.ts b/server/src/lib/phpstan/configManager.ts index f8333bf..419c712 100644 --- a/server/src/lib/phpstan/configManager.ts +++ b/server/src/lib/phpstan/configManager.ts @@ -32,7 +32,7 @@ export class ConfigurationManager { public static async getPathMapper( config: ClassConfig - ): Promise<(filePath: string) => string> { + ): Promise<(filePath: string, inverse?: boolean) => string> { const pathMapping = ( await getConfiguration( @@ -41,12 +41,15 @@ export class ConfigurationManager { ) ).paths ?? {}; - return (filePath: string) => { + return (filePath: string, inverse: boolean = false) => { if (Object.keys(pathMapping).length === 0) { return filePath; } const expandedFilePath = filePath.replace(/^~/, os.homedir()); - for (const [from, to] of Object.entries(pathMapping)) { + for (const [fromPath, toPath] of Object.entries(pathMapping)) { + const [from, to] = inverse + ? [toPath, fromPath] + : [fromPath, toPath]; const expandedFromPath = from.replace(/^~/, os.homedir()); if (expandedFilePath.startsWith(expandedFromPath)) { return expandedFilePath.replace( diff --git a/server/src/lib/phpstan/runner.ts b/server/src/lib/phpstan/runner.ts index dcb80c6..a024b09 100644 --- a/server/src/lib/phpstan/runner.ts +++ b/server/src/lib/phpstan/runner.ts @@ -414,7 +414,7 @@ export class PHPStanRunner implements Disposable { normalized[ URI.from({ scheme: 'file', - path: pathMapper(filePath), + path: pathMapper(filePath, true), }).toString() ] = parsed[filePath]; }