square / square/wire

Pruner ignore redundant import.

Open
#2,704 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Kotlin
Stars
4.4k
Forks
627
Avg merge
3d 15m
Merged PRs (30d)
20

Description

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;
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the test method should_remove_redundant_import and the files test/file_with_redundant_dependency.proto, test/file_with_dependency.proto, and test/dependency.proto. Trace Schema.prune with the listed pruning roots, then run the relevant test and verify that optimizedProtoFile.toSchema() matches the expected output without the redundant import.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
39/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.