roslyn-sdk icon indicating copy to clipboard operation
roslyn-sdk copied to clipboard

Does Source Generator allow to reference a file from web?

Open xperiandri opened this issue 3 years ago • 2 comments

Like we do in F# type providers. For example http://fsprojects.github.io/SwaggerProvider/#/#getting-started

xperiandri avatar Jan 30 '21 12:01 xperiandri

You can run any .netstandard 2.0 code in a source generator. ~~This includes web requests.~~ You I would recommend using local files if you can so you do not make web requests every time someone types.

jmarolf avatar Feb 01 '21 14:02 jmarolf

While a source generator might still execute if it performs I/O, the behavior of the output is undefined if the generator reads data from any location which is not part of the Compilation passed to its initialization. For example:

  • Source generators must not read files directly from disk (or the output is undefined)
  • Source generators must not read data from web requests (or the output is undefined)
  • Source generators must not read environment variables at the time of execution (or the output is undefined)

If you need access to data from one of these sources, it must be provided by modifying the build to obtain the items before compilation, and add the data to the compilation (e.g. by downloading at item from the web and including it through <AdditionalFiles>). Any targets used for this need to adhere to the MSBuild rules for up-to-date checks (beyond the scope of this answer).

/cc @jaredpar

sharwell avatar Jun 23 '21 17:06 sharwell