bufbuild / buf

The best way of working with Protocol Buffers.
https://buf.build
Apache License 2.0
8.89k stars 265 forks source link

`buf build` thinks .proto file is directory that should contain Buf workspace file #3183

Closed rofrankel closed 1 month ago

rofrankel commented 1 month ago

GitHub Repository

https://github.com/bufbuild/buf

Commands

// Given a filename, return a DescFile.
export function readProtoFile(
  filename: string,
  protoRoot: string
): protobuf.DescFile {
  // Create a temporary file.
  const tmpDir = tmp.dirSync({ unsafeCleanup: true });

  const imagePath = path.join(tmpDir.name, "image.bin");

  // Use buf to generate an image of the file descriptor.
  const args = ["build", "-o", imagePath, protoRoot, "--path", filename];
  const result = spawnSync("buf", args);
  let error;
  if (result.error) {
    error = result.error;
  } else if (result.status !== 0) {
    error = result.stderr.toString();
  }

  if (error) {
    throw new Error(
      `buf build failed: ${error}\n\ncommand: buf ${args.join(" ")}`
    );
  }

  // ...
}

Output

buf build failed: Failure: stat /tmp/vnJL2j/path/to/my.proto/buf.work.yaml: not a directory

    command: buf build -o /tmp/tmp-545545-9JZ9IzSiyrH2/image.bin /tmp/vnJL2j --path /tmp/vnJL2j/path/to/my.proto

Expected Output

buf build should succeed

Anything else?

This is happening in the unit tests of an NPM package with a dependency that runs buf build to compile .proto files and then do stuff with the FileDescriptorSet. The issue occurs on GHA Linux workers, but I can't reproduce it locally on my Windows laptop (where the tests pass).

bufdev commented 1 month ago

I’ve just taken a quick look at this and without a repro, I’m honestly not sure how to diagnose it - @doriable and I wrote the code here, and it’s an extremely weird error and I’d need more information on what is within /tmp/vnJL2j to diagnose. Perhaps something will spring to @doriable's mind but I'm at a loss without further info. Is there any way to provide a redacted version of /tmp/vnJL2j, even just the file tree?

doriable commented 1 month ago

Was able to get a repro and an approximation on the root cause, working on a fix today.

rofrankel commented 1 month ago

Whoa, amazing, thanks for the quick response!

bufdev commented 1 month ago

This has been fixed in 1.35.1.

rofrankel commented 1 month ago

I had to ask an internal team to sync our cache of the action (we cache all public GHAs) to 1.35.1, so it took me a day to be able to try this out...but with 1.35.1 I am still seeing the same issue.

Run Roblox-ActionsCache/bufbuild-buf-setup-action@v1
Setting up buf version "1.35.1"
Resolving the download URL for the current platform...
Downloading buf version "1.35.1" from https://github.com/bufbuild/buf/releases/download/v1.35.1/buf-Linux-x8[6](https://<repo>/actions/runs/10537863/job/31272702?pr=272#step:6:7)_64.tar.gz
Successfully downloaded buf version "1.35.1" from https://github.com/bufbuild/buf/releases/download/v1.35.1/buf-Linux-x86_64.tar.gz
Extracting buf...
/usr/bin/tar xz --warning=no-unknown-keyword --overwrite -C /actions-runner/_work/_temp/06b26d90-0382-43[7](https://<repo>/actions/runs/10537863/job/31272702?pr=272#step:6:8)4-be88-d563541cf491 -f /actions-runner/_work/_temp/4b7192de-0e[8](https://<repo>/actions/runs/10537863/job/31272702?pr=272#step:6:9)9-442b-b9af-97f6675141b3
Successfully extracted buf to /actions-runner/_work/_temp/06b26d[9](https://<repo>/actions/runs/10537863/job/31272702?pr=272#step:6:10)0-0382-4374-be88-d563541cf491
Adding buf to the cache...
Successfully cached buf to /actions-runner/_work/_tool/buf/1.35.1/x64
Adding buf binary to PATH
Successfully setup buf version 1.35.1
1.35.1

buf_user and buf_token are not supplied, not logging into Buf Schema Registry

Let me know if trying to create a public repro would be a good next step.

bufdev commented 1 month ago

Maybe I'm missing something, but I don't see an issue in what you copy/pasted above - can you double check?

rofrankel commented 1 month ago

Sorry, I just pasted that to show that I was indeed picking up the new version of Buf. Same exact error as before:

    buf build failed: Failure: stat /tmp/TzuFG9/path/to/my.proto/buf.work.yaml: not a directory

    command: buf build -o /tmp/tmp-238025-zB5jjgqsMIKo/image.bin /tmp/TzuFG9 --path /tmp/TzuFG9/path/to/my.proto
rofrankel commented 1 month ago

OK, I created https://github.com/rofrankel/buf-3183 with the contents - let me know how else I can help.

doriable commented 1 month ago

I cloned the repro and ran the following commands using v1.35.1 and did not get the build error:

$ buf build /Users/dkeung/src/playground/buf-3183/buf-3183/contents --path /Users/dkeung/src/playground/buf-3183/buf-3183/contents/organization/api_platform/api/documentation_metadata.proto
$ buf build /Users/dkeung/src/playground/buf-3183/buf-3183/contents --path /Users/dkeung/src/playground/buf-3183/buf-3183/contents/aep/api/field_info.proto
$ buf build /Users/dkeung/src/playground/buf-3183/buf-3183/contents --path /Users/dkeung/src/playground/buf-3183/buf-3183/contents/organization/api_platform/virtual_library/v1/resources/book__resource.proto

This is the file tree, just double checking:

$ tree .
.
├── README.md
└── contents
    ├── aep
    │   └── api
    │       ├── field_info.proto
    │       └── idempotency_key.proto
    ├── buf
    │   └── validate
    │       ├── expression.proto
    │       ├── priv
    │       │   └── private.proto
    │       └── validate.proto
    ├── google
    │   ├── api
    │   │   ├── annotations.proto
    │   │   ├── client.proto
    │   │   ├── field_behavior.proto
    │   │   ├── http.proto
    │   │   └── resource.proto
    │   └── rpc
    │       └── status.proto
    └── organization
        ├── api
        │   ├── api_method.proto
        │   ├── documentation_metadata.proto
        │   └── field_validity.proto
        └── api_platform
            ├── api
            │   ├── api_method.proto
            │   ├── documentation_metadata.proto
            │   └── field_validity.proto
            └── virtual_library
                └── v1
                    ├── custom_methods
                    │   ├── book
                    │   │   └── burn_book__custom_method.proto
                    │   └── librarian
                    │       ├── compare_librarian__custom_method.proto
                    │       └── survey_librarians__custom_method.proto
                    ├── operations
                    │   └── librarian
                    │       ├── create_librarian_metadata__metadata.proto
                    │       └── create_librarian_metadata__metadata_other.proto
                    └── resources
                        ├── book__resource.proto
                        └── librarian__resource.proto

22 directories, 25 files
rofrankel commented 1 month ago

Hmm...the specific file that failed was organization/api_platform/virtual_library/v1/operations/librarian/create_librarian_metadata__metadata.proto.

I'll see if I can find a way to repro it locally (as opposed to on our GHES GHA runners) and follow up.

doriable commented 1 month ago

We looped through all the paths and tested each one and didn't run in to the error with v1.35.1, so unfortunately we'll need to defer to you to see if we can get more information on a local repro.

rofrankel commented 1 month ago

OK, I found the issue: I had upgraded buf-setup-action, but I had an older version of buf in the dependencies list in my package.json. Of course it was something silly like that. Thanks again for the fix!

doriable commented 1 month ago

Haha, I'm glad this is resolved!