Skip to content

Commit bb9afd2

Browse files
authored
Disable ATA when no user cache is available (#64836)
1 parent ce30dd5 commit bb9afd2

5 files changed

Lines changed: 83 additions & 5 deletions

File tree

‎tsc/cmd/tsc/lsp.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func runLSP(args []string) int {
5757
Cwd: cwd,
5858
FS: fs,
5959
DefaultLibraryPath: defaultLibraryPath,
60-
TypingsLocation: tspath.ToRootedDirectoryPath(typingsLocation, cwd),
60+
TypingsLocation: typingsLocation,
6161
NpmInstall: func(ctx context.Context, cwd string, args []string) ([]byte, error) {
6262
cmd := exec.CommandContext(ctx, "npm", args...)
6363
cmd.Dir = cwd

‎tsc/internal/lsp/replay_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func TestReplay(t *testing.T) {
5858
Cwd: cwd,
5959
FS: fs,
6060
DefaultLibraryPath: defaultLibraryPath,
61-
TypingsLocation: tspath.ToRootedDirectoryPath(typingsLocation, cwd),
61+
TypingsLocation: typingsLocation,
6262
NpmInstall: func(ctx context.Context, cwd string, args []string) ([]byte, error) {
6363
cmd := exec.CommandContext(ctx, "npm", args...)
6464
cmd.Dir = cwd

‎tsc/internal/project/ata/ata_test.go‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/microsoft/TypeScript/tsc/internal/lsp/lsproto"
1111
"github.com/microsoft/TypeScript/tsc/internal/project"
1212
"github.com/microsoft/TypeScript/tsc/internal/testutil/projecttestutil"
13+
"github.com/microsoft/TypeScript/tsc/internal/tspath"
1314
"gotest.tools/v3/assert"
1415
)
1516

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

23+
t.Run("no typings cache disables acquisition", func(t *testing.T) {
24+
t.Parallel()
25+
26+
files := map[string]any{
27+
"/user/username/projects/project/app.js": `const $ = require("jquery");`,
28+
"/user/username/projects/project/jsconfig.json": `{
29+
"typeAcquisition": { "enable": true }
30+
}`,
31+
}
32+
session, utils := projecttestutil.SetupWithOptionsAndTypingsInstaller(files, &project.SessionOptions{
33+
CurrentDirectory: tspath.RootedDirectoryPathFromNormalized("/"),
34+
DefaultLibraryPath: bundled.LibPath(),
35+
WatchEnabled: true,
36+
}, &projecttestutil.TypingsInstallerOptions{
37+
PackageToFile: map[string]string{
38+
"jquery": `export const x: number;`,
39+
},
40+
})
41+
42+
uri := lsproto.DocumentUri("file:///user/username/projects/project/app.js")
43+
session.DidOpenFile(context.Background(), uri, 1, files["/user/username/projects/project/app.js"].(string), lsproto.LanguageKindJavaScript)
44+
session.WaitForBackgroundTasks()
45+
46+
ls, err := session.GetLanguageService(context.Background(), uri)
47+
assert.NilError(t, err)
48+
assert.Assert(t, ls.GetProgram().GetSourceFile("/user/username/projects/project/app.js") != nil)
49+
assert.Equal(t, len(utils.NpmExecutor().NpmInstallCalls()), 0)
50+
assert.Assert(t, !session.FS().FileExists(projecttestutil.TestTypingsDirectory.ResolveFile("package.json")))
51+
})
52+
2253
t.Run("local module should not be picked up", func(t *testing.T) {
2354
t.Parallel()
2455
files := map[string]any{

‎tsc/internal/vfs/osvfs/os.go‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,14 @@ func (vfs *osFS) Chtimes(path tspath.RootedPath, aTime time.Time, mTime time.Tim
228228
return os.Chtimes(path.AsString(), aTime, mTime)
229229
}
230230

231-
func GetGlobalTypingsCacheLocation() string {
231+
func GetGlobalTypingsCacheLocation() tspath.RootedDirectoryPath {
232232
cacheDir, err := os.UserCacheDir()
233233
if err != nil {
234-
cacheDir = os.TempDir()
234+
return ""
235+
}
236+
cachePath, ok := tspath.TryRootedPathFromAbsolute(cacheDir)
237+
if !ok {
238+
return ""
235239
}
236240

237241
var subdir string
@@ -240,5 +244,5 @@ func GetGlobalTypingsCacheLocation() string {
240244
} else {
241245
subdir = "typescript"
242246
}
243-
return tspath.CombinePaths(cacheDir, subdir, core.VersionMajorMinor())
247+
return tspath.RootedDirectoryPathFromPath(cachePath).ResolveDirectory(subdir).ResolveDirectory(core.VersionMajorMinor())
244248
}

‎tsc/internal/vfs/osvfs/os_test.go‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,56 @@ import (
88
"testing"
99
"time"
1010

11+
"github.com/microsoft/TypeScript/tsc/internal/core"
1112
"github.com/microsoft/TypeScript/tsc/internal/nativepath"
1213
"github.com/microsoft/TypeScript/tsc/internal/repo"
1314
"github.com/microsoft/TypeScript/tsc/internal/tspath"
1415
"github.com/microsoft/TypeScript/tsc/internal/vfs/osvfs"
1516
"gotest.tools/v3/assert"
1617
)
1718

19+
func TestGetGlobalTypingsCacheLocation(t *testing.T) {
20+
t.Run("user cache", func(t *testing.T) {
21+
cacheRoot := t.TempDir()
22+
t.Setenv("HOME", cacheRoot)
23+
t.Setenv("home", cacheRoot)
24+
t.Setenv("LocalAppData", cacheRoot)
25+
t.Setenv("XDG_CACHE_HOME", cacheRoot)
26+
cacheDir, err := os.UserCacheDir()
27+
assert.NilError(t, err)
28+
29+
subdir := "typescript"
30+
if runtime.GOOS == "windows" {
31+
subdir = "Microsoft/TypeScript"
32+
}
33+
assert.Equal(t, osvfs.GetGlobalTypingsCacheLocation(), tspath.RootedDirectoryPathFromAbsolute(tspath.CombinePaths(cacheDir, subdir, core.VersionMajorMinor())))
34+
})
35+
36+
t.Run("missing user cache", func(t *testing.T) {
37+
for _, name := range []string{"HOME", "home", "LocalAppData", "XDG_CACHE_HOME"} {
38+
t.Setenv(name, "")
39+
}
40+
assert.Equal(t, osvfs.GetGlobalTypingsCacheLocation(), tspath.RootedDirectoryPath(""))
41+
})
42+
43+
t.Run("relative XDG cache", func(t *testing.T) {
44+
switch runtime.GOOS {
45+
case "windows", "darwin", "ios", "plan9":
46+
t.Skip("XDG_CACHE_HOME is not used on this platform")
47+
}
48+
t.Setenv("XDG_CACHE_HOME", "relative")
49+
assert.Equal(t, osvfs.GetGlobalTypingsCacheLocation(), tspath.RootedDirectoryPath(""))
50+
})
51+
52+
t.Run("relative user cache", func(t *testing.T) {
53+
t.Setenv("XDG_CACHE_HOME", "")
54+
for _, name := range []string{"HOME", "home", "LocalAppData"} {
55+
t.Setenv(name, "relative-home")
56+
}
57+
assert.Equal(t, osvfs.GetGlobalTypingsCacheLocation(), tspath.RootedDirectoryPath(""))
58+
})
59+
}
60+
1861
func TestOS(t *testing.T) {
1962
t.Parallel()
2063

0 commit comments

Comments
 (0)