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
47 changes: 42 additions & 5 deletions src/DynamoCore/Configuration/PathManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,49 @@ struct PathManagerParams

internal class PathManager : IPathManager
{
internal static Lazy<PathManager>
lazy =
new Lazy<PathManager>
(() => new PathManager(new PathManagerParams()));
private static Lazy<PathManager> lazy;
Copy link
Member

@mjkkirschner mjkkirschner Oct 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this implementation Lazy the docs say:
Use lazy initialization to defer the creation of a large or resource-intensive object, or the execution of a resource-intensive task, particularly when such creation or execution might not occur during the lifetime of the program.

I think it's unlikely any of our clients will not need a path manager.

If it was to make initialization thread safe, well then, this PR breaks that invariant, so whats the point?

private static readonly object lockObject = new object();

public static PathManager Instance { get { return lazy.Value; } }
/// <summary>
/// Initialize the PathManager singleton passing as a parameter a PathManagerParams object (which contains the Major and Minor version values).
/// </summary>
/// <param name="parameters"></param>
public static void Initialize(PathManagerParams parameters)
{
lock (lockObject)
{
if (lazy != null)
{
// Or do we want to reset the existing instance? See below for discussions.
throw new InvalidOperationException("PathManager has already been initialized.");
}

lazy = new Lazy<PathManager>(() => new PathManager(parameters));
}
}

/// <summary>
/// Instance is the property used as an access point to the PathManager singleton (if is not created will be created with default parameters).
/// </summary>
public static PathManager Instance
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some comments for public property please

{
get
{
if (lazy == null)
{
lock (lockObject)
{
if (lazy == null)
{
// Fallback to default if not initialized
lazy = new Lazy<PathManager>(() => new PathManager(new PathManagerParams()));
}
}
}

return lazy.Value;
}
}

#region Class Private Data Members

Expand Down
12 changes: 12 additions & 0 deletions src/DynamoCore/Models/DynamoModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,18 @@ private void SearchModel_ItemProduced(NodeModel node)
/// <returns></returns>
internal PathManager CreatePathManager(IStartConfiguration config)
{
var pathManagerParams = new PathManagerParams();

var version = config.HostAnalyticsInfo.HostVersion;
if (version != null)
{
// Use host versions if provided
pathManagerParams.MajorFileVersion = version.Major;
pathManagerParams.MinorFileVersion = version.Minor;

Dynamo.Core.PathManager.Initialize(pathManagerParams);
}

if (!config.StartInTestMode)
{
if (!Core.PathManager.Instance.HasPathResolver)
Expand Down
2 changes: 1 addition & 1 deletion src/Tools/DynamoInstallDetective/ProductLookUp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ public static string GetDynamoPath(Version version, string debugPath = null)
if (installs == null) return string.Empty;

return installs.Products
.Where(p => p.VersionInfo.Item1 == version.Major)
.Where(p => p.VersionInfo.Item1 <= version.Major)
.Where(p => p.VersionInfo.Item2 >= version.Minor)
.Select(p => p.InstallLocation)
.LastOrDefault();
Expand Down
28 changes: 28 additions & 0 deletions test/DynamoCoreTests/PackageDependencyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Dynamo.Configuration;
using Dynamo.Core;
using Dynamo.Graph.Workspaces;
using Dynamo.Interfaces;
using Dynamo.Models;
Expand Down Expand Up @@ -213,6 +215,32 @@ public void PackageDependencyIsCollectedForNewWorkspace()
}
}


/// <summary>
/// This test verifies that the PathManager singleton instance is created with the expected properties
/// e.g. DefaultPackagesDirectory has a structure like C:\Users\<user>\AppData\Roaming\Dynamo\Dynamo Core\4.0\packages
/// </summary>
[Test]
public void PackageInstallationPathTest()
{
int CurrentMajorFileVersion = 4;
int CurrentMinorFileVersion = 0;

//The PathManager was already created with empty parameters when PreferenceSettings is created.
PathManager singletonPathManager = PathManager.Instance;
var dynCorePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var appDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

var commonDataDirectory = dynCorePath;
var defaultPackagesDirectory = Path.Combine(appDataFolder, "Dynamo","Dynamo Core", CurrentMajorFileVersion.ToString("F1"), "packages");

//Checking that the properties in PathManager are the expected ones
Assert.IsTrue(singletonPathManager.MajorFileVersion == CurrentMajorFileVersion);
Assert.IsTrue(singletonPathManager.MinorFileVersion == CurrentMinorFileVersion);
Assert.IsTrue(singletonPathManager.CommonDataDirectory.Equals(commonDataDirectory));
Assert.IsTrue(singletonPathManager.DefaultPackagesDirectory.Equals(defaultPackagesDirectory));
}

[Test]
public void PackageDependenciesUpdatedAfterNodesAdded()
{
Expand Down
Loading