Skip to content

Commit da09eb6

Browse files
authored
Make repo package funcs lazy, not broken (#2413)
1 parent a7b5685 commit da09eb6

19 files changed

Lines changed: 81 additions & 57 deletions

File tree

internal/api/encoder/encoder_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func TestEncodeSourceFileWithUnicodeEscapes(t *testing.T) {
5555

5656
func BenchmarkEncodeSourceFile(b *testing.B) {
5757
repo.SkipIfNoTypeScriptSubmodule(b)
58-
filePath := filepath.Join(repo.TypeScriptSubmodulePath, "src/compiler/checker.ts")
58+
filePath := filepath.Join(repo.TypeScriptSubmodulePath(), "src/compiler/checker.ts")
5959
fileContent, err := os.ReadFile(filePath)
6060
assert.NilError(b, err)
6161
sourceFile := parser.ParseSourceFile(ast.SourceFileParseOptions{

internal/astnav/tokens_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
)
2121

2222
var testFiles = []string{
23-
filepath.Join(repo.TypeScriptSubmodulePath, "src/services/mapCode.ts"),
23+
filepath.Join(repo.TypeScriptSubmodulePath(), "src/services/mapCode.ts"),
2424
}
2525

2626
func TestGetTokenAtPosition(t *testing.T) {

internal/bundled/generate.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import (
2121
)
2222

2323
var (
24-
libInputDir = filepath.Join(repo.TypeScriptSubmodulePath, "src", "lib")
25-
copyrightNotice = filepath.Join(repo.TypeScriptSubmodulePath, "scripts", "CopyrightNotice.txt")
24+
libInputDir = filepath.Join(repo.TypeScriptSubmodulePath(), "src", "lib")
25+
copyrightNotice = filepath.Join(repo.TypeScriptSubmodulePath(), "scripts", "CopyrightNotice.txt")
2626
)
2727

2828
func main() {

internal/checker/checker_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func BenchmarkNewChecker(b *testing.B) {
6666
fs := osvfs.FS()
6767
fs = bundled.WrapFS(fs)
6868

69-
rootPath := tspath.CombinePaths(tspath.NormalizeSlashes(repo.TypeScriptSubmodulePath), "src", "compiler")
69+
rootPath := tspath.CombinePaths(tspath.NormalizeSlashes(repo.TypeScriptSubmodulePath()), "src", "compiler")
7070

7171
host := compiler.NewCompilerHost(rootPath, fs, bundled.LibPath(), nil, nil)
7272
parsed, errors := tsoptions.GetParsedCommandLineOfConfigFile(tspath.CombinePaths(rootPath, "tsconfig.json"), &core.CompilerOptions{}, nil, host, nil)

internal/compiler/program_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ func BenchmarkNewProgram(b *testing.B) {
292292
b.Run("compiler", func(b *testing.B) {
293293
repo.SkipIfNoTypeScriptSubmodule(b)
294294

295-
rootPath := tspath.NormalizeSlashes(filepath.Join(repo.TypeScriptSubmodulePath, "src", "compiler"))
295+
rootPath := tspath.NormalizeSlashes(filepath.Join(repo.TypeScriptSubmodulePath(), "src", "compiler"))
296296

297297
fs := osvfs.FS()
298298
fs = bundled.WrapFS(fs)

internal/diagnostics/generate.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func main() {
8181
return
8282
}
8383

84-
rawDiagnosticMessages := readRawMessages(filepath.Join(repo.TypeScriptSubmodulePath, "src", "compiler", "diagnosticMessages.json"))
84+
rawDiagnosticMessages := readRawMessages(filepath.Join(repo.TypeScriptSubmodulePath(), "src", "compiler", "diagnosticMessages.json"))
8585

8686
_, filename, _, ok := runtime.Caller(0)
8787
if !ok {
@@ -198,12 +198,12 @@ func generateLocalizations(knownKeys map[string]bool, locDir string) *bytes.Buff
198198
}
199199

200200
// Generate locale maps
201-
localeFiles, err := filepath.Glob(filepath.Join(repo.TypeScriptSubmodulePath, "src", "loc", "lcl", "*", "diagnosticMessages", "diagnosticMessages.generated.json.lcl"))
201+
localeFiles, err := filepath.Glob(filepath.Join(repo.TypeScriptSubmodulePath(), "src", "loc", "lcl", "*", "diagnosticMessages", "diagnosticMessages.generated.json.lcl"))
202202
if err != nil {
203203
log.Fatalf("failed to find locale files: %v", err)
204204
}
205205
if len(localeFiles) == 0 {
206-
log.Fatalf("no locale files found in %s", filepath.Join(repo.TypeScriptSubmodulePath, "src", "loc", "lcl"))
206+
log.Fatalf("no locale files found in %s", filepath.Join(repo.TypeScriptSubmodulePath(), "src", "loc", "lcl"))
207207
}
208208
slices.Sort(localeFiles)
209209

internal/format/api_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func TestFormat(t *testing.T) {
5151
InsertSpaceBeforeTypeAnnotation: core.TSTrue,
5252
}, "\n")
5353
repo.SkipIfNoTypeScriptSubmodule(t)
54-
filePath := filepath.Join(repo.TypeScriptSubmodulePath, "src/compiler/checker.ts")
54+
filePath := filepath.Join(repo.TypeScriptSubmodulePath(), "src/compiler/checker.ts")
5555
fileContent, err := os.ReadFile(filePath)
5656
assert.NilError(t, err)
5757
text := string(fileContent)
@@ -80,7 +80,7 @@ func BenchmarkFormat(b *testing.B) {
8080
InsertSpaceBeforeTypeAnnotation: core.TSTrue,
8181
}, "\n")
8282
repo.SkipIfNoTypeScriptSubmodule(b)
83-
filePath := filepath.Join(repo.TypeScriptSubmodulePath, "src/compiler/checker.ts")
83+
filePath := filepath.Join(repo.TypeScriptSubmodulePath(), "src/compiler/checker.ts")
8484
fileContent, err := os.ReadFile(filePath)
8585
assert.NilError(b, err)
8686
text := string(fileContent)

internal/packagejson/packagejson_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import (
1717
)
1818

1919
var packageJsonFixtures = []filefixture.Fixture{
20-
filefixture.FromFile("package.json", filepath.Join(repo.RootPath, "package.json")),
21-
filefixture.FromFile("date-fns.json", filepath.Join(repo.TestDataPath, "fixtures", "packagejson", "date-fns.json")),
20+
filefixture.FromFile("package.json", filepath.Join(repo.RootPath(), "package.json")),
21+
filefixture.FromFile("date-fns.json", filepath.Join(repo.TestDataPath(), "fixtures", "packagejson", "date-fns.json")),
2222
}
2323

2424
func BenchmarkPackageJSON(b *testing.B) {

internal/parser/parser_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func FuzzParser(f *testing.F) {
106106
}
107107

108108
for _, test := range tests {
109-
root := filepath.Join(repo.TypeScriptSubmodulePath, test)
109+
root := filepath.Join(repo.TypeScriptSubmodulePath(), test)
110110

111111
for file := range allParsableFiles(f, root) {
112112
sourceText, err := os.ReadFile(file.path)
@@ -117,9 +117,9 @@ func FuzzParser(f *testing.F) {
117117
}
118118

119119
testDirs := []string{
120-
filepath.Join(repo.TypeScriptSubmodulePath, "tests/cases/compiler"),
121-
filepath.Join(repo.TypeScriptSubmodulePath, "tests/cases/conformance"),
122-
filepath.Join(repo.TestDataPath, "tests/cases/compiler"),
120+
filepath.Join(repo.TypeScriptSubmodulePath(), "tests/cases/compiler"),
121+
filepath.Join(repo.TypeScriptSubmodulePath(), "tests/cases/conformance"),
122+
filepath.Join(repo.TestDataPath(), "tests/cases/compiler"),
123123
}
124124

125125
for _, testDir := range testDirs {

internal/repo/paths.go

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,63 @@ import (
44
"os"
55
"path/filepath"
66
"runtime"
7+
"strings"
78
"sync"
89
)
910

10-
var (
11-
RootPath string
12-
TypeScriptSubmodulePath string
13-
TestDataPath string
14-
)
15-
16-
func init() {
11+
var rootPath = sync.OnceValue(func() string {
1712
_, filename, _, ok := runtime.Caller(0)
1813
if !ok {
1914
panic("could not get current filename")
2015
}
2116
filename = filepath.FromSlash(filename) // runtime.Caller always returns forward slashes; https://go.dev/issues/3335, https://go.dev/cl/603275
22-
RootPath = findGoMod(filepath.Dir(filename))
23-
TypeScriptSubmodulePath = filepath.Join(RootPath, "_submodules", "TypeScript")
24-
TestDataPath = filepath.Join(RootPath, "testdata")
25-
}
2617

27-
func findGoMod(dir string) string {
28-
root := filepath.VolumeName(dir)
29-
for dir != root {
18+
if strings.HasPrefix(filename, "github.com/") {
19+
panic("repo root cannot be found when built with -trimpath")
20+
}
21+
22+
if !filepath.IsAbs(filename) {
23+
panic(filename + " is not an absolute path")
24+
}
25+
26+
root := filepath.VolumeName(filename) + string(filepath.Separator)
27+
28+
dir := filepath.Dir(filename)
29+
for {
3030
if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
3131
return dir
3232
}
33+
if dir == root {
34+
break
35+
}
3336
dir = filepath.Dir(dir)
3437
}
35-
panic("could not find go.mod")
38+
39+
panic("could not find go.mod above " + filename)
40+
})
41+
42+
func RootPath() string {
43+
return rootPath()
44+
}
45+
46+
var typeScriptSubmodulePath = sync.OnceValue(func() string {
47+
return filepath.Join(rootPath(), "_submodules", "TypeScript")
48+
})
49+
50+
func TypeScriptSubmodulePath() string {
51+
return typeScriptSubmodulePath()
52+
}
53+
54+
var testDataPath = sync.OnceValue(func() string {
55+
return filepath.Join(rootPath(), "testdata")
56+
})
57+
58+
func TestDataPath() string {
59+
return testDataPath()
3660
}
3761

3862
var typeScriptSubmoduleExists = sync.OnceValue(func() bool {
39-
p := filepath.Join(TypeScriptSubmodulePath, "package.json")
63+
p := filepath.Join(typeScriptSubmodulePath(), "package.json")
4064
if _, err := os.Stat(p); err != nil {
4165
if os.IsNotExist(err) {
4266
return false

0 commit comments

Comments
 (0)