Skip to content

Commit 176a3e4

Browse files
Fix yarn link issues with robust conflict resolution
Co-authored-by: paolomainardi <8747+paolomainardi@users.noreply.github.com>
1 parent 50c710a commit 176a3e4

1 file changed

Lines changed: 60 additions & 3 deletions

File tree

ansible/macos/macos/base.yml

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,69 @@
128128
failed_when: brew_check.rc != 0
129129
become: false
130130

131-
- name: Link required packages
131+
- name: Handle yarn linking with conflict resolution
132+
shell: |
133+
set -euo pipefail
134+
135+
# Function to check if yarn is installed via brew
136+
is_yarn_installed() {
137+
brew list yarn >/dev/null 2>&1
138+
}
139+
140+
# Function to check if yarn symlink points to brew installation
141+
is_yarn_properly_linked() {
142+
if [ -L "${homebrew_prefix}/bin/yarn" ]; then
143+
local target=$(readlink "${homebrew_prefix}/bin/yarn")
144+
# Check if target contains yarn cellar path
145+
echo "$target" | grep -q "Cellar/yarn/"
146+
else
147+
false
148+
fi
149+
}
150+
151+
# Function to remove orphaned yarn symlink
152+
remove_orphaned_yarn_symlink() {
153+
if [ -L "${homebrew_prefix}/bin/yarn" ] || [ -f "${homebrew_prefix}/bin/yarn" ]; then
154+
echo "Removing orphaned yarn symlink..."
155+
rm -f "${homebrew_prefix}/bin/yarn"
156+
fi
157+
}
158+
159+
if is_yarn_installed; then
160+
echo "Yarn is installed via brew"
161+
if is_yarn_properly_linked; then
162+
echo "Yarn is already properly linked"
163+
else
164+
echo "Yarn is not properly linked, attempting to fix..."
165+
# Use --overwrite to handle conflicts
166+
brew link --overwrite yarn
167+
echo "Yarn linked successfully with --overwrite"
168+
fi
169+
else
170+
echo "Yarn is not installed via brew"
171+
# If yarn symlink exists but package is not installed, remove it
172+
if [ -L "${homebrew_prefix}/bin/yarn" ] || [ -f "${homebrew_prefix}/bin/yarn" ]; then
173+
echo "Found orphaned yarn symlink, removing it..."
174+
remove_orphaned_yarn_symlink
175+
fi
176+
fi
177+
args:
178+
executable: /bin/bash
179+
when: "'yarn' in all_linked_packages"
180+
become: false
181+
register: yarn_link_result
182+
183+
- name: Show yarn linking result
184+
debug:
185+
msg: "{{ yarn_link_result.stdout_lines }}"
186+
when: "'yarn' in all_linked_packages and yarn_link_result is defined"
187+
188+
- name: Link other required packages (non-yarn)
132189
community.general.homebrew:
133190
name: "{{ item }}"
134191
state: linked
135-
loop: "{{ all_linked_packages }}"
136-
when: all_linked_packages | length > 0
192+
loop: "{{ all_linked_packages | difference(['yarn']) }}"
193+
when: all_linked_packages | difference(['yarn']) | length > 0
137194
become: false
138195

139196
- name: Run brew doctor

0 commit comments

Comments
 (0)