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

deterministic/relative paths in CallerFilePath #4061

Open
joprice opened this issue Feb 21, 2025 · 2 comments
Open

deterministic/relative paths in CallerFilePath #4061

joprice opened this issue Feb 21, 2025 · 2 comments

Comments

@joprice
Copy link
Contributor

joprice commented Feb 21, 2025

I'm trying to get CallerFilePath to output relative paths using the ideas from the first answer here: https://stackoverflow.com/questions/65935784/is-it-possible-to-remove-the-full-paths-from-net-assemblies-created-with-dotnet.

It suggests that adding config like the following to an fsproj or Directory.Build.props file should result in relative paths being produced:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
      <Deterministic>true</Deterministic>
      <DeterministicSourcePaths>true</DeterministicSourcePaths>
      <PathMap>$(MSBuildThisFileDirectory)=.</PathMap>
    </PropertyGroup>
    <ItemGroup>
        <SourceRoot Include="$(MSBuildThisFileDirectory)/"/>
    </ItemGroup>

I've tried a few variations of this but always get absolute paths in my generated js. I see some references to PathMap in the fable source, but not sure if it's currently a supported feature. This is a nice feature to have to add source locations to logs, and generate stable ids based on source definitions for debugging.

Using fsc along with test code like:

type Log =
      static member inline info
        (
          message: string,
          [<CallerMemberName; Optional; DefaultParameterValue("")>] name: string,
          [<CallerFilePath; Optional; DefaultParameterValue("")>] path: string
        )
        =
        printfn $"%s{path}:%s{name}: %s{message}"

Log.info ("test")

The output is

./Api/Program.fs:main: test
@MangelMaxime
Copy link
Member

MangelMaxime commented Feb 22, 2025

Hello,

I didn't investigate this error but in Glutinum I do something similar using this code:

        let errorOrigin =
            let filePath =
                let index = filePath.IndexOf("src/Glutinum.Converter")
                "./" + filePath.Substring(index)

            $"%s{filePath}(%d{fileLine})".Replace("\\", "/")

This don't work in all situation but the assumption here is that I am able to identify a "unique enough" section of the path to decide to remove everything before that path.

You could imagine initialization the logger with the __SOURCE_DIRECTORY__ of the top level file for example.

This is mostly a workaround, and if we can support PathMap not to difficulty we should give it a try. But this would probably be limited to Fable (.NET) version as Fable (JavaScript) don't really understand MSBuild.

My assumption would have been that FCS was responsible to provide the correct path in this situation but I can be wrong.

@joprice
Copy link
Contributor Author

joprice commented Feb 22, 2025

Thanks for the example. I did think about doing that but I still ship code to a mobile decide with lots of long strings and the extra string manipulation operations. Perhaps I could use a babel plugin to remove them for now.

Im also experimenting with a fable plugin for autogenerating ids for zedux atom names as a workaround where I use the MemberRef info to grab the FullName of the identifier. It’s just a bit brittle since fable compiler plugins can’t influence the type of the Fsharp code, so I have to return a function and coerce with ‘!!’ to the actual return type.

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

No branches or pull requests

2 participants