Skip to content

Commit

Permalink
Add code to reproduce the issue [_ReferenceError: proto is not define…
Browse files Browse the repository at this point in the history
  • Loading branch information
yaobinwen committed Dec 6, 2022
0 parents commit a12b81b
Show file tree
Hide file tree
Showing 32 changed files with 21,889 additions and 0 deletions.
103 changes: 103 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# protobuf-javascript: Try to reproduce "'proto' is not defined"

## Overview

This repository aims to reproduce the "'proto' is not defined" error as described in [_ReferenceError: proto is not defined #10_](https://github.com/protocolbuffers/protobuf-javascript/issues/10) to help the developers figure out whether there is a bug and, if there is, how to fix it.

There two sub-directories:

- `noejs` is a `node.js` native app that cannot reproduce the error.
- `react-app` is a React.js app that can reproduce the error.

I'll first mention the other existing work in the next section and then describe how to use these two apps.

## Other work

The issue [1] [_Failed to compile. 'proto' is not defined (also 'COMPILED') #447_](https://github.com/grpc/grpc-web/issues/447) in `grpc-web` mentions a similar problem that used TypeScript. The quick workaround was to disable `eslint` as mentioned [in this comment](https://github.com/grpc/grpc-web/issues/447#issuecomment-459122012). Others in the discussion also provided alternative workarounds. See [this](https://github.com/grpc/grpc-web/issues/447#issuecomment-460420957), [this](https://github.com/grpc/grpc-web/issues/447#issuecomment-491628171), [this](https://github.com/grpc/grpc-web/issues/447#issuecomment-564285432), [this](https://github.com/grpc/grpc-web/issues/447#issuecomment-568559394), and [this](https://github.com/grpc/grpc-web/issues/447#issuecomment-586668982).

The issue also mentions the Pull Request [_Disable static checkers on generated js files #752_](https://github.com/grpc/grpc-web/pull/752). But note that this PR is for `grpc-web` so it only fixes the output of `--grpc-web_out` but not `--js_out`.

But [1] gave me the hint to try to disable `eslint` in the React.js app and that did work around the issue.

## How to use

## Environment

My environment:
- Ubuntu 18.04 Desktop
- `npm`: 6.14.15
- `yarn`: 1.22.15
- `node` (or `nodejs`): 14.18.2
- `google-protobuf`: 3.19.4

### `nodejs`

`cd` into the sub-directory `nodejs` and run `run.sh`. This should print `good` at the end and doesn't report any error.

### `react-app`

There are two shell scripts:
- `run-npm.sh`
- `run-yarn.sh`

Running `run-npm.sh` produced the following output (which suggests `eslint` might be the cause):

```
Failed to compile.
[eslint]
src/foo_pb.js
Line 26:1: 'proto' is not defined no-undef
Line 29:15: 'proto' is not defined no-undef
Line 30:20: 'COMPILED' is not defined no-undef
Line 31:3: 'proto' is not defined no-undef
Line 46:1: 'proto' is not defined no-undef
Line 47:10: 'proto' is not defined no-undef
Line 60:1: 'proto' is not defined no-undef
Line 78:1: 'proto' is not defined no-undef
Line 80:17: 'proto' is not defined no-undef
Line 81:10: 'proto' is not defined no-undef
Line 92:1: 'proto' is not defined no-undef
Line 116:1: 'proto' is not defined no-undef
Line 118:3: 'proto' is not defined no-undef
Line 130:1: 'proto' is not defined no-undef
Line 146:1: 'proto' is not defined no-undef
Line 152:1: 'proto' is not defined no-undef
Line 157:29: 'proto' is not defined no-undef
Search for the keywords to learn more about each error.
WARNING in [eslint]
src/App.js
Line 4:7: 'proj' is assigned a value but never used no-unused-vars
src/foo_pb.js
Line 12:14: The Function constructor is eval no-new-func
Line 61:7: 'f' is defined but never used no-unused-vars
Line 80:13: Missing '()' invoking a constructor new-parens
ERROR in [eslint]
src/foo_pb.js
Line 26:1: 'proto' is not defined no-undef
Line 29:15: 'proto' is not defined no-undef
Line 30:20: 'COMPILED' is not defined no-undef
Line 31:3: 'proto' is not defined no-undef
Line 46:1: 'proto' is not defined no-undef
Line 47:10: 'proto' is not defined no-undef
Line 60:1: 'proto' is not defined no-undef
Line 78:1: 'proto' is not defined no-undef
Line 80:17: 'proto' is not defined no-undef
Line 81:10: 'proto' is not defined no-undef
Line 92:1: 'proto' is not defined no-undef
Line 116:1: 'proto' is not defined no-undef
Line 118:3: 'proto' is not defined no-undef
Line 130:1: 'proto' is not defined no-undef
Line 146:1: 'proto' is not defined no-undef
Line 152:1: 'proto' is not defined no-undef
Line 157:29: 'proto' is not defined no-undef
Search for the keywords to learn more about each error.
webpack compiled with 1 error and 1 warning
```

Running `run-yarn.sh` produced the same output as `run-npm.sh`.
7 changes: 7 additions & 0 deletions nodejs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules

# protobuf generated files
*_pb.js
13 changes: 13 additions & 0 deletions nodejs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions nodejs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "protobuf-js-issue",
"version": "0.1.0",
"private": true,
"dependencies": {
"google-protobuf": "^3.19.4"
},
"scripts": {
"start": "nodejs ./tests/test.js",
"proto": "protoc --proto_path=./protos/ --js_out=import_style=commonjs,binary:./protos/ protos/*.proto"
}
}
8 changes: 8 additions & 0 deletions nodejs/protos/bar.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
syntax = "proto3";

package example;

message Bar {
/// Some variable.
string bar_name = 1;
}
8 changes: 8 additions & 0 deletions nodejs/protos/foo.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
syntax = "proto3";

package example;

message Foo {
/// Some variable.
string foo_name = 1;
}
11 changes: 11 additions & 0 deletions nodejs/protos/proj.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
syntax = "proto3";

package example;

import "foo.proto";
import "bar.proto";

message project {
Foo foo = 1;
Bar bar = 2;
}
7 changes: 7 additions & 0 deletions nodejs/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh

npm install || exit

npm run proto || exit

npm run start || exit
9 changes: 9 additions & 0 deletions nodejs/tests/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const foopb = require('../protos/foo_pb');
const barpb = require('../protos/bar_pb');
const projpb = require('../protos/proj_pb');

console.log(foopb);
console.log(barpb);
console.log(projpb);

console.log("good!");
8 changes: 8 additions & 0 deletions nodejs/yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


google-protobuf@^3.19.4:
version "3.21.2"
resolved "https://registry.yarnpkg.com/google-protobuf/-/google-protobuf-3.21.2.tgz#4580a2bea8bbb291ee579d1fefb14d6fa3070ea4"
integrity sha512-3MSOYFO5U9mPGikIYCzK0SaThypfGgS6bHqrUGXG3DPHCrb+txNqeEcns1W0lkGfk0rCyNXm7xB9rMxnCiZOoA==
26 changes: 26 additions & 0 deletions react-app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# protobuf generated files
*_pb.js

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
70 changes: 70 additions & 0 deletions react-app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Getting Started with Create React App

This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).

## Available Scripts

In the project directory, you can run:

### `npm start`

Runs the app in the development mode.\
Open [http://localhost:3000](http://localhost:3000) to view it in your browser.

The page will reload when you make changes.\
You may also see any lint errors in the console.

### `npm test`

Launches the test runner in the interactive watch mode.\
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.

### `npm run build`

Builds the app for production to the `build` folder.\
It correctly bundles React in production mode and optimizes the build for the best performance.

The build is minified and the filenames include the hashes.\
Your app is ready to be deployed!

See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.

### `npm run eject`

**Note: this is a one-way operation. Once you `eject`, you can't go back!**

If you aren't satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.

Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you're on your own.

You don't have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn't feel obligated to use this feature. However we understand that this tool wouldn't be useful if you couldn't customize it when you are ready for it.

## Learn More

You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).

To learn React, check out the [React documentation](https://reactjs.org/).

### Code Splitting

This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting)

### Analyzing the Bundle Size

This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size)

### Making a Progressive Web App

This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app)

### Advanced Configuration

This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration)

### Deployment

This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment)

### `npm run build` fails to minify

This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify)
Loading

0 comments on commit a12b81b

Please sign in to comment.