dotnet / sdk

Core functionality needed to create .NET Core projects, that is shared between Visual Studio and CLI
https://dot.net/core
MIT License
2.7k stars 1.06k forks source link

Idea: Support "dotnet run"ning single source files without a project file. #12328

Open teo-tsirpanis opened 4 years ago

teo-tsirpanis commented 4 years ago

When released, the .NET Core CLI tools made .NET development significantly easier with the command line. All it needs for a .NET Core app to run is a typically small .*proj file and the source files next to it.

However, for very small programs (such as code for educational purposes or one-off scripts) whose project file is the same unremarkable one, it is a source of redundancy, especially when there are many source files on the same directory.

For this reason, and to emulate the behavior of tools like python and node with their respective languages, I am proposing to make commands like dotnet run MyFile.cs compile and run a single-source-file app.

This change will give a LINQPad-like feature to the .NET Core SDK and will make the .NET languages significantly more accessible and even enable them to be used like scripting languages.

Some other details I have thought:

* A command like dotnet run MyFile.cs will behave like creating a project file like this:

  <Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
      <Language>C#</Language>
      <TargetFramework>net5.0</TargetFramework>
      <EnableDefaultCompileItems>false</EnableDefaultCompileItems>
      <OutputType>Exe</OutputType>
    </PropertyGroup>
    <ItemGroup>
      <Compile Include="MyFile.cs"/>
    </ItemGroup>
  </Project>

and then running dotnet run on this. I intentionally included the Language element because the project file might not necessarily need to be stored on disk for its language to be determined by its extension.

WeihanLi commented 2 years ago

I'm trying on this project(https://github.com/WeihanLi/dotnet-exec) to execute code directly without a project file, it would be great and simple to run without a project file

kant2002 commented 2 years ago

I think this is closely related to the https://github.com/dotnet/fsharp/issues/13341 where people like to have FSX files to be compiled down to exe files. Currently dotnet SDK assumes that every executable target has project file, but for scripting environments that's not true. Great that some interest was shown in https://github.com/dotnet/designs/pull/213, but seems to be initiative to move this forward does not materialized yet

Untersander commented 1 year ago

dotnet-repl which is based on dotnet-interactive achieves a similar thing, it also allows to execute C# and F# files.