Skip to content

Conversation

@Phillammon
Copy link

What is the goal of this PR?

Compile protobuf files to typescript rather than raw javascript in the nodejs target.

What are the changes implemented in this PR?

Create genrule calling protoc compiler directly with the appropriate plugins to output both javascript and typescript type definition files when built.

.gitignore Outdated
__pycache__

# Node Modules #
node_modules
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having added node_modules, we do not want to add it to git

workspace(name = "graknlabs_protocol")
workspace(
name = "graknlabs_protocol",
managed_directories = {"@npm": ["node_modules"]},
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we've added node modules, we want to be able to use the modules as targets, which necessitates managing it as a directory.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like we should rename @npm to @node_modules for clarity - what do you think?

We would make the same change in client-nodejs.

name = "npm",
package_json = "//:package.json",
package_lock_json = "//:package-lock.json",
)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Performing an npm_install is required in order to get said node modules up to date and in the build, and we need to set up the nodejs rules so that we can fetch them in. (The node modules specifically required are the protoc compiler and the gen_grpc plugin for it)

"protobuf/session_pb.d.ts",
"protobuf/session_pb.js",
"protobuf/transaction_pb.d.ts",
"protobuf/transaction_pb.js",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set of outputs changed to reflect we a) now only have a single service and b) we now have both a .js and a .d.ts file for every incoming proto file.

--grpc_out='./$(@D)/' \
--ts_out='./$(@D)/' \
--proto_path=`dirname $(execpath //:WORKSPACE)` \
$(execpaths //protobuf:proto-raw-buffers);",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This command is a bit brave, but divided by line:

  • Symlinks the node runfiles required by the typescript plugin into the directory where the protocol compiler can see it
  • Runs the node protoc compiler
  • Using the protoc-gen-ts plugin to generate typescript type definitions in addition to the javascript definitions
  • Outputting the javascript, typescript, and service definitions to the output directory specified by bazel ($(@d))
  • Looking in the workspace for the relative paths to the .proto files
  • using the .proto files as specified in the proto-raw-buffers filegroup as the inputs

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is excellent documentation, nice work! I think we should add it to the code itself, in comments, so this doesn't get lost in the ether.

"@npm//google-protobuf",
"//protobuf:proto-raw-buffers",
"//:WORKSPACE"
],
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The required tools for the above command. Workspace only has to be included in order to find the relative path for the .proto files.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, let's add this as a comment in the code itself 🙂 Something like: "We use //:WORKSPACE to find the relative path for the .proto files".

"skipLibCheck": true, /* Skip type checking of declaration files. */
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
}
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standard tsconfig file generated automatically by tsc, for tsc.

"grpc-tools": "^1.9.1",
"grpc_tools_node_protoc_ts": "^5.0.1"
}
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Node dependencies in order to compile typescript

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need both package.json files?

"session.proto",
"transaction.proto",
]
)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The libraries we currently have pre-compile the .proto files, which is inconvenient as we wish to recompile them. Where we could (and in future should) build a full blown bazel rule extracting .src_file from the proto_library targets, for the time being providing a filegroup is equally legitimate

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally with you on this one - let's raise a GitHub issue for it (in graknlabs/protocol) and possibly put a TODO in the source code linking to the issue so it doesn't get lost.

@Phillammon Phillammon requested review from alexjpwalker, haikalpribadi and lolski and removed request for alexjpwalker and lolski November 30, 2020 20:40
py_library(
name = "protocol",
srcs = ["protocol_pkg"],
srcs = ["protocol-pkg"],
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed for consistency on the way past

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice effort!

However, will a Bazel target with - rather than _ work in Python @vmax ?

},
"devDependencies": {
"typescript": "3.9.7"
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Package.json for compiling the npm package

@Phillammon Phillammon requested a review from lolski November 30, 2020 20:43
Copy link
Member

@alexjpwalker alexjpwalker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall this looks excellent. I've highlighted a couple of trailing newlines and suggested that we add relevant documentation to GitHub and the source code where applicable.

workspace(name = "graknlabs_protocol")
workspace(
name = "graknlabs_protocol",
managed_directories = {"@npm": ["node_modules"]},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like we should rename @npm to @node_modules for clarity - what do you think?

We would make the same change in client-nodejs.

--grpc_out='./$(@D)/' \
--ts_out='./$(@D)/' \
--proto_path=`dirname $(execpath //:WORKSPACE)` \
$(execpaths //protobuf:proto-raw-buffers);",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is excellent documentation, nice work! I think we should add it to the code itself, in comments, so this doesn't get lost in the ether.

"@npm//google-protobuf",
"//protobuf:proto-raw-buffers",
"//:WORKSPACE"
],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, let's add this as a comment in the code itself 🙂 Something like: "We use //:WORKSPACE to find the relative path for the .proto files".

"devDependencies": {
"typescript": "3.9.7"
}
} No newline at end of file
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
}

"grpc-tools": "^1.9.1",
"grpc_tools_node_protoc_ts": "^5.0.1"
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need both package.json files?

"session.proto",
"transaction.proto",
]
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally with you on this one - let's raise a GitHub issue for it (in graknlabs/protocol) and possibly put a TODO in the source code linking to the issue so it doesn't get lost.

export DEPLOY_NPM_PASSWORD=$REPO_GRAKN_PASSWORD
export DEPLOY_NPM_EMAIL=$REPO_GRAKN_EMAIL
bazel run --define version=$(git rev-parse HEAD) //grpc/nodejs:deploy-npm -- snapshot
dependencies: [build, build-dependency, test]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have a job test do we? In that case this should be removed

WORKSPACE Outdated
#########################

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we update the version of rules_nodejs in @graknlabs_dependencies with this one and then have the WORKSPACE file load from there instead? You can get @alexjpwalker 's help for the know-how.

package.json Outdated
@@ -0,0 +1,11 @@
{
"name": "protocol",
"version": "1.0.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VERSION should match the version file shouldn't it? In that case, 2.0.0-alpha?

py_library(
name = "protocol",
srcs = ["protocol_pkg"],
srcs = ["protocol-pkg"],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice effort!

However, will a Bazel target with - rather than _ work in Python @vmax ?

Alistair Crook and others added 2 commits December 1, 2020 10:07
Co-authored-by: Alex Walker <alexjpwalker@gmail.com>
name = "graknlabs_dependencies",
remote = "https://github.com/Phillammon/dependencies",
commit = "8526f49ce77fcd8e9029ab4b08d0fd5d0b4497f8", # sync-marker: do not remove this comment, this is used for sync-dependencies by @graknlabs_dependencies
commit = "63bfceddc8493e743f54ab230239cd42eb1bccdf", # sync-marker: do not remove this comment, this is used for sync-dependencies by @graknlabs_dependencies
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update this before merging

Copy link
Member

@alexjpwalker alexjpwalker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving - just remember to bump dependencies and verify that it builds in Grabl

@lolski lolski merged commit e161ae8 into typedb:master Dec 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants