Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(checkYarn): Use realpath to detect corepack #10

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 30 additions & 11 deletions cli/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ func checkNode() error {

func checkYarn() error {
yarns := which.All("yarn")
realpaths := which.All("realpath")
if len(yarns) == 0 {
return fmt.Errorf("yarn not found")
}
Expand All @@ -352,20 +353,38 @@ func checkYarn() error {
for _, yarn := range yarns {
slog.Debug("yarn found", slog.String("path", yarn))

// Run `exec env | grep COREPACK_ROOT`
out, err := exec.Command(yarn, "exec", "env").Output()
if err != nil {
slog.Error("failed to run yarn exec env", slog.String("error", err.Error()))
return err
}
sOut := string(out)
found := false
for _, line := range strings.Split(sOut, "\n") {
if strings.Contains(line, "COREPACK_ROOT=") {
slog.Debug("yarn is used via corepack", slog.String("line", line))
found = true

if len(realpaths) > 0 {
realpath := realpaths[0]
yarnPath, err := exec.Command(realpath, yarn).Output()

if err == nil {
// TODO: Figure out Windows support
found = strings.Contains(string(yarnPath), "/node_modules/corepack/")
}
}

if !found {
// Run `yarn exec env | grep COREPACK_ROOT`
out, err := exec.Command(yarn, "exec", "env").Output()
if err != nil {
slog.Error(
"failed to run yarn exec env",
slog.String("error", err.Error()),
slog.String("msg", string(out)),
)
return err
}
sOut := string(out)
for _, line := range strings.Split(sOut, "\n") {
if strings.Contains(line, "COREPACK_ROOT=") {
slog.Debug("yarn is used via corepack", slog.String("line", line))
found = true
}
}
}

allCorepack = allCorepack && found
}

Expand Down