From 37cd2c7df4cb67ae7a867c66aa1ec5995e83aca6 Mon Sep 17 00:00:00 2001 From: Zetazzz Date: Tue, 4 Jul 2023 12:42:00 +0800 Subject: [PATCH] using resolver to fix deps bug --- .../store/__snapshots__/store.test.ts.snap | 6 +++++ .../__tests__/traverse/bad.traversal.test.ts | 2 +- packages/parser/src/store.ts | 24 ++++++++++++------- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/packages/parser/__tests__/store/__snapshots__/store.test.ts.snap b/packages/parser/__tests__/store/__snapshots__/store.test.ts.snap index 34ebe3e2c2..6d370596f8 100644 --- a/packages/parser/__tests__/store/__snapshots__/store.test.ts.snap +++ b/packages/parser/__tests__/store/__snapshots__/store.test.ts.snap @@ -186,6 +186,7 @@ exports[`osmosis excluded gamm 1`] = ` "cosmos.bank.v1beta1", "cosmos.base.query.v1beta1", "cosmos.base.v1beta1", + "cosmos.msg.v1", "cosmos.staking.v1beta1", "cosmos_proto", "gogoproto", @@ -204,13 +205,18 @@ exports[`osmosis excluded gamm 1`] = ` "osmosis.tokenfactory.v1beta1", "osmosis.twap.v1beta1", "osmosis.txfees.v1beta1", + "tendermint.crypto", + "tendermint.types", + "tendermint.version", ] `; exports[`osmosis/gamm/v1beta1/tx.proto 1`] = ` [ "cosmos.base.v1beta1", + "cosmos_proto", "gogoproto", + "google.protobuf", "osmosis.gamm.v1beta1", ] `; diff --git a/packages/parser/__tests__/traverse/bad.traversal.test.ts b/packages/parser/__tests__/traverse/bad.traversal.test.ts index 3c27daaab5..7d63665d01 100644 --- a/packages/parser/__tests__/traverse/bad.traversal.test.ts +++ b/packages/parser/__tests__/traverse/bad.traversal.test.ts @@ -36,7 +36,7 @@ message MyMessage { store.traverseAll(); } catch (e) { failed = true; - expect(e.message).toEqual('missing proto import gogoproto/gogo.proto') + expect(e.message).toEqual('Dependency Not Found gogoproto/gogo.proto') } expect(failed).toBe(true); }); diff --git a/packages/parser/src/store.ts b/packages/parser/src/store.ts index b34a78eec8..0d3c7a2a1a 100644 --- a/packages/parser/src/store.ts +++ b/packages/parser/src/store.ts @@ -16,6 +16,7 @@ import google_empty from './native/empty'; import google_field_mask from './native/field_mask'; import google_struct from './native/struct'; import google_wrappers from './native/wrappers'; +import { ProtoResolver } from './resolver'; const GOOGLE_PROTOS = [ ['google/protobuf/any.proto', google_any], @@ -208,18 +209,23 @@ export class ProtoStore { } traverseAll(): void { + if (this._traversed) return; + let actualFiles = new Set(); + let resolver = new ProtoResolver(this.getDeps()); - if (this._traversed) return; this.protos = this.getProtos().map((ref: ProtoRef) => { - // get included imported files - const isIncluded = isRefIncluded(ref, this.options.prototypes.includes) - const isExcluded = isRefExcluded(ref, this.options.prototypes.excluded) - - if(isIncluded && !isExcluded){ - actualFiles.add(ref.filename); - if(ref.proto.imports && ref.proto.imports.length){ - actualFiles = new Set([...actualFiles, ...ref.proto.imports]) + if( !actualFiles.has(ref.filename) ){ + // get included imported files + const isIncluded = isRefIncluded(ref, this.options.prototypes.includes) + const isExcluded = isRefExcluded(ref, this.options.prototypes.excluded) + + if(isIncluded && !isExcluded){ + const deps = resolver.resolve(ref.filename); + + for (const dep of deps) { + actualFiles.add(dep); + } } }