Skip to content

Commit 5579ba7

Browse files
committed
fix: MacOS conda mamba referencing
1 parent a864760 commit 5579ba7

File tree

2 files changed

+269
-60
lines changed

2 files changed

+269
-60
lines changed

scripts/activate_workspace.sh

Lines changed: 169 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@
1717
# env_name Optional. Name of the environment to activate.
1818
# Default: ros_env (for both venv and conda environments)
1919
# ros_distro Optional. ROS2 distribution to use.
20-
# Default: humble
20+
# Default: jazzy
2121
#
2222
# EXAMPLES:
23-
# # Use defaults (ros_env, humble)
23+
# # Use defaults (ros_env, jazzy)
2424
# source activate_workspace.sh
2525
#
2626
# # Use specific environment name
2727
# source activate_workspace.sh my_ros_env
2828
#
29-
# # Use specific ROS distribution
29+
# # Use specific ROS distribution (e.g., humble)
3030
# source activate_workspace.sh ros_env humble
3131
#
3232
# # Run from any directory
@@ -57,12 +57,18 @@ fi
5757

5858
# Default values
5959
DEFAULT_ENV_NAME="ros_env"
60-
DEFAULT_ROS_DISTRO="humble"
60+
DEFAULT_ROS_DISTRO="jazzy"
6161
ENV_NAME="${1:-$DEFAULT_ENV_NAME}"
6262
ROS_DISTRO="${2:-$DEFAULT_ROS_DISTRO}"
6363

64-
# Find the repository root
65-
REPO_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
64+
# Find the repository root (resolve absolute path, handling sourcing from any directory)
65+
# Use bash parameter expansion to avoid external commands
66+
SCRIPT_PATH="${BASH_SOURCE[0]}"
67+
# Get directory of script (equivalent to dirname)
68+
SCRIPT_DIR="${SCRIPT_PATH%/*}"
69+
# Convert to absolute path
70+
SCRIPT_DIR="$(cd "$SCRIPT_DIR" && pwd)"
71+
REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
6672

6773
# Define paths
6874
SCRIPTS_PATH="$REPO_ROOT/scripts"
@@ -133,31 +139,114 @@ check_macos_env() {
133139
return 0
134140
fi
135141

136-
if ! command -v mamba &> /dev/null && ! command -v conda &> /dev/null; then
137-
log_error "Neither mamba nor conda found!"
138-
echo ""
139-
echo "Please install miniforge first:"
140-
echo " curl -L -O https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-MacOSX-$(uname -m).sh"
141-
echo " bash Miniforge3-MacOSX-$(uname -m).sh"
142-
echo ""
143-
return 1
142+
# Check if mamba/conda commands are available in PATH
143+
if command -v mamba &> /dev/null || command -v conda &> /dev/null; then
144+
log_info "Conda/mamba found in PATH"
145+
146+
# Determine which manager to use
147+
local manager="mamba"
148+
if ! command -v mamba &> /dev/null; then
149+
manager="conda"
150+
fi
151+
152+
# Check if environment exists
153+
# Use absolute path to grep to avoid conda PATH issues
154+
# Match environment name with word boundaries
155+
if ! $manager env list | /usr/bin/grep -q "^\s*$ENV_NAME\s"; then
156+
log_error "Conda environment '$ENV_NAME' not found!"
157+
echo ""
158+
echo "It looks like you haven't run the initial setup yet."
159+
echo "Please run the setup script first:"
160+
echo " ./scripts/setup_workspace.sh --env-name $ENV_NAME --ros-distro $ROS_DISTRO"
161+
echo ""
162+
echo "This will create the mamba environment with RoboStack."
163+
return 1
164+
fi
165+
166+
return 0
144167
fi
145168

146-
local manager="mamba"
147-
if ! command -v mamba &> /dev/null; then
148-
manager="conda"
149-
fi
169+
# Commands not found in PATH - check if miniforge/conda is installed but not initialized
170+
log_info "Conda/mamba not found in PATH, searching for existing installations..."
150171

151-
if ! $manager env list | grep -q "\s$ENV_NAME\s"; then
152-
log_error "Conda environment '$ENV_NAME' not found!"
153-
echo ""
154-
echo "It looks like you haven't run the initial setup yet."
155-
echo "Please run the setup script first:"
156-
echo " ./scripts/setup_workspace.sh --env-name $ENV_NAME --ros-distro $ROS_DISTRO"
157-
echo ""
158-
echo "This will create the mamba environment with RoboStack."
159-
return 1
160-
fi
172+
local miniforge_paths=(
173+
"$HOME/miniforge3"
174+
"$HOME/mambaforge"
175+
"$HOME/miniconda3"
176+
"$HOME/anaconda3"
177+
)
178+
179+
for path in "${miniforge_paths[@]}"; do
180+
if [ -d "$path" ] && [ -f "$path/bin/conda" ]; then
181+
log_info "Found existing conda installation at: $path"
182+
log_info "Initializing conda for current shell session..."
183+
184+
# Save original PATH to restore system paths after conda initialization
185+
# Conda's hook may corrupt PATH during initialization
186+
local original_path="$PATH"
187+
188+
# Initialize conda for the current shell
189+
# Note: We always use conda for initialization (even if mamba exists)
190+
# because conda's shell hook is more reliable
191+
local shell_name="${SHELL##*/}"
192+
eval "$($path/bin/conda shell.$shell_name hook)"
193+
194+
# Ensure system paths are present after conda initialization
195+
# Conda may have removed them during its hook execution
196+
# Only add back system paths if they're missing
197+
if [[ ":$PATH:" != *":/usr/bin:"* ]]; then
198+
export PATH="$PATH:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
199+
fi
200+
201+
# Check if mamba is available after initialization
202+
if command -v mamba &> /dev/null; then
203+
log_success "Mamba is now available"
204+
elif command -v conda &> /dev/null; then
205+
log_success "Conda is now available"
206+
else
207+
log_error "Failed to initialize conda from $path"
208+
return 1
209+
fi
210+
211+
# Determine which manager to use
212+
local manager="mamba"
213+
if ! command -v mamba &> /dev/null; then
214+
manager="conda"
215+
fi
216+
217+
# Check if environment exists
218+
# Use absolute path to grep to avoid conda PATH issues
219+
# Match environment name with word boundaries
220+
if ! $manager env list | /usr/bin/grep -q "^\s*$ENV_NAME\s"; then
221+
log_error "Conda environment '$ENV_NAME' not found!"
222+
echo ""
223+
echo "It looks like you haven't run the initial setup yet."
224+
echo "Please run the setup script first:"
225+
echo " ./scripts/setup_workspace.sh --env-name $ENV_NAME --ros-distro $ROS_DISTRO"
226+
echo ""
227+
echo "This will create the mamba environment with RoboStack."
228+
return 1
229+
fi
230+
231+
log_info "For permanent access to conda/mamba in future shells, run:"
232+
log_info " $path/bin/conda init $shell_name"
233+
echo ""
234+
235+
return 0
236+
fi
237+
done
238+
239+
# No existing installation found
240+
log_error "Neither mamba nor conda found!"
241+
echo ""
242+
echo "Please install miniforge first:"
243+
echo " curl -L -O https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-MacOSX-$(uname -m).sh"
244+
echo " bash Miniforge3-MacOSX-$(uname -m).sh"
245+
echo ""
246+
echo "Or run the setup script which will install it automatically:"
247+
echo " ./scripts/setup_workspace.sh --env-name $ENV_NAME --ros-distro $ROS_DISTRO"
248+
echo ""
249+
return 1
161250
}
162251

163252
# Check if already in conda environment
@@ -206,18 +295,65 @@ activate_ubuntu() {
206295

207296
# Activate macOS environment
208297
activate_macos() {
298+
# Determine which manager to use
209299
local manager="mamba"
210300
if ! command -v mamba &> /dev/null; then
211-
manager="conda"
301+
if command -v conda &> /dev/null; then
302+
manager="conda"
303+
else
304+
log_error "Neither mamba nor conda available. This should not happen after check_macos_env."
305+
return 1
306+
fi
212307
fi
213308

214309
# Check if already in correct environment
215310
if check_conda_env_status; then
216311
log_info "[1/3] Already in conda environment '$ENV_NAME', skipping activation..."
217312
else
218313
log_info "[1/3] Activating conda environment '$ENV_NAME'..."
219-
eval "$($manager shell hook)"
220-
$manager activate "$ENV_NAME"
314+
315+
# If we're already in ANY conda environment, we need to ensure proper initialization
316+
if [[ -n "$CONDA_DEFAULT_ENV" ]]; then
317+
log_info "Switching from '$CONDA_DEFAULT_ENV' to '$ENV_NAME'..."
318+
319+
# Save PATH before activation to restore system paths if needed
320+
local pre_activation_path="$PATH"
321+
322+
# If in a conda environment, the shell is already initialized
323+
# Just activate using conda (more reliable than mamba for switching)
324+
conda activate "$ENV_NAME"
325+
326+
# Ensure system paths are present after activation
327+
if [[ ":$PATH:" != *":/usr/bin:"* ]]; then
328+
export PATH="$PATH:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
329+
fi
330+
else
331+
# Save PATH before initialization
332+
local pre_init_path="$PATH"
333+
334+
# Not in any conda environment - need to initialize first
335+
# Use appropriate syntax based on the manager
336+
if [[ "$manager" == "mamba" ]]; then
337+
# Mamba uses: mamba shell hook (auto-detects shell)
338+
eval "$(mamba shell hook)"
339+
else
340+
# Conda uses: conda shell.<shell> hook
341+
local shell_name="${SHELL##*/}"
342+
eval "$(conda shell.$shell_name hook)"
343+
fi
344+
345+
# Ensure system paths are present after initialization
346+
if [[ ":$PATH:" != *":/usr/bin:"* ]]; then
347+
export PATH="$PATH:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
348+
fi
349+
350+
$manager activate "$ENV_NAME"
351+
352+
# Final check: ensure system paths after activation
353+
if [[ ":$PATH:" != *":/usr/bin:"* ]]; then
354+
export PATH="$PATH:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
355+
fi
356+
fi
221357
fi
222358

223359
log_info "[2/3] ROS2 environment configured via RoboStack..."
@@ -270,7 +406,8 @@ verify_setup() {
270406
fi
271407

272408
# Check if workspace packages are available
273-
if ros2 pkg list | grep -q "coffee_voice_agent_ui"; then
409+
# Use absolute path to grep to avoid conda PATH issues
410+
if ros2 pkg list | /usr/bin/grep -q "coffee_voice_agent_ui"; then
274411
log_success "Coffee Voice Agent UI package found"
275412
else
276413
log_warning "Coffee Voice Agent UI package not found. Workspace may need to be built."

0 commit comments

Comments
 (0)