@@ -661,29 +661,38 @@ func (m *Manager) ComputeUpdateDiff() (*UpdateDiff, error) {
661661 return diff , nil
662662}
663663
664- // DownloadLibrary downloads a library to the cache directory
664+ // DownloadLibrary downloads a library to the default (flat) libs directory
665665func (m * Manager ) DownloadLibrary (ctx context.Context , lib Library , progressFn func (downloaded , total int64 )) error {
666666 m .mu .Lock ()
667667 defer m .mu .Unlock ()
668668
669- return m .downloadLibraryUnsafe (ctx , lib , progressFn )
669+ return m .downloadLibraryToDir (ctx , lib , m . paths . LibsDir () , progressFn )
670670}
671671
672- func (m * Manager ) downloadLibraryUnsafe (ctx context.Context , lib Library , progressFn func (downloaded , total int64 )) error {
672+ // DownloadLibraryToDir downloads a library to a specific libs directory.
673+ func (m * Manager ) DownloadLibraryToDir (ctx context.Context , lib Library , libsDir string , progressFn func (downloaded , total int64 )) error {
674+ m .mu .Lock ()
675+ defer m .mu .Unlock ()
676+
677+ return m .downloadLibraryToDir (ctx , lib , libsDir , progressFn )
678+ }
679+
680+ // downloadLibraryToDir downloads a library to a specific libs directory.
681+ // Shared libraries (.so/.dll) go to libsDir; binaries go to cache root.
682+ func (m * Manager ) downloadLibraryToDir (ctx context.Context , lib Library , libsDir string , progressFn func (downloaded , total int64 )) error {
673683 // Ensure cache directory exists
674684 cacheDir := m .paths .CacheDir ()
675685 if err := os .MkdirAll (cacheDir , 0755 ); err != nil {
676686 return fmt .Errorf ("failed to create cache directory: %w" , err )
677687 }
678688
679689 // Ensure libs directory exists for shared libraries
680- libsDir := m .paths .LibsDir ()
681690 if err := os .MkdirAll (libsDir , 0755 ); err != nil {
682691 return fmt .Errorf ("failed to create libs directory: %w" , err )
683692 }
684693
685694 // Destination path - shared libraries go to libs dir, binaries to cache dir
686- destPath := m .GetLibraryPath (lib .Name )
695+ destPath := m .GetLibraryPathInDir (lib .Name , libsDir )
687696 tmpPath := destPath + ".tmp"
688697
689698 // Check if already downloaded with correct version
@@ -866,7 +875,7 @@ func (m *Manager) DownloadAllRequired(ctx context.Context, progressFn func(lib L
866875 }
867876 }
868877
869- if err := m .downloadLibraryUnsafe (ctx , lib , libProgressFn ); err != nil {
878+ if err := m .downloadLibraryToDir (ctx , lib , m . paths . LibsDir () , libProgressFn ); err != nil {
870879 result .Status = DownloadStatusFailed
871880 result .Error = err .Error ()
872881 klog .Errorf ("Failed to download library: name=%s error=%v" , lib .Name , err )
@@ -917,6 +926,15 @@ func (m *Manager) GetLibraryPath(name string) string {
917926 return filepath .Join (m .paths .CacheDir (), name )
918927}
919928
929+ // GetLibraryPathInDir returns the path to a library in a specific libs directory.
930+ // For shared libraries, uses the given libsDir; for binaries, uses cache root.
931+ func (m * Manager ) GetLibraryPathInDir (name string , libsDir string ) string {
932+ if isSharedLibrary (name ) {
933+ return filepath .Join (libsDir , name )
934+ }
935+ return filepath .Join (m .paths .CacheDir (), name )
936+ }
937+
920938// GetLibsDir returns the directory for .so/.dll library files
921939// This directory is used for LD_LIBRARY_PATH, ld.so.conf, and ld.so.preload
922940func (m * Manager ) GetLibsDir () string {
@@ -1115,9 +1133,28 @@ func (m *Manager) EnsureLibrariesByTypes(ctx context.Context, libTypes []string,
11151133
11161134// EnsureLibrariesByTypesForPlatform ensures ALL libraries of the specified types exist and are downloaded for a specific platform
11171135// targetOS and targetArch specify the target platform (e.g., "linux", "arm64")
1118- // If both are empty, uses the current platform
1119- // This is useful when running on macOS but needing Linux libraries for containers
1136+ // If both are empty, uses the current platform and the flat libs directory (agent/worker path).
1137+ // If targetOS is specified, uses arch-specific subdirectory (e.g., libs/linux-amd64/) to avoid
1138+ // collisions between different architectures (studio/use/launch path).
11201139func (m * Manager ) EnsureLibrariesByTypesForPlatform (ctx context.Context , libTypes []string , vendorSlug , targetOS , targetArch string , progressFn func (lib Library , downloaded , total int64 )) ([]Library , error ) {
1140+ // Resolve effective platform values
1141+ effectiveOS := targetOS
1142+ effectiveArch := targetArch
1143+ if effectiveOS == "" {
1144+ effectiveOS = runtime .GOOS
1145+ }
1146+ if effectiveArch == "" {
1147+ effectiveArch = runtime .GOARCH
1148+ }
1149+
1150+ // Determine the libs directory:
1151+ // - If targetOS was explicitly specified, use arch-specific subdir (studio/use/launch)
1152+ // - Otherwise, use flat libs dir (agent/worker)
1153+ libsDir := m .paths .LibsDir ()
1154+ if targetOS != "" {
1155+ libsDir = m .paths .LibsDirForPlatform (effectiveOS , effectiveArch )
1156+ }
1157+
11211158 // Ensure deps manifest exists and is up to date for the target platform
11221159 if err := m .ensureDepsManifestForPlatform (ctx , targetOS , targetArch ); err != nil {
11231160 return nil , err
@@ -1167,7 +1204,7 @@ func (m *Manager) EnsureLibrariesByTypesForPlatform(ctx context.Context, libType
11671204 var toDownload []Library
11681205
11691206 for _ , lib := range targetLibs {
1170- filePath := m .GetLibraryPath (lib .Name )
1207+ filePath := m .GetLibraryPathInDir (lib .Name , libsDir )
11711208 downloadedLib , exists := downloaded .Libraries [lib .Key ()]
11721209
11731210 needsDownload := false
@@ -1186,7 +1223,7 @@ func (m *Manager) EnsureLibrariesByTypesForPlatform(ctx context.Context, libType
11861223 }
11871224 }
11881225
1189- klog .V (4 ).Infof ("Libraries to download: %d out of %d total (libs will go to: %s)" , len (toDownload ), len (targetLibs ), m . paths . LibsDir () )
1226+ klog .V (4 ).Infof ("Libraries to download: %d out of %d total (libs will go to: %s)" , len (toDownload ), len (targetLibs ), libsDir )
11901227
11911228 // Download missing libraries
11921229 for _ , lib := range toDownload {
@@ -1196,8 +1233,8 @@ func (m *Manager) EnsureLibrariesByTypesForPlatform(ctx context.Context, libType
11961233 }
11971234 }
11981235
1199- klog .Infof ("Downloading library: name=%s version=%s type=%s" , lib .Name , lib .Version , lib .Type )
1200- if err := m .DownloadLibrary (ctx , lib , libProgressFn ); err != nil {
1236+ klog .Infof ("Downloading library: name=%s version=%s type=%s to=%s " , lib .Name , lib .Version , lib .Type , libsDir )
1237+ if err := m .DownloadLibraryToDir (ctx , lib , libsDir , libProgressFn ); err != nil {
12011238 return nil , fmt .Errorf ("failed to download library %s: %w" , lib .Name , err )
12021239 }
12031240 }
0 commit comments