Tarmil / FSharp.Data.LiteralProviders

F# Type providers generating literals from the compile-time environment
MIT License
134 stars 6 forks source link

I want a parameter in `TextFile` that will cause a compile error if the file specified in` Path` does not exist. #14

Closed wraikny closed 2 years ago

wraikny commented 3 years ago

In the current implementation of TextFile,DefaultValue is used for the Text member if the file specified inPath does not exist. If DefaultValue is not specified, it will be an empty string.

If the contents of the file should always be used, but a non-existent file path is specified (for example, due to a mistake in the file path, a change in the file name, or a difference in the build environment), empty string is used as DefaultValue and no error occurs. This can cause unexpected bugs.

I think there is a way, for example, to add a bool type parameter namedUseDefaultValue and raise an exception if the file is not found and this parameter is false.

Tarmil commented 3 years ago

One way to ensure the existence of the file is to use the non-parameterized version of the provider, ie instead of TextFile<"foo/bar.txt">, use TextFile.foo.``bar.txt``.

But now I realize that you might want to provide the path as a string anyway (perhaps because it itself comes from another LiteralProvider), so yeah then this makes sense.

wraikny commented 3 years ago

In my situation I had to specify the file path of the project's parent hierarchy (for example, ../../misc/foobar.txt). In any case, thank you for your consideration.

Tarmil commented 3 years ago

There's actually another improvement that could be done, and would also solve your issue: making the non-parameterized version also provide a ``..`` to access the parent folder. But it gets pretty hard to read:

Textfile.``..``.``..``.misc.``foobar.txt``
Tarmil commented 2 years ago

Released in v0.4 as EnsureExists.