-
Notifications
You must be signed in to change notification settings - Fork 333
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
feat(options): Add M option #597
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have any better solutions to "making the npm registry of proto files" in terms of having the tooling know how to map the proto world --> each language binding world, so 🤷 this seems good to me, and if it follows the proto-gen-go approach, then all the better for following precedence.
Just out of curiosity, I saw that Buf has this new Connect/go/etc. plugin; are they solving the "map proto imports --> language imports" in a sufficiently different/interesting way?
I saw the connect announcement on HN, but TBH I know very little about it. From https://github.com/bufbuild/connect-go/blob/main/cmd/protoc-gen-connect-go/main.go, it looks like they are calling protoc-gen-go as a library to produce |
I'm working on an RPC implementation which supports client -> server streams (which connect-go does not, nor does grpc-web or any other library I'm aware of) via using libp2p-mplex over WebSocket. Using protowrap to call the protoc generator: https://github.com/aperturerobotics/starpc/blob/main/Makefile#L101 This PR (or one similar to it) would fix an issue I have with the import paths being generated:
... need to remap this so that the project root - "../../../../github.com/aperturerobotics/protobuf-project" - is instead mapped to something like "../" (path to the repo root). |
@paralin to clarify, are you saying that this feature would cause an issue for you? Or perhaps it's close to what you need, but not quite? |
No! Actually I'm trying to support the case for an option to remap the imports. I think this would fix it (I'll test it out too) Edit: sorry I've noticed I had a typo in the original post |
@paralin thanks for the clarification. Note that the import path is constructed by ts-poet by the maybeRelativePath function, copied here: export function maybeRelativePath(outputPath: string, importPath: string): string {
if (!importPath.startsWith('./')) {
return importPath;
}
// Drop the `./` prefix from the outputPath if it exists.
const basePath = outputPath.replace(/^.\//, '');
// Ideally we'd use a path library to do all this.
const numberOfDirs = basePath.split('').filter((l) => l === '/').length;
if (numberOfDirs === 0) {
return importPath;
}
// Make an array of `..` to get our importPath to the root directory.
const a: string[] = new Array(numberOfDirs);
const prefix = a.fill('..', 0, numberOfDirs).join('/');
return prefix + importPath.substring(1);
} So in your example The prefix is derived from the |
@pcj Thanks for looking into this. I checked the function you suggested & I think I've found a fix. Moved to stephenh/ts-poet#30 |
@stephenh anything else todo before merging this? |
@pcj would you mind creating a new Disclaimer we've got quite a few of those tests at this point. :-) Separately, I had a chance to glance just a bit more at this; I'm still good with the current approach, of using ts-poet as a slice point to rewrite imports; I'm wondering if maybe another approach would have been to do it around here:
Where we'd use something like And then maybe the
As This might be tomato/tomato, but I'm wondering if "catching" the path sooner / before As a disclaimer, I've not personally had to solve the "translating proto import X" --> "language import Y" (across separate protoc invocations/artifacts), so am really deferring to you guys for use cases/the best approach. Thanks! |
@stephenh thanks, I'll take a closer look. The Not sure if there are other side-effects, but that sounds promising. |
I've posted a full example of what I'm trying to accomplish with the import remaps (leveraging the Go vendor/ tree) under stephenh/ts-poet#30 I don't think it's exactly the same issue as what's being addressed here - probably totally separate - although the fixes to use path.relative for maybeRelativePath might be useful. |
@paralin I believe this PR would hopefully help you replace these lines:
With a few Although looking at your
Should do a similar is check of "is I suppose how @pcj 's import map avoids needing to do this, b/c we assume any entry the user put in the |
@stephenh The goal of that code is to leverage the Go vendoring system, which will automatically materialize any imports - i.e. "github.com/aperturerobotics/protobuf-project/example" - into the vendor/ tree with dependency version control, including the .proto and the .ts files there. The Makefile symlinks the project to its fully qualified import path (declared in go.mod) to vendor/. So vendor/github.com/my/project goes to ./. Ts-proto only sees the fully qualified import paths. So, I passed the prefix - "github.com/my/project" - as Any paths which are /not/ in that prefix are considered to be located at ./vendor/, so ts-poet is correctly generating the relative imports - "../../vendor/github.com/..." It seems that the portion of this which checks the module path for a given prefix, and treats those files as being in the project root ./, could be added as a generic option to ts-proto. |
Haven't forgot about this but unable to get the docker integration tests to work on my arm mac. Switching to intel. |
2e85359
to
f8cbf31
Compare
Adds an option to remap import paths.
f8cbf31
to
674bcb0
Compare
@paralin awesome; thanks for rebasing. Two questions: a) I think @pcj was going to try my suggestion of moving the slice point to We can still merge this as-is, and keep relying on ts-poets b) Could you add an integration test that shows the |
@stephenh How would that work if two protobuf files have the same package name but are from different locations in the tree? I guess at least for my situation, updating the import mapping based on the relative path to the .proto file would be fine, But I agree that if the option is |
...how does protoc-go handle it? 🤷 :-)
Yeah, I have the same intuition, but am really deferring to whatever you and @pcj are cool with actually doing the work for. :-) I'm happy to merge this as-is, or hold off until either you or @pcj update it to use the protoc-go style |
👋🏼 i'm interested in seeing this land - is there anything i can do help push this through? it seems like this solves something we want to achieve: saying |
Ah sure, @iamricard , that'd be great; I think where the PR is at is: a) try the approach in this comment: Which would basically be a new PR, that moves the slice point from b) Include an And that's about it, I think. |
sounds good! i'll put something together, and thanks for the guidance 🙂 |
👋🏼 my first attempt at this #672 |
This should be fixed in #672 ; thanks @iamricard ! |
Fixes #596
In conjuction with https://github.com/stephenh/ts-poet/pull/28/files