@@ -6,11 +6,13 @@ import (
66 "fmt"
77 "io"
88 "os"
9+ "runtime"
910
1011 "github.com/microsoft/typescript-go/internal/bundled"
1112 "github.com/microsoft/typescript-go/internal/core"
1213 "github.com/microsoft/typescript-go/internal/lsp"
1314 "github.com/microsoft/typescript-go/internal/pprof"
15+ "github.com/microsoft/typescript-go/internal/tspath"
1416 "github.com/microsoft/typescript-go/internal/vfs/osvfs"
1517)
1618
@@ -39,6 +41,7 @@ func runLSP(args []string) int {
3941
4042 fs := bundled .WrapFS (osvfs .FS ())
4143 defaultLibraryPath := bundled .LibPath ()
44+ typingsLocation := getGlobalTypingsCacheLocation ()
4245
4346 s := lsp .NewServer (& lsp.ServerOptions {
4447 In : os .Stdin ,
@@ -47,10 +50,79 @@ func runLSP(args []string) int {
4750 Cwd : core .Must (os .Getwd ()),
4851 FS : fs ,
4952 DefaultLibraryPath : defaultLibraryPath ,
53+ TypingsLocation : typingsLocation ,
5054 })
5155
5256 if err := s .Run (); err != nil && ! errors .Is (err , io .EOF ) {
5357 return 1
5458 }
5559 return 0
5660}
61+
62+ func getGlobalTypingsCacheLocation () string {
63+ switch runtime .GOOS {
64+ case "windows" :
65+ {
66+ basePath , err := os .UserCacheDir ()
67+ if err != nil {
68+ if basePath , err = os .UserConfigDir (); err != nil {
69+ if basePath , err = os .UserHomeDir (); err != nil {
70+ if userProfile := os .Getenv ("USERPROFILE" ); userProfile != "" {
71+ basePath = userProfile
72+ } else if homeDrive , homePath := os .Getenv ("HOMEDRIVE" ), os .Getenv ("HOMEPATH" ); homeDrive != "" && homePath != "" {
73+ basePath = homeDrive + homePath
74+ } else {
75+ basePath = os .TempDir ()
76+ }
77+ }
78+ }
79+ }
80+ return tspath .CombinePaths (tspath .CombinePaths (basePath , "Microsoft/TypeScript" ), core .VersionMajorMinor )
81+ }
82+ case "openbsd" , "freebsd" , "netbsd" , "darwin" , "linux" , "android" :
83+ {
84+ cacheLocation := getNonWindowsCacheLocation ()
85+ return tspath .CombinePaths (tspath .CombinePaths (cacheLocation , "typescript" ), core .VersionMajorMinor )
86+ }
87+ default :
88+ panic ("unsupported platform: " + runtime .GOOS )
89+ }
90+ }
91+
92+ func getNonWindowsCacheLocation () string {
93+ if xdgCacheHome := os .Getenv ("XDG_CACHE_HOME" ); xdgCacheHome != "" {
94+ return xdgCacheHome
95+ }
96+ const platformIsDarwin = runtime .GOOS == "darwin"
97+ var usersDir string
98+ if platformIsDarwin {
99+ usersDir = "Users"
100+ } else {
101+ usersDir = "home"
102+ }
103+ homePath , err := os .UserHomeDir ()
104+ if err != nil {
105+ if home := os .Getenv ("HOME" ); home != "" {
106+ homePath = home
107+ } else {
108+ var userName string
109+ if logName := os .Getenv ("LOGNAME" ); logName != "" {
110+ userName = logName
111+ } else if user := os .Getenv ("USER" ); user != "" {
112+ userName = user
113+ }
114+ if userName != "" {
115+ homePath = "/" + usersDir + "/" + userName
116+ } else {
117+ homePath = os .TempDir ()
118+ }
119+ }
120+ }
121+ var cacheFolder string
122+ if platformIsDarwin {
123+ cacheFolder = "Library/Caches"
124+ } else {
125+ cacheFolder = ".cache"
126+ }
127+ return tspath .CombinePaths (homePath , cacheFolder )
128+ }
0 commit comments