question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Guidance or suggestions needed for when .proto files are generating classes

See original GitHub issue

I have projects that have .proto files in them and that are using the Grpc.Tools nuget package. That package will “compile” the contents of the proto file into c# classes whenever the proto is changed and saved or when the project is built.

This generated code does not carry the [ExcludedFromCodeCoverage] attribute, nor is it in a .g.cs file, but it does have an //<AutoGenerated> on the first line, and all of the code is inside of a #region Designer generated code region. Coverlet is currently including this code as “not covered”, but as its code that is generated by a 3rd party tool, I think it should be excluded from the coverage evaluation.

What should or can I do here to avoid having this code be included in the coverage evaluation?

EDIT For completeness, the things that are commonly used to denote code that is generated and that should be excluded are

  • [ExcludeFromCodeCoverage]
  • [DebuggerNonUserCode]
  • [GeneratedCode]
  • files ending in .g.cs, .g.vb, or similar
  • files ending in .designer.cs, .designer.vb, or similar
  • code inside a region #region Windows Form Designer generated code
  • There are one or two more that I cant remember at the moment.

protobuf compiler uses none of these conventions and instead uses

  • code inside a region #region Designer generated code
  • a comment preceding code in the file the form of an xml element that looks like // <AutoGenerated>

An example proto file contents

syntax = "proto3";
option csharp_namespace = "Core.Schema";
package example_data_schema;

message ExampleData {
	bytes id = 1;
	string name  = 3;
	int32 code = 5;
	string description = 7;
}

generates a code file that starts like this

// <auto-generated>
//     Generated by the protocol buffer compiler.  DO NOT EDIT!
//     source: Protos/example_data_payloads.proto
// </auto-generated>
#pragma warning disable 1591, 0612, 3021
#region Designer generated code

using pb = global::Google.Protobuf;
using pbc = global::Google.Protobuf.Collections;
using pbr = global::Google.Protobuf.Reflection;
using scg = global::System.Collections.Generic;
namespace Core.Payloads {

  /// <summary>Holder for reflection information generated from Protos/example_data_payloads.proto</summary>
  public static partial class ExampleDataPayloadsReflection {

    #region Descriptor
    /// <summary>File descriptor for Protos/example_data_payloads.proto</summary>
    public static pbr::FileDescriptor Descriptor {
      get { return descriptor; }
    }
    private static pbr::FileDescriptor descriptor;

    static ExampleDataPayloadsReflection() {
      byte[] descriptorData = global::System.Convert.FromBase64String(
//400 more lines of protocol buffer support

Also linking https://github.com/grpc/grpc/issues/25950

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
StingyJackcommented, May 17, 2021

@petli - once the GrpcTools version that marks the generated code as [GeneratedCode] is released I think the runsettings is going to be the way to go since we can just push a single file for every repo, or alter the build defs to create/modify to include one. I didnt consider the runsettings since we use a mix of mstest, nunit, and xunit, and had mentally associated it as being an mstest thing.

2reactions
petlicommented, May 11, 2021

I think it is because there is no IncludeByAttribute, so users who would want code coverage on [GeneratedCode] would have no way of getting that. [ExcludeFromCoverage]/[ExcludeFromCodeCoverage] are the only two attributes that clearly states that code should be excluded.

Adding [GeneratedCode] to the list of standard exclusion would be a backward-incompatible change, so it would require a new major version in addition to a mechanism to revert to the old behaviour.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Proto Best Practices
Proto generated code is referred to in normal code. Ensure that options in .proto file do not result in generation of code which...
Read more >
Style Guide | Protocol Buffers Documentation
This topic provides direction for how best to structure your proto definitions. This document provides a style guide for .proto files. By following...
Read more >
Compiling Google Protobuf Binding Classes
Perform the following steps to compile the Google Protobuf (GPB) ... have ap_client.proto file, the next step is to generate the classes for ......
Read more >
Introduction to Google Protocol Buffer
Learn the basic concepts of the Google's Protocol Buffers ... The java-out specifies a directory where the generated class will be created.
Read more >
Packaging Generated Code for gRPC Services
Learn how we package generated code from Protocol Buffer files to have a simple way to create, modify and integrate gRPC services.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found