Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grpc.Tools: Customize path to gRPC codegen plugin via Environment Variable #27099

Closed
mleutner opened this issue Aug 24, 2021 · 10 comments
Closed

Comments

@mleutner
Copy link

Is your feature request related to a problem? Please describe.

Provide an environment variable to specify a custom path to gRPC plugin executable (gRPC_PluginFullPath) for csharp. While there is a environment variable and configuration capabilities for protoc (via export PROTOBUF_PROTOC=</path/to/protoc>, there is non comprehensive way for RPC_Plugin (e.g. /path/to/grpc_csharp_plugin).
This FR is related to the lack of linux-musl_x64 target support and the resulting issue #24188 "Grpc.Tools generation doesn't work in alpine".

Describe the solution you'd like

In _Grpc.Tools.targets provide configuration for a custom environment variable to specify a path to the grpc_csharp_plugin, see TODO(kkm).

Describe alternatives you've considered

An alternative would be to support and distribute linux-musl_x64 targets with Grpc.Tools

Additional context

Alpine is widely used and also one of the supported targets of Microsoft .NET

@jtattermusch
Copy link
Contributor

FTR

<Protobuf_ProtocFullPath Condition=" '$(Protobuf_ProtocFullPath)' == '' ">$(PROTOBUF_PROTOC)</Protobuf_ProtocFullPath>

<gRPC_PluginFullPath Condition=" '$(gRPC_PluginFullPath)' == '' and '$(Protobuf_ToolsOs)' == 'windows' "

@jtattermusch
Copy link
Contributor

@mleutner would you be open to creating a PR that adds the configuration point?

@jtattermusch jtattermusch changed the title Grpc.Tools: Customize path to gRPC plugin via Environment Variable Grpc.Tools: Customize path to gRPC codegen plugin via Environment Variable Mar 14, 2022
@tonydnewell
Copy link
Contributor

Added PR #30411

Added environment variable GRPC_TOOL_PLUGIN to override the grpc_csharp_plugin location/executable

@tonydnewell
Copy link
Contributor

Renamed the env variable to GRPC_PROTOC_PLUGIN

@cwhsu1984
Copy link

yesterday I upgrade my project from net6 to net7 and getting this error when doing dotnet test
my image is dotnet/sdk:7.0.100-alpine3.16-amd64

the command that gets error

    - dotnet restore
    - apk update
      && apk --no-cache add protobuf grpc
      && path="$(dirname "$(find /root/.nuget/packages/grpc.tools -type d -name linux_x64 | head -1)")"
      && ln -sf /usr/bin/protoc $path/linux_x64/protoc
      && ln -sf /usr/bin/grpc_csharp_plugin $path/linux_x64/grpc_csharp_plugin
    - dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:CoverletOutput=./coverage/ ./$PROJECT_NAME.UnitTests --no-restore

the error shows

/root/.nuget/packages/grpc.tools/2.49.0/build/_protobuf/Google.Protobuf.Tools.targets(280,5): error MSB6003:
The specified task executable "/root/.nuget/packages/grpc.tools/2.49.0/tools/linux_x64/protoc" could not be run. System.ComponentModel.Win32Exception (2): An error occurred trying to start process 
'/root/.nuget/packages/grpc.tools/2.49.0/tools/linux_x64/protoc' with working directory '/path/to/myporj'. No such file or 
directory [/path/to/myporj/myporj.csproj]

Is this due to the same reason?

@tonydnewell
Copy link
Contributor

@cwhsu1984 This looks the same as #24188 - see that issue for workarounds

The issue you've commented on here enables a workaround by letting you to set the GRPC_PROTOC_PLUGIN environment variable to point to your own protoc plugin that has been built to work on Alpine. Again - see #24188

@cwhsu1984
Copy link

I try belows but failed for the same reason, any suggestion?

use script

script:
    - dotnet restore 
    - apk update
      && apk --no-cache add protobuf grpc
      && path="$(dirname "$(find /root/.nuget/packages/grpc.tools -type d -name linux_x64 | head -1)")"
      && ln -sf /usr/bin/protoc $path/linux_x64/protoc
      && ln -sf /usr/bin/grpc_csharp_plugin $path/linux_x64/grpc_csharp_plugin
    - export GRPC_PROTOC_PLUGIN=$path/linux_x64/grpc_csharp_plugin
    - dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:CoverletOutput=./coverage/ ./$PROJECT_NAME.UnitTests.csproj --no-restore

use before_script

before_script:
     - apk update
       && apk --no-cache add protobuf grpc
       && path="$(dirname "$(find /root/.nuget/packages/grpc.tools -type d -name linux_x64 | head -1)")"
       && ln -sf /usr/bin/protoc $path/linux_x64/protoc
       && ln -sf /usr/bin/grpc_csharp_plugin $path/linux_x64/grpc_csharp_plugin
     - export GRPC_PROTOC_PLUGIN=$path/linux_x64/grpc_csharp_plugin
script:
     - dotnet restore
     - dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:CoverletOutput=./coverage/ ./$PROJECT_NAME.UnitTests.csproj --no-restore

@tonydnewell
Copy link
Contributor

@cwhsu1984 Make sure you have Grpc.Tools version 2.50.0 or later
Also you may want to set PROTOBUF_PROTOC to point to the protoc executable (see #27099 (comment))

@cwhsu1984
Copy link

@tonydnewell after doing what you told me, my unit tests work now, thanks!

To sum up,

  • upgrade Grpc.Tools 2.50.0
  • export PROTOBUF_PROTOC and GRPC_PROTOC_PLUGIN

belows are modified script that works for me

  script:
    - dotnet restore
    - apk update
      && apk --no-cache add protobuf grpc
      && path="$(dirname "$(find /root/.nuget/packages/grpc.tools -type d -name linux_x64 | head -1)")"
      && ln -sf /usr/bin/protoc $path/linux_x64/protoc
      && ln -sf /usr/bin/grpc_csharp_plugin $path/linux_x64/grpc_csharp_plugin
    - export PROTOBUF_PROTOC=$path/linux_x64/protoc
    - export GRPC_PROTOC_PLUGIN=$path/linux_x64/grpc_csharp_plugin
    - dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:CoverletOutput=./coverage/ ./$PROJECT_NAME.UnitTests.csproj --no-restore

@vahpetr
Copy link

vahpetr commented Dec 19, 2022

I got the some error on M1 docker build. Also you write something like as:

# Dockerfile
FROM mcr.microsoft.com/dotnet/sdk:7.0-alpine AS builder
RUN apk update --no-cache && apk upgrade --no-cache
RUN apk add --no-cache ca-certificates wget
RUN wget -q -O /etc/apk/keys/sgerrand.rsa.pub https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub
RUN wget https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.35-r0/glibc-2.35-r0.apk
RUN apk add --no-cache --force-overwrite glibc-2.35-r0.apk
RUN apk fix --force-overwrite alpine-baselayout-data
RUN apk add --no-cache protoc grpc
ENV PROTOBUF_PROTOC=/usr/bin/protoc GRPC_PROTOC_PLUGIN=/usr/bin/grpc_csharp_plugin

# build here

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

No branches or pull requests

7 participants