# EnvSwitch Troubleshooting Guide
This guide helps you diagnose and resolve common issues with EnvSwitch.
## Table of Contents
- [Installation Issues](#installation-issues)
- [Configuration Problems](#configuration-problems)
- [Import/Export Issues](#importexport-issues)
- [Shell Integration Problems](#shell-integration-problems)
- [Performance Issues](#performance-issues)
- [File System Issues](#file-system-issues)
- [Error Messages](#error-messages)
- [Recovery Procedures](#recovery-procedures)
## Installation Issues
### Command Not Found
**Problem:** `envswitch: command not found`
**Solutions:**
1. **Check if EnvSwitch is installed:**
```bash
which envswitch
ls -la /usr/local/bin/envswitch
```
2. **Add to PATH if installed elsewhere:**
```bash
export PATH="$PATH:/path/to/envswitch"
```
3. **Reinstall from source:**
```bash
git clone https://github.com/soddygo/envswitch
cd envswitch
cargo build --release
sudo cp target/release/envswitch /usr/local/bin/
```
### Permission Denied
**Problem:** `Permission denied` when running EnvSwitch
**Solutions:**
1. **Make executable:**
```bash
chmod +x /usr/local/bin/envswitch
```
2. **Check ownership:**
```bash
ls -la /usr/local/bin/envswitch
sudo chown $USER:$USER /usr/local/bin/envswitch
```
## Configuration Problems
### Configuration Not Found
**Problem:** `Configuration 'myconfig' not found`
**Diagnosis:**
```bash
# List all configurations
envswitch list
# Check for similar names
**Solutions:**
1. **Check spelling:**
```bash
envswitch use myconfg ```
2. **Create the configuration:**
```bash
envswitch set myconfig -e KEY=value
```
3. **Import from backup:**
```bash
envswitch import backup.json --merge
```
### Configuration File Corruption
**Problem:** `JSON parsing error` or corrupted configuration file
**Diagnosis:**
```bash
# Check configuration file
ls -la ~/.config/envswitch/config.json
```
**Solutions:**
1. **Restore from backup:**
```bash
ls -la ~/.config/envswitch/backups/
envswitch import ~/.config/envswitch/backups/latest.json --force
```
2. **Manual repair:**
```bash
cp ~/.config/envswitch/config.json ~/.config/envswitch/config.json.corrupted
echo '{"configs": {}, "active_config": null}' > ~/.config/envswitch/config.json
```
3. **Start fresh:**
```bash
rm ~/.config/envswitch/config.json
envswitch list ```
### Invalid Configuration Names
**Problem:** `Invalid configuration name` error
**Diagnosis:**
```bash
# Check what characters are causing issues
envswitch set "my config" -e KEY=value # Spaces not allowed
envswitch set "my-config!" -e KEY=value # Special chars not allowed
```
**Solutions:**
1. **Use valid characters only:**
```bash
envswitch set my-config -e KEY=value
envswitch set my_config -e KEY=value
envswitch set myconfig123 -e KEY=value
envswitch set "my config" envswitch set "my.config" envswitch set "my@config" ```
2. **Rename existing configurations:**
```bash
envswitch export -c "invalid-name" -o temp.json
envswitch delete "invalid-name" --force
envswitch import temp.json
```
## Import/Export Issues
### File Format Errors
**Problem:** `Invalid JSON format` or `Format validation failed`
**Diagnosis:**
```bash
# Check file format
file myconfig.json
head -n 5 myconfig.json
# Validate JSON
python -m json.tool myconfig.json > /dev/null
```
**Solutions:**
1. **Fix JSON syntax:**
```bash
python -m json.tool myconfig.json > fixed.json
envswitch import fixed.json
```
2. **Convert from other formats:**
```bash
envswitch import myconfig.env ```
3. **Use dry-run to preview:**
```bash
envswitch import myconfig.json --dry-run --verbose
```
### Import Conflicts
**Problem:** `Configuration conflicts found` during import
**Diagnosis:**
```bash
# Preview what would be imported
envswitch import configs.json --dry-run
```
**Solutions:**
1. **Use merge mode:**
```bash
envswitch import configs.json --merge --backup
```
2. **Use force mode:**
```bash
envswitch import configs.json --force --backup
```
3. **Resolve manually:**
```bash
envswitch import configs.json --dry-run
envswitch delete conflicting-config --force
envswitch import configs.json
```
### Export Failures
**Problem:** Export command fails or produces empty files
**Diagnosis:**
```bash
# Check if configurations exist
envswitch list
# Check output directory permissions
ls -la $(dirname output-file.json)
# Try with verbose output
envswitch export -o test.json --verbose
```
**Solutions:**
1. **Check permissions:**
```bash
mkdir -p $(dirname output-file.json)
touch output-file.json && rm output-file.json
```
2. **Use different output location:**
```bash
envswitch export -o ~/configs.json
```
3. **Export specific configurations:**
```bash
envswitch export -c "config1,config2" -o partial.json
```
## Shell Integration Problems
### Commands Not Working
**Problem:** `eval "$(envswitch use config)"` doesn't set variables
**Diagnosis:**
```bash
# Test command generation
envswitch use myconfig --dry-run
# Check shell type
echo $SHELL
# Test without eval
envswitch use myconfig
```
**Solutions:**
1. **Use correct shell syntax:**
```bash
eval "$(envswitch use myconfig)"
eval (envswitch use myconfig)
```
2. **Check shell detection:**
```bash
SHELL=/bin/zsh envswitch use myconfig
```
3. **Manual variable setting:**
```bash
envswitch use myconfig --dry-run > temp_vars.sh
source temp_vars.sh
rm temp_vars.sh
```
### Shell Detection Issues
**Problem:** Wrong shell commands generated
**Diagnosis:**
```bash
# Check current shell
echo $SHELL
ps -p $$
# Test with different shells
envswitch use myconfig --dry-run
```
**Solutions:**
1. **Set SHELL environment variable:**
```bash
export SHELL=/usr/local/bin/fish
envswitch use myconfig
```
2. **Use shell-specific commands:**
```bash
envswitch init --shell zsh
envswitch init --shell fish
envswitch init --shell bash
```
### Alias and Function Issues
**Problem:** Shell aliases or functions not working
**Diagnosis:**
```bash
# Check if aliases are defined
# Check function definitions
type switch-to
```
**Solutions:**
1. **Reload shell configuration:**
```bash
source ~/.zshrc source ~/.bashrc exec fish ```
2. **Add aliases manually:**
```bash
alias es='envswitch'
alias esl='envswitch list'
alias esu='envswitch use'
```
## Performance Issues
### Slow Operations
**Problem:** EnvSwitch commands are slow
**Diagnosis:**
```bash
# Time operations
time envswitch list
time envswitch export -o test.json
# Check configuration file size
ls -lh ~/.config/envswitch/config.json
# Count configurations
**Solutions:**
1. **Clean up old configurations:**
```bash
envswitch delete old-config --force
envswitch export -o compact.json
mv ~/.config/envswitch/config.json ~/.config/envswitch/config.json.backup
envswitch import compact.json --force
```
2. **Use specific operations:**
```bash
envswitch export -c "config1,config2" -o subset.json
envswitch export -o configs.json ```
3. **Check disk space:**
```bash
df -h ~/.config/
```
### Memory Issues
**Problem:** High memory usage or out of memory errors
**Diagnosis:**
```bash
# Check configuration file size
du -h ~/.config/envswitch/
# Monitor memory usage
top -p $(pgrep envswitch)
```
**Solutions:**
1. **Reduce configuration size:**
```bash
envswitch export -c "large-config" -o large.json
envswitch import smaller-configs.json
```
2. **Clean up backups:**
```bash
find ~/.config/envswitch/backups -name "*.json" -mtime +30 -delete
```
## File System Issues
### Permission Denied
**Problem:** `Permission denied` accessing configuration files
**Diagnosis:**
```bash
# Check permissions
ls -la ~/.config/envswitch/
ls -la ~/.config/envswitch/config.json
# Check ownership
stat ~/.config/envswitch/config.json
```
**Solutions:**
1. **Fix permissions:**
```bash
chmod 700 ~/.config/envswitch/
chmod 600 ~/.config/envswitch/config.json
```
2. **Fix ownership:**
```bash
chown -R $USER:$USER ~/.config/envswitch/
```
3. **Recreate directory:**
```bash
cp ~/.config/envswitch/config.json ~/envswitch-backup.json
rm -rf ~/.config/envswitch/
mkdir -p ~/.config/envswitch/
envswitch import ~/envswitch-backup.json --force
```
### Disk Space Issues
**Problem:** `No space left on device` errors
**Diagnosis:**
```bash
# Check disk space
df -h ~/.config/
du -h ~/.config/envswitch/
```
**Solutions:**
1. **Clean up backup files:**
```bash
find ~/.config/envswitch/backups -name "*.json" -mtime +7 -delete
gzip ~/.config/envswitch/backups/*.json
```
2. **Move to different location:**
```bash
mv ~/.config/envswitch ~/Documents/envswitch-backup
ln -s ~/Documents/envswitch-backup ~/.config/envswitch
```
## Error Messages
### Common Error Messages and Solutions
#### "Configuration file is corrupted"
```bash
# Restore from backup
envswitch import ~/.config/envswitch/backups/latest.json --force
# Or start fresh
rm ~/.config/envswitch/config.json
envswitch list
```
#### "Invalid environment variable name"
```bash
# Check variable name format
envswitch set myconfig -e "VALID_NAME=value" # ✅ Good
envswitch set myconfig -e "invalid-name=value" # ❌ Bad (hyphen)
```
#### "Import file not found"
```bash
# Check file path
ls -la import-file.json
# Use absolute path
envswitch import /full/path/to/import-file.json
```
#### "Export directory not writable"
```bash
# Check directory permissions
ls -la $(dirname output-file.json)
# Create directory if needed
mkdir -p $(dirname output-file.json)
```
#### "Shell detection failed"
```bash
# Set SHELL environment variable
export SHELL=/usr/local/bin/zsh
envswitch use myconfig
```
## Recovery Procedures
### Complete System Recovery
If EnvSwitch is completely broken:
1. **Backup current state:**
```bash
cp -r ~/.config/envswitch ~/.config/envswitch.backup
```
2. **Reinstall EnvSwitch:**
```bash
rm /usr/local/bin/envswitch
cargo install --git https://github.com/soddygo/envswitch --force
```
3. **Restore configurations:**
```bash
envswitch import ~/.config/envswitch.backup/config.json --force
envswitch import ~/backups/envswitch-backup.json --force
```
### Partial Recovery
If only some configurations are lost:
1. **Check what's available:**
```bash
envswitch list --verbose
```
2. **Restore from backup:**
```bash
envswitch import backup.json --merge --backup
```
3. **Recreate missing configurations:**
```bash
envswitch set missing-config -e KEY=value -d "Recreated configuration"
```
### Emergency Procedures
If you need to quickly restore a working environment:
1. **Create minimal working configuration:**
```bash
envswitch set emergency \
-e PATH=$PATH \
-e HOME=$HOME \
-e USER=$USER \
-d "Emergency configuration"
```
2. **Use system environment as base:**
```bash
env > current-env.txt
```
3. **Quick restore from known good state:**
```bash
rm ~/.config/envswitch/config.json
envswitch import known-good-backup.json --force
```
## Getting Help
### Diagnostic Information
When reporting issues, include:
```bash
# System information
uname -a
echo $SHELL
# EnvSwitch version
envswitch --version
# Configuration status
envswitch list --verbose
ls -la ~/.config/envswitch/
# Error output with verbose mode
envswitch command --verbose 2>&1
```
### Log Files
EnvSwitch doesn't create log files by default, but you can capture output:
```bash
# Capture all output
envswitch command --verbose > envswitch.log 2>&1
# Monitor in real-time
### Community Support
- Check the GitHub issues for similar problems
- Include diagnostic information when reporting bugs
- Use verbose mode output to help with troubleshooting
- Provide minimal reproduction steps