Can't Import Glide Using gopkg.in/yaml.v2

Troubleshooting guide for Go YAML import issues with Glide dependency manager

Last updated: November 2024 7 min read

Introduction

When using Glide for Go dependency management, you may encounter issues importing gopkg.in/yaml.v2. This guide covers common causes and solutions for these import problems.

Common Error Messages

Error: Cannot find package

package gopkg.in/yaml.v2: cannot find package "gopkg.in/yaml.v2"

Error: Import path mismatch

import "gopkg.in/yaml.v2": cannot find package

Error: Glide dependency not found

[ERROR] Failed to retrieve gopkg.in/yaml.v2

Solutions

Solution 1: Add to glide.yaml

Ensure gopkg.in/yaml.v2 is properly listed in your glide.yaml:

package: your-package-name
import:
  - package: gopkg.in/yaml.v2
    version: ^2.4.0

Then run:

glide install
glide update

Solution 2: Use glide get

Add the dependency using Glide's get command:

# Add the package
glide get gopkg.in/yaml.v2

# This will update glide.yaml and glide.lock
# Then install dependencies
glide install

Solution 3: Specify version explicitly

In glide.yaml, specify the exact version:

import:
  - package: gopkg.in/yaml.v2
    version: v2.4.0  # Use specific version tag

Solution 4: Clean and reinstall

Remove vendor directory and reinstall:

# Remove vendor directory
rm -rf vendor/

# Clear Glide cache (if needed)
glide cache-clear

# Reinstall all dependencies
glide install

Solution 5: Check Go module compatibility

If using Go modules, ensure compatibility:

# If using Go modules (go.mod)
go get gopkg.in/yaml.v2@v2.4.0

# Or in go.mod
require gopkg.in/yaml.v2 v2.4.0

Note: If your project uses Go modules, consider migrating from Glide to Go modules.

Complete glide.yaml Example

A properly configured glide.yaml with yaml.v2:

package: github.com/yourusername/yourproject
import:
  - package: gopkg.in/yaml.v2
    version: ^2.4.0
  - package: github.com/some/other-package
    version: ^1.0.0
testImport:
  - package: github.com/stretchr/testify
    version: ^1.7.0

Alternative: Migrating to Go Modules

If you're experiencing persistent issues with Glide, consider migrating to Go modules (the official Go dependency management system):

Step 1: Initialize Go modules

# Initialize go.mod
go mod init github.com/yourusername/yourproject

# This will read glide.yaml and create go.mod

Step 2: Add yaml.v2 dependency

# Add yaml.v2
go get gopkg.in/yaml.v2@v2.4.0

# Or edit go.mod directly
require gopkg.in/yaml.v2 v2.4.0

Step 3: Update imports (if needed)

Go modules handle the import path automatically:

import "gopkg.in/yaml.v2"

// Usage remains the same
var data map[string]interface{}
err := yaml.Unmarshal([]byte(yamlData), &data)

Verifying the Installation

Check Glide dependencies

# List installed packages
glide list

# Check specific package
glide tree | grep yaml

Test the import

# Create a test file
package main

import (
    "fmt"
    "gopkg.in/yaml.v2"
)

func main() {
    fmt.Println("yaml.v2 imported successfully")
}

# Build to verify
go build

Common Issues

Issue: Network connectivity

Ensure you can access gopkg.in:

# Test connectivity
curl -I https://gopkg.in/yaml.v2

# Check DNS resolution
nslookup gopkg.in

Issue: GOPATH not set

Ensure GOPATH is properly configured:

# Check GOPATH
echo $GOPATH

# Set GOPATH if needed (Linux/macOS)
export GOPATH=$HOME/go

# Windows
set GOPATH=C:\Users\YourName\go

Issue: Version conflicts

Check for version conflicts in glide.lock:

# Review glide.lock
cat glide.lock | grep yaml

# Update to resolve conflicts
glide update

Best Practices

  • Pin versions - Use specific version tags in glide.yaml for reproducibility
  • Commit glide.lock - Include glide.lock in version control for consistent builds
  • Consider Go modules - For new projects, use Go modules instead of Glide
  • Regular updates - Run glide update periodically to get security fixes

Related Articles