Skip to content
This repository was archived by the owner on Dec 1, 2024. It is now read-only.

Commit 986f489

Browse files
committed
When running in LSP mode, use the specified root, not cwd
Some implementations (e.g. ale) don't run it from the project root.
1 parent 758bae1 commit 986f489

9 files changed

+66
-67
lines changed

src/__Private/LSPImpl/CodeActionCommand.php

-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
namespace Facebook\HHAST\__Private\LSPImpl;
1212

13-
use type Facebook\HHAST\__Private\LintRunConfig;
1413
use namespace Facebook\HHAST\__Private\{LSP, LSPLib};
1514
use namespace Facebook\HHAST\Linters;
1615
use namespace HH\Lib\{C, Str, Vec};
@@ -20,7 +19,6 @@ final class CodeActionCommand extends LSPLib\CodeActionCommand {
2019

2120
public function __construct(
2221
private LSPLib\Client $client,
23-
private ?LintRunConfig $config,
2422
private ServerState $state,
2523
) {
2624
}

src/__Private/LSPImpl/DidChangeWatchedFilesNotification.php

+2-6
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@
1010

1111
namespace Facebook\HHAST\__Private\LSPImpl;
1212

13-
use type Facebook\HHAST\__Private\{
14-
LintRunConfig,
15-
LintRunLSPPublishDiagnosticsEventHandler,
16-
};
13+
use type Facebook\HHAST\__Private\LintRunLSPPublishDiagnosticsEventHandler;
1714
use namespace Facebook\HHAST\__Private\{LSP, LSPLib};
1815
use namespace HH\Lib\{C, Str, Vec};
1916

@@ -22,7 +19,6 @@ final class DidChangeWatchedFilesNotification
2219

2320
public function __construct(
2421
private LSPLib\Client $client,
25-
private ?LintRunConfig $config,
2622
private ServerState $state,
2723
) {
2824
}
@@ -63,7 +59,7 @@ public function __construct(
6359

6460
await relint_uris_async(
6561
new LintRunLSPPublishDiagnosticsEventHandler($this->client, $this->state),
66-
$this->config,
62+
$this->state->config,
6763
$to_relint,
6864
);
6965
}

src/__Private/LSPImpl/DidOpenTextDocumentNotification.php

+2-6
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@
1010

1111
namespace Facebook\HHAST\__Private\LSPImpl;
1212

13-
use type Facebook\HHAST\__Private\{
14-
LintRunConfig,
15-
LintRunLSPPublishDiagnosticsEventHandler,
16-
};
13+
use type Facebook\HHAST\__Private\LintRunLSPPublishDiagnosticsEventHandler;
1714
use namespace Facebook\HHAST\__Private\LSPLib;
1815
use namespace HH\Lib\Str;
1916

@@ -22,7 +19,6 @@ final class DidOpenTextDocumentNotification
2219

2320
public function __construct(
2421
private LSPLib\Client $client,
25-
private ?LintRunConfig $config,
2622
private ServerState $state,
2723
) {
2824
}
@@ -41,7 +37,7 @@ public function __construct(
4137

4238
await relint_uri_async(
4339
new LintRunLSPPublishDiagnosticsEventHandler($this->client, $this->state),
44-
$this->config,
40+
$this->state->config,
4541
$uri,
4642
);
4743
}

src/__Private/LSPImpl/DidSaveTextDocumentNotification.php

+2-6
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@
1010

1111
namespace Facebook\HHAST\__Private\LSPImpl;
1212

13-
use type Facebook\HHAST\__Private\{
14-
LintRunConfig,
15-
LintRunLSPPublishDiagnosticsEventHandler,
16-
};
13+
use type Facebook\HHAST\__Private\LintRunLSPPublishDiagnosticsEventHandler;
1714
use namespace Facebook\HHAST\__Private\LSPLib;
1815
use namespace HH\Lib\Str;
1916

@@ -22,7 +19,6 @@ final class DidSaveTextDocumentNotification
2219

2320
public function __construct(
2421
private LSPLib\Client $client,
25-
private ?LintRunConfig $config,
2622
private ServerState $state,
2723
) {
2824
}
@@ -36,7 +32,7 @@ public function __construct(
3632

3733
await relint_uri_async(
3834
new LintRunLSPPublishDiagnosticsEventHandler($this->client, $this->state),
39-
$this->config,
35+
$this->state->config,
4036
$uri,
4137
);
4238
}

src/__Private/LSPImpl/InitializeCommand.php

+13
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
use namespace Facebook\TypeAssert;
1414
use namespace Facebook\HHAST\__Private\{LSP, LSPLib};
15+
use namespace HH\Lib\Str;
16+
use type Facebook\HHAST\__Private\LintRunConfig;
1517

1618
final class InitializeCommand extends LSPLib\InitializeCommand<ServerState> {
1719

@@ -43,10 +45,21 @@ final class InitializeCommand extends LSPLib\InitializeCommand<ServerState> {
4345
);
4446

4547
$lint_mode = $options['lintMode'] ?? null;
48+
4649
if ($lint_mode !== null) {
4750
$this->state->lintMode = $lint_mode;
4851
}
4952

53+
invariant($this->state->config === null, 'Tried to set config twice');
54+
$uri = $p['rootUri'];
55+
if ($uri === null) {
56+
$uri = 'file://'.\getcwd();
57+
}
58+
if (Str\starts_with($uri, 'file://')) {
59+
$path = Str\strip_prefix($uri, 'file://');
60+
$this->state->config = LintRunConfig::getForPath($path);
61+
}
62+
5063
return await parent::executeAsync($p);
5164
}
5265
}

src/__Private/LSPImpl/Server.php

+12-19
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
use type Facebook\HHAST\__Private\{
1414
Asio\AsyncPoll,
15-
LintRunConfig,
1615
LintRunLSPPublishDiagnosticsEventHandler,
1716
LintRun,
1817
};
@@ -21,11 +20,7 @@
2120
use namespace HH\Lib\Str;
2221

2322
final class Server extends LSPLib\Server<ServerState> {
24-
public function __construct(
25-
private ITerminal $terminal,
26-
private ?LintRunConfig $config,
27-
private vec<string> $roots,
28-
) {
23+
public function __construct(private ITerminal $terminal) {
2924
parent::__construct(new LSPImpl\Client($terminal), new ServerState());
3025
}
3126

@@ -34,7 +29,7 @@ protected function getSupportedServerCommands(): vec<LSPLib\ServerCommand> {
3429
return vec[
3530
new LSPImpl\InitializeCommand($this->state),
3631
new LSPLib\ShutdownCommand($this->state),
37-
new LSPImpl\CodeActionCommand($this->client, $this->config, $this->state),
32+
new LSPImpl\CodeActionCommand($this->client, $this->state),
3833
new LSPImpl\ExecuteCommandCommand($this->client),
3934
];
4035
}
@@ -45,19 +40,10 @@ protected function getSupportedClientNotifications(
4540
return vec[
4641
new LSPImpl\DidChangeWatchedFilesNotification(
4742
$this->client,
48-
$this->config,
49-
$this->state,
50-
),
51-
new LSPImpl\DidSaveTextDocumentNotification(
52-
$this->client,
53-
$this->config,
54-
$this->state,
55-
),
56-
new LSPImpl\DidOpenTextDocumentNotification(
57-
$this->client,
58-
$this->config,
5943
$this->state,
6044
),
45+
new LSPImpl\DidSaveTextDocumentNotification($this->client, $this->state),
46+
new LSPImpl\DidOpenTextDocumentNotification($this->client, $this->state),
6147
new LSPImpl\DidCloseTextDocumentNotification($this->client, $this->state),
6248
new LSPImpl\ExitNotification($this->state),
6349
new LSPImpl\InitializedNotification($this->client, $this->state),
@@ -116,7 +102,14 @@ protected function getSupportedClientNotifications(
116102

117103
$handler =
118104
new LintRunLSPPublishDiagnosticsEventHandler($this->client, $this->state);
119-
await (new LintRun($this->config, $handler, $this->roots))->runAsync();
105+
await (
106+
new LintRun(
107+
$this->state->config,
108+
$handler,
109+
$this->state->config?->getRoots() ?? vec[],
110+
)
111+
)
112+
->runAsync();
120113
}
121114

122115
private async function readMessageAsync(): Awaitable<string> {

src/__Private/LSPImpl/ServerState.php

+2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212

1313
use namespace Facebook\HHAST\Linters;
1414
use namespace Facebook\HHAST\__Private\LSPLib;
15+
use type Facebook\HHAST\__Private\LintRunConfig;
1516

1617
final class ServerState extends LSPLib\ServerState {
18+
public ?LintRunConfig $config = null;
1719
public LintMode $lintMode = LintMode::WHOLE_PROJECT;
1820
public keyset<string> $openFiles = keyset[];
1921
public dict<string, vec<Linters\LintError>> $lintErrors = dict[];

src/__Private/LintRunConfig.php

+10-7
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,20 @@ private static function getNamedLinterGroup(
9898
}
9999
}
100100

101-
private function __construct(private self::TConfigFile $configFile) {
101+
private function __construct(
102+
private string $projectRoot,
103+
private self::TConfigFile $configFile,
104+
) {
102105
}
103106

104107
<<__Memoize>>
105108
private static function getFromConfigFile(string $path): this {
106-
return new self(self::getConfigFromFile($path));
109+
return new self(\dirname($path), self::getConfigFromFile($path));
107110
}
108111

109112
<<__Memoize>>
110113
private static function getDefault(): this {
111-
return new self(shape('roots' => vec[]));
114+
return new self(\getcwd(), shape('roots' => vec[]));
112115
}
113116

114117
public static function getForPath(string $path): this {
@@ -135,13 +138,13 @@ private static function getForPathImpl(string $path): this {
135138
}
136139

137140
public function getRoots(): vec<string> {
138-
return $this->configFile['roots'];
141+
return
142+
Vec\map($this->configFile['roots'], $dir ==> $this->projectRoot.'/'.$dir);
139143
}
140144

141145
public function getConfigForFile(string $file_path): self::TFileConfig {
142-
$roots = Vec\map($this->getRoots(), $s ==> Str\strip_suffix($s, '/').'/');
143-
$file_path =
144-
Str\strip_prefix($file_path, Str\strip_suffix(\getcwd(), '/').'/');
146+
$roots = Vec\map($this->configFile['roots'], $s ==> Str\strip_suffix($s, '/').'/');
147+
$file_path = Str\strip_prefix($file_path, $this->projectRoot.'/');
145148
if (
146149
$roots !== vec[] &&
147150
!C\any($roots, $root ==> Str\starts_with($file_path, $root))

src/__Private/LinterCLI.php

+23-21
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,28 @@ protected function getSupportedOptions(): vec<CLIOptions\CLIOption> {
9090
}
9191

9292
private async function mainImplAsync(): Awaitable<int> {
93+
$terminal = $this->getTerminal();
94+
$log_prefix = $this->ioLogPrefix;
95+
if ($log_prefix !== null) {
96+
$terminal = new Terminal(
97+
new LoggingInputTap(
98+
$terminal->getStdin(),
99+
\fopen($log_prefix.'in', 'w+'),
100+
),
101+
new LoggingOutputTap(
102+
$terminal->getStdout(),
103+
\fopen($log_prefix.'out', 'w+'),
104+
),
105+
new LoggingOutputTap(
106+
$terminal->getStderr(),
107+
\fopen($log_prefix.'err', 'w+'),
108+
),
109+
);
110+
}
111+
if ($this->mode === LinterCLIMode::LSP) {
112+
return await (new LSPImpl\Server($terminal))->mainAsync();
113+
}
114+
93115
$err = $this->getStderr();
94116
$roots = $this->getArguments();
95117

@@ -122,25 +144,6 @@ protected function getSupportedOptions(): vec<CLIOptions\CLIOption> {
122144
$config = null;
123145
}
124146

125-
$terminal = $this->getTerminal();
126-
$log_prefix = $this->ioLogPrefix;
127-
if ($log_prefix !== null) {
128-
$terminal = new Terminal(
129-
new LoggingInputTap(
130-
$terminal->getStdin(),
131-
\fopen($log_prefix.'in', 'w+'),
132-
),
133-
new LoggingOutputTap(
134-
$terminal->getStdout(),
135-
\fopen($log_prefix.'out', 'w+'),
136-
),
137-
new LoggingOutputTap(
138-
$terminal->getStderr(),
139-
\fopen($log_prefix.'err', 'w+'),
140-
),
141-
);
142-
}
143-
144147
switch ($this->mode) {
145148
case LinterCLIMode::PLAIN:
146149
$error_handler = new LintRunCLIEventHandler($terminal);
@@ -149,8 +152,7 @@ protected function getSupportedOptions(): vec<CLIOptions\CLIOption> {
149152
$error_handler = new LintRunJSONEventHandler($terminal);
150153
break;
151154
case LinterCLIMode::LSP:
152-
return await (new LSPImpl\Server($terminal, $config, $roots))
153-
->mainAsync();
155+
invariant_violation('should have returned earlier');
154156
}
155157

156158
try {

0 commit comments

Comments
 (0)