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

Invalid bundle while using import as statements in the code #184

Closed
bazaglia opened this issue Jan 5, 2022 · 2 comments · Fixed by #270
Closed

Invalid bundle while using import as statements in the code #184

bazaglia opened this issue Jan 5, 2022 · 2 comments · Fixed by #270
Assignees
Labels
Milestone

Comments

@bazaglia
Copy link

bazaglia commented Jan 5, 2022

Bug report

I'm trying to bundle the files outputted from a generator that I have no control over since it's an npm package (proto-loader-gen-types). It basically generates typescript definitions for a protobuf file. They only compose typescript types and interfaces.

The file that I use as my entrypoint imports other files using the syntax:

import type { PricingReply as _provider_a_PricingReply, PricingReply__Output as _provider_a_PricingReply__Output } from '../provider_a/PricingReply';

The problem is that dts-bundle-generator is copying the PricingReply interface but using it later on as _provider_a_PricingReply. So it generates invalid TypeScript.

I'm adding two possible examples of expected outputs:

  1. Ignore the alias set in the import as so that the original interface name can always be used.
  2. Copy the interface to the bundle file with the alias name, so it will be found by TypeScript.

Input code

provider_a/ProviderA.ts

import type * as grpc from '@grpc/grpc-js'
import type { MethodDefinition } from '@grpc/proto-loader'
import type { PricingReply as _provider_a_PricingReply, PricingReply__Output as _provider_a_PricingReply__Output } from '../provider_a/PricingReply';
import type { PricingRequest as _provider_a_PricingRequest, PricingRequest__Output as _provider_a_PricingRequest__Output } from '../provider_a/PricingRequest';

export interface ProviderAClient extends grpc.Client {
  GetPricing(argument: _provider_a_PricingRequest, metadata: grpc.Metadata, options: grpc.CallOptions, callback: grpc.requestCallback<_provider_a_PricingReply__Output>): grpc.ClientUnaryCall;
  GetPricing(argument: _provider_a_PricingRequest, metadata: grpc.Metadata, callback: grpc.requestCallback<_provider_a_PricingReply__Output>): grpc.ClientUnaryCall;
  GetPricing(argument: _provider_a_PricingRequest, options: grpc.CallOptions, callback: grpc.requestCallback<_provider_a_PricingReply__Output>): grpc.ClientUnaryCall;
  GetPricing(argument: _provider_a_PricingRequest, callback: grpc.requestCallback<_provider_a_PricingReply__Output>): grpc.ClientUnaryCall;
  getPricing(argument: _provider_a_PricingRequest, metadata: grpc.Metadata, options: grpc.CallOptions, callback: grpc.requestCallback<_provider_a_PricingReply__Output>): grpc.ClientUnaryCall;
  getPricing(argument: _provider_a_PricingRequest, metadata: grpc.Metadata, callback: grpc.requestCallback<_provider_a_PricingReply__Output>): grpc.ClientUnaryCall;
  getPricing(argument: _provider_a_PricingRequest, options: grpc.CallOptions, callback: grpc.requestCallback<_provider_a_PricingReply__Output>): grpc.ClientUnaryCall;
  getPricing(argument: _provider_a_PricingRequest, callback: grpc.requestCallback<_provider_a_PricingReply__Output>): grpc.ClientUnaryCall;
  
}

export interface ProviderAHandlers extends grpc.UntypedServiceImplementation {
  GetPricing: grpc.handleUnaryCall<_provider_a_PricingRequest__Output, _provider_a_PricingReply>;
  
}

export interface ProviderADefinition extends grpc.ServiceDefinition {
  GetPricing: MethodDefinition<_provider_a_PricingRequest, _provider_a_PricingReply, _provider_a_PricingRequest__Output, _provider_a_PricingReply__Output>
}

provider_a/PricingRequest.ts

export interface PricingRequest {
  'foo'?: (number);
}

export interface PricingRequest__Output {
  'foo': (number);
}

provider_a/PricingReply.ts

export interface PricingReply {
  'foo'?: (number | string);
  'bar'?: (number | string);
}

export interface PricingReply__Output {
  'foo': (number);
  'bar': (number);
}

Expected output (1)

// Generated by dts-bundle-generator v6.3.0

import * as grpc from '@grpc/grpc-js';
import { MethodDefinition } from '@grpc/proto-loader';

export interface PricingReply {
        "foo"?: (number | string);
        "bar"?: (number | string);
}
export interface PricingReply__Output {
        "foo": (number);
        "bar": (number);
}
export interface PricingRequest {
        "foo"?: (number);
}
export interface PricingRequest__Output {
        "foo": (number);
}
export interface ProviderAClient extends grpc.Client {
        GetPricing(argument: PricingRequest, metadata: grpc.Metadata, options: grpc.CallOptions, callback: grpc.requestCallback<PricingReply__Output>): grpc.ClientUnaryCall;
        GetPricing(argument: PricingRequest, metadata: grpc.Metadata, callback: grpc.requestCallback<PricingReply__Output>): grpc.ClientUnaryCall;
        GetPricing(argument: PricingRequest, options: grpc.CallOptions, callback: grpc.requestCallback<PricingReply__Output>): grpc.ClientUnaryCall;
        GetPricing(argument: PricingRequest, callback: grpc.requestCallback<PricingReply__Output>): grpc.ClientUnaryCall;
        getPricing(argument: PricingRequest, metadata: grpc.Metadata, options: grpc.CallOptions, callback: grpc.requestCallback<PricingReply__Output>): grpc.ClientUnaryCall;
        getPricing(argument: PricingRequest, metadata: grpc.Metadata, callback: grpc.requestCallback<PricingReply__Output>): grpc.ClientUnaryCall;
        getPricing(argument: PricingRequest, options: grpc.CallOptions, callback: grpc.requestCallback<PricingReply__Output>): grpc.ClientUnaryCall;
        getPricing(argument: PricingRequest, callback: grpc.requestCallback<PricingReply__Output>): grpc.ClientUnaryCall;
}
export interface ProviderAHandlers extends grpc.UntypedServiceImplementation {
        GetPricing: grpc.handleUnaryCall<PricingRequest__Output, PricingReply>;
}
export interface ProviderADefinition extends grpc.ServiceDefinition {
        GetPricing: MethodDefinition<PricingRequest, PricingReply, PricingRequest__Output, PricingReply__Output>;
}

export {};

Expected output (2)

// Generated by dts-bundle-generator v6.3.0

import * as grpc from '@grpc/grpc-js';
import { MethodDefinition } from '@grpc/proto-loader';

export interface _provider_a_PricingReply {
        "foo"?: (number | string);
        "bar"?: (number | string);
}
export interface _provider_a_PricingReply__Output {
        "foo": (number);
        "bar": (number);
}
export interface _provider_a_PricingRequest {
        "foo"?: (number);
}
export interface _provider_a_PricingRequest__Output {
        "foo": (number);
}
export interface ProviderAClient extends grpc.Client {
        GetPricing(argument: _provider_a_PricingRequest, metadata: grpc.Metadata, options: grpc.CallOptions, callback: grpc.requestCallback<_provider_a_PricingReply__Output>): grpc.ClientUnaryCall;
        GetPricing(argument: _provider_a_PricingRequest, metadata: grpc.Metadata, callback: grpc.requestCallback<_provider_a_PricingReply__Output>): grpc.ClientUnaryCall;
        GetPricing(argument: _provider_a_PricingRequest, options: grpc.CallOptions, callback: grpc.requestCallback<_provider_a_PricingReply__Output>): grpc.ClientUnaryCall;
        GetPricing(argument: _provider_a_PricingRequest, callback: grpc.requestCallback<_provider_a_PricingReply__Output>): grpc.ClientUnaryCall;
        getPricing(argument: _provider_a_PricingRequest, metadata: grpc.Metadata, options: grpc.CallOptions, callback: grpc.requestCallback<_provider_a_PricingReply__Output>): grpc.ClientUnaryCall;
        getPricing(argument: _provider_a_PricingRequest, metadata: grpc.Metadata, callback: grpc.requestCallback<_provider_a_PricingReply__Output>): grpc.ClientUnaryCall;
        getPricing(argument: _provider_a_PricingRequest, options: grpc.CallOptions, callback: grpc.requestCallback<_provider_a_PricingReply__Output>): grpc.ClientUnaryCall;
        getPricing(argument: _provider_a_PricingRequest, callback: grpc.requestCallback<_provider_a_PricingReply__Output>): grpc.ClientUnaryCall;
}
export interface ProviderAHandlers extends grpc.UntypedServiceImplementation {
        GetPricing: grpc.handleUnaryCall<_provider_a_PricingRequest__Output, _provider_a_PricingReply>;
}
export interface ProviderADefinition extends grpc.ServiceDefinition {
        GetPricing: MethodDefinition<_provider_a_PricingRequest, _provider_a_PricingReply, _provider_a_PricingRequest__Output, _provider_a_PricingReply__Output>;
}

export {};

Actual output

// Generated by dts-bundle-generator v6.3.0

import * as grpc from '@grpc/grpc-js';
import { MethodDefinition } from '@grpc/proto-loader';

export interface PricingReply {
        "foo"?: (number | string);
        "bar"?: (number | string);
}
export interface PricingReply__Output {
        "foo": (number);
        "bar": (number);
}
export interface PricingRequest {
        "foo"?: (number);
}
export interface PricingRequest__Output {
        "foo": (number);
}
export interface ProviderAClient extends grpc.Client {
        GetPricing(argument: _provider_a_PricingRequest, metadata: grpc.Metadata, options: grpc.CallOptions, callback: grpc.requestCallback<_provider_a_PricingReply__Output>): grpc.ClientUnaryCall;
        GetPricing(argument: _provider_a_PricingRequest, metadata: grpc.Metadata, callback: grpc.requestCallback<_provider_a_PricingReply__Output>): grpc.ClientUnaryCall;
        GetPricing(argument: _provider_a_PricingRequest, options: grpc.CallOptions, callback: grpc.requestCallback<_provider_a_PricingReply__Output>): grpc.ClientUnaryCall;
        GetPricing(argument: _provider_a_PricingRequest, callback: grpc.requestCallback<_provider_a_PricingReply__Output>): grpc.ClientUnaryCall;
        getPricing(argument: _provider_a_PricingRequest, metadata: grpc.Metadata, options: grpc.CallOptions, callback: grpc.requestCallback<_provider_a_PricingReply__Output>): grpc.ClientUnaryCall;
        getPricing(argument: _provider_a_PricingRequest, metadata: grpc.Metadata, callback: grpc.requestCallback<_provider_a_PricingReply__Output>): grpc.ClientUnaryCall;
        getPricing(argument: _provider_a_PricingRequest, options: grpc.CallOptions, callback: grpc.requestCallback<_provider_a_PricingReply__Output>): grpc.ClientUnaryCall;
        getPricing(argument: _provider_a_PricingRequest, callback: grpc.requestCallback<_provider_a_PricingReply__Output>): grpc.ClientUnaryCall;
}
export interface ProviderAHandlers extends grpc.UntypedServiceImplementation {
        GetPricing: grpc.handleUnaryCall<_provider_a_PricingRequest__Output, _provider_a_PricingReply>;
}
export interface ProviderADefinition extends grpc.ServiceDefinition {
        GetPricing: MethodDefinition<_provider_a_PricingRequest, _provider_a_PricingReply, _provider_a_PricingRequest__Output, _provider_a_PricingReply__Output>;
}

export {};
@timocov
Copy link
Owner

timocov commented Jan 10, 2022

Hm, looks like renaming issue which was the known limitation for a while but I don't remember why it was gone from readme.

The tool doesn't follow renamings so the code of all nodes except some keywords is the same as it was originally declared.

Probably it could be done via tricks like type NewLocalName = Type for types and declare const newLocalName: typeof originalName; for classes/functions/enums/variables, but I'm not sure how tricky it would be to implement it.

@timocov timocov added the Bug label Jan 10, 2022
@timocov timocov changed the title Invalid bundle file when using import as Invalid bundle while using import as statements in the code Jan 10, 2022
@timocov timocov added this to the 9.0 milestone Nov 19, 2023
@timocov timocov self-assigned this Nov 19, 2023
timocov added a commit that referenced this issue Nov 19, 2023
timocov added a commit that referenced this issue Nov 26, 2023
@timocov
Copy link
Owner

timocov commented Nov 27, 2023

The fix has been released in v9.0.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants