Skip to content

Update PS should try to load the assembly from file path first before…#5161

Merged
daxian-dbw merged 4 commits intoPowerShell:masterfrom
chunqingchen:bugfix4
Nov 3, 2017
Merged

Update PS should try to load the assembly from file path first before…#5161
daxian-dbw merged 4 commits intoPowerShell:masterfrom
chunqingchen:bugfix4

Conversation

@chunqingchen
Copy link
Copy Markdown
Contributor

fix issue #3325

After we merge #4196, the fix will break some our partner's working code. Although the working code often works with an invalid assembly path.

This fix is to follow the old logic to minimize the breaking change.

  1. we first try to load the assembly from the file path
  2. if it fails, we try to load the assembly from the assembly name
  3. if 2 passes, we ignore the exception and return the assembly. otherwise we throw the exception.

… loading from assemblyName to avoid breaks for existed code
}
}
else if (!String.IsNullOrEmpty(name))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

How about we check if the file exists before trying Assembly.LoadFrom? So maybe just change if (!String.IsNullOrEmpty(filename)) to if (File.Exists(filename))?

@chunqingchen
Copy link
Copy Markdown
Contributor Author

@daxian-dbw your comments are resolved

catch (BadImageFormatException badImage)
{
error = badImage;
return null;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The return null at this line and line 1269 are not needed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

resolved

@@ -1258,14 +1261,20 @@ internal static Assembly LoadAssembly(string name, string filename, out Exceptio
catch (BadImageFormatException badImage)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The comments in the catch (FileLoadException fileLoadException) block right above this line seems not needed anymore, since we now try loading from path first.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

resolved

$results[1] | Should BeExactly "BinaryModuleCmdlet1 exported by the ModuleCmdlets module."
}

It "PS should try to load the assembly from assembly name if file path doesn't exist" -skip:(!$IsWindows) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why is this test skipped on Unix plats?

$module = Import-Module $TESTDRIVE\test.psd1 -PassThru
$module.NestedModules | Should Not BeNullOrEmpty
$assemblyLocation = [psobject].Assembly.location
$module.NestedModules.ImplementingAssembly.Location | Should Be $assemblyLocation
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please unload the module after the test.

@chunqingchen
Copy link
Copy Markdown
Contributor Author

@daxian-dbw your comment is resolved

else
{
New-ModuleManifest -Path $TESTDRIVE/test.psd1 -NestedModules /NOExistedPath/System.Management.Automation.dll
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you use Join-Path to avoid the if ($IsWindows) else blocks? For example,

$psdFile = Join-Path $TESTDRIVE test.psd1
$nestedModule = Join-Path NOExistedPath System.Management.Automation.dll
New-ModuleManifest -Path $psdFile -NestedModules $nestedModule 

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

resolved

}
finally
{
Remove-Module $TESTDRIVE\test.psd1 -ErrorAction SilentlyContinue
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Remove-Module $module -ErrorAction SilentlyContinue would do

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

resolved

}
try
{
$module = Import-Module $TESTDRIVE\test.psd1 -PassThru
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Once you have $psdFil replace the $TESTDRIVE\test.psd1 with $psdFil.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

resolved

@chunqingchen
Copy link
Copy Markdown
Contributor Author

@daxian-dbw your comment is resolved

@chunqingchen chunqingchen force-pushed the bugfix4 branch 3 times, most recently from 689ce0c to 87a9bd7 Compare November 1, 2017 23:29
Copy link
Copy Markdown
Member

@daxian-dbw daxian-dbw left a comment

Choose a reason for hiding this comment

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

LGTM. Can you please add [Feature] tag to the commit message of your last commit so that we can validate the fix with a full test run?

@daxian-dbw daxian-dbw merged commit 2ae776a into PowerShell:master Nov 3, 2017
@daxian-dbw
Copy link
Copy Markdown
Member

@chunqingchen I have assigned #5084 to you so you can verify whether this PR fixes that issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants