You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: documentation/general/dotnet-run-file.md
+24-15Lines changed: 24 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -65,6 +65,16 @@ For example, the remaining command-line arguments after the first argument (the
65
65
(except for the arguments recognized by `dotnet run` unless they are after the `--` separator)
66
66
and working directory is not changed (e.g., `cd /x/ && dotnet run /y/file.cs` runs the program in directory `/x/`).
67
67
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
+
68
78
## Entry points
69
79
70
80
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
153
163
154
164
It is possible to specify some project metadata via *file-level directives*
155
165
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.
157
168
Other directives result in an error, reserving them for future use.
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}</{
174
186
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.
175
187
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>`.
176
188
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).
177
192
178
193
Because these directives are limited by the C# language to only appear before the first "C# token" and any `#if`,
179
194
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
286
301
287
302
### Shebang support
288
303
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
292
305
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:
294
308
295
309
```cs
296
310
#!/usr/bin/env dotnet run
297
311
// ^ Might not work in all shells. "dotnet run" might be passed as a single argument to "env".
298
312
```
299
313
```cs
300
-
#!/usr/bin/env dotnet-run
314
+
#!/usr/bin/env dotnet
301
315
// ^ Should work in all shells.
302
316
```
303
317
```cs
304
318
#!/usr/bin/env -S dotnet run
305
-
// ^ Workaround in some shells.
319
+
// ^ Works in some shells.
306
320
```
307
321
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
312
323
313
-
Commands `dotnet restore file.cs` and `dotnet build file.cs` are needed for IDE support and hence work for file-based programs.
314
324
We can consider supporting other commands like `dotnet pack`, `dotnet watch`,
315
325
however the primary scenario is `dotnet run` and we might never support additional commands.
316
326
@@ -325,9 +335,8 @@ We could also add `dotnet compile` command that would be the equivalent of `dotn
325
335
e.g., via `dotnet clean --file-based-program <path-to-entry-point>`
326
336
or `dotnet clean --all-file-based-programs`.
327
337
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.
0 commit comments