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
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ public class ProviderInfo
private SessionState _sessionState;

private string _fullName;
private string _cachedModuleName;

/// <summary>
/// Gets the name of the provider.
/// </summary>
public string Name { get; }

/// <summary>
/// Gets the full name of the provider including the pssnapin name if available.
/// Gets the full name of the provider including the module name if available.
/// </summary>
internal string FullName
{
Expand Down Expand Up @@ -77,7 +78,13 @@ string GetFullName(string name, string psSnapInName, string moduleName)
return result;
}

return _fullName ?? (_fullName = GetFullName(Name, PSSnapInName, ModuleName));
if (_fullName != null && ModuleName.Equals(_cachedModuleName, StringComparison.Ordinal))
{
return _fullName;
}

_cachedModuleName = ModuleName;
return _fullName = GetFullName(Name, PSSnapInName, ModuleName);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

Describe "Import PowerShell provider" -Tags "CI" {
BeforeAll {
$testModulePath = Join-Path $TestDrive "ReproModule"
New-Item -Path $testModulePath -ItemType Directory > $null

New-ModuleManifest -Path "$testModulePath/ReproModule.psd1" -RootModule 'testmodule.dll'

$testBinaryModulePath = Join-Path $testModulePath "testmodule.dll"
$binaryModule = @'
using System;
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Provider;

namespace module {
[CmdletProvider(
"SamplePrv",
ProviderCapabilities.ShouldProcess)]
public class SampleProvider : ContainerCmdletProvider {
protected override bool IsValidPath(string path) {
return true;
}

protected override bool ItemExists(string path) {
return path == "test.txt";
}

protected override void GetItem(string path) {
Item resultItem;
if (path == "test.txt") {
resultItem = new Item { Name = "test.txt" };
} else {
throw new Exception("Item not found.");
}

WriteItemObject(resultItem, path, false);
}

protected override Collection<PSDriveInfo> InitializeDefaultDrives() {
var drive = new PSDriveInfo(
"defaultSampleDrive",
ProviderInfo,
"/",
"Sample default drive",
null);
var result = new Collection<PSDriveInfo> {drive};
return result;
}

private class Item {
public string Name { get; set; }
}
}
}
'@
Add-Type -OutputAssembly $testBinaryModulePath -TypeDefinition $binaryModule

$pwsh = "$PSHOME\pwsh"
}

It "Import a PowerShell provider with correct name" {
$result = & $pwsh -NoProfile -Command "Import-Module -Name $testModulePath; Get-Item ReproModule\SamplePrv::test.txt"
$result.PSPath | Should -BeExactly "ReproModule\SamplePrv::test.txt"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3088,7 +3088,7 @@ internal PSModuleInfo LoadModuleManifest(
// In that case, the nested module will first be loaded with a different session state, and then when trying to load the RootModule via 'LoadModuleNamedInManifest',
// the same loaded nested module will be reused for the RootModule by 'LoadModuleNamedInManifest'.

// Change the module name to match the manifest name, not the original name
// Change the module name to match the manifest name, not the original name.
newManifestInfo.SetName(manifestInfo.Name);

// Copy in any nested modules...
Expand Down