fsprojects / FAKE

FAKE - F# Make
https://fake.build
Other
1.28k stars 581 forks source link

Using `MetadataReaderProvider` in build scripts #2663

Closed peterhirn closed 2 years ago

peterhirn commented 2 years ago

Description

Using System.Reflection.Metadata.MetadataReaderProvider in build scripts used to work (v5.20.4) but now fails with a strange error message (System.IO doesn't contain Stream)

Repro steps

#r "paket:
nuget Fake.Core.Target
nuget System.Reflection.Metadata"

#load ".fake/build.fsx/intellisense.fsx"

open Fake.Core
open System.IO
open System.Reflection.Metadata

let isPortableSymbol path =
    try
        use fileStream = new FileStream(path, FileMode.Open, FileAccess.Read)
        use provider = MetadataReaderProvider.FromPortablePdbStream fileStream
        provider.GetMetadataReader() |> ignore
        true
    with
    | :? System.BadImageFormatException -> false

Target.create "Test" ignore

Target.runOrDefault "Test"

dotnet fake run

Actual behavior

Script is not valid:
        unknown (1,0)-(1,0): Error FS0193: The module/namespace 'System.IO' from compilation unit 'System.Runtime' did not contain the namespace, module or type 'Stream'
        C:\...\build.fsx (14,23)-(14,78): Error FS1109: A reference to the type 'System.IO.Stream' in assembly 'System.Runtime' was found, but the type could not be found in 
that assembly
        C:\...\build.fsx (15,8)-(15,34): Error FS0072: Lookup on object of indeterminate type based on information prior to this program point. A type annotation may be needed prior to this program point to constrain the type of the object. This may allow the lookup to be resolved

Related information

Additional

Issue can be reproduced in docker

FROM mcr.microsoft.com/dotnet/sdk:6.0.201-alpine3.15-amd64
WORKDIR /build

COPY .config/ .config/
COPY build.fsx build.fsx

RUN dotnet tool restore
RUN dotnet fake run

.config/dotnet-tools.json just contains latest fake-cli and paket tools.

github-actions[bot] commented 2 years ago

Welcome to the FAKE community! Thank you so much for creating your first issue and therefore improving the project!

peterhirn commented 2 years ago

Workaround

Using FromPortablePdbImage instead of FromPortablePdbStream.

let isPortableSymbol path =
    try
        use provider =
            File.ReadAllBytes path
            |> ImmutableArray.ToImmutableArray
            |> MetadataReaderProvider.FromPortablePdbImage

        provider.GetMetadataReader() |> ignore
        true
    with
    | :? System.BadImageFormatException -> false
yazeedobaid commented 2 years ago

I looked at the documentation for MetadataReaderProvider and FromPortablePdbImage is the method that expects an immutable array.

Please feel free to re-open this issue or another one if you still have the issue.

Thanks