Skip to content

Commit

Permalink
Improve typescript_tsdk lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
alecmev committed Jun 1, 2020
1 parent f21aba5 commit 352f992
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 18 deletions.
24 changes: 20 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,32 @@ The plug-in uses **Node.js** to run the TypeScript server. The plug-in looks fo
If the `node_path` setting is present, this will override the PATH environment variable and the plug-in will use the value of the `node_path` setting as the node executable to run.
See more information in [our Tips and Known Issues](https://github.com/Microsoft/TypeScript-Sublime-Plugin/wiki/Tips-and-Known-Issues) wiki page.

Note: Using different versions of TypeScript
Using different versions of TypeScript
--------------
This plugin can be configured to load an alternate version of TypeScript.
This is typically useful for trying out nightly builds, or prototyping with custom builds.
To do that, update the `Settings - User` file with the following:

This plugin can be configured to load an alternate version of TypeScript. This is typically useful for trying out nightly builds, prototyping with custom builds, or compatibility with node_modules-free package managers like Yarn 2. To do that, update the `Settings - User` file or your project settings with the following:

```json5
"typescript_tsdk": "<path to your folder>/node_modules/typescript/lib"
```

The path may be relative. In such case the plugin will look in the following locations in this order:

```
/foo/project_folder_1/<typescript_tsdk>
/foo/<typescript_tsdk>
/<typescript_tsdk>
/bar/project_folder_2/<typescript_tsdk>
/bar/<typescript_tsdk>
/baz/open_file_1_folder/<typescript_tsdk>
/baz/<typescript_tsdk>
/baz/open_file_2_folder/<typescript_tsdk>
```

In case of Yarn 2, just [install its editor SDK](https://yarnpkg.com/advanced/editor-sdks) and set `typescript_tsdk` to `.vscode/pnpify/typescript/lib`.

**Note.** The plugin isn't reloaded when switching projects or updating settings at the moment, so you must restart Sublime Text when doing so, unfortunately. When in doubt, check the console for "Path of tsserver.js" and "Path of tsc.js", and see if they point where you expect them to point.

Installation
------------
If using [Package Control](https://packagecontrol.io/) for Sublime Text, simply install the `TypeScript` package.
Expand Down
65 changes: 51 additions & 14 deletions typescript/libs/editor_client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from .reference import RefInfo
import collections
import logging

from .reference import RefInfo
from .node_client import ServerClient, WorkerClient
from .service_proxy import ServiceProxy
from .logger import log
Expand Down Expand Up @@ -49,19 +52,53 @@ def initialize(self):
initialized during loading time
"""

# retrieve the path to tsserver.js
# first see if user set the path to the file
settings = sublime.load_settings("Preferences.sublime-settings")
tsdk_location = settings.get("typescript_tsdk")
if tsdk_location:
proc_file = os.path.join(tsdk_location, "tsserver.js")
global_vars._tsc_path = os.path.join(tsdk_location, "tsc.js")
else:
# otherwise, get tsserver.js from package directory
proc_file = os.path.join(PLUGIN_DIR, "tsserver", "tsserver.js")
global_vars._tsc_path = os.path.join(PLUGIN_DIR, "tsserver", "tsc.js")
log.debug("Path of tsserver.js: " + proc_file)
log.debug("Path of tsc.js: " + get_tsc_path())
# Default to and fall back to the bundled SDK
tsdk_location_default = os.path.join(PLUGIN_DIR, "tsserver")
tsdk_location = tsdk_location_default

is_tsdk_location_good = lambda x: (
os.path.isfile(os.path.join(x, "tsserver.js"))
and os.path.isfile(os.path.join(x, "tsc.js"))
)

active_window = sublime.active_window()
settings = active_window.active_view().settings()

This comment has been minimized.

Copy link
@orta

orta Dec 7, 2020

Contributor

This line is raising for me when I use it on master:

2020-12-07 12:24:02,121: 4654489024: WARNING: TypeScript plugin initialized.
plugins loaded
Traceback (most recent call last):
  File "/Applications/Sublime Text.app/Contents/MacOS/sublime_plugin.py", line 298, in on_api_ready
    plc()
  File "/Users/ortatherox/Library/Application Support/Sublime Text 3/Packages/TypeScript/main.py", line 69, in plugin_loaded
    cli.initialize()
  File "/Users/ortatherox/Library/Application Support/Sublime Text 3/Packages/TypeScript/typescript/libs/editor_client.py", line 65, in initialize
    settings = active_window.active_view().settings()
AttributeError: 'NoneType' object has no attribute 'settings'
2020-12-07 12:24:04,115: 4654489024: WARNING: Path of tsserver.js: /Users/ortatherox/Library/Application Support/Sublime Text 3/Packages/TypeScript/tsserver/tsserver.js
2020-12-07 12:24:04,116: 4654489024: WARNING: Path of tsc.js: /Users/ortatherox/Library/Application Support/Sublime Text 3/Packages/TypeScript/tsserver/tsc.js

It doesn't seem to affect the runtime behavior to for the plugin, but do you have any ideas how to not make it be tied to a window existing?

typescript_tsdk_setting = settings.get("typescript_tsdk")
if typescript_tsdk_setting:
if os.path.isabs(typescript_tsdk_setting):
if is_tsdk_location_good(typescript_tsdk_setting):
tsdk_location = typescript_tsdk_setting
else:
def look_for_tsdk(x):
x_appended = os.path.join(x, typescript_tsdk_setting)
if is_tsdk_location_good(x_appended):
return x_appended
parent = os.path.dirname(x)
if parent == x:
return None # We have reached the root
return look_for_tsdk(parent)

# list(OrderedDict.fromkeys(x)) = deduped x, order preserved
folders = active_window.folders() + list(
collections.OrderedDict.fromkeys(
[
os.path.dirname(x.file_name())
for x in active_window.views()
if x.file_name() # It's None for unsaved files
]
)
)
for folder in folders:
x = look_for_tsdk(folder)
if x != None:
tsdk_location = x
break

proc_file = os.path.join(tsdk_location, "tsserver.js")
global_vars._tsc_path = os.path.join(tsdk_location, "tsc.js")

log.warn("Path of tsserver.js: " + proc_file)
log.warn("Path of tsc.js: " + get_tsc_path())

self.node_client = ServerClient(proc_file)
self.worker_client = WorkerClient(proc_file)
Expand Down

1 comment on commit 352f992

@isaacl
Copy link

@isaacl isaacl commented on 352f992 Nov 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still not released :-\

Please sign in to comment.