Skip to content
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

Add-Type ReferencedAssemblies only works with full file paths now? #23791

Open
5 tasks done
Jaykul opened this issue May 13, 2024 · 1 comment
Open
5 tasks done

Add-Type ReferencedAssemblies only works with full file paths now? #23791

Jaykul opened this issue May 13, 2024 · 1 comment
Labels
Issue-Bug Issue has been identified as a bug in the product Needs-Triage The issue is new and needs to be triaged by a work group. WG-Engine core PowerShell engine, interpreter, and runtime

Comments

@Jaykul
Copy link
Contributor

Jaykul commented May 13, 2024

Prerequisites

Steps to reproduce

This should work, but it does not anymore.

Add-Type "public class Style : System.Windows.DependencyObject {}" -ReferencedAssemblies "WindowsBase"

I have to pass the full path to the DLLs to get them to load:

Add-Type "public class Style : System.Windows.DependencyObject {}" -ReferencedAssemblies "$PSHome\WindowsBase.dll"

This appears to be because the assemblies are in $PSHome but Add-Type is searching $PSHome\ref

if (isForReferenceAssembly)
{
// If it's for resolving a reference assembly, then we look in NetCoreApp ref assemblies first
string netcoreAppRefPath = PathType.Combine(s_netcoreAppRefFolder, refAssemblyDll);
if (File.Exists(netcoreAppRefPath))
{
return netcoreAppRefPath;
}

private static readonly string s_netcoreAppRefFolder = PathType.Combine(
PathType.GetDirectoryName(
(Assembly.GetEntryAssembly() ?? typeof(PSObject).Assembly).Location),
"ref");

Expected behavior

PS> Add-Type "public class Style : System.Windows.DependencyObject {}" -ReferencedAssemblies "WindowsBase"
PS>

Actual behavior

PS> Add-Type "public class Style : System.Windows.DependencyObject {}" -ReferencedAssemblies "WindowsBase"

Add-Type: (1,37): error CS0234: The type or namespace name 'DependencyObject' does not exist in the namespace 'System.Windows' (are you missing an assembly reference?)
public class Style : System.Windows.DependencyObject {}
                                    ^
Add-Type: Cannot add type. Compilation errors occurred.

PS>

Error details

Exception             : 
    Type       : System.IO.FileNotFoundException
    Message    : Could not find file 'C:\Program Files\PowerShell\7\WindowsBase'.
    FileName   : C:\Program Files\PowerShell\7\WindowsBase
    TargetSite : 
        Name          : CreateFile
        DeclaringType : [Microsoft.Win32.SafeHandles.SafeFileHandle]
        MemberType    : Method
        Module        : System.Private.CoreLib.dll
    Source     : System.Private.CoreLib
    HResult    : -2147024894
    StackTrace : 
   at Microsoft.Win32.SafeHandles.SafeFileHandle.CreateFile(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options)
   at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode)
   at System.IO.Strategies.OSFileStreamStrategy..ctor(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode)
   at System.IO.Strategies.FileStreamHelpers.ChooseStrategyCore(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
   at Roslyn.Utilities.StandardFileSystem.OpenFile(String filePath, FileMode mode, FileAccess access, FileShare share)
   at Roslyn.Utilities.CommonCompilerFileSystemExtensions.OpenFileWithNormalizedException(ICommonCompilerFileSystem fileSystem, String filePath, FileMode fileMode, FileAccess fileAccess, FileShare fileShare)
   at Microsoft.CodeAnalysis.MetadataReference.CreateFromFile(String path, MetadataReferenceProperties properties, DocumentationProvider documentation)
   at Microsoft.PowerShell.Commands.AddTypeCommand.GetPortableExecutableReferences()
   at Microsoft.PowerShell.Commands.AddTypeCommand.CompileToAssembly(List`1 syntaxTrees, CompilationOptions compilationOptions, EmitOptions emitOptions)
   at Microsoft.PowerShell.Commands.AddTypeCommand.SourceCodeProcessing()
   at System.Management.Automation.CommandProcessorBase.Complete()
CategoryInfo          : NotSpecified: (:) [Add-Type], FileNotFoundException
FullyQualifiedErrorId : System.IO.FileNotFoundException,Microsoft.PowerShell.Commands.AddTypeCommand
InvocationInfo        : 
    MyCommand        : Add-Type
    ScriptLineNumber : 1
    OffsetInLine     : 1
    HistoryId        : 2
    Line             : Add-Type "public class Style : System.Windows.DependencyObject {}" -ReferencedAssemblies "$PSHome\WindowsBase"
    Statement        : Add-Type "public class Style : System.Windows.DependencyObject {}" -ReferencedAssemblies "$PSHome\WindowsBase"
    PositionMessage  : At line:1 char:1
                       + Add-Type "public class Style : System.Windows.DependencyObject {}" -R …
                       + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    InvocationName   : Add-Type
    CommandOrigin    : Internal
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1

Environment data

Name                           Value
----                           -----
PSVersion                      7.4.2
PSEdition                      Core
GitCommitId                    7.4.2
OS                             Microsoft Windows 10.0.22635
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

@Jaykul Jaykul added the Needs-Triage The issue is new and needs to be triaged by a work group. label May 13, 2024
@iSazonov iSazonov added the WG-Engine core PowerShell engine, interpreter, and runtime label May 13, 2024
@mklement0
Copy link
Contributor

mklement0 commented May 13, 2024

The curious thing is that it does find the assembly in principle; e.g.,
Add-Type "public class Style {}" -ReferencedAssemblies WindowsBase.dll compiles (but wouldn't in a pristine session if you specified a non-existent assembly).
The bug doesn't affect Windows PowerShell, but is present since at least 7.3 (haven't tried to go back further).

@iSazonov iSazonov added the Issue-Bug Issue has been identified as a bug in the product label May 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Bug Issue has been identified as a bug in the product Needs-Triage The issue is new and needs to be triaged by a work group. WG-Engine core PowerShell engine, interpreter, and runtime
Projects
None yet
Development

No branches or pull requests

3 participants