Skip to content

Commit b6f2605

Browse files
committed
verbose logging to debug cache resolution
1 parent 40143ff commit b6f2605

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

server/server.go

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ type s3FS struct {
9898
}
9999

100100
func newS3FS(version string) *s3FS {
101+
log.Printf("[newS3FS] Creating filesystem for version: %s", version)
101102
return &s3FS{
102103
version: version,
103104
files: map[string]string{
@@ -115,7 +116,7 @@ func getFromCache(ctx context.Context, version, path string) *CacheEntry {
115116
cacheKey := fmt.Sprintf("%s%s", version, strings.TrimPrefix(path, "/"))
116117

117118
// Log S3 access for debugging
118-
// log.Printf("S3 lookup: bucket=%s, key=%s", s3Bucket, cacheKey)
119+
log.Printf("[getFromCache] S3 lookup: bucket=%s, key=%s, version=%s, path=%s", s3Bucket, cacheKey, version, path)
119120

120121
// Check cache first
121122
cacheMutex.RLock()
@@ -150,7 +151,7 @@ func getFromCache(ctx context.Context, version, path string) *CacheEntry {
150151
defer result.Body.Close()
151152
content, err := io.ReadAll(result.Body)
152153
if err == nil {
153-
// log.Printf("S3 file found: %s (%d bytes)", cacheKey, len(content))
154+
log.Printf("[getFromCache] S3 file found: %s (%d bytes)", cacheKey, len(content))
154155
entry := &CacheEntry{
155156
Exists: true,
156157
IsFile: true,
@@ -162,10 +163,10 @@ func getFromCache(ctx context.Context, version, path string) *CacheEntry {
162163
cacheMutex.Unlock()
163164
return entry
164165
} else {
165-
// log.Printf("S3 file read error for %s: %v", cacheKey, err)
166+
log.Printf("[getFromCache] S3 file read error for %s: %v", cacheKey, err)
166167
}
167168
} else {
168-
// log.Printf("S3 file fetch error for %s: %v", cacheKey, err)
169+
log.Printf("[getFromCache] S3 file fetch error for %s: %v", cacheKey, err)
169170
}
170171

171172
// Not a file, try as directory
@@ -184,7 +185,7 @@ func getFromCache(ctx context.Context, version, path string) *CacheEntry {
184185

185186
if err != nil {
186187
s3ListErrors.Inc()
187-
// log.Printf("S3 list error for key %s: %v", cacheKey, err)
188+
log.Printf("[getFromCache] S3 list error for key %s: %v", cacheKey, err)
188189
// Cache as non-existent with shorter TTL
189190
entry := &CacheEntry{
190191
Exists: false,
@@ -197,6 +198,7 @@ func getFromCache(ctx context.Context, version, path string) *CacheEntry {
197198
}
198199

199200
// Build directory entry
201+
log.Printf("[getFromCache] S3 list results for %s: %d objects, %d prefixes", cacheKey, len(listResult.Contents), len(listResult.CommonPrefixes))
200202
entry := &CacheEntry{
201203
Exists: false,
202204
IsFile: false,
@@ -235,6 +237,7 @@ func getFromCache(ctx context.Context, version, path string) *CacheEntry {
235237
}
236238

237239
// Cache the result
240+
log.Printf("[getFromCache] Caching result for %s: exists=%v, isFile=%v, files=%d, dirs=%d", cacheKey, entry.Exists, entry.IsFile, len(entry.Files), len(entry.Dirs))
238241
cacheMutex.Lock()
239242
cache.Add(cacheKey, entry)
240243
cacheMutex.Unlock()
@@ -256,15 +259,21 @@ func (fs *s3FS) FileExists(path string) bool {
256259
return false
257260
}
258261

262+
// Log the file access for debugging
263+
log.Printf("[FileExists] Checking path: %s (version: %s)", path, fs.version)
264+
259265
// Check S3 cache
260266
ctx := context.Background()
261267
entry := getFromCache(ctx, fs.version, path)
262-
return entry.Exists && entry.IsFile
268+
result := entry.Exists && entry.IsFile
269+
log.Printf("[FileExists] Path: %s -> exists=%v, isFile=%v", path, entry.Exists, entry.IsFile)
270+
return result
263271
}
264272

265273
func (fs *s3FS) ReadFile(path string) (string, bool) {
266274
// Check in-memory cache first
267275
if content, ok := fs.files[path]; ok {
276+
log.Printf("[ReadFile] Found in memory: %s (%d bytes)", path, len(content))
268277
return content, true
269278
}
270279

@@ -274,13 +283,16 @@ func (fs *s3FS) ReadFile(path string) (string, bool) {
274283
}
275284

276285
// Check S3 cache
286+
log.Printf("[ReadFile] Checking S3 for: %s (version: %s)", path, fs.version)
277287
ctx := context.Background()
278288
entry := getFromCache(ctx, fs.version, path)
279289
if !entry.Exists || !entry.IsFile {
290+
log.Printf("[ReadFile] Not found or not a file: %s (exists: %v, isFile: %v)", path, entry.Exists, entry.IsFile)
280291
return "", false
281292
}
282293

283294
contentStr := string(entry.Content)
295+
log.Printf("[ReadFile] Found in S3: %s (%d bytes)", path, len(contentStr))
284296
fs.files[path] = contentStr // Cache locally for this request
285297
return contentStr, true
286298
}
@@ -299,11 +311,14 @@ func (fs *s3FS) DirectoryExists(path string) bool {
299311
if path == "/" || path == "" {
300312
return true
301313
}
314+
log.Printf("[DirectoryExists] Checking: %s (version: %s)", path, fs.version)
302315

303316
// Check S3 cache
304317
ctx := context.Background()
305318
entry := getFromCache(ctx, fs.version, path)
306-
return entry.Exists && !entry.IsFile
319+
result := entry.Exists && !entry.IsFile
320+
log.Printf("[DirectoryExists] Path: %s -> exists=%v, isFile=%v, result=%v", path, entry.Exists, entry.IsFile, result)
321+
return result
307322
}
308323

309324
func (fs *s3FS) GetAccessibleEntries(path string) vfs.Entries {
@@ -448,7 +463,7 @@ func buildTypeScript(code string, version string) BuildResponse {
448463

449464
// Create virtual file resolver for esbuild
450465
resolver := func(path string) (api.OnLoadResult, error) {
451-
log.Printf("Resolving: %s", path)
466+
log.Printf("[esbuild resolver] Resolving: %s", path)
452467

453468
// Track package resolutions
454469
trackPackageResolution(path)
@@ -463,7 +478,7 @@ func buildTypeScript(code string, version string) BuildResponse {
463478
} else if strings.HasSuffix(path, ".json") {
464479
loader = api.LoaderJSON
465480
}
466-
log.Printf("Found exact path: %s", path)
481+
log.Printf("[esbuild resolver] Found exact path: %s (loader: %v)", path, loader)
467482
return api.OnLoadResult{
468483
Contents: &content,
469484
Loader: loader,
@@ -484,7 +499,7 @@ func buildTypeScript(code string, version string) BuildResponse {
484499
} else if ext == ".json" {
485500
loader = api.LoaderJSON
486501
}
487-
log.Printf("Found with extension: %s", testPath)
502+
log.Printf("[esbuild resolver] Found with extension: %s (loader: %v)", testPath, loader)
488503
return api.OnLoadResult{
489504
Contents: &content,
490505
Loader: loader,
@@ -590,15 +605,15 @@ func buildTypeScript(code string, version string) BuildResponse {
590605
} else if strings.HasSuffix(pattern, ".json") {
591606
loader = api.LoaderJSON
592607
}
593-
log.Printf("Found at: %s", pattern)
608+
log.Printf("[esbuild resolver] Found at: %s (loader: %v)", pattern, loader)
594609
return api.OnLoadResult{
595610
Contents: &content,
596611
Loader: loader,
597612
}, nil
598613
}
599614
}
600615

601-
log.Printf("Tried patterns: %v", patterns)
616+
log.Printf("[esbuild resolver] Failed to find %s. Tried patterns: %v", path, patterns)
602617
}
603618

604619
return api.OnLoadResult{}, fmt.Errorf("file not found: %s", path)
@@ -622,6 +637,7 @@ func buildTypeScript(code string, version string) BuildResponse {
622637
Name: "virtual-fs",
623638
Setup: func(pb api.PluginBuild) {
624639
pb.OnResolve(api.OnResolveOptions{Filter: ".*"}, func(args api.OnResolveArgs) (api.OnResolveResult, error) {
640+
log.Printf("[esbuild OnResolve] path=%s, importer=%s, kind=%v", args.Path, args.Importer, args.Kind)
625641
// Track package resolutions in esbuild plugin
626642
trackPackageResolution(args.Path)
627643

@@ -676,11 +692,13 @@ func buildTypeScript(code string, version string) BuildResponse {
676692
})
677693

678694
pb.OnLoad(api.OnLoadOptions{Filter: ".*", Namespace: "virtual"}, func(args api.OnLoadArgs) (api.OnLoadResult, error) {
695+
log.Printf("[esbuild OnLoad] path=%s, namespace=%s", args.Path, args.Namespace)
679696
return resolver(args.Path)
680697
})
681698

682699
// Handle react global transform
683700
pb.OnLoad(api.OnLoadOptions{Filter: ".*", Namespace: "use-crayon-react-global"}, func(args api.OnLoadArgs) (api.OnLoadResult, error) {
701+
log.Printf("[esbuild OnLoad] React global transform for: %s", args.Path)
684702
contents := "module.exports = _CRAYONCORE_$REACT"
685703
return api.OnLoadResult{
686704
Contents: &contents,

0 commit comments

Comments
 (0)