Skip to content

deterministic/relative paths in CallerFilePath #4061

@joprice

Description

@joprice

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions