Skip to content

Addresses issue #261 with providing more detail information missing d… #298

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.

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

Closed
wants to merge 3 commits into from

Conversation

fdanny
Copy link
Contributor

@fdanny fdanny commented Dec 1, 2016

This addresses issue #261 to provide more information when there is a missing assembly dependency. The logic basically does the following

  1. After importing module check if an ImportError exception occured
  2. If it does load the assembly dependencies and try to load each one
  3. if I can't load that assembly I add to the list
  4. At the end I display the list of missing assemblies.

…missing dependencies with ImportError Exceptions
@den-run-ai
Copy link
Contributor

@fdanny can you please add some tests? i.e. you can look at test_import.py file.

@fdanny
Copy link
Contributor Author

fdanny commented Dec 1, 2016

@denfromufa Yup. I can do that. The way I tested it I created a dll that had a missing dependency. I made assure my ImportError Exception contained the specific missing dependency. Is this something that you were thinking?

@den-run-ai
Copy link
Contributor

den-run-ai commented Dec 2, 2016 via email

Copy link
Contributor

@den-run-ai den-run-ai left a comment

Choose a reason for hiding this comment

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

Can you make TestDependencyAssembly.dll compile as part of project and remove its dependencies before import? Just including a binary without source code is not maintainable.

@fdanny
Copy link
Contributor Author

fdanny commented Dec 2, 2016

@denfromufa Is there a location in the solution you want me to put this in TestDependencyAssembly? Is it okay just to check the solution as a reference and treat this dll as a resource. If there are any changes needed then the source code is included to build

Also why did my test failed on the Travis CI but passed AppVeyor? It has the following error
clr.AddReference(path_to_dll)
System.IO.FileNotFoundException: Unable to find assembly

@den-run-ai
Copy link
Contributor

You can just add a new project to solution. Otherwise just compile at
runtime - have a look at clrmagic.

That dll loading issue is pretty weird, please share the source code and
how you compiled it.

Here is repeatable issue using Mono C# REPL when using reflection, but not
when adding reference (LoadAssembly):


Mono C# Shell, type "help;" for help


Enter statements below.

csharp>
LoadAssembly("/home/dta/Documents/clrmagic/TestDependencyAssembly.dll")

csharp> using System.Reflection;

csharp> Assembly

Assembly AssemblyAlgorithmIdAttribute AssemblyCompanyAttribute
AssemblyConfigurationAttribute AssemblyContentType
AssemblyCopyrightAttribute AssemblyCultureAttribute
AssemblyDefaultAliasAttribute AssemblyDelaySignAttribute
AssemblyDescriptionAttribute AssemblyFileVersionAttribute
AssemblyFlagsAttribute AssemblyInformationalVersionAttribute
AssemblyKeyFileAttribute AssemblyKeyNameAttribute AssemblyLoadEventArgs
AssemblyLoadEventHandler AssemblyMetadataAttribute AssemblyName
AssemblyNameFlags AssemblyNameProxy AssemblyProductAttribute
AssemblySignatureKeyAttribute AssemblyTitleAttribute
AssemblyTrademarkAttribute AssemblyVersionAttribute

csharp> Assembly testdll =
Assembly.Load("/home/dta/Documents/clrmagic/TestDependencyAssembly.dll")

System.IO.FileNotFoundException: Could not load file or assembly
'/home/dta/Documents/clrmagic/TestDependencyAssembly.dll' or one of its
dependencies

File name: '/home/dta/Documents/clrmagic/TestDependencyAssembly.dll'

  at System.AppDomain.Load (System.String assemblyString,
System.Security.Policy.Evidence assemblySecurity, Boolean refonly)
<0x7f976292e650 + 0x000a2> in <filename unknown>:0

  at System.AppDomain.Load (System.String assemblyString) <0x7f976292e610 +
0x00015> in <filename unknown>:0

  at (wrapper remoting-invoke-with-check) System.AppDomain:Load (string)

  at System.Reflection.Assembly.Load (System.String assemblyString)
<0x7f97629a1980 + 0x0001b> in <filename unknown>:0

  at <InteractiveExpressionClass>.Host (System.Object& $retval) <0x41b71120
+ 0x0001a> in <filename unknown>:0

  at Mono.CSharp.Evaluator.Evaluate (System.String input, System.Object&
result, System.Boolean& result_set) <0x41ae4d30 + 0x000dd> in <filename
unknown>:0

  at Mono.CSharpShell.Evaluate (System.String input) <0x41ae4c30 + 0x00053>
in <filename unknown>:0

csharp> Assembly testdll =
Assembly.Load("/home/dta/Documents/clrmagic/TestDependencyAssembly")

System.IO.FileNotFoundException: Could not load file or assembly
'/home/dta/Documents/clrmagic/TestDependencyAssembly' or one of its
dependencies

File name: '/home/dta/Documents/clrmagic/TestDependencyAssembly'

  at System.AppDomain.Load (System.String assemblyString,
System.Security.Policy.Evidence assemblySecurity, Boolean refonly)
<0x7f976292e650 + 0x000a2> in <filename unknown>:0

  at System.AppDomain.Load (System.String assemblyString) <0x7f976292e610 +
0x00015> in <filename unknown>:0

  at (wrapper remoting-invoke-with-check) System.AppDomain:Load (string)

  at System.Reflection.Assembly.Load (System.String assemblyString)
<0x7f97629a1980 + 0x0001b> in <filename unknown>:0

  at <InteractiveExpressionClass>.Host (System.Object& $retval) <0x41b71690
+ 0x0001a> in <filename unknown>:0

  at Mono.CSharp.Evaluator.Evaluate (System.String input, System.Object&
result, System.Boolean& result_set) <0x41ae4d30 + 0x000dd> in <filename
unknown>:0

  at Mono.CSharpShell.Evaluate (System.String input) <0x41ae4c30 + 0x00053>
in <filename unknown>:0

csharp>

@fdanny
Copy link
Contributor Author

fdanny commented Dec 6, 2016

@denfromufa I ran into an issue trying to resolve this. My method will only work if the namespace imported is the same as the assembly name. I have no way of mapping a namespace to an assembly.

It looks like when clr.AddReference is called an AssemblyLoadEventHandle is initiated when an assembly is loaded and eventually goes into the AssemblyManager::ScanAssembly. It looks like this method parses out the namespaces and loads it into ConcurrentDictionary.

The issue in ScanAssembly is when
types = assembly.GetTypes();

This doesn't work since it doesn't have all the dependencies so we don't get the namespace mapping.

I'm thinking the following maybe

  1. Raise an exception clr.AddReference if not all dependencies are present
  2. On a failure on assembly.GetTypes() Read the assembly in Stream and parse the out the information sort of like Mono Cecil.

What do you think???

@den-run-ai
Copy link
Contributor

@fdanny maybe it is ok without namespace in the error message? but i'm surprised that it is hard to track it down.

@vmuriart
Copy link
Contributor

vmuriart commented Feb 7, 2017

@denfromufa @fdanny is there any help needed for this pull request ?

@den-run-ai
Copy link
Contributor

@fdanny needs help tracking down the namespace name in the Exception traceback

@filmor filmor added this to the 2.4.0 milestone Aug 30, 2018
@filmor filmor modified the milestones: 2.4.0, 2.5.0 May 8, 2019
lostmsu added a commit to losttech/pythonnet that referenced this pull request Mar 4, 2020
…ore info when CLR assemblies are failed to be loaded

1. When trying to implicitly load assemblies, and that fails NOT because an assembly is missing, but because loading failed for some reason, emit Python warning.
2. When trying to import a module in our import hook, if the module name is an assembly name, and we fail to load it, and Python also fails to find a module with the same name, add the exceptions we got during the attempt to load it into __cause__ of the final ImportError

BREAKING: clr.AddReference will now throw exceptions besides FileNotFoundException.

Additional: a few uses of BorrowedReference

This addresses pythonnet#261
It is an alternative to pythonnet#298
lostmsu added a commit to losttech/pythonnet that referenced this pull request Mar 4, 2020
…ore info when CLR assemblies are failed to be loaded

1. When trying to implicitly load assemblies, and that fails NOT because an assembly is missing, but because loading failed for some reason, emit Python warning.
2. When trying to import a module in our import hook, if the module name is an assembly name, and we fail to load it, and Python also fails to find a module with the same name, add the exceptions we got during the attempt to load it into __cause__ of the final ImportError

BREAKING: clr.AddReference will now throw exceptions besides FileNotFoundException.

Additional: a few uses of BorrowedReference

This addresses pythonnet#261
It is an alternative to pythonnet#298
@lostmsu lostmsu removed this from the 2.5.0 milestone Apr 23, 2020
lostmsu added a commit to losttech/pythonnet that referenced this pull request Apr 23, 2020
…ore info when CLR assemblies are failed to be loaded

1. When trying to implicitly load assemblies, and that fails NOT because an assembly is missing, but because loading failed for some reason, emit Python warning.
2. When trying to import a module in our import hook, if the module name is an assembly name, and we fail to load it, and Python also fails to find a module with the same name, add the exceptions we got during the attempt to load it into __cause__ of the final ImportError

BREAKING: clr.AddReference will now throw exceptions besides FileNotFoundException.

Additional: a few uses of BorrowedReference

This addresses pythonnet#261
It is an alternative to pythonnet#298
lostmsu added a commit to losttech/pythonnet that referenced this pull request Apr 27, 2020
…ore info when CLR assemblies are failed to be loaded

1. When trying to implicitly load assemblies, and that fails NOT because an assembly is missing, but because loading failed for some reason, emit Python warning.
2. When trying to import a module in our import hook, if the module name is an assembly name, and we fail to load it, and Python also fails to find a module with the same name, add the exceptions we got during the attempt to load it into __cause__ of the final ImportError

BREAKING: clr.AddReference will now throw exceptions besides FileNotFoundException.

Additional: a few uses of BorrowedReference

This addresses pythonnet#261
It is an alternative to pythonnet#298
lostmsu added a commit to losttech/pythonnet that referenced this pull request May 13, 2020
…ore info when CLR assemblies are failed to be loaded

1. When trying to implicitly load assemblies, and that fails NOT because an assembly is missing, but because loading failed for some reason, emit Python warning.
2. When trying to import a module in our import hook, if the module name is an assembly name, and we fail to load it, and Python also fails to find a module with the same name, add the exceptions we got during the attempt to load it into __cause__ of the final ImportError

BREAKING: clr.AddReference will now throw exceptions besides FileNotFoundException.

Additional: a few uses of BorrowedReference

This addresses pythonnet#261
It is an alternative to pythonnet#298
lostmsu added a commit to losttech/pythonnet that referenced this pull request Aug 18, 2020
…ore info when CLR assemblies are failed to be loaded

1. When trying to implicitly load assemblies, and that fails NOT because an assembly is missing, but because loading failed for some reason, emit Python warning.
2. When trying to import a module in our import hook, if the module name is an assembly name, and we fail to load it, and Python also fails to find a module with the same name, add the exceptions we got during the attempt to load it into __cause__ of the final ImportError

BREAKING: clr.AddReference will now throw exceptions besides FileNotFoundException.

Additional: a few uses of BorrowedReference

This addresses pythonnet#261
It is an alternative to pythonnet#298
lostmsu added a commit to losttech/pythonnet that referenced this pull request Aug 18, 2020
…ore info when CLR assemblies are failed to be loaded

1. When trying to implicitly load assemblies, and that fails NOT because an assembly is missing, but because loading failed for some reason, emit Python warning.
2. When trying to import a module in our import hook, if the module name is an assembly name, and we fail to load it, and Python also fails to find a module with the same name, add the exceptions we got during the attempt to load it into __cause__ of the final ImportError

BREAKING: clr.AddReference will now throw exceptions besides FileNotFoundException.

Additional: a few uses of BorrowedReference

This addresses pythonnet#261
It is an alternative to pythonnet#298
lostmsu added a commit to losttech/pythonnet that referenced this pull request Aug 18, 2020
…ore info when CLR assemblies are failed to be loaded

1. When trying to implicitly load assemblies, and that fails NOT because an assembly is missing, but because loading failed for some reason, emit Python warning.
2. When trying to import a module in our import hook, if the module name is an assembly name, and we fail to load it, and Python also fails to find a module with the same name, add the exceptions we got during the attempt to load it into __cause__ of the final ImportError

BREAKING: clr.AddReference will now throw exceptions besides FileNotFoundException.

Additional: a few uses of BorrowedReference

This addresses pythonnet#261
It is an alternative to pythonnet#298
lostmsu added a commit to losttech/pythonnet that referenced this pull request Aug 18, 2020
…ore info when CLR assemblies are failed to be loaded

1. When trying to implicitly load assemblies, and that fails NOT because an assembly is missing, but because loading failed for some reason, emit Python warning.
2. When trying to import a module in our import hook, if the module name is an assembly name, and we fail to load it, and Python also fails to find a module with the same name, add the exceptions we got during the attempt to load it into __cause__ of the final ImportError

BREAKING: clr.AddReference will now throw exceptions besides FileNotFoundException.

Additional: a few uses of BorrowedReference

This addresses pythonnet#261
It is an alternative to pythonnet#298
lostmsu added a commit to losttech/pythonnet that referenced this pull request Aug 18, 2020
…ore info when CLR assemblies are failed to be loaded

1. When trying to implicitly load assemblies, and that fails NOT because an assembly is missing, but because loading failed for some reason, emit Python warning.
2. When trying to import a module in our import hook, if the module name is an assembly name, and we fail to load it, and Python also fails to find a module with the same name, add the exceptions we got during the attempt to load it into __cause__ of the final ImportError

BREAKING: clr.AddReference will now throw exceptions besides FileNotFoundException.

Additional: a few uses of BorrowedReference

This addresses pythonnet#261
It is an alternative to pythonnet#298
lostmsu added a commit that referenced this pull request Aug 19, 2020
…ore info when CLR assemblies are failed to be loaded

1. When trying to implicitly load assemblies, and that fails NOT because an assembly is missing, but because loading failed for some reason, emit Python warning.
2. When trying to import a module in our import hook, if the module name is an assembly name, and we fail to load it, and Python also fails to find a module with the same name, add the exceptions we got during the attempt to load it into __cause__ of the final ImportError

BREAKING: clr.AddReference will now throw exceptions besides FileNotFoundException.

Additional: a few uses of BorrowedReference

This addresses #261
It is an alternative to #298
@filmor
Copy link
Member

filmor commented Feb 15, 2021

The alternative PR #1076 was merged a while ago.

@filmor filmor closed this Feb 15, 2021
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.

5 participants