Skip to content

Commit d9beff6

Browse files
Merging main into darc-main-2934249d-8bbb-43a2-a565-1b7bf5537ece
2 parents 56ef1d7 + 590390a commit d9beff6

File tree

97 files changed

+2132
-1407
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

97 files changed

+2132
-1407
lines changed

documentation/general/dotnet-run-file.md

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ For example, the remaining command-line arguments after the first argument (the
6565
(except for the arguments recognized by `dotnet run` unless they are after the `--` separator)
6666
and working directory is not changed (e.g., `cd /x/ && dotnet run /y/file.cs` runs the program in directory `/x/`).
6767

68+
`dotnet path.cs` is a shortcut for `dotnet run path.cs` provided that `path.cs` is a valid [target path](#target-path).
69+
70+
### Other commands
71+
72+
Commands `dotnet restore file.cs` and `dotnet build file.cs` are needed for IDE support and hence work for file-based programs.
73+
74+
Command `dotnet publish file.cs` is also supported for file-based programs.
75+
Note that file-based apps have implicitly set `PublishAot=true`, so publishing uses Native AOT (and building reports AOT warnings).
76+
To opt out, use `#:property PublishAot=false` directive in your `.cs` file.
77+
6878
## Entry points
6979

7080
If a file is given to `dotnet run`, it has to be an *entry-point file*, otherwise an error is reported.
@@ -153,14 +163,16 @@ They are not cleaned immediately because they can be re-used on subsequent runs
153163

154164
It is possible to specify some project metadata via *file-level directives*
155165
which are [ignored][ignored-directives] by the C# language but recognized by the SDK CLI.
156-
Directives `sdk`, `package`, and `property` are translated into `<Project Sdk="...">`, `<PackageReference>`, and `<Property>` project elements, respectively.
166+
Directives `sdk`, `package`, `property`, and `project` are translated into
167+
`<Project Sdk="...">`, `<PackageReference>`, `<PropertyGroup>`, and `<ProjectReference>` project elements, respectively.
157168
Other directives result in an error, reserving them for future use.
158169

159170
```cs
160171
#:sdk Microsoft.NET.Sdk.Web
161172
#:property TargetFramework=net11.0
162173
#:property LangVersion=preview
163174
175+
#:project ../MyLibrary
164176
```
165177

166178
The value must be separated from the kind (`package`/`sdk`/`property`) of the directive by whitespace
@@ -174,6 +186,9 @@ The value of `#:property` is split by the separator and injected as `<{0}>{1}</{
174186
It is an error if no separator appears in the value or if the first part (property name) is empty (the property value is allowed to be empty) or contains invalid characters.
175187
The value of `#:package` is split by the separator and injected as `<PackageReference Include="{0}" Version="{1}">` (or without the `Version` attribute if there is no separator) in an `<ItemGroup>`.
176188
It is an error if the first part (package name) is empty (the package version is allowed to be empty, but that results in empty `Version=""`).
189+
The value of `#:project` is injected as `<ProjectReference Include="{0}" />` in an `<ItemGroup>`.
190+
If the value points to an existing directory, a project file is found inside that directory and its path is used instead
191+
(because `ProjectReference` items don't support directory paths).
177192

178193
Because these directives are limited by the C# language to only appear before the first "C# token" and any `#if`,
179194
dotnet CLI can look for them via a regex or Roslyn lexer without any knowledge of defined conditional symbols
@@ -286,31 +301,26 @@ Also, `InternalsVisibleTo` needs to be added into a C# file as an attribute, or
286301

287302
### Shebang support
288303

289-
It might be beneficial to also ship `dotnet-run` binary
290-
(or `dotnet-run-file` that would only work with file-based programs, not project-based ones, perhaps simply named `cs`)
291-
because some shells do not support multiple command-line arguments in the shebang
304+
Some shells do not support multiple command-line arguments in the shebang
292305
which is needed if one wants to use `/usr/bin/env` to find the `dotnet` executable
293-
(although `-S` argument can be sometimes used to enable multiple argument support):
306+
(although `-S` argument can be sometimes used to enable multiple argument support),
307+
so `dotnet file.cs` instead of `dotnet run file.cs` should be used in shebangs:
294308

295309
```cs
296310
#!/usr/bin/env dotnet run
297311
// ^ Might not work in all shells. "dotnet run" might be passed as a single argument to "env".
298312
```
299313
```cs
300-
#!/usr/bin/env dotnet-run
314+
#!/usr/bin/env dotnet
301315
// ^ Should work in all shells.
302316
```
303317
```cs
304318
#!/usr/bin/env -S dotnet run
305-
// ^ Workaround in some shells.
319+
// ^ Works in some shells.
306320
```
307321

308-
We could also consider making `dotnet file.cs` work because `dotnet file.dll` also works today
309-
but that would require changes to the native dotnet host.
310-
311-
### Other commands
322+
### Other possible commands
312323

313-
Commands `dotnet restore file.cs` and `dotnet build file.cs` are needed for IDE support and hence work for file-based programs.
314324
We can consider supporting other commands like `dotnet pack`, `dotnet watch`,
315325
however the primary scenario is `dotnet run` and we might never support additional commands.
316326

@@ -325,9 +335,8 @@ We could also add `dotnet compile` command that would be the equivalent of `dotn
325335
e.g., via `dotnet clean --file-based-program <path-to-entry-point>`
326336
or `dotnet clean --all-file-based-programs`.
327337

328-
329-
Adding package references via `dotnet package add` could be supported for file-based programs as well,
330-
i.e., the command would add a `#:package` directive to the top of a `.cs` file.
338+
Adding references via `dotnet package add`/`dotnet reference add` could be supported for file-based programs as well,
339+
i.e., the command would add a `#:package`/`#:project` directive to the top of a `.cs` file.
331340

332341
### Explicit importing
333342

0 commit comments

Comments
 (0)