Skip to content

WriteFileHeader with a multi-line copyright emits uncommented lines, so the generated file doesn't compile #26

Description

@matt-edmondson

What's wrong

GeneratorBase.WriteFileHeader (SourceGeneratorToolkit/GeneratorBase.cs:128) writes // {copyright} once. Only the first line of the copyright is commented. The parameter is meant to keep the generated header in step with the consumer's file-header template, and those templates are often more than one line. Nothing rejects a line break. WriteSourceFile goes through the same code.

Repro

WriteFileHeader(codeBlocker, "Copyright (c) Example.\nAll rights reserved.");
codeBlocker.WriteLine("namespace Generated { }");

Output:

// Copyright (c) Example.
All rights reserved.
// <auto-generated />

When compiled with CSharpCompilation this gives 4 errors (CS1003, CS1002, CS0246 'All', …). In a real consumer the error shows up in the generated file rather than at the point where the copyright was configured.

Suggested fix

Split the copyright on \r\n, \n and \r, and write // plus each line, using a bare // for empty lines. If a single line is the intended contract, throw ArgumentException on a line break instead of emitting broken code. Add a test that compiles the header produced from a multi-line copyright.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    readyFully specified; implement as written

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions