Compare commits

...

1 Commits

Author SHA1 Message Date
jmorganca
91a3b77553 x/imagegen: respect OLLAMA_MODELS environment variable
Use envconfig.Models() instead of hardcoded ~/.ollama/models path
for manifest and blob directories.
2026-01-20 12:46:20 -08:00
2 changed files with 33 additions and 30 deletions

View File

@@ -6,8 +6,9 @@ import (
"io"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/ollama/ollama/envconfig"
)
// ManifestLayer represents a layer in the manifest.
@@ -32,33 +33,6 @@ type ModelManifest struct {
BlobDir string
}
// DefaultBlobDir returns the default blob storage directory.
func DefaultBlobDir() string {
home, err := os.UserHomeDir()
if err != nil {
home = "."
}
switch runtime.GOOS {
case "darwin":
return filepath.Join(home, ".ollama", "models", "blobs")
case "linux":
return filepath.Join(home, ".ollama", "models", "blobs")
case "windows":
return filepath.Join(home, ".ollama", "models", "blobs")
default:
return filepath.Join(home, ".ollama", "models", "blobs")
}
}
// DefaultManifestDir returns the default manifest storage directory.
func DefaultManifestDir() string {
home, err := os.UserHomeDir()
if err != nil {
home = "."
}
return filepath.Join(home, ".ollama", "models", "manifests")
}
// LoadManifest loads a manifest for the given model name.
// Model name format: "modelname" or "modelname:tag" or "host/namespace/name:tag"
func LoadManifest(modelName string) (*ModelManifest, error) {
@@ -76,7 +50,7 @@ func LoadManifest(modelName string) (*ModelManifest, error) {
return &ModelManifest{
Manifest: &manifest,
BlobDir: DefaultBlobDir(),
BlobDir: filepath.Join(envconfig.Models(), "blobs"),
}, nil
}
@@ -107,7 +81,7 @@ func resolveManifestPath(modelName string) string {
name = parts[1]
}
return filepath.Join(DefaultManifestDir(), host, namespace, name, tag)
return filepath.Join(envconfig.Models(), "manifests", host, namespace, name, tag)
}
// BlobPath returns the full path to a blob given its digest.

View File

@@ -0,0 +1,29 @@
package imagegen
import (
"path/filepath"
"testing"
"github.com/ollama/ollama/envconfig"
)
func TestLoadManifestRespectsOLLAMA_MODELS(t *testing.T) {
t.Setenv("OLLAMA_MODELS", "/custom/models/path")
// Verify envconfig.Models() returns our custom path
if got := envconfig.Models(); got != "/custom/models/path" {
t.Fatalf("envconfig.Models() = %q, want %q", got, "/custom/models/path")
}
// LoadManifest will fail (no manifest exists), but we can verify
// the error message contains the custom path
_, err := LoadManifest("test-model")
if err == nil {
t.Fatal("expected error, got nil")
}
expectedPath := filepath.Join("/custom/models/path", "manifests", "registry.ollama.ai", "library", "test-model", "latest")
if got := err.Error(); got != "read manifest: open "+expectedPath+": no such file or directory" {
t.Errorf("error = %q, want path to contain %q", got, expectedPath)
}
}