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

Moving protos out of protoc-gen-openapiv2/options directory to support Python #4407

Open
wsoesanto-arbo opened this issue Jun 5, 2024 · 3 comments

Comments

@wsoesanto-arbo
Copy link

🚀 Feature

Python codebase that is using Proto + Bazel would like to depend on those protos. Unfortunately having the protos being listed in a directory that has - causes issue with py_proto_library. Python proto compiler replaces "-" with "_" which means it resides in different directory (Ref).

Is it possible if we move it to another directory without "-"? Thanks!

@johanbrandhorst
Copy link
Collaborator

Hi, thanks for your issue! I think this is very likely to break other users unfortunately. I'm sympathetic to the problem, but we can't break existing users. Are you able to do some other workaround perhaps? I'm not very familiar with bazel unfortunately.

@AlejoAsd
Copy link
Contributor

AlejoAsd commented Sep 5, 2024

I am unfortunately running into the same issue, and it looks like a reasonable Bazel solution is to define a genrule, or perhaps update py_proto_library to account for this renaming, although I have no idea if this is feasible.

I want to note that the replacement of dashes with underscores is happening due to a Python protoc compiler policy and not because of Bazel.

This makes the output with underscores intended behavior and not specifically a Bazel issue, so I would expect other users of this repo using Python to also run into this issue, to some degree. When compiling protos manually, this is an easy fix because you can just go to the directory using underscores, but for more automated build systems this might be a problem.

@wsoesanto-arbo
Copy link
Author

I ran the following bash scripts to generate the patch to rename the directory and apply it on my http_archive's patches. Hopefully it helps!

#!/bin/bash

# This script moves the directory `protoc-gen-openapiv2/options` to `protoc_gen_openapiv2/options`
# and replaces any occurrences of `protoc-gen-openapiv2/options` with `protoc_gen_openapiv2/options`
# in all files within the current directory and its subdirectories.
# This change is necessary because Python does not support directories with the `-` character.

# Define the source and destination directories
SOURCE_DIR="protoc-gen-openapiv2/options"
DEST_DIR="protoc_gen_openapiv2/options"

# Check if the first argument is provided for the output file
if [ -z "$1" ]; then
  echo "Please provide an output file for the git diff."
  exit 1
fi

OUTPUT_FILE=$1

echo "Starting the directory move and string replacement script."

# Check if the source directory exists, exit if it doesn't
if [ ! -d "$SOURCE_DIR" ]; then
  echo "Source directory $SOURCE_DIR does not exist."
  exit 1
fi

# Create the destination directory if it does not exist
mkdir -p "$DEST_DIR"

# Move the contents from the source to the destination directory
mv "$SOURCE_DIR"/* "$DEST_DIR"

# Remove the old source directory
rmdir "$SOURCE_DIR"

echo "Directory moved successfully from $SOURCE_DIR to $DEST_DIR"

# Find and replace the string in all files within the current directory and its subdirectories
find . -type f -exec sed -i 's|protoc-gen-openapiv2/options|protoc_gen_openapiv2/options|g' {} +

echo "String replacement completed in all files under the current directory"

# Add all changes to git staging
git add .

# Create a git diff patch file
git diff --staged --patch --no-prefix > "$OUTPUT_FILE"

echo "Git diff patch created at $OUTPUT_FILE"

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

No branches or pull requests

3 participants