dotnet / dotnet/sdk

Conditional compiling dependencies with Preprocessor Directives

Open
#41,615 1 comment 0 reactions 0 assignees View on GitHub
Area-NetSDK untriaged
Dominant language
C#
Stars
3.2k
Forks
1.3k
PR merge metrics
PR metrics pending

Description

### Is your feature request related to a problem? Please describe.
I am trying to compile **dependency** project (its sources use **preprocessor directives**) with defined constants at entrypoint project, but I receive errors like
```
Error CS1061 : '{type name}' does not contain a definition for '{method name}' and no accessible extension method '{method name}' accepting a first argument of type '{type name}' could be found (are you missing a using directive or an assembly reference?)
```
because my **dependency** project isn't using these constants on compile.

---

### Describe the solution you'd like
I would like to have an option to _force_ my solution dependency projects to use defined at entrypoint proj constants.
May it be `dotnet build` option or .csproj option in ``?

---

### Additional context

I am using .NET 6.0:
- .NET SDK 6.0.423
- .NET runtime Microsoft.NETCore.App/Microsoft.NETCore.App 6.0.31

>Why is this all necessary?

Because there are **dialects** - differences of different countries' laws and requirements.
Currently I work on project that has a hundred of dependency projects. They reference files in src/** directories.
The reason for this decision is that most of these files share a common part, but differ in **dialects**.

In my case, what will I do if new requirements will appear:
- Either modify file and add some logic with preprocessor directive (if an amount of business logic changes isn't that big):
```cs
#if DIALECT_A
public void NewOrModifiedMethod() { }
#endif
```
- Either create new file in _dialect folder_ and define dialect logic in it (if the logic and behavior are radically different:
```cs
// 'src/.../dir/dialect_a/Class.cs' is a dialect A version of 'src/.../dir/Class.cs'
class Class
{
// some Dialect A logic
}
```

In both cases there will appear `Module.DialectA.csproj` that contains Dialect A specific files and does (or rather imports `platform_DIALECT_A.Targets` with them):
```csproj

$(DefineConstants),DIALECT_A


```

And this is the domino effect - we need not only add dialect version of entrypoint project (`App.DialectA` instead of `App`), but also every dependency on the way to the modified module (if they reference this `Module.DialectA` or reference to those who reference).

---

I am attaching example with solution sources of what structure and behavior I expect:
[TestConditionalCompile.zip](https://github.com/user-attachments/files/15847827/TestConditionalCompile.zip)

App.A.csproj:
```csproj




com.app
Exe
enable




```

App.B.csproj:
```csrpoj




com.app
Exe
enable




```

ConditionalLib.csproj
```csproj



com.test.lib
enable










```

---

I will take any advice (except for complete refactoring and rewriting of the entire code).
I guess this is **very very bad** design, but currently our org doesn't have development resources enough to rewrite that. So I have what I have and search for solution to compile sources conditionally from the entrypoint.

Thanks

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.