Skip to content

Commit

Permalink
fix: Evaluate RootLayout.ForCurrentDirectory lazily
Browse files Browse the repository at this point in the history
This prevents start-up failures in the container environment (and
any other time we're not in a repo).

Fixes googleapis#14005
  • Loading branch information
jskeet committed Dec 14, 2024
1 parent 82c1063 commit cc96e0b
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions tools/Google.Cloud.Tools.ReleaseManager/CommandBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@

using Google.Cloud.Tools.Common;
using LibGit2Sharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

namespace Google.Cloud.Tools.ReleaseManager
{
Expand All @@ -28,6 +30,10 @@ public abstract class CommandBase : ICommand
// releases, but fundamentally it's "the current source of truth we're basing this release on".
internal const string PrimaryBranch = "main";

// This is lazy so that we can construct instead of CommandBase even when the current directory isn't
// in a repo, but we don't go hunting for the root multiple times.
private readonly Lazy<RootLayout> _lazyLayout = new(RootLayout.ForCurrentDirectory, LazyThreadSafetyMode.ExecutionAndPublication);

private readonly int _minArgs;
private readonly int _maxArgs;

Expand All @@ -43,7 +49,7 @@ public abstract class CommandBase : ICommand
/// <remarks>
/// Currently this is always derived from the current directory, but we may later have a way of specifying it separately.
/// </remarks>
protected RootLayout RootLayout { get; }
protected RootLayout RootLayout => _lazyLayout.Value;

protected CommandBase(string command, string description, int minArgs, int maxArgs, string expectedArguments)
{
Expand All @@ -52,7 +58,6 @@ protected CommandBase(string command, string description, int minArgs, int maxAr
_minArgs = minArgs;
_maxArgs = maxArgs;
ExpectedArguments = expectedArguments;
RootLayout = RootLayout.ForCurrentDirectory();
}

protected CommandBase(string command, string description, params string[] argNames)
Expand Down

0 comments on commit cc96e0b

Please sign in to comment.