dotnet / dotnet/roslyn

Make GeneratorDriverOptions.BaseDirectory public

Open
#75,387 9 comments 0 reactions 0 assignees View on GitHub
api-approved Area-Compilers blocking Concept-API Feature Request
Dominant language
C#
Stars
20.7k
Forks
4.3k
PR merge metrics
PR metrics pending

Description

## Background and Motivation

The compiler uses this option to construct the path of a source generated file. This path is then used to generate metadata name of file-scoped types.

This path is currently only set by the compiler task to the value of `CompilerGeneratedFilesOutputPath` msbuild property. It does not flow from the project system in the IDE. This causes compiler crashes during EnC since the metadata names generated by the detal emit differ from those generated by the compiler during build.

## Proposed API

```diff
namespace Microsoft.CodeAnalysis;

public readonly struct GeneratorDriverOptions
{
// existing public API:
public readonly IncrementalGeneratorOutputKind DisabledOutputs;
public readonly bool TrackIncrementalGeneratorSteps;

public GeneratorDriverOptions(IncrementalGeneratorOutputKind disabledOutputs);
public GeneratorDriverOptions(IncrementalGeneratorOutputKind disabledOutputs, bool trackIncrementalGeneratorSteps);

- internal string? BaseDirectory { get; init; }

+ ///
+ /// Absolute path to the base directory used for file paths of generated files. Usually the project's output directory unless is specified.
+ ///
+ public string? BaseDirectory { get; }

+ ///
+ /// Creates .
+ ///
+ /// Absolute path to the base directory used for file paths of generated files, or null.
+ ///
+ ///
+ /// is specified but not an absolute path.
+ public GeneratorDriverOptions(IncrementalGeneratorOutputKind disabledOutputs = IncrementalGeneratorOutputKind.None, bool trackIncrementalGeneratorSteps = false, string? baseDirectory = null);
}
```

**Update**
Make `baseDirectory` optional.

## Usage Examples

https://github.com/dotnet/roslyn/pull/75311/files#diff-e5be48a46768235a257d73711ef134c71a03f8f896de4a977012e4b85e7bc599R57

## Alternative Designs

Should we make `BaseDirectory` non-nullable? It would only be null if the obsoleted constructors were used.

Alternative:

```diff
public readonly struct GeneratorDriverOptions
{
// existing public API:
public readonly IncrementalGeneratorOutputKind DisabledOutputs;
public readonly bool TrackIncrementalGeneratorSteps;

+ [Obsolete]
public GeneratorDriverOptions(IncrementalGeneratorOutputKind disabledOutputs);

+ [Obsolete]
public GeneratorDriverOptions(IncrementalGeneratorOutputKind disabledOutputs, bool trackIncrementalGeneratorSteps);

+ ///
+ /// Builder that takes a Type of a source generator and a relative file path of a generated file and returns an absolute path of the file.
+ /// The generated absolute path is used for SyntaxTree.FilePath of syntax trees parsed from the generated source file.
+ ///
+ public Func FilePathBuilder { get; }

+ ///
+ /// Creates .
+ ///
+ /// Builds absolute file paths for generated files.
+ ///
+ ///
+ /// is null.
+ public GeneratorDriverOptions(Func filePathBuilder, IncrementalGeneratorOutputKind disabledOutputs = IncrementalGeneratorOutputKind.None, bool trackIncrementalGeneratorSteps = false);
}
```

## Risks

Contributor guide

Open the contributing guide

Research direction

Start with GeneratorDriverOptions and the compiler task path that sets BaseDirectory from CompilerGeneratedFilesOutputPath; compare it with the IDE/project-system path described in the issue. Review the proposed public constructor and property alongside the usage example in PR #75311. Done means the base directory API is public and generated metadata names remain consistent during EnC, including the documented null and absolute-path behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.