Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 0 additions & 1 deletion documentation/general/dotnet-run-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,6 @@ and can do that efficiently by stopping the search when it sees the first "C# to

For a given `dotnet run file.cs`, we include directives from the current entry point file (`file.cs`) and all other non-entry-point C# files,
specifically from all `Compile` items included in the project, no matter whether the `Compile` items are specified in some MSBuild code or inferred from `#:include`.
(Processing directives from other files is currently gated under a feature flag that can be enabled by setting the MSBuild property `ExperimentalFileBasedProgramEnableTransitiveDirectives=true`.)
The order in which other files are processed is currently unspecified (can change across SDK versions) but deterministic (stable in a given SDK version).
We do not limit these directives to appear only in entry point files because it allows:
- a non-entry-point file like `Util.cs` to be self-contained and have all the `#:package`s it needs specified in it,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,6 @@ public enum IncludeOrExcludeKind
/// </summary>
public sealed class IncludeOrExclude(in ParseInfo info) : Named(info)
{
public const string ExperimentalFileBasedProgramEnableTransitiveDirectives = nameof(ExperimentalFileBasedProgramEnableTransitiveDirectives);
public const string MappingPropertyName = "FileBasedProgramsItemMapping";
Comment thread
jjonescz marked this conversation as resolved.

public static string DefaultMappingString => ".cs=Compile;.resx=EmbeddedResource;.json=None;.razor=Content";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const Microsoft.DotNet.FileBasedPrograms.CSharpDirective.IncludeOrExclude.ExperimentalFileBasedProgramEnableTransitiveDirectives = "ExperimentalFileBasedProgramEnableTransitiveDirectives" -> string!
const Microsoft.DotNet.FileBasedPrograms.CSharpDirective.IncludeOrExclude.MappingPropertyName = "FileBasedProgramsItemMapping" -> string!
const Microsoft.DotNet.FileBasedPrograms.CSharpDirective.Ref.ExperimentalFileBasedProgramEnableRefDirective = "ExperimentalFileBasedProgramEnableRefDirective" -> string!
Microsoft.DotNet.FileBasedPrograms.CSharpDirective
Expand Down
6 changes: 0 additions & 6 deletions src/Microsoft.DotNet.ProjectTools/VirtualProjectBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -437,19 +437,13 @@ private void CheckDirectives(
ErrorReporter reportError)
{
bool? refEnabled = null;
bool? transitiveEnabled = null;

foreach (var directive in directives)
{
if (directive is CSharpDirective.Ref)
{
CheckFlagEnabled(ref refEnabled, CSharpDirective.Ref.ExperimentalFileBasedProgramEnableRefDirective, directive);
}

if (directive.Info.SourceFile.Path != EntryPointSourceFile.Path)
{
CheckFlagEnabled(ref transitiveEnabled, CSharpDirective.IncludeOrExclude.ExperimentalFileBasedProgramEnableTransitiveDirectives, directive);
}
}

void CheckFlagEnabled(ref bool? flag, string flagName, CSharpDirective directive)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,6 @@ public void RefDirective_DuplicateFolderName_ViaInclude()
<Project>
<PropertyGroup>
<{CSharpDirective.Ref.ExperimentalFileBasedProgramEnableRefDirective}>true</{CSharpDirective.Ref.ExperimentalFileBasedProgramEnableRefDirective}>
<{CSharpDirective.IncludeOrExclude.ExperimentalFileBasedProgramEnableTransitiveDirectives}>true</{CSharpDirective.IncludeOrExclude.ExperimentalFileBasedProgramEnableTransitiveDirectives}>
</PropertyGroup>
</Project>
""");
Expand Down Expand Up @@ -2715,8 +2714,7 @@ public void DeleteSource_WithIncludeDirective_Transitive()
var testInstance = _testAssetsManager.CreateTestDirectory();

// Create entry point file with #:include directive
File.WriteAllText(Path.Join(testInstance.Path, "Program.cs"), $"""
#:property {CSharpDirective.IncludeOrExclude.ExperimentalFileBasedProgramEnableTransitiveDirectives}=true
File.WriteAllText(Path.Join(testInstance.Path, "Program.cs"), """
#:include Util.cs
Console.WriteLine("Test");
""");
Expand Down
60 changes: 0 additions & 60 deletions test/dotnet.Tests/CommandTests/Run/RunFileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1249,9 +1249,6 @@ public static class B
""");
File.WriteAllText(Path.Join(testInstance.Path, "Directory.Build.props"), $"""
<Project>
<PropertyGroup>
<{CSharpDirective.IncludeOrExclude.ExperimentalFileBasedProgramEnableTransitiveDirectives}>true</{CSharpDirective.IncludeOrExclude.ExperimentalFileBasedProgramEnableTransitiveDirectives}>
</PropertyGroup>
<ItemGroup>
<Compile Include="B.cs" />
</ItemGroup>
Expand Down Expand Up @@ -4081,7 +4078,6 @@ public void RefDirective_DuplicateRefFromIncludedFiles()
<Project>
<PropertyGroup>
<{CSharpDirective.Ref.ExperimentalFileBasedProgramEnableRefDirective}>true</{CSharpDirective.Ref.ExperimentalFileBasedProgramEnableRefDirective}>
<{CSharpDirective.IncludeOrExclude.ExperimentalFileBasedProgramEnableTransitiveDirectives}>true</{CSharpDirective.IncludeOrExclude.ExperimentalFileBasedProgramEnableTransitiveDirectives}>
</PropertyGroup>
</Project>
""");
Expand Down Expand Up @@ -4139,7 +4135,6 @@ public void RefDirective_DuplicateRefFromIncludedFiles_Subdirectories()
<Project>
<PropertyGroup>
<{CSharpDirective.Ref.ExperimentalFileBasedProgramEnableRefDirective}>true</{CSharpDirective.Ref.ExperimentalFileBasedProgramEnableRefDirective}>
<{CSharpDirective.IncludeOrExclude.ExperimentalFileBasedProgramEnableTransitiveDirectives}>true</{CSharpDirective.IncludeOrExclude.ExperimentalFileBasedProgramEnableTransitiveDirectives}>
</PropertyGroup>
</Project>
""");
Expand Down Expand Up @@ -4204,7 +4199,6 @@ public void RefDirective_IncludeAndRefSameFile()
<Project>
<PropertyGroup>
<{CSharpDirective.Ref.ExperimentalFileBasedProgramEnableRefDirective}>true</{CSharpDirective.Ref.ExperimentalFileBasedProgramEnableRefDirective}>
<{CSharpDirective.IncludeOrExclude.ExperimentalFileBasedProgramEnableTransitiveDirectives}>true</{CSharpDirective.IncludeOrExclude.ExperimentalFileBasedProgramEnableTransitiveDirectives}>
</PropertyGroup>
</Project>
""");
Expand Down Expand Up @@ -4340,14 +4334,6 @@ public void IncludeDirective_Transitive()
Directory.CreateDirectory(Path.Join(testInstance.Path, "dir1/dir2"));
Directory.CreateDirectory(Path.Join(testInstance.Path, "dir3"));

File.WriteAllText(Path.Join(testInstance.Path, "dir1/Directory.Build.props"), $"""
<Project>
<PropertyGroup>
<{CSharpDirective.IncludeOrExclude.ExperimentalFileBasedProgramEnableTransitiveDirectives}>true</{CSharpDirective.IncludeOrExclude.ExperimentalFileBasedProgramEnableTransitiveDirectives}>
</PropertyGroup>
</Project>
""");

var a = """
B.M();
""";
Expand Down Expand Up @@ -4598,14 +4584,6 @@ public void IncludeDirective_UpToDate_ProjectReference()
{
var testInstance = _testAssetsManager.CreateTestDirectory();

File.WriteAllText(Path.Join(testInstance.Path, "Directory.Build.props"), $"""
<Project>
<PropertyGroup>
<{CSharpDirective.IncludeOrExclude.ExperimentalFileBasedProgramEnableTransitiveDirectives}>true</{CSharpDirective.IncludeOrExclude.ExperimentalFileBasedProgramEnableTransitiveDirectives}>
</PropertyGroup>
</Project>
""");

var libDir = Path.Join(testInstance.Path, "Lib");
Directory.CreateDirectory(libDir);

Expand Down Expand Up @@ -4665,44 +4643,6 @@ class UtilClass
Build(testInstance, BuildLevel.All, expectedOutput: expectedOutput, workDir: appDir);
}

/// <summary>
/// Transitive directives (directives in non-entry-point files) are gated behind a feature flag.
/// </summary>
[Fact]
public void IncludeDirective_FeatureFlags()
{
var testInstance = _testAssetsManager.CreateTestDirectory();

File.WriteAllText(Path.Join(testInstance.Path, "Program.cs"), """
#!/usr/bin/env dotnet
#:include Util.cs
Console.WriteLine(Util.M());
""");

var utilPath = Path.Join(testInstance.Path, "Util.cs");
File.WriteAllText(utilPath, """
#:property DefineConstants=MY_CONST
static class Util { public static string M() => "Hello from Util"; }
""");

new DotnetCommand(Log, "run", "Program.cs")
.WithWorkingDirectory(testInstance.Path)
.Execute()
.Should().Fail()
.And.HaveStdErr($"""
{DirectiveError(utilPath, 1, Resources.ExperimentalFeatureDisabled, CSharpDirective.IncludeOrExclude.ExperimentalFileBasedProgramEnableTransitiveDirectives)}

{CliCommandStrings.RunCommandException}
""");

new DotnetCommand(Log, "run", "Program.cs")
.WithWorkingDirectory(testInstance.Path)
.WithEnvironmentVariable(CSharpDirective.IncludeOrExclude.ExperimentalFileBasedProgramEnableTransitiveDirectives, "true")
.Execute()
.Should().Pass()
.And.HaveStdOut("Hello from Util");
}

[Fact]
public void IncludeDirective_CustomMapping()
{
Expand Down
Loading