@@ -378,6 +378,82 @@ func TestTryLoadFromCache_NoFingerprint(t *testing.T) {
378378 }
379379}
380380
381+ func TestTryLoadFromCache_ForceRebuild (t * testing.T ) {
382+ td := t .TempDir ()
383+ oldFunc := cacheRootFunc
384+ cacheRootFunc = func () string { return td }
385+ defer func () { cacheRootFunc = oldFunc }()
386+
387+ ctx := & context {
388+ conf : & packages.Config {},
389+ buildConf : & Config {
390+ Goos : "darwin" ,
391+ Goarch : "arm64" ,
392+ ForceRebuild : true , // Force rebuild enabled
393+ },
394+ crossCompile : crosscompile.Export {
395+ LLVMTarget : "arm64-apple-darwin" ,
396+ },
397+ }
398+
399+ // Create a fake cache entry
400+ pkg := & aPackage {
401+ Package : & packages.Package {
402+ PkgPath : "example.com/cached" ,
403+ Name : "cached" ,
404+ },
405+ Fingerprint : "test123" ,
406+ Manifest : func () string {
407+ m := newManifestBuilder ()
408+ m .env .Goos = "darwin"
409+ m .pkg .PkgPath = "example.com/cached"
410+ return m .Build ()
411+ }(),
412+ }
413+
414+ // Create a temporary .o file
415+ objFile , err := os .CreateTemp (td , "test-*.o" )
416+ if err != nil {
417+ t .Fatalf ("CreateTemp: %v" , err )
418+ }
419+ objFile .WriteString ("fake object file" )
420+ objFile .Close ()
421+
422+ pkg .LLFiles = []string {objFile .Name ()}
423+
424+ // First save to cache
425+ ctx .buildConf .ForceRebuild = false
426+ if err := ctx .saveToCache (pkg ); err != nil {
427+ t .Fatalf ("saveToCache: %v" , err )
428+ }
429+
430+ // Verify cache exists
431+ cm := ctx .ensureCacheManager ()
432+ paths := cm .PackagePaths ("arm64-apple-darwin" , "example.com/cached" , "test123" )
433+ if _ , err := os .Stat (paths .Archive ); err != nil {
434+ t .Fatalf ("cache should exist: %v" , err )
435+ }
436+
437+ // Now enable ForceRebuild and try to load
438+ ctx .buildConf .ForceRebuild = true
439+
440+ // Clear LLFiles to verify it's not loaded from cache
441+ pkg .LLFiles = nil
442+ pkg .CacheHit = false
443+
444+ if ctx .tryLoadFromCache (pkg ) {
445+ t .Error ("should return false when ForceRebuild is enabled, even with valid cache" )
446+ }
447+
448+ if pkg .CacheHit {
449+ t .Error ("CacheHit should remain false when ForceRebuild is enabled" )
450+ }
451+
452+ if len (pkg .LLFiles ) > 0 {
453+ t .Error ("LLFiles should not be populated when ForceRebuild is enabled" )
454+ }
455+ }
456+
381457func TestSaveToCache_MainPackage (t * testing.T ) {
382458 td := t .TempDir ()
383459 oldFunc := cacheRootFunc
0 commit comments