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/proto-loader and readFileSync fails to load files in Vercel/Netlify #245

Closed
Richard87 opened this issue Nov 7, 2021 · 2 comments
Closed

Comments

@Richard87
Copy link

Hi!

I'm struggling with including files in my api functions. Specifically I want to include proto files, but everything is failing now...

All code works flawless when running next dev, but failing on Vercel and on Netlify, also when running netlify dev

All code is here: https://github.com/richard87/protoloader

I have created a simple reproduction of the error, and deployed it at Vercel and Netlify to test:
https://protoloader.vercel.app/api/json / https://protoloader.vercel.app/api/grpc
https://protoloader-test.netlify.app/api/json / https://protoloader-test.netlify.app/api/grpc

This is my grpc.js api function:

const protoLoader = require('@grpc/proto-loader');

const grpcOptions = {
  keepCase: true,
  longs: String,
  enums: String,
  defaults: true,
  oneofs: true
};

const protoFile = "test.proto";

export const config = {
  unstable_includeFiles: ['test.proto'],
};

export default function handler(req, res) {
  const packageDefinition = protoLoader.loadSync( protoFile, grpcOptions);
  res.status(200).json(packageDefinition)
}

And my vercel.json file:

{
  "functions": {
    "api/json.js": {
      "includeFiles": "test.json"
    },
    "api/grpc.js": {
      "includeFiles": "test.proto"
    }
  }
}

With the error log:

Vercel:
2021-11-07T19:43:37.835Z	dd740350-1ac3-43c1-819b-cfcfe848babc	ERROR
	Error: ENOENT: no such file or directory, open 'test.proto' 
   at Object.openSync (fs.js:498:3)
    at Object.readFileSync (fs.js:394:35)
    at fetch (/var/task/node_modules/protobufjs/src/root.js:172:34) 
   at Root.load (/var/task/node_modules/protobufjs/src/root.js:206:13) 
   at Root.loadSync (/var/task/node_modules/protobufjs/src/root.js:247:17)
    at Object.loadProtosWithOptionsSync (/var/task/node_modules/@grpc/proto-loader/build/src/util.js:66:29)
    at Object.loadSync (/var/task/node_modules/@grpc/proto-loader/build/src/index.js:194:31)
    at handler (/var/task/.next/server/pages/api/grpc.js:38:43)
    at Object.apiResolver (/var/task/node_modules/next/dist/server/api-utils.js:102:15)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)

 {  errno: -2,  syscall: 'open',  code: 'ENOENT',  path: 'test.proto'}

Netlify:
{
  "errorType": "Runtime.UnhandledPromiseRejection",
  "errorMessage": "Error: ENOENT: no such file or directory, open 'test.proto'",
  "trace": [
    "Runtime.UnhandledPromiseRejection: Error: ENOENT: no such file or directory, open 'test.proto'",
    "    at process.<anonymous> (/var/runtime/index.js:35:15)",
    "    at process.emit (events.js:314:20)",
    "    at processPromiseRejections (internal/process/promises.js:209:33)",
    "    at processTicksAndRejections (internal/process/task_queues.js:98:32)"
  ]
}

I have looked at clues for a solution in
#194
vercel/next.js#8251

Extra:
Adding this netlify.toml solved the issue on Netlify, but the problem still persists on Vercel

[functions]
  # Includes all Markdown files inside the "files/" directory.
  included_files = ["test.json", "test.proto"]
@styfle
Copy link
Member

styfle commented Nov 11, 2021

I think that unstable_includeFiles isn't used for Next.js APIs yet, only Pages. cc @ijjk

The easiest way to get nft to include a file is to read it like this:

import { readFileSync } from 'fs'
import { join } from 'path'

const file = readFileSync(join(process.cwd(), 'test.proto'));

@styfle
Copy link
Member

styfle commented Dec 17, 2021

This will fix it Richard87/protoloader#1

@styfle styfle closed this as completed Dec 17, 2021
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

Successfully merging a pull request may close this issue.

2 participants