1
votes

I have a Makefile for go project which has one step to generate protobuf sources. The minimal reproducible example is (assuming all required dependencies are installed and proto files are located correctly):

OUT := ${CURDIR}/go/proto
GOPATH := $(shell go env GOPATH)
PROTOC := env PATH=$(GOPATH)/bin:${PATH} protoc

all:
    $(PROTOC) --go_out=plugins=grpc:$(OUT) --go_opt=paths=source_relative *.proto

You can check the full version here: https://github.com/cqfn/degitx/blob/master/proto/Makefile

It works correctly on Linux and CI pipeline, but it fails on Windows under WSL with error:

syntax error: "(" unexpected

The failing line is $(PROTOC) --go_out=plugins=grpc:$(OUT) --go_opt=paths=source_relative *.proto.

It seems Windows has some braces in PATH env variable, make --debug shows it as:

env PATH=/home/username/go/bin:/home/username/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/mnt/c/Program Files (x86)/VMware/VMware Player/bin/:/mnt/c/Ruby25-x64/bin:/mnt/c/WINDOWS:/mnt/c/WINDOWS/system32:/mnt/c/WINDOWS/System32/Wbem:/mnt/c/WINDOWS/System32/WindowsPowerShell/v1.0:/mnt/c/Windows:/mnt/c/Windows/system32:/mnt/c/Windows/System32/Wbem:/mnt/c/Windows/System32/WindowsPowerShell/v1.0:/mnt/c/WINDOWS/System32/WindowsPowerShell/v1.0/:/mnt/c/WINDOWS/System32/OpenSSH/:/mnt/c/Program Files (x86)/NVIDIA Corporation/PhysX/Common:/mnt/c/Program Files/NVIDIA Corporation/NVIDIA NvDLISR:/mnt/c/Go/bin:/mnt/c/Program Files/Git/cmd:/mnt/c/Program Files/CMake/bin:/mnt/c/WINDOWS/system32:/mnt/c/WINDOWS:/mnt/c/WINDOWS/System32/Wbem:/mnt/c/WINDOWS/System32/WindowsPowerShell/v1.0/:/mnt/c/WINDOWS/System32/OpenSSH/:/mnt/c/Program Files/PowerShell/7/:/mnt/c/Program Files/Docker/Docker/resources/bin:/mnt/c/ProgramData/DockerDesktop/version-bin:/mnt/c/Ruby26-x64/bin:/mnt/c/Users/username/AppData/Local/Microsoft/WindowsApps:/mnt/c/Program Files/JetBrains/IntelliJ IDEA 2020.2.1/bin:/mnt/c/Users/username/go/bin:/mnt/c/Program Files/JetBrains/GoLand 2020.2.2/bin:/mnt/c/Program Files/JetBrains/RubyMine 2020.2.1/bin:/snap/bin:/home/username/go/bin:/home/username/.local/bin protoc --go_out=plugins=grpc:/mnt/c/Users/username/GitHub/degitx/proto/go/degitxpb --go_opt=paths=source_relative *.proto

Pay attention to :/mnt/c/Program Files (x86)/* PATH entries.

How to run correctly this script on Windows to avoid this error? Maybe I need to escape environment variables somehow?

1

1 Answers

2
votes

In POSIX shells you need to apply quoting, something like this:

PROTOC := env PATH="$(GOPATH)/bin:${PATH}" protoc