GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — PS should try to load the assembly from file path first before loading from assemblyName by chunqingchen · Pull Request #4196 · PowerShell/PowerShell

PS should try to load the assembly from file path first before loading from assemblyName #4196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Dongbo Wang (daxian-dbw) merged 3 commits into PowerShell:master from chunqingchen:bugfix1
Sep 29, 2017

Uh oh!

There was an error while loading. Please reload this page.

Merged

PS should try to load the assembly from file path first before loading from assemblyName #4196

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
3 commits
Select commit Hold shift + click to select a range
cb1f0af
PS should try to load the assembly from file path first before loadin…
chunqingchen Aug 1, 2017
192de66
[Feature] fix test so it would test if the temporary assembly is real…
chunqingchen Sep 27, 2017
52ac1bb
[Feature] fix test so it would test if the temporary assembly is real…
chunqingchen Sep 28, 2017
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

170 changes: 66 additions & 104 deletions 170 src/System.Management.Automation/engine/ExecutionContext.cs
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1323,110 +1323,72 @@ internal void RemoveAssembly(string name)
[SuppressMessage("Microsoft.Reliability", "CA2001:AvoidCallingProblematicMethods", MessageId = "System.Reflection.Assembly.LoadFrom")]
internal static Assembly LoadAssembly(string name, string filename, out Exception error)
{
// First we try to load the assembly based on the given name

Assembly loadedAssembly = null;
error = null;

string fixedName = null;
if (!String.IsNullOrEmpty(name))
{
// Remove the '.dll' if it's there...
fixedName = name.EndsWith(".dll", StringComparison.OrdinalIgnoreCase)
? Path.GetFileNameWithoutExtension(name)
: name;

var assemblyString = Utils.IsPowerShellAssembly(fixedName)
? Utils.GetPowerShellAssemblyStrongName(fixedName)
: fixedName;

try
{
loadedAssembly = Assembly.Load(new AssemblyName(assemblyString));
}
catch (FileNotFoundException fileNotFound)
{
error = fileNotFound;
}
catch (FileLoadException fileLoadException)
{
error = fileLoadException;
// this is a legitimate error on CoreCLR for a newly emited with Add-Type assemblies
// they cannot be loaded by name, but we are only interested in importing them by path
}
catch (BadImageFormatException badImage)
{
error = badImage;
return null;
}
catch (SecurityException securityException)
{
error = securityException;
return null;
}
}

if (loadedAssembly != null)
return loadedAssembly;

if (!String.IsNullOrEmpty(filename))
{
error = null;

try
{
loadedAssembly = Assembly.LoadFrom(filename);
return loadedAssembly;
}
catch (FileNotFoundException fileNotFound)
{
error = fileNotFound;
}
catch (FileLoadException fileLoadException)
{
error = fileLoadException;
return null;
}
catch (BadImageFormatException badImage)
{
error = badImage;
return null;
}
catch (SecurityException securityException)
{
error = securityException;
return null;
}
}

#if !CORECLR// Assembly.LoadWithPartialName is not in CoreCLR. In CoreCLR, 'LoadWithPartialName' can be replaced by Assembly.Load with the help of AssemblyLoadContext.
// Finally try with partial name...
if (!String.IsNullOrEmpty(fixedName))
{
try
{
// This is a deprecated API, use of this API needs to be
// reviewed periodically.
#pragma warning disable 0618
loadedAssembly = Assembly.LoadWithPartialName(fixedName);

if (loadedAssembly != null)
{
// In the past, LoadWithPartialName would just return null in most cases when the assembly could not be found or loaded.
// In addition to this, the error was always cleared. So now, clear the error variable only if the assembly was loaded.
error = null;
}
return loadedAssembly;
}

// Expected exceptions are ArgumentNullException and BadImageFormatException. See https://msdn.microsoft.com/en-us/library/12xc5368(v=vs.110).aspx
catch (BadImageFormatException badImage)
{
error = badImage;
}
}
#endif
return null;
// First we try to load the assembly based on the filename

Assembly loadedAssembly = null;
error = null;

if (!String.IsNullOrEmpty(filename))
{
try
{
loadedAssembly = Assembly.LoadFrom(filename);
}
catch (FileNotFoundException fileNotFound)
{
error = fileNotFound;
}
catch (FileLoadException fileLoadException)
{
error = fileLoadException;
Comment thread Outdated

@SteveL-MSFT Steve Lee (SteveL-MSFT) Aug 1, 2017

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

if we were given a filepath and it doesn't exist, it doesn't seem like we should try to load something that happens to have the same name but different path

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@chunqingchen Chunqing Chen (chunqingchen) Aug 2, 2017

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

resolved

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
}
catch (BadImageFormatException badImage)
{
error = badImage;
}
catch (SecurityException securityException)
{
error = securityException;
}
}
else if (!String.IsNullOrEmpty(name))
{
string fixedName = null;
// Remove the '.dll' if it's there...
fixedName = name.EndsWith(".dll", StringComparison.OrdinalIgnoreCase)
? Path.GetFileNameWithoutExtension(name)
: name;

var assemblyString = Utils.IsPowerShellAssembly(fixedName)
? Utils.GetPowerShellAssemblyStrongName(fixedName)
: fixedName;

try
{
loadedAssembly = Assembly.Load(new AssemblyName(assemblyString));
}
catch (FileNotFoundException fileNotFound)
{
error = fileNotFound;
}
catch (FileLoadException fileLoadException)
{
error = fileLoadException;
// this is a legitimate error on CoreCLR for a newly emited with Add-Type assemblies
// they cannot be loaded by name, but we are only interested in importing them by path
}
catch (BadImageFormatException badImage)
{
error = badImage;
}
catch (SecurityException securityException)
{
error = securityException;
}
}

Comment thread Outdated

@SteveL-MSFT Steve Lee (SteveL-MSFT) Aug 3, 2017

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Delete blank line

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@chunqingchen Chunqing Chen (chunqingchen) Aug 4, 2017

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

resolved

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
// We either return the loaded Assembly, or return null.
return loadedAssembly;
}

/// <summary>
Expand Down
30 changes: 30 additions & 0 deletions 30 test/powershell/Modules/Microsoft.PowerShell.Core/Import-Module.Tests.ps1
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,33 @@ Describe "Import-Module for Binary Modules in GAC" -Tags 'CI' {
}
}
}

Describe "Import-Module for Binary Modules" -Tags 'CI' {

It "PS should try to load the assembly from file path first" {
Comment thread

@daxian-dbw Dongbo Wang (daxian-dbw) Sep 20, 2017

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Are you sure this test validates the fix? It passes even without your fix.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@chunqingchen Chunqing Chen (chunqingchen) Sep 27, 2017

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

previously when system.automation.management is used loading error will be occured.
Now we have to check the actual cmdlet is loaded.
Resolved.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
$src = @"
using System.Management.Automation; // Windows PowerShell namespace.

namespace ModuleCmdlets
{
[Cmdlet(VerbsDiagnostic.Test,"BinaryModuleCmdlet1")]
public class TestBinaryModuleCmdlet1Command : Cmdlet
{
protected override void BeginProcessing()
{
WriteObject("BinaryModuleCmdlet1 exported by the ModuleCmdlets module.");
}
}
}
"@

Add-Type -TypeDefinition $src -OutputAssembly $TESTDRIVE\System.dll
$results = powershell -noprofile -c "`$module = Import-Module $TESTDRIVE\System.dll -Passthru; `$module.ImplementingAssembly.Location; Test-BinaryModuleCmdlet1"

#Ignore slash format difference under windows/Unix
$path = (Get-ChildItem $TESTDRIVE\System.dll).FullName
$results[0] | Should Be $path
$results[1] | Should BeExactly "BinaryModuleCmdlet1 exported by the ModuleCmdlets module."
}
}

Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.