8000 Update multi-file 'dotnet run file' documentation by jjonescz · Pull Request #48437 · dotnet/sdk · GitHub
[go: up one dir, main page]

Skip to content

Update multi-file 'dotnet run file' documentation #48437

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

Merged
merged 22 commits into from
May 7, 2025
Merged
Changes from 14 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 64 additions & 39 deletions documentation/general/dotnet-run-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ dotnet run file.cs
> [!NOTE]
> This document describes the ideal final state, but the feature will be implemented in [stages](#stages).

> [!CAUTION]
> The current implementation has been limited to single file support for the initial preview
> (as if the implicit project file had `<EnableDefaultItems>false</EnableDefaultItems>` and an explicit `<Compile Include="file.cs" />`),
> but this proposal describes a situation where all files in the target directory are included.
> Once a final decision is made, the proposal will be updated.

## Motivation

File-based programs
Expand All @@ -29,7 +23,8 @@ Previous file-based approaches like scripting are a variant of C# and as such ha

## Implicit project file

The [guiding principle](#guiding-principle) implies that we can think of file-based programs as having an implicit project file.
The [guiding principle](#guiding-principle) implies that we can think of file-based programs as having an implicit project file
(also known as a virtual project file because it exists only in memory unless the file-based program is converted to a project-based program).

The implicit project file is the default project that would be created by running `dotnet new console`.
This means that the behavior of `dotnet run file.cs` can change between SDK versions if the `dotnet new console` template changes.
Expand All @@ -43,20 +38,26 @@ In fact, this command simply materializes the [implicit project file](#implicit-
This action should not change the behavior of the target program.

```ps1
dotnet project convert
dotnet project convert file.cs
```

The command takes a path which can be either
- path to the entry-point file in case of single entry-point programs, or
- path to the target directory (then all entry points are converted;
it is not possible to convert just a single entry point in multi-entry-point program).

## Target path

The path passed to `dotnet run ./some/path.cs` is called *the target path*.
If it is a file, it is called *the target file*.
*The target directory* is the directory of the target file, or the target path if it is not a file.
The target path must be a file which has the `.cs` file extension, but we could allow folders as well in the future.
*The target directory* is the directory of the target file.

We can consider adding an option like `dotnet run --from-stdin` which would read the C# file from the standard input.
We can consider adding an option like `dotnet run --cs-from-stdin` which would read the C# file from the standard input
(or `dotnet run -` although then it's unclear which programming language the input is in)
In this case, the current working directory would not be used to search for project or other C# files,
the compilation would consist solely of the single file read from the standard input.
Similarly, it could be possible to specify the whole C# source text in a command-like argument
like `dotnet run --code 'Console.WriteLine("Hi")'`.
like `dotnet run --cs-code 'Console.WriteLine("Hi")'`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This documentation is a bit hard to use a spec since it's hypothesizing features that we may not have. Can we either [a] make a separate spec that removes this or [b] remove these from this file if this is intended to be the spec? Besides the IDE needing a precise spec for purposes of implementing our stuff, I worry this might 'train' a copilot these commands exist, but don't.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, yes, it started as a design doc with lots of unknowns, I guess I should clean it up now, thanks.


## Integration into the existing `dotnet run` command

Expand All @@ -65,8 +66,7 @@ specifically `file.cs` is passed as the first command-line argument to the targe
We preserve this behavior to avoid a breaking change.
The file-based build and run kicks in only when:
- a project file cannot be found (in the current directory or via the `--project` option), and
- if the target path is a file, it has the `.cs` file extension, and
- the target path (file or directory) exists.
- if the target file exists and has the `.cs` file extension.

> [!NOTE]
> This means that `dotnet run path` stops working when a file-based program [grows up](#grow-up) into a project-based program.
Expand All @@ -85,7 +85,8 @@ The file-based build and run kicks in only when:

File-based programs are processed by `dotnet run` equivalently to project-based programs unless specified otherwise in this document.
For example, the remaining command-line arguments after the first argument (the target path) are passed through to the target app
(except for the arguments recognized by `dotnet run` unless they are after the `--` separator).
(except for the arguments recognized by `dotnet run` unless they are after the `--` separator)
and working directory is not changed (e.g., `cd /x/ && dotnet run /y/file.cs` runs the program in directory `/x/`).

## Entry points

Expand All @@ -97,6 +98,11 @@ to check whether the file contains an entry point (top-level statements or a val
(We cannot simply use Roslyn APIs to detect entry points ourselves because parsing depends on conditional symbols like those from `<DefineConstants>`
and we can reliably know the set of those only after invoking MSBuild, and doing that up front would be an unnecessary performance hit just to detect entry points.)

Similarly, during [grow up](#grow-up), we ask Roslyn via MSBuild to give us the set of entry-point files
(we can also use this to ask user during the conversion whether they want to continue despite compilation errors if there are any).

## Multiple C# files

Because of the [implicit project file](#implicit-project-file),
other files in the target directory or its subdirectories are included in the compilation.
For example, other `.cs` files but also `.resx` (embedded resources).
Expand Down Expand Up @@ -135,9 +141,9 @@ That's because it might be perfectly reasonable to have file-based programs nest

### Multiple entry points

If there are multiple entry-point files in the target directory, the target path must be a file
(an error is reported if it points to a directory instead).
Then the build ignores other entry-point files.
If there are multiple entry-point files in the target directory, the build ignores other entry-point files.
It is an error to have an entry-point file in a subdirectory of the target directory
(because it is unclear how such program should be converted to a project-based one).

Thanks to this, it is possible to have a structure like
```
Expand All @@ -147,7 +153,7 @@ App/Program2.cs
```
where either `Program1.cs` or `Program2.cs` can be run and both of them have access to `Util.cs`.

In this case, there are multiple implicit projects
Behind the scenes, there are multiple implicit projects
(and during [grow up](#grow-up), multiple project files are materialized
and the original C# files are moved to the corresponding project subdirectories):
```
Expand All @@ -161,26 +167,36 @@ App/Program2/Program2.csproj
The generated folders might need to be named differently to avoid clashes with existing folders.

The entry-point projects (`Program1` and `Program2` in our example)
have the shared `.cs` files source-included via `<Content Include="../Shared/**/*.cs" />`.
We could consider having the projects directly in the top-level folder instead
but that might result in clashes of build outputs that are not project-scoped, like `project.assets.json`.
If we did that though, it would be enough to exclude the other entry points rather than including all the shared `.cs` files.

Unless the [artifacts output layout][artifacts-output] is used (which is recommended),
those implicit projects mean that build artifacts are placed under those implicit directories
even though they don't exist on disk prior to build:
```
App/Program1/bin/
App/Program1/obj/
App/Program2/bin/
App/Program2/obj/
have the shared `.cs` files source-included via `<Compile Include="../Shared/**/*.cs" />`.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, this wouldn't include non-C# files like .resx. We could add items like <EmbeddedResource Include="../Shared/**/*.cs" /> etc for all the default items, but I think I'm going to investigate if it would be possible to add a property like <AdditionalDefaultIncludeItemsDirectory>..\Shared</> that would be recognized by the .NET SDK props/targets.

Copy link
Member Author
@jjonescz jjonescz Apr 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I haven't done this in the corresponding implementation PR to keep it more self-contained, I think we can implement this in a follow up.)


We could consider using `InternalsVisibleTo` attribute but that might result in slight differences between single- and multi-entry-point programs
(if not now then perhaps in the futu EDBE re if [some "more internal" accessibility](https://github.com/dotnet/csharplang/issues/6794) is added to C# which doesn't respect `InternalsVisibleTo`)
which would be undesirable when users start with a single entry point and later add another.
Also, `InternalsVisibleTo` needs to be added into a C# file as an attribute, or via a complex-looking `AssemblyAttribute` item group into the `.csproj` like:

```xml
<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute" _Parameter1="App.Shared" />
</ItemGroup>
```

## Build outputs

Build outputs are placed under a subdirectory whose name is hashed file path of the entry point
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if I have two places running the same file twice in parallel? The usual rule would be you shouldn't run dotnet build twice on the same project, but from the user's perspective, there isn't a build.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from the user's perspective, there isn't a build.

I'm not sure about that, there is even --no-build option. Anyway this seems like a pre-existing issue - project-based dotnet run should behave equivalently to the new file-based dotnet run.

inside a temp or app data directory which is owned by the current user per [runtime guidelines][temp-guidelines].
Note that it is possible for multiple users to run the same file-based program, however each user's run works with different build artifacts.
Apart from keeping the source directory clean, such artifact isolation also avoids clashes of build outputs that are not project-scoped, like `project.assets.json`, in the case of multiple entry-point files.

Artifacts are cleaned periodically by a background task that is started by `dotnet run` and
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the motivation for not deleting right away to allow for caching?

Copy link
Member Author
@jjonescz jjonescz May 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I will clarify, thanks.

removes current user's `dotnet run` build outputs that haven't been used in some time.
We could also consider [integrating with `dotnet clean`](#other-commands) for an explicit cleanup gesture.

## Directives for project metadata

It is possible to specify some project metadata via [ignored C# directives][ignored-directives].
It is possible to specify some project metadata via *app directives*
which are [ignored][ignored-directives] by the C# language but recognized by the SDK CLI.
Directives `sdk`, `package`, and `property` are translated into `<Project Sdk="...">`, `<PackageReference>`, and `<Property>` project elements, respectively.
Other directives result in a warning, reserving them for future use.
Other directives resul F438 t in an error, reserving them for future use.

```cs
#:sdk Microsoft.NET.Sdk.Web
Expand All @@ -189,7 +205,8 @@ Other directives result in a warning, reserving them for future use.
#:package System.CommandLine 2.0.0-*
```

The value must be separated from the name of the directive by white space and any leading and trailing white space is not considered part of the value.
The value must be separated from the name of the directive by white space (`@` is additionally allowed separator for the package directive)
and any leading and trailing white space is not considered part of the value.
Any value can optionally have two parts separated by a space (more whitespace characters could be allowed in the future).
The value of the first `#:sdk` is injected into `<Project Sdk="{0}">` with the separator (if any) replaced with `/`,
and the subsequent `#:sdk` directive values are split by the separator and injected as `<Sdk Name="{0}" Version="{1}" />` elements (or without the `Version` attribute if there is no separator).
Expand All @@ -203,10 +220,13 @@ Because these directives are limited by the C# language to only appear before th
dotnet CLI can look for them via a regex or Roslyn lexer without any knowledge of defined conditional symbols
and can do that efficiently by stopping the search when it sees the first "C# token".

We do not limit these directives to appear only in entry point files.
Indeed, it might be beneficial to let a non-entry-point file like `Util.cs` be self-contained and have all the `#:package`s it needs specified in it,
which also makes it possible to share it independently or symlink it to multiple script folders.
This is also similar to `global using`s which users usually put into a single file but don't have to.
It is an error if an app directive appears in a non-entry-point file.
Otherwise it is unclear how the CLI would determine which files to parse directives from
(presumably we would want to exclude other entry points but the CLI defers to the compiler to detect entry points).
We could relax this in the future because it would allow:
- a non-entry-point file like `Util.cs` to be self-contained and have all the `#:package`s it needs specified in it,
- which would also make it possible to share it independently or symlink it to multiple script folders,
- and it would be similar to `global using`s which users usually put into a single file but don't have to.

We could consider deduplicating `#:` directives
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd at least expect us to block duplicate stuff, otherwise we've boxed ourselves into a corner we can't later design properly.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We currently don't do any deduplication so all directives end up in the final project and MSBuild should fail for invalid duplicates.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What duplicates do you expect MSBuild to fail on? ProjectReference maybe, but properties being assigned more than once is totally allowed.

Copy link
Member Author
C84A

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I guess it should be simple to block duplicate directives for now, thanks.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, this seems a bit orthogonal to multi-file support (we can block duplicate directives in single files as well), so instead of bloating up the implementation PR, I filed an issue to track this: #48842

(e.g., properties could be concatenated via `;`, more specific package versions could override less specific ones),
Expand Down Expand Up @@ -266,6 +286,10 @@ or as the first argument if it makes sense for them.
We could also add `dotnet compile` command that would be the equivalent of `dotnet build` but for file-based programs
(because "compiling" might make more sense for file-based programs than "building").

`dotnet clean` could be extended to support cleaning [the output directory](#build-outputs),
e.g., via `dotnet clean --file-based-program <path-to-entry-point>`
or `dotnet clean --all-file-based-programs`.

### `dotnet package add`

Adding package references via `dotnet package add` could be supported for file-based programs as well,
Expand Down Expand Up @@ -313,3 +337,4 @@ Instead of implicitly including files from the target directory, the importing c
[artifacts-output]: https://learn.microsoft.com/dotnet/core/sdk/artifacts-output
[ignored-directives]: https://github.com/dotnet/csharplang/blob/main/proposals/ignored-directives.md
[shebang]: https://en.wikipedia.org/wiki/Shebang_%28Unix%29
[temp-guidelines]: https://github.com/dotnet/runtime/blob/d0e6ce8332a514d70b635ca4829bf863157256fe/docs/design/security/unix-tmp.md
0