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

Relative imports to google's type #51

Open
ffortier opened this issue Sep 7, 2023 · 0 comments · May be fixed by #52
Open

Relative imports to google's type #51

ffortier opened this issue Sep 7, 2023 · 0 comments · May be fixed by #52

Comments

@ffortier
Copy link
Contributor

ffortier commented Sep 7, 2023

Hello. I have the following protobuf definition

~/proto/contract/contract.proto

syntax = "proto3";

import "google/protobuf/timestamp.proto";

message Metadata {
  google.protobuf.Timestamp creation_time = 1;
}

when generating the assembly script, it creates the following import

import {Timestamp} from '/some/absolute/path/where/my/code/is/google/protobuf/Timestamp.ts`

class Metadata {
  //...
}

The reason for this is this code:

export function getOutputFilePath(
fileDescriptor: FileDescriptorProto,
messageOrEnumDescriptor: DescriptorProto | EnumDescriptorProto,
parentMessageDescriptors: DescriptorProto[] = []
) {
const messageName = messageOrEnumDescriptor.getName();
assert.ok(messageName !== undefined);
const outputFileName = sanitizeFileName(`${messageName}.ts`);
return [
getFilePrefix(fileDescriptor),
getNestedMessagePrefix(parentMessageDescriptors),
outputFileName,
].join("/");
}

The file prefix and the nested message prefix will both be undefined because it's not a nested message and there's no package defined. Which means taht the output file path will be //Metadata.ts so when this code will be executed

const relativeMessageOrEnumFilePath = path.relative(
path.dirname(fileContext.getFilePath()),
messageOrEnumFilePath
);

It will try to find the relative path of //Metadata.ts with google/protobuf/Timestamp.ts which will result in an absolute path containing the current working directory.

The workaround is to always set a package name to get proto/contract//Metadata.ts and this seems to work correctly.

@ffortier ffortier linked a pull request Sep 7, 2023 that will close this issue
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.

1 participant