Skip to content
Merged
Show file tree
Hide file tree
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
51 changes: 51 additions & 0 deletions src/ruby/supply/supply.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,18 @@ func (s *Supplier) InstallYarn() error {
return nil
}

// .yarnrc.yml is the marker file Yarn Berry (2.x/3.x/4.x) itself uses to
// identify a project as using the "modern" (non-Classic) CLI/protocol -
// Yarn Classic (1.x) projects never have this file. See
// https://yarnpkg.com/configuration/yarnrc.
usesBerry, err := libbuildpack.FileExists(filepath.Join(s.Stager.BuildDir(), ".yarnrc.yml"))
if err != nil {
return err
}
if usesBerry {
return s.InstallYarnBerry()
}

yarnInstallDir := filepath.Join(s.Stager.DepDir(), "yarn")
if err != nil {
return err
Expand All @@ -309,6 +321,45 @@ func (s *Supplier) InstallYarn() error {
return s.Stager.LinkDirectoryInDepDir(filepath.Join(yarnInstallDir, "bin"), "bin")
}

// InstallYarnBerry installs the "yarn-berry" dependency (Yarn 2.x/3.x/4.x)
// for projects whose .yarnrc.yml marks them as using the modern Yarn CLI.
//
// Unlike Yarn Classic, Yarn Berry is not distributed as a self-contained
// installable tarball with a bin/ directory - it ships as a single bundled
// CLI script (see https://repo.yarnpkg.com). InstallOnlyVersion therefore
// just copies that file as-is into the install dir (libbuildpack falls back
// to a plain copy for any dependency URI it doesn't recognize as an
// archive), so a small wrapper script is created here to expose it on PATH
// as a normal "yarn" executable via `node <script> "$@"`.
func (s *Supplier) InstallYarnBerry() error {
yarnBerryInstallDir := filepath.Join(s.Stager.DepDir(), "yarn-berry")

if err := s.Installer.InstallOnlyVersion("yarn-berry", yarnBerryInstallDir); err != nil {
return err
}

matches, err := filepath.Glob(filepath.Join(yarnBerryInstallDir, "*.js"))
if err != nil {
return err
}
if len(matches) == 0 {
return fmt.Errorf("could not locate yarn-berry CLI script in %s", yarnBerryInstallDir)
}
yarnBerryScript := matches[0]

binDir := filepath.Join(yarnBerryInstallDir, "bin")
if err := os.MkdirAll(binDir, 0755); err != nil {
return err
}

shim := fmt.Sprintf("#!/usr/bin/env bash\nexec node \"%s\" \"$@\"\n", yarnBerryScript)
if err := os.WriteFile(filepath.Join(binDir, "yarn"), []byte(shim), 0755); err != nil {
return err
}

return s.Stager.LinkDirectoryInDepDir(binDir, "bin")
}

func (s *Supplier) InstallBundler() error {
contents, err := os.ReadFile(fmt.Sprintf("%s.lock", s.Versions.Gemfile()))
if err != nil {
Expand Down
27 changes: 27 additions & 0 deletions src/ruby/supply/supply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1062,6 +1062,33 @@ var _ = Describe("Supply", func() {
Expect(filepath.Join(depsDir, depsIdx, "bin", "yarn")).ToNot(BeAnExistingFile())
})
})
Context("app has yarn.lock and .yarnrc.yml files (Yarn Berry)", func() {
BeforeEach(func() {
Expect(os.WriteFile(filepath.Join(buildDir, "yarn.lock"), []byte("contents"), 0644)).To(Succeed())
Expect(os.WriteFile(filepath.Join(buildDir, ".yarnrc.yml"), []byte("yarnPath: .yarn/releases/yarn-4.18.0.cjs\n"), 0644)).To(Succeed())
})
It("installs yarn-berry instead of yarn, and shims a yarn wrapper script", func() {
mockInstaller.EXPECT().InstallOnlyVersion("yarn-berry", gomock.Any()).Do(func(_, installDir string) error {
Expect(os.MkdirAll(installDir, 0755)).To(Succeed())
Expect(os.WriteFile(filepath.Join(installDir, "yarn-berry-4.18.0.js"), []byte("#!/usr/bin/env node\n"), 0644)).To(Succeed())
return nil
})
Expect(supplier.InstallYarn()).To(Succeed())

yarnBerryInstallDir := filepath.Join(depsDir, depsIdx, "yarn-berry")
link, err := os.Readlink(filepath.Join(depsDir, depsIdx, "bin", "yarn"))
Expect(err).ToNot(HaveOccurred())
Expect(link).To(Equal("../yarn-berry/bin/yarn"))

shimContents, err := os.ReadFile(filepath.Join(yarnBerryInstallDir, "bin", "yarn"))
Expect(err).ToNot(HaveOccurred())
Expect(string(shimContents)).To(ContainSubstring(`exec node "` + filepath.Join(yarnBerryInstallDir, "yarn-berry-4.18.0.js") + `" "$@"`))

info, err := os.Stat(filepath.Join(yarnBerryInstallDir, "bin", "yarn"))
Expect(err).ToNot(HaveOccurred())
Expect(info.Mode().Perm()&0111).ToNot(BeZero(), "shim script should be executable")
})
})
})

Describe("NeedsNode", func() {
Expand Down
Loading