Skip to content

Commit 8c3199c

Browse files
eerhardtCopilot
andauthored
Detect which version of yarn is being used (#12633)
* Detect which version of yarn is being used Change the install args based on yarn 1.x or 2+ * Update tests/Aspire.Hosting.NodeJs.Tests/PackageInstallationTests.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update comment --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 64ff63c commit 8c3199c

2 files changed

Lines changed: 56 additions & 6 deletions

File tree

src/Aspire.Hosting.NodeJs/NodeExtensions.cs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -475,11 +475,29 @@ public static IResourceBuilder<TResource> WithYarn<TResource>(this IResourceBuil
475475
return resource;
476476
}
477477

478-
private static string[] GetDefaultYarnInstallArgs(IResourceBuilder<JavaScriptAppResource> resource) =>
479-
resource.ApplicationBuilder.ExecutionContext.IsPublishMode &&
480-
File.Exists(Path.Combine(resource.Resource.WorkingDirectory, "yarn.lock"))
481-
? ["--immutable"]
482-
: [];
478+
private static string[] GetDefaultYarnInstallArgs(IResourceBuilder<JavaScriptAppResource> resource)
479+
{
480+
var workingDirectory = resource.Resource.WorkingDirectory;
481+
if (!resource.ApplicationBuilder.ExecutionContext.IsPublishMode ||
482+
!File.Exists(Path.Combine(workingDirectory, "yarn.lock")))
483+
{
484+
// Not publish mode or no yarn.lock, use default install args
485+
return [];
486+
}
487+
488+
var yarnRcYml = Path.Combine(workingDirectory, ".yarnrc.yml");
489+
var yarnBerryReleaseDir = Path.Combine(workingDirectory, ".yarn", "releases");
490+
var hasYarnBerry = File.Exists(yarnRcYml) || Directory.Exists(yarnBerryReleaseDir);
491+
492+
if (hasYarnBerry)
493+
{
494+
// Yarn 2+ detected, --frozen-lockfile is deprecated in v2+, use --immutable instead
495+
return ["--immutable"];
496+
}
497+
498+
// Fallback: default to Yarn v1.x behavior
499+
return ["--frozen-lockfile"];
500+
}
483501

484502
/// <summary>
485503
/// Configures the Node.js resource to use pnmp as the package manager and optionally installs packages before the application starts.

tests/Aspire.Hosting.NodeJs.Tests/PackageInstallationTests.cs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ public void WithYarn_DefaultsArgsInPublishMode()
476476
.WithYarn();
477477

478478
Assert.True(app.Resource.TryGetLastAnnotation<JavaScriptInstallCommandAnnotation>(out var installCommand));
479-
Assert.Equal(["install", "--immutable"], installCommand.Args);
479+
Assert.Equal(["install", "--frozen-lockfile"], installCommand.Args);
480480

481481
var app2 = builder.AddViteApp("test-app2", tempDir.Path)
482482
.WithYarn(installArgs: ["--immutable-cache"]);
@@ -485,6 +485,38 @@ public void WithYarn_DefaultsArgsInPublishMode()
485485
Assert.Equal(["install", "--immutable-cache"], installCommand.Args);
486486
}
487487

488+
[Fact]
489+
public void WithYarn_ReturnsImmutable_WhenYarnRcYmlExists()
490+
{
491+
using var tempDir = new TempDirectory();
492+
File.WriteAllText(Path.Combine(tempDir.Path, "yarn.lock"), "empty");
493+
File.WriteAllText(Path.Combine(tempDir.Path, ".yarnrc.yml"), "empty");
494+
495+
using var builder = TestDistributedApplicationBuilder.Create(DistributedApplicationOperation.Publish);
496+
497+
var app = builder.AddViteApp("test-app", tempDir.Path)
498+
.WithYarn();
499+
500+
Assert.True(app.Resource.TryGetLastAnnotation<JavaScriptInstallCommandAnnotation>(out var installCommand));
501+
Assert.Equal(["install", "--immutable"], installCommand.Args);
502+
}
503+
504+
[Fact]
505+
public void WithYarn_ReturnsImmutable_WhenYarnReleasesDirExists()
506+
{
507+
using var tempDir = new TempDirectory();
508+
File.WriteAllText(Path.Combine(tempDir.Path, "yarn.lock"), "empty");
509+
Directory.CreateDirectory(Path.Combine(tempDir.Path, ".yarn", "releases"));
510+
511+
using var builder = TestDistributedApplicationBuilder.Create(DistributedApplicationOperation.Publish);
512+
513+
var app = builder.AddViteApp("test-app", tempDir.Path)
514+
.WithYarn();
515+
516+
Assert.True(app.Resource.TryGetLastAnnotation<JavaScriptInstallCommandAnnotation>(out var installCommand));
517+
Assert.Equal(["install", "--immutable"], installCommand.Args);
518+
}
519+
488520
[Fact]
489521
public void WithPnpm_DefaultsArgsInPublishMode()
490522
{

0 commit comments

Comments
 (0)