Skip to content

Commit dd0f5b3

Browse files
committed
supporting dynamic workspaces
1 parent 5217dda commit dd0f5b3

2 files changed

Lines changed: 59 additions & 6 deletions

File tree

lsp_types/pyright/session.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
from __future__ import annotations
22

33
import json
4-
import os
5-
from pathlib import Path
64
import typing as t
5+
from pathlib import Path
76

87
import lsp_types
98
from lsp_types.process import LSPProcess, ProcessLaunchInfo
@@ -12,7 +11,10 @@
1211

1312

1413
class PyrightSession(lsp_types.Session):
15-
"""Pyright LSP session implementation"""
14+
"""
15+
Pyright LSP session implementation.
16+
TODO: Move process initialization and config_path within the session instance
17+
"""
1618

1719
@classmethod
1820
async def create(
@@ -23,6 +25,8 @@ async def create(
2325
options: PyrightConfig = {},
2426
) -> t.Self:
2527
"""Create a new Pyright session"""
28+
base_path = base_path.resolve()
29+
2630
config_path = base_path / "pyrightconfig.json"
2731
config_path.write_text(json.dumps(options, indent=2))
2832

@@ -35,8 +39,8 @@ async def create(
3539
await lsp_process.send.initialize(
3640
{
3741
"processId": None,
38-
"rootUri": f"file://{os.getcwd()}",
39-
"rootPath": os.getcwd(),
42+
"rootUri": f"file://{base_path}",
43+
"rootPath": str(base_path),
4044
"capabilities": {
4145
"textDocument": {
4246
"publishDiagnostics": {

tests/test_pyright/test_pyright_session.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,54 @@
1-
from lsp_types.pyright.session import PyrightSession
1+
from pathlib import Path
2+
23
import lsp_types
4+
from lsp_types.pyright.session import PyrightSession
5+
6+
7+
async def test_pyright_session_with_dynamic_environment():
8+
"""Test Pyright session with a dynamic temporary environment"""
9+
import tempfile
10+
11+
# Create a temporary directory
12+
with tempfile.TemporaryDirectory() as temp_dir:
13+
temp_path = Path(temp_dir)
14+
15+
module_path = temp_path / "mymodule"
16+
module_path.mkdir()
17+
18+
# Create a utils.py file with a simple function
19+
utils_file = module_path / "utils.py"
20+
utils_content = """\
21+
def add_numbers(a: int, b: int) -> int:
22+
'''Add two numbers together.'''
23+
return a + b
24+
"""
25+
utils_file.write_text(utils_content)
26+
module_path.joinpath("__init__.py").touch()
27+
28+
# Create code that imports from the utils.py file
29+
code = """\
30+
from mymodule.utils import add_numbers
31+
32+
result = add_numbers(5, 10)
33+
print(f"The result is: {result}")
34+
"""
35+
36+
# Create a Pyright session with the temporary directory as the base_path
37+
pyright_session = await PyrightSession.create(
38+
base_path=temp_path,
39+
initial_code=code,
40+
options={"include": ["."]}, # Include the current directory for imports
41+
)
42+
43+
# Get diagnostics to check for any errors
44+
diagnostics = await pyright_session.get_diagnostics()
45+
diags = diagnostics.get("diagnostics", [])
46+
47+
# Verify no errors are reported
48+
assert len(diags) == 0, f"Expected no diagnostics, but got: {diags}"
49+
50+
# Shutdown the Pyright session
51+
await pyright_session.shutdown()
352

453

554
async def test_pyright_session_diagnostics():

0 commit comments

Comments
 (0)