Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion tsc/cmd/tsc/lsp.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func runLSP(args []string) int {
Cwd: cwd,
FS: fs,
DefaultLibraryPath: defaultLibraryPath,
TypingsLocation: tspath.ToRootedDirectoryPath(typingsLocation, cwd),
TypingsLocation: typingsLocation,
NpmInstall: func(ctx context.Context, cwd string, args []string) ([]byte, error) {
cmd := exec.CommandContext(ctx, "npm", args...)
cmd.Dir = cwd
Expand Down
2 changes: 1 addition & 1 deletion tsc/internal/lsp/replay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestReplay(t *testing.T) {
Cwd: cwd,
FS: fs,
DefaultLibraryPath: defaultLibraryPath,
TypingsLocation: tspath.ToRootedDirectoryPath(typingsLocation, cwd),
TypingsLocation: typingsLocation,
NpmInstall: func(ctx context.Context, cwd string, args []string) ([]byte, error) {
cmd := exec.CommandContext(ctx, "npm", args...)
cmd.Dir = cwd
Expand Down
31 changes: 31 additions & 0 deletions tsc/internal/project/ata/ata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/microsoft/TypeScript/tsc/internal/lsp/lsproto"
"github.com/microsoft/TypeScript/tsc/internal/project"
"github.com/microsoft/TypeScript/tsc/internal/testutil/projecttestutil"
"github.com/microsoft/TypeScript/tsc/internal/tspath"
"gotest.tools/v3/assert"
)

Expand All @@ -19,6 +20,36 @@ func TestATA(t *testing.T) {
t.Skip("bundled files are not embedded")
}

t.Run("no typings cache disables acquisition", func(t *testing.T) {
t.Parallel()

files := map[string]any{
"/user/username/projects/project/app.js": `const $ = require("jquery");`,
"/user/username/projects/project/jsconfig.json": `{
"typeAcquisition": { "enable": true }
}`,
}
session, utils := projecttestutil.SetupWithOptionsAndTypingsInstaller(files, &project.SessionOptions{
CurrentDirectory: tspath.RootedDirectoryPathFromNormalized("/"),
DefaultLibraryPath: bundled.LibPath(),
WatchEnabled: true,
}, &projecttestutil.TypingsInstallerOptions{
PackageToFile: map[string]string{
"jquery": `export const x: number;`,
},
})

uri := lsproto.DocumentUri("file:///user/username/projects/project/app.js")
session.DidOpenFile(context.Background(), uri, 1, files["/user/username/projects/project/app.js"].(string), lsproto.LanguageKindJavaScript)
session.WaitForBackgroundTasks()

ls, err := session.GetLanguageService(context.Background(), uri)
assert.NilError(t, err)
assert.Assert(t, ls.GetProgram().GetSourceFile("/user/username/projects/project/app.js") != nil)
assert.Equal(t, len(utils.NpmExecutor().NpmInstallCalls()), 0)
assert.Assert(t, !session.FS().FileExists(projecttestutil.TestTypingsDirectory.ResolveFile("package.json")))
})

t.Run("local module should not be picked up", func(t *testing.T) {
t.Parallel()
files := map[string]any{
Expand Down
10 changes: 7 additions & 3 deletions tsc/internal/vfs/osvfs/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,14 @@ func (vfs *osFS) Chtimes(path tspath.RootedPath, aTime time.Time, mTime time.Tim
return os.Chtimes(path.AsString(), aTime, mTime)
}

func GetGlobalTypingsCacheLocation() string {
func GetGlobalTypingsCacheLocation() tspath.RootedDirectoryPath {
cacheDir, err := os.UserCacheDir()
if err != nil {
cacheDir = os.TempDir()
return ""
}
cachePath, ok := tspath.TryRootedPathFromAbsolute(cacheDir)
if !ok {
return ""
}

var subdir string
Expand All @@ -200,5 +204,5 @@ func GetGlobalTypingsCacheLocation() string {
} else {
subdir = "typescript"
}
return tspath.CombinePaths(cacheDir, subdir, core.VersionMajorMinor())
return tspath.RootedDirectoryPathFromPath(cachePath).ResolveDirectory(subdir).ResolveDirectory(core.VersionMajorMinor())
}
43 changes: 43 additions & 0 deletions tsc/internal/vfs/osvfs/os_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,55 @@ import (
"strings"
"testing"

"github.com/microsoft/TypeScript/tsc/internal/core"
"github.com/microsoft/TypeScript/tsc/internal/repo"
"github.com/microsoft/TypeScript/tsc/internal/tspath"
"github.com/microsoft/TypeScript/tsc/internal/vfs/osvfs"
"gotest.tools/v3/assert"
)

func TestGetGlobalTypingsCacheLocation(t *testing.T) {
t.Run("user cache", func(t *testing.T) {
cacheRoot := t.TempDir()
t.Setenv("HOME", cacheRoot)
t.Setenv("home", cacheRoot)
t.Setenv("LocalAppData", cacheRoot)
t.Setenv("XDG_CACHE_HOME", cacheRoot)
cacheDir, err := os.UserCacheDir()
assert.NilError(t, err)

subdir := "typescript"
if runtime.GOOS == "windows" {
subdir = "Microsoft/TypeScript"
}
assert.Equal(t, osvfs.GetGlobalTypingsCacheLocation(), tspath.RootedDirectoryPathFromAbsolute(tspath.CombinePaths(cacheDir, subdir, core.VersionMajorMinor())))
})

t.Run("missing user cache", func(t *testing.T) {
for _, name := range []string{"HOME", "home", "LocalAppData", "XDG_CACHE_HOME"} {
t.Setenv(name, "")
}
assert.Equal(t, osvfs.GetGlobalTypingsCacheLocation(), tspath.RootedDirectoryPath(""))
})

t.Run("relative XDG cache", func(t *testing.T) {
switch runtime.GOOS {
case "windows", "darwin", "ios", "plan9":
t.Skip("XDG_CACHE_HOME is not used on this platform")
}
t.Setenv("XDG_CACHE_HOME", "relative")
assert.Equal(t, osvfs.GetGlobalTypingsCacheLocation(), tspath.RootedDirectoryPath(""))
})

t.Run("relative user cache", func(t *testing.T) {
t.Setenv("XDG_CACHE_HOME", "")
for _, name := range []string{"HOME", "home", "LocalAppData"} {
t.Setenv(name, "relative-home")
}
assert.Equal(t, osvfs.GetGlobalTypingsCacheLocation(), tspath.RootedDirectoryPath(""))
})
}

func TestOS(t *testing.T) {
t.Parallel()

Expand Down
Loading