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

Pruner ignore redundant import. #2704

Open
daniszam opened this issue Nov 13, 2023 · 1 comment
Open

Pruner ignore redundant import. #2704

daniszam opened this issue Nov 13, 2023 · 1 comment

Comments

@daniszam
Copy link

Hi! I got some unexpected behaviour when trying to optimise collection proto files.
Seems like imports a.proto won't be removed if any of proto file in collection has a dependency to a.proto

File test/file_with_redundant_dependency.proto contains redundant import import "test/dependency.proto";. After using Pruner, I expected to get a file without this import, but that's not really the case.

    @Test
    public void should_remove_redundant_import() {
        // given
        var loader = new SchemaLoader(FileSystems.getDefault());
        var linker = new Linker(loader, new ErrorCollector(), true, true);
        var testFileLocation = Location.get("<TEST_FILE_LOCATION>");
        loader.initRoots(List.of(testFileLocation), List.of(testFileLocation));
        var fileWithDependency = loader.load("test/file_with_dependency.proto");
        var fileWithRedundantDependency = loader.load("test/file_with_redundant_dependency.proto");

        var schema = linker.link(List.of(fileWithDependency, fileWithRedundantDependency));

        // when
        var optimizedSchema = schema.prune(new PruningRules.Builder()
                .addRoot(List.of(
                        "test.dependency.TestMessage",
                        "test.dependency.TestMessageWithDependency"))
                .build());
        var optimizedProtoFile = optimizedSchema.protoFile("test/file_with_redundant_dependency.proto");

        // then
        assertThat(optimizedProtoFile.toSchema())
                .isEqualTo("""
                        // Proto schema formatted by Wire, do not edit.
                        // Source: test/file_with_redundant_dependency.proto
                         
                        syntax = "proto3";
                         
                        package test.dependency;
                         
                        message TestMessage {
                          string field_1 = 1;
                        }
                        """);
    }

test/file_with_dependency.proto

syntax = "proto3";
package test.dependency;

import "test/dependency.proto";

message TestMessageWithDependency {

  string field_1 = 1;
  Dependency field_2 = 2;
}

test/file_with_redundant_dependency.proto

syntax = "proto3";
package test.dependency;

import "test/dependency.proto";

message TestMessage {

  string field_1 = 1;
}

test/dependency.proto

syntax = "proto3";

package test.dependency;

message Dependency {

  string field_1 = 1;
}
@oldergod
Copy link
Member

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

No branches or pull requests

2 participants