Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix paths not using mapping when scanning project #28

Merged
merged 2 commits into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 26 additions & 13 deletions server/src/lib/phpstan/configManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,40 @@ export class ConfigurationManager {
config: ClassConfig,
filePath: string
): Promise<string> {
const pathMapper = await this.getPathMapper(config);
return pathMapper(filePath);
}

public static async getPathMapper(
config: ClassConfig
): Promise<(filePath: string, inverse?: boolean) => string> {
const pathMapping =
(
await getConfiguration(
config.connection,
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, inverse: boolean = false) => {
if (Object.keys(pathMapping).length === 0) {
return filePath;
}
}
return filePath;
const expandedFilePath = filePath.replace(/^~/, os.homedir());
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(
expandedFromPath,
to.replace(/^~/, os.homedir())
);
}
}
return filePath;
};
}

private async _fileIfExists(filePath: string): Promise<string | null> {
Expand Down
5 changes: 4 additions & 1 deletion server/src/lib/phpstan/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand All @@ -411,7 +414,7 @@ export class PHPStanRunner implements Disposable {
normalized[
URI.from({
scheme: 'file',
path: filePath,
path: pathMapper(filePath, true),
}).toString()
] = parsed[filePath];
}
Expand Down